Codebase list python-cx-oracle / master src / cxoSodaOperation.c
master

Tree @master (Download .tar.gz)

cxoSodaOperation.c @masterraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//-----------------------------------------------------------------------------
// Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// cxoSodaOperation.c
//   Defines the routines for the various operations performed on SODA
// collections.
//-----------------------------------------------------------------------------

#include "cxoModule.h"

//-----------------------------------------------------------------------------
// Declaration of functions
//-----------------------------------------------------------------------------
static void cxoSodaOperation_free(cxoSodaOperation*);
static PyObject *cxoSodaOperation_repr(cxoSodaOperation*);
static PyObject *cxoSodaOperation_filter(cxoSodaOperation*, PyObject*);
static PyObject *cxoSodaOperation_key(cxoSodaOperation*, PyObject*);
static PyObject *cxoSodaOperation_keys(cxoSodaOperation*, PyObject*);
static PyObject *cxoSodaOperation_limit(cxoSodaOperation*, PyObject*);
static PyObject *cxoSodaOperation_skip(cxoSodaOperation*, PyObject*);
static PyObject *cxoSodaOperation_version(cxoSodaOperation*, PyObject*);
static PyObject *cxoSodaOperation_count(cxoSodaOperation*, PyObject*);
static PyObject *cxoSodaOperation_getCursor(cxoSodaOperation*, PyObject*);
static PyObject *cxoSodaOperation_getDocuments(cxoSodaOperation*, PyObject*);
static PyObject *cxoSodaOperation_getOne(cxoSodaOperation*, PyObject*);
static PyObject *cxoSodaOperation_remove(cxoSodaOperation*, PyObject*);
static PyObject *cxoSodaOperation_replaceOne(cxoSodaOperation*, PyObject*);
static PyObject *cxoSodaOperation_replaceOneAndGet(cxoSodaOperation*,
        PyObject*);


//-----------------------------------------------------------------------------
// declaration of methods for Python type "SodaOperation"
//-----------------------------------------------------------------------------
static PyMethodDef cxoMethods[] = {
    { "filter", (PyCFunction) cxoSodaOperation_filter, METH_O },
    { "key", (PyCFunction) cxoSodaOperation_key, METH_O },
    { "keys", (PyCFunction) cxoSodaOperation_keys, METH_O },
    { "limit", (PyCFunction) cxoSodaOperation_limit, METH_O },
    { "skip", (PyCFunction) cxoSodaOperation_skip, METH_O },
    { "version", (PyCFunction) cxoSodaOperation_version, METH_O },
    { "count", (PyCFunction) cxoSodaOperation_count, METH_NOARGS },
    { "getCursor", (PyCFunction) cxoSodaOperation_getCursor, METH_NOARGS },
    { "getDocuments", (PyCFunction) cxoSodaOperation_getDocuments,
            METH_NOARGS },
    { "getOne", (PyCFunction) cxoSodaOperation_getOne, METH_NOARGS },
    { "remove", (PyCFunction) cxoSodaOperation_remove, METH_NOARGS },
    { "replaceOne", (PyCFunction) cxoSodaOperation_replaceOne, METH_O },
    { "replaceOneAndGet", (PyCFunction) cxoSodaOperation_replaceOneAndGet,
            METH_O },
    { NULL }
};


//-----------------------------------------------------------------------------
// Python type declarations
//-----------------------------------------------------------------------------
PyTypeObject cxoPyTypeSodaOperation = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "cx_Oracle.SodaOperation",          // tp_name
    sizeof(cxoSodaOperation),           // tp_basicsize
    0,                                  // tp_itemsize
    (destructor) cxoSodaOperation_free, // tp_dealloc
    0,                                  // tp_print
    0,                                  // tp_getattr
    0,                                  // tp_setattr
    0,                                  // tp_compare
    (reprfunc) cxoSodaOperation_repr,   // tp_repr
    0,                                  // tp_as_number
    0,                                  // tp_as_sequence
    0,                                  // tp_as_mapping
    0,                                  // tp_hash
    0,                                  // tp_call
    0,                                  // tp_str
    0,                                  // tp_getattro
    0,                                  // tp_setattro
    0,                                  // tp_as_buffer
    Py_TPFLAGS_DEFAULT,                 // tp_flags
    0,                                  // tp_doc
    0,                                  // tp_traverse
    0,                                  // tp_clear
    0,                                  // tp_richcompare
    0,                                  // tp_weaklistoffset
    0,                                  // tp_iter
    0,                                  // tp_iternext
    cxoMethods,                         // tp_methods
    0,                                  // tp_members
    0,                                  // tp_getset
    0,                                  // tp_base
    0,                                  // tp_dict
    0,                                  // tp_descr_get
    0,                                  // tp_descr_set
    0,                                  // tp_dictoffset
    0,                                  // tp_init
    0,                                  // tp_alloc
    0,                                  // tp_new
    0,                                  // tp_free
    0,                                  // tp_is_gc
    0                                   // tp_bases
};


//-----------------------------------------------------------------------------
// cxoSodaOperation_clearKeys()
//   Clear the keys set on the operation object, if applicable.
//-----------------------------------------------------------------------------
void cxoSodaOperation_clearKeys(cxoSodaOperation *op)
{
    uint32_t i;

    if (op->keyBuffers) {
        for (i = 0; i < op->numKeyBuffers; i++)
            cxoBuffer_clear(&op->keyBuffers[i]);
        PyMem_Free(op->keyBuffers);
        op->keyBuffers = NULL;
    }
    op->numKeyBuffers = 0;
    op->options.numKeys = 0;
    if (op->options.keys) {
        PyMem_Free(op->options.keys);
        op->options.keys = NULL;
    }
    if (op->options.keyLengths) {
        PyMem_Free(op->options.keyLengths);
        op->options.keyLengths = NULL;
    }
}


//-----------------------------------------------------------------------------
// cxoSodaOperation_new()
//   Create a new SODA operation object.
//-----------------------------------------------------------------------------
cxoSodaOperation *cxoSodaOperation_new(cxoSodaCollection *coll)
{
    cxoSodaOperation *op;

    op = (cxoSodaOperation*)
            cxoPyTypeSodaOperation.tp_alloc(&cxoPyTypeSodaOperation, 0);
    if (!op)
        return NULL;
    if (dpiContext_initSodaOperOptions(cxoDpiContext, &op->options) < 0) {
        Py_DECREF(op);
        return NULL;
    }
    cxoBuffer_init(&op->keyBuffer);
    cxoBuffer_init(&op->versionBuffer);
    cxoBuffer_init(&op->filterBuffer);
    Py_INCREF(coll);
    op->coll = coll;

    return op;
}


//-----------------------------------------------------------------------------
// cxoSodaOperation_free()
//   Free the memory associated with a SODA operation object.
//-----------------------------------------------------------------------------
static void cxoSodaOperation_free(cxoSodaOperation *op)
{
    cxoSodaOperation_clearKeys(op);
    cxoBuffer_clear(&op->keyBuffer);
    cxoBuffer_clear(&op->versionBuffer);
    cxoBuffer_clear(&op->filterBuffer);
    Py_CLEAR(op->coll);
    Py_TYPE(op)->tp_free((PyObject*) op);
}


//-----------------------------------------------------------------------------
// cxoSodaOperation_repr()
//   Return a string representation of a SODA operation object.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaOperation_repr(cxoSodaOperation *op)
{
    PyObject *collRepr, *module, *name, *result;

    collRepr = PyObject_Repr((PyObject*) op->coll);
    if (!collRepr)
        return NULL;
    if (cxoUtils_getModuleAndName(Py_TYPE(op), &module, &name) < 0) {
        Py_DECREF(collRepr);
        return NULL;
    }
    result = cxoUtils_formatString("<%s.%s on %s>",
            PyTuple_Pack(3, module, name, collRepr));
    Py_DECREF(module);
    Py_DECREF(name);
    Py_DECREF(collRepr);
    return result;
}


//-----------------------------------------------------------------------------
// cxoSodaOperation_filter()
//   Set the filter to be used for the operation.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaOperation_filter(cxoSodaOperation *op,
        PyObject *filterObj)
{
    cxoBuffer_clear(&op->filterBuffer);
    if (PyDict_Check(filterObj)) {
        filterObj = PyObject_CallFunctionObjArgs(cxoJsonDumpFunction,
                filterObj, NULL);
        if (!filterObj)
            return NULL;
    }
    if (cxoBuffer_fromObject(&op->filterBuffer, filterObj,
            op->coll->db->connection->encodingInfo.encoding) < 0)
        return NULL;
    op->options.filter = op->filterBuffer.ptr;
    op->options.filterLength = op->filterBuffer.size;
    Py_INCREF(op);
    return (PyObject*) op;
}


//-----------------------------------------------------------------------------
// cxoSodaOperation_key()
//   Set the key to be used for the operation.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaOperation_key(cxoSodaOperation *op,
        PyObject *keyObj)
{
    cxoBuffer_clear(&op->keyBuffer);
    if (cxoBuffer_fromObject(&op->keyBuffer, keyObj,
            op->coll->db->connection->encodingInfo.encoding) < 0)
        return NULL;
    op->options.key = op->keyBuffer.ptr;
    op->options.keyLength = op->keyBuffer.size;
    Py_INCREF(op);
    return (PyObject*) op;
}


//-----------------------------------------------------------------------------
// cxoSodaOperation_keys()
//   Set the keys to be used for the operation.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaOperation_keys(cxoSodaOperation *op,
        PyObject *keysObj)
{
    Py_ssize_t size, i;
    PyObject *element;

    // determine size of sequence passed to method
    size = PySequence_Size(keysObj);
    if (PyErr_Occurred())
        return NULL;

    // clear original keys, if applicable
    cxoSodaOperation_clearKeys(op);

    // zero-length arrays don't need any further processing
    if (size == 0) {
        Py_INCREF(op);
        return (PyObject*) op;
    }

    // initialize memory
    op->keyBuffers = PyMem_Malloc(size * sizeof(cxoBuffer));
    if (!op->keyBuffers)
        return NULL;
    op->numKeyBuffers = (uint32_t) size;
    for (i = 0; i < size; i++)
        cxoBuffer_init(&op->keyBuffers[i]);
    op->options.keys = PyMem_Malloc(size * sizeof(const char *));
    op->options.keyLengths = PyMem_Malloc(size * sizeof(uint32_t));
    if (!op->options.keys || !op->options.keyLengths) {
        cxoSodaOperation_clearKeys(op);
        return NULL;
    }
    op->options.numKeys = op->numKeyBuffers;

    // process each of the elements of the sequence
    for (i = 0; i < size; i++) {
        element = PySequence_GetItem(keysObj, i);
        if (!element) {
            cxoSodaOperation_clearKeys(op);
            return NULL;
        }
        if (cxoBuffer_fromObject(&op->keyBuffers[i], element,
                op->coll->db->connection->encodingInfo.encoding) < 0) {
            Py_DECREF(element);
            cxoSodaOperation_clearKeys(op);
            return NULL;
        }
        Py_DECREF(element);
        op->options.keys[i] = op->keyBuffers[i].ptr;
        op->options.keyLengths[i] = op->keyBuffers[i].size;
    }

    Py_INCREF(op);
    return (PyObject*) op;
}


//-----------------------------------------------------------------------------
// cxoSodaOperation_limit()
//   Set the limit value to be used for the operation.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaOperation_limit(cxoSodaOperation *op,
        PyObject *limitObj)
{
    op->options.limit = PyLong_AsUnsignedLong(limitObj);
    if (PyErr_Occurred())
        return NULL;
    Py_INCREF(op);
    return (PyObject*) op;
}


//-----------------------------------------------------------------------------
// cxoSodaOperation_skip()
//   Set the skip value to be used for the operation.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaOperation_skip(cxoSodaOperation *op,
        PyObject *skipObj)
{
    op->options.skip = PyLong_AsUnsignedLong(skipObj);
    if (PyErr_Occurred())
        return NULL;
    Py_INCREF(op);
    return (PyObject*) op;
}


//-----------------------------------------------------------------------------
// cxoSodaOperation_version()
//   Set the version to be used for the operation.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaOperation_version(cxoSodaOperation *op,
        PyObject *versionObj)
{
    cxoBuffer_clear(&op->versionBuffer);
    if (cxoBuffer_fromObject(&op->versionBuffer, versionObj,
            op->coll->db->connection->encodingInfo.encoding) < 0)
        return NULL;
    op->options.version = op->versionBuffer.ptr;
    op->options.versionLength = op->versionBuffer.size;
    Py_INCREF(op);
    return (PyObject*) op;
}


//-----------------------------------------------------------------------------
// cxoSodaOperation_count()
//   Returns the number of documents that match the criteria.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaOperation_count(cxoSodaOperation *op, PyObject *args)
{
    uint64_t count;
    uint32_t flags;
    int status;

    if (cxoConnection_getSodaFlags(op->coll->db->connection, &flags) < 0)
        return NULL;
    Py_BEGIN_ALLOW_THREADS
    status = dpiSodaColl_getDocCount(op->coll->handle, &op->options, flags,
            &count);
    Py_END_ALLOW_THREADS
    if (status < 0)
        return cxoError_raiseAndReturnNull();
    return PyLong_FromUnsignedLongLong(count);
}


//-----------------------------------------------------------------------------
// cxoSodaOperation_getCursor()
//   Returns a document cursor which can be used to iterate over the documents
// that match the criteria.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaOperation_getCursor(cxoSodaOperation *op,
        PyObject *args)
{
    dpiSodaDocCursor *handle;
    cxoSodaDocCursor *cursor;
    uint32_t flags;
    int status;

    if (cxoConnection_getSodaFlags(op->coll->db->connection, &flags) < 0)
        return NULL;
    Py_BEGIN_ALLOW_THREADS
    status = dpiSodaColl_find(op->coll->handle, &op->options, flags, &handle);
    Py_END_ALLOW_THREADS
    if (status < 0)
        return cxoError_raiseAndReturnNull();
    cursor = cxoSodaDocCursor_new(op->coll->db, handle);
    if (!cursor)
        return NULL;
    return (PyObject*) cursor;
}


//-----------------------------------------------------------------------------
// cxoSodaOperation_getDocuments()
//   Returns a list of documents that match the criteria.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaOperation_getDocuments(cxoSodaOperation *op,
        PyObject *args)
{
    dpiSodaDocCursor *cursor;
    PyObject *docObj;
    dpiSodaDoc *doc;
    PyObject *list;
    uint32_t flags;
    int status;

    // acquire cursor
    if (cxoConnection_getSodaFlags(op->coll->db->connection, &flags) < 0)
        return NULL;
    Py_BEGIN_ALLOW_THREADS
    status = dpiSodaColl_find(op->coll->handle, &op->options, flags, &cursor);
    Py_END_ALLOW_THREADS
    if (status < 0)
        return cxoError_raiseAndReturnNull();

    // iterate cursor and create array of documents
    list = PyList_New(0);
    if (!list) {
        dpiSodaDocCursor_release(cursor);
        return NULL;
    }
    while (1) {
        Py_BEGIN_ALLOW_THREADS
        status = dpiSodaDocCursor_getNext(cursor, flags, &doc);
        Py_END_ALLOW_THREADS
        if (status < 0) {
            cxoError_raiseAndReturnNull();
            dpiSodaDocCursor_release(cursor);
            return NULL;
        }
        if (!doc)
            break;
        docObj = (PyObject*) cxoSodaDoc_new(op->coll->db, doc);
        if (!docObj) {
            dpiSodaDocCursor_release(cursor);
            return NULL;
        }
        if (PyList_Append(list, docObj) < 0) {
            Py_DECREF(docObj);
            dpiSodaDocCursor_release(cursor);
            return NULL;
        }
        Py_DECREF(docObj);
    }
    dpiSodaDocCursor_release(cursor);

    return list;
}


//-----------------------------------------------------------------------------
// cxoSodaOperation_getOne()
//   Returns a single document that matches the criteria or None if no
// documents match the criteria.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaOperation_getOne(cxoSodaOperation *op, PyObject *args)
{
    dpiSodaDoc *handle;
    uint32_t flags;
    int status;

    if (cxoConnection_getSodaFlags(op->coll->db->connection, &flags) < 0)
        return NULL;
    Py_BEGIN_ALLOW_THREADS
    status = dpiSodaColl_findOne(op->coll->handle, &op->options, flags,
            &handle);
    Py_END_ALLOW_THREADS
    if (status < 0)
        return cxoError_raiseAndReturnNull();
    if (handle)
        return (PyObject*) cxoSodaDoc_new(op->coll->db, handle);
    Py_RETURN_NONE;
}


//-----------------------------------------------------------------------------
// cxoSodaOperation_remove()
//   Remove all of the documents that match the criteria.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaOperation_remove(cxoSodaOperation *op, PyObject *args)
{
    uint64_t count;
    uint32_t flags;
    int status;

    if (cxoConnection_getSodaFlags(op->coll->db->connection, &flags) < 0)
        return NULL;
    Py_BEGIN_ALLOW_THREADS
    status = dpiSodaColl_remove(op->coll->handle, &op->options, flags, &count);
    Py_END_ALLOW_THREADS
    if (status < 0)
        return cxoError_raiseAndReturnNull();
    return PyLong_FromUnsignedLongLong(count);
}


//-----------------------------------------------------------------------------
// cxoSodaOperation_replaceOne()
//   Replace a single document in the collection with the provided replacement.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaOperation_replaceOne(cxoSodaOperation *op,
        PyObject *arg)
{
    int status, replaced;
    cxoSodaDoc *doc;
    uint32_t flags;

    if (cxoConnection_getSodaFlags(op->coll->db->connection, &flags) < 0)
        return NULL;
    if (cxoUtils_processSodaDocArg(op->coll->db, arg, &doc) < 0)
        return NULL;
    Py_BEGIN_ALLOW_THREADS
    status = dpiSodaColl_replaceOne(op->coll->handle, &op->options,
            doc->handle, flags, &replaced, NULL);
    Py_END_ALLOW_THREADS
    if (status < 0) {
        cxoError_raiseAndReturnNull();
        Py_DECREF(doc);
        return NULL;
    }
    Py_DECREF(doc);
    if (replaced)
        Py_RETURN_TRUE;
    Py_RETURN_FALSE;
}


//-----------------------------------------------------------------------------
// cxoSodaOperation_replaceOneAndGet()
//   Replace a single document in the collection with the provided replacement
// and return a document (without the content) to the caller.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaOperation_replaceOneAndGet(cxoSodaOperation *op,
        PyObject *arg)
{
    dpiSodaDoc *replacedDoc;
    cxoSodaDoc *doc;
    uint32_t flags;
    int status;

    if (cxoConnection_getSodaFlags(op->coll->db->connection, &flags) < 0)
        return NULL;
    if (cxoUtils_processSodaDocArg(op->coll->db, arg, &doc) < 0)
        return NULL;
    Py_BEGIN_ALLOW_THREADS
    status = dpiSodaColl_replaceOne(op->coll->handle, &op->options,
            doc->handle, flags, NULL, &replacedDoc);
    Py_END_ALLOW_THREADS
    if (status < 0) {
        cxoError_raiseAndReturnNull();
        Py_DECREF(doc);
        return NULL;
    }
    Py_DECREF(doc);
    if (replacedDoc)
        return (PyObject*) cxoSodaDoc_new(op->coll->db, replacedDoc);
    Py_RETURN_NONE;
}