forked from oracle/python-cx_Oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcxoQueue.c
391 lines (336 loc) · 12.4 KB
/
cxoQueue.c
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
//-----------------------------------------------------------------------------
// Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// cxoQueue.c
// Defines the routines for handling queues (advanced queuing). These queues
// permit sending and receiving messages defined by the database.
//-----------------------------------------------------------------------------
#include "cxoModule.h"
//-----------------------------------------------------------------------------
// cxoQueue_new()
// Create a new queue (advanced queuing).
//-----------------------------------------------------------------------------
cxoQueue *cxoQueue_new(cxoConnection *conn, dpiQueue *handle)
{
dpiDeqOptions *deqOptions;
dpiEnqOptions *enqOptions;
cxoQueue *queue;
// create queue and populate basic attributes
queue = (cxoQueue*) cxoPyTypeQueue.tp_alloc(&cxoPyTypeQueue, 0);
if (!queue) {
dpiQueue_release(handle);
return NULL;
}
Py_INCREF(conn);
queue->conn = conn;
queue->handle = handle;
// get dequeue options
if (dpiQueue_getDeqOptions(queue->handle, &deqOptions) < 0) {
cxoError_raiseAndReturnNull();
Py_DECREF(queue);
return NULL;
}
queue->deqOptions = (PyObject*) cxoDeqOptions_new(conn, deqOptions);
if (!queue->deqOptions) {
Py_DECREF(queue);
return NULL;
}
// get enqueue options
if (dpiQueue_getEnqOptions(queue->handle, &enqOptions) < 0) {
cxoError_raiseAndReturnNull();
Py_DECREF(queue);
return NULL;
}
queue->enqOptions = (PyObject*) cxoEnqOptions_new(conn, enqOptions);
if (!queue->enqOptions) {
Py_DECREF(queue);
return NULL;
}
return queue;
}
//-----------------------------------------------------------------------------
// cxoQueue_free()
// Free the memory associated with a queue.
//-----------------------------------------------------------------------------
static void cxoQueue_free(cxoQueue *queue)
{
if (queue->handle) {
dpiQueue_release(queue->handle);
queue->handle = NULL;
}
Py_CLEAR(queue->conn);
Py_CLEAR(queue->name);
Py_CLEAR(queue->payloadType);
Py_CLEAR(queue->deqOptions);
Py_CLEAR(queue->enqOptions);
Py_TYPE(queue)->tp_free((PyObject*) queue);
}
//-----------------------------------------------------------------------------
// cxoQueue_repr()
// Return a string representation of a queue.
//-----------------------------------------------------------------------------
static PyObject *cxoQueue_repr(cxoQueue *queue)
{
PyObject *module, *name, *result;
if (cxoUtils_getModuleAndName(Py_TYPE(queue), &module, &name) < 0)
return NULL;
result = cxoUtils_formatString("<%s.%s %r>",
PyTuple_Pack(3, module, name, queue->name));
Py_DECREF(module);
Py_DECREF(name);
return result;
}
//-----------------------------------------------------------------------------
// cxoQueue_deqHelper()
// Helper for dequeuing messages from a queue.
//-----------------------------------------------------------------------------
int cxoQueue_deqHelper(cxoQueue *queue, uint32_t *numProps,
cxoMsgProps **props)
{
uint32_t bufferLength, i, j;
dpiMsgProps **handles;
dpiObject *objHandle;
const char *buffer;
cxoMsgProps *temp;
cxoObject *obj;
int ok, status;
// use the same array to store the intermediate values provided by ODPI-C;
// by doing so there is no need to allocate an additional array and any
// values created by this helper routine are cleaned up on error
handles = (dpiMsgProps**) props;
// perform dequeue
Py_BEGIN_ALLOW_THREADS
status = dpiQueue_deqMany(queue->handle, numProps, handles);
Py_END_ALLOW_THREADS
if (status < 0)
return cxoError_raiseAndReturnInt();
// create objects that are returned to the user
for (i = 0; i < *numProps; i++) {
// create message property object
temp = cxoMsgProps_new(queue->conn, handles[i]);
ok = (temp) ? 1 : 0;
props[i] = temp;
// get payload from ODPI-C message property
if (ok && dpiMsgProps_getPayload(temp->handle, &objHandle, &buffer,
&bufferLength) < 0) {
cxoError_raiseAndReturnInt();
ok = 0;
}
// store payload on cx_Oracle message property
if (ok && objHandle) {
obj = (cxoObject*) cxoObject_new(queue->payloadType, objHandle);
if (obj && dpiObject_addRef(objHandle) < 0) {
cxoError_raiseAndReturnInt();
obj->handle = NULL;
Py_CLEAR(obj);
ok = 0;
}
temp->payload = (PyObject*) obj;
} else if (ok) {
temp->payload = PyBytes_FromStringAndSize(buffer, bufferLength);
}
// if an error occurred, do some cleanup
if (!ok || !temp->payload) {
Py_XDECREF(temp);
for (j = 0; j < i; j++)
Py_DECREF(props[j]);
for (j = i + 1; j < *numProps; j++)
dpiMsgProps_release(handles[j]);
return -1;
}
}
return 0;
}
//-----------------------------------------------------------------------------
// cxoQueue_enqHelper()
// Helper for enqueuing messages from a queue.
//-----------------------------------------------------------------------------
int cxoQueue_enqHelper(cxoQueue *queue, uint32_t numProps,
cxoMsgProps **props)
{
dpiMsgProps **handles, *tempHandle;
cxoBuffer buffer;
cxoObject *obj;
uint32_t i;
int status;
// use the same array to store the intermediate values required by ODPI-C;
// by doing so there is no need to allocate an additional array
handles = (dpiMsgProps**) props;
// process array
for (i = 0; i < numProps; i++) {
// verify that the message property object has a payload
if (!props[i]->payload || props[i]->payload == Py_None) {
cxoError_raiseFromString(cxoProgrammingErrorException,
"message has no payload");
return -1;
}
// transfer payload to message properties object
tempHandle = props[i]->handle;
if (PyObject_IsInstance(props[i]->payload,
(PyObject*) &cxoPyTypeObject)) {
obj = (cxoObject*) props[i]->payload;
if (dpiMsgProps_setPayloadObject(props[i]->handle,
obj->handle) < 0)
return cxoError_raiseAndReturnInt();
} else {
if (cxoBuffer_fromObject(&buffer, props[i]->payload,
props[i]->encoding) < 0)
return -1;
status = dpiMsgProps_setPayloadBytes(props[i]->handle, buffer.ptr,
buffer.size);
cxoBuffer_clear(&buffer);
if (status < 0)
return cxoError_raiseAndReturnInt();
}
handles[i] = tempHandle;
}
// perform enqueue
Py_BEGIN_ALLOW_THREADS
status = dpiQueue_enqMany(queue->handle, numProps, handles);
Py_END_ALLOW_THREADS
if (status < 0)
return cxoError_raiseAndReturnInt();
return 0;
}
//-----------------------------------------------------------------------------
// cxoQueue_deqMany()
// Dequeue a single message to the queue.
//-----------------------------------------------------------------------------
static PyObject *cxoQueue_deqMany(cxoQueue *queue, PyObject *args)
{
unsigned int numPropsFromPython;
uint32_t numProps, i;
cxoMsgProps **props;
PyObject *result;
if (!PyArg_ParseTuple(args, "I", &numPropsFromPython))
return NULL;
numProps = (uint32_t) numPropsFromPython;
props = PyMem_Malloc(numProps * sizeof(cxoMsgProps*));
if (!props)
return NULL;
if (cxoQueue_deqHelper(queue, &numProps, props) < 0) {
PyMem_Free(props);
return NULL;
}
result = PyList_New(numProps);
if (!result) {
for (i = 0; i < numProps; i++)
Py_DECREF(props[i]);
PyMem_Free(props);
return NULL;
}
for (i = 0; i < numProps; i++)
PyList_SET_ITEM(result, i, (PyObject*) props[i]);
PyMem_Free(props);
return result;
}
//-----------------------------------------------------------------------------
// cxoQueue_deqOne()
// Dequeue a single message to the queue.
//-----------------------------------------------------------------------------
static PyObject *cxoQueue_deqOne(cxoQueue *queue, PyObject *args)
{
uint32_t numProps = 1;
cxoMsgProps *props;
if (cxoQueue_deqHelper(queue, &numProps, &props) < 0)
return NULL;
if (numProps > 0)
return (PyObject*) props;
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// cxoQueue_enqMany()
// Enqueue multiple messages to the queue.
//-----------------------------------------------------------------------------
static PyObject *cxoQueue_enqMany(cxoQueue *queue, PyObject *args)
{
PyObject *seq, *seqCheck, *temp;
Py_ssize_t seqLength, i;
cxoMsgProps **props;
int status;
// validate arguments
if (!PyArg_ParseTuple(args, "O", &seqCheck))
return NULL;
seq = PySequence_Fast(seqCheck, "expecting sequence");
if (!seq)
return NULL;
// zero messages means nothing to do
seqLength = PySequence_Length(seq);
if (seqLength == 0) {
Py_DECREF(seq);
Py_RETURN_NONE;
}
// populate array of properties
props = PyMem_Malloc(seqLength * sizeof(cxoMsgProps*));
if (!props) {
PyErr_NoMemory();
Py_DECREF(seq);
return NULL;
}
for (i = 0; i < seqLength; i++) {
temp = PySequence_Fast_GET_ITEM(seq, i);
if (Py_TYPE(temp) != &cxoPyTypeMsgProps) {
Py_DECREF(seq);
PyMem_Free(props);
PyErr_SetString(PyExc_TypeError,
"expecting sequence of message property objects");
return NULL;
}
props[i] = (cxoMsgProps*) temp;
}
// perform enqueue
status = cxoQueue_enqHelper(queue, (uint32_t) seqLength, props);
Py_DECREF(seq);
PyMem_Free(props);
if (status < 0)
return NULL;
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// cxoQueue_enqOne()
// Enqueue a single message to the queue.
//-----------------------------------------------------------------------------
static PyObject *cxoQueue_enqOne(cxoQueue *queue, PyObject *args)
{
cxoMsgProps *props;
if (!PyArg_ParseTuple(args, "O!", &cxoPyTypeMsgProps, &props))
return NULL;
if (cxoQueue_enqHelper(queue, 1, &props) < 0)
return NULL;
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// declaration of methods
//-----------------------------------------------------------------------------
static PyMethodDef cxoMethods[] = {
{ "deqMany", (PyCFunction) cxoQueue_deqMany, METH_VARARGS },
{ "deqOne", (PyCFunction) cxoQueue_deqOne, METH_NOARGS },
{ "enqMany", (PyCFunction) cxoQueue_enqMany, METH_VARARGS },
{ "enqOne", (PyCFunction) cxoQueue_enqOne, METH_VARARGS },
{ NULL }
};
//-----------------------------------------------------------------------------
// declaration of members
//-----------------------------------------------------------------------------
static PyMemberDef cxoMembers[] = {
{ "connection", T_OBJECT, offsetof(cxoQueue, conn), READONLY },
{ "deqOptions", T_OBJECT, offsetof(cxoQueue, deqOptions), READONLY },
{ "enqOptions", T_OBJECT, offsetof(cxoQueue, enqOptions), READONLY },
{ "name", T_OBJECT, offsetof(cxoQueue, name), READONLY },
{ "payloadType", T_OBJECT, offsetof(cxoQueue, payloadType), READONLY },
{ NULL }
};
//-----------------------------------------------------------------------------
// Python type declarations
//-----------------------------------------------------------------------------
PyTypeObject cxoPyTypeQueue = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "cx_Oracle.Queue",
.tp_basicsize = sizeof(cxoQueue),
.tp_dealloc = (destructor) cxoQueue_free,
.tp_repr = (reprfunc) cxoQueue_repr,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_methods = cxoMethods,
.tp_members = cxoMembers
};