-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathnjsAqQueue.c
484 lines (427 loc) · 17.2 KB
/
njsAqQueue.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
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
// Copyright (c) 2019, 2024, Oracle and/or its affiliates.
//-----------------------------------------------------------------------------
//
// This software is dual-licensed to you under the Universal Permissive License
// (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
// 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
// either license.
//
// If you elect to accept the software under the Apache License, Version 2.0,
// the following applies:
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// NAME
// njsAqQueue.c
//
// DESCRIPTION
// AqQueue (Advanced Queuing Queue) class implementation.
//
//-----------------------------------------------------------------------------
#include "njsModule.h"
// class methods
NJS_NAPI_METHOD_DECL_ASYNC(njsAqQueue_deq);
NJS_NAPI_METHOD_DECL_ASYNC(njsAqQueue_enq);
// asynchronous methods
static NJS_ASYNC_METHOD(njsAqQueue_deqAsync);
static NJS_ASYNC_METHOD(njsAqQueue_enqAsync);
// post asynchronous methods
static NJS_ASYNC_POST_METHOD(njsAqQueue_deqPostAsync);
static NJS_ASYNC_POST_METHOD(njsAqQueue_enqPostAsync);
// finalize
static NJS_NAPI_FINALIZE(njsAqQueue_finalize);
// properties defined by the class
static const napi_property_descriptor njsClassProperties[] = {
{ "deq", NULL, njsAqQueue_deq, NULL, NULL, NULL, napi_default,
NULL },
{ "enq", NULL, njsAqQueue_enq, NULL, NULL, NULL, napi_default,
NULL },
{ NULL, NULL, NULL, NULL, NULL, NULL, napi_default, NULL }
};
// class definition
const njsClassDef njsClassDefAqQueue = {
"AqQueueImpl", sizeof(njsAqQueue), njsAqQueue_finalize,
njsClassProperties, false
};
// other methods used internally
static bool njsAqQueue_createMessage(njsBaton *baton, njsAqQueue *queue,
napi_env env, napi_value value, dpiMsgProps **handle);
static bool njsAqQueue_setRecipients(njsBaton *baton, dpiMsgProps *handle,
char **recipArr, uint32_t *recipLengths, uint32_t recipCount);
//----------------------------------------------------------------------------
// njsAqQueue_setRecipients()
// To process the recipient list
//----------------------------------------------------------------------------
bool njsAqQueue_setRecipients(njsBaton *baton, dpiMsgProps *handle,
char **recipArr, uint32_t *recipLengths, uint32_t recipCount)
{
uint32_t i;
int status;
dpiMsgRecipient *recipients = malloc(sizeof(dpiMsgRecipient) * recipCount);
if (!recipients)
return njsBaton_setErrorInsufficientMemory(baton);
for (i = 0; i < recipCount; i++) {
recipients[i].name = recipArr[i];
recipients[i].nameLength = recipLengths[i];
}
status = dpiMsgProps_setRecipients(handle, recipients, recipCount);
free(recipients);
if (status < 0)
return njsBaton_setErrorDPI(baton);
return true;
}
//-----------------------------------------------------------------------------
// njsAqQueue_createMessage()
// Creates a ODPI-C message properties handle which will be used to perform
// the actual enqueue operation. Each message must be a plain buffer or string
// (in which case all message properties are defaulted), or an object
// containing a "payload" property along with other properties specifying the
// properties to use during the enqueue.
//-----------------------------------------------------------------------------
static bool njsAqQueue_createMessage(njsBaton *baton, njsAqQueue *queue,
napi_env env, napi_value value, dpiMsgProps **handle)
{
napi_value payloadObj, constructor, tempObj;
dpiMsgProps *tempHandle;
bool isDbObject;
size_t bufferLength;
njsDbObject *obj;
int32_t intValue;
uint32_t recipCount = 0;
char **recipArr = NULL;
uint32_t *recipLengths = NULL;
char *buffer;
int status;
bool ok = true;
uint32_t i;
njsJsonBuffer jsonBuffer;
dpiJson *json;
// create new ODPI-C message properties handle
if (dpiConn_newMsgProps(queue->conn->handle, &tempHandle))
return njsBaton_setErrorDPI(baton);
*handle = tempHandle;
// set payload
NJS_CHECK_NAPI(env, napi_get_named_property(env, value, "payload",
&payloadObj))
NJS_CHECK_NAPI(env, napi_get_reference_value(env,
baton->globals->jsDbObjectConstructor, &constructor))
NJS_CHECK_NAPI(env, napi_instanceof(env, payloadObj, constructor,
&isDbObject))
if (isDbObject) {
// DB Object
if (!njsDbObject_getInstance(baton->globals, env, payloadObj, &obj))
return false;
status = dpiMsgProps_setPayloadObject(tempHandle, obj->handle);
} else if (queue->isJson) {
// JSON
if (!njsJsonBuffer_fromValue(&jsonBuffer, env, payloadObj, baton))
return false;
if (dpiConn_newJson(queue->conn->handle, &json) < 0) {
njsJsonBuffer_free(&jsonBuffer);
return njsBaton_setErrorDPI(baton);
}
if (dpiJson_setValue(json, &jsonBuffer.topNode) < 0) {
njsJsonBuffer_free(&jsonBuffer);
return false;
}
status = dpiMsgProps_setPayloadJson(*handle, json);
njsJsonBuffer_free(&jsonBuffer);
} else {
// RAW case
NJS_CHECK_NAPI(env, napi_get_buffer_info(env, payloadObj,
(void**) &buffer, &bufferLength))
status = dpiMsgProps_setPayloadBytes(tempHandle, buffer,
(uint32_t) bufferLength);
}
if (status < 0)
return njsBaton_setErrorDPI(baton);
// set correlation, if applicable
buffer = NULL;
if (!njsUtils_getNamedPropertyString(env, value, "correlation",
&buffer, &bufferLength))
return false;
if (buffer) {
status = dpiMsgProps_setCorrelation(tempHandle, buffer,
(uint32_t) bufferLength);
free(buffer);
if (status < 0)
return njsBaton_setErrorDPI(baton);
}
// set delay, if applicable
if (!njsUtils_getNamedProperty(env, value, "delay", &tempObj))
return false;
if (tempObj) {
NJS_CHECK_NAPI(env, napi_get_value_int32(env, tempObj, &intValue))
if (dpiMsgProps_setDelay(tempHandle, intValue) < 0)
return njsBaton_setErrorDPI(baton);
}
// set exception queue, if applicable
buffer = NULL;
if (!njsUtils_getNamedPropertyString(env, value, "exceptionQueue",
&buffer, &bufferLength))
return false;
if (buffer) {
status = dpiMsgProps_setExceptionQ(tempHandle, buffer,
(uint32_t) bufferLength);
free(buffer);
if (status < 0)
return njsBaton_setErrorDPI(baton);
}
// set expiration, if applicable
if (!njsUtils_getNamedProperty(env, value, "expiration", &tempObj))
return false;
if (tempObj) {
NJS_CHECK_NAPI(env, napi_get_value_int32(env, tempObj, &intValue))
if (dpiMsgProps_setExpiration(tempHandle, intValue) < 0)
return njsBaton_setErrorDPI(baton);
}
// set priority, if applicable
if (!njsUtils_getNamedProperty(env, value, "priority", &tempObj))
return false;
if (tempObj) {
NJS_CHECK_NAPI(env, napi_get_value_int32(env, tempObj, &intValue))
if (dpiMsgProps_setPriority(tempHandle, intValue) < 0)
return njsBaton_setErrorDPI(baton);
}
// set recipient list, if applicable
if (!njsUtils_getNamedPropertyStringArray(env, value, "recipients",
&recipCount, &recipArr, &recipLengths))
return false;
if (recipCount > 0) {
ok = njsAqQueue_setRecipients(baton, tempHandle, recipArr,
recipLengths, recipCount);
for (i = 0; i < recipCount; i ++) {
free(recipArr[i]);
}
free(recipArr);
free(recipLengths);
}
return ok;
}
//-----------------------------------------------------------------------------
// njsAqQueue_createFromHandle()
// Creates a new AQ queue object given the ODPI-C handle.
//-----------------------------------------------------------------------------
bool njsAqQueue_createFromHandle(njsBaton *baton, napi_env env,
napi_value *queueObj)
{
njsConnection *conn = (njsConnection*) baton->callingInstance;
napi_value deqOptionsObj, enqOptionsObj, temp;
napi_property_descriptor descriptors[4];
dpiDeqOptions *deqOptionsHandle;
dpiEnqOptions *enqOptionsHandle;
njsAqDeqOptions *deqOptions;
njsAqEnqOptions *enqOptions;
njsAqQueue *queue;
uint32_t typeNum;
// create new instance
if (!njsUtils_genericNew(env, &njsClassDefAqQueue,
baton->globals->jsAqQueueConstructor, queueObj, (void**) &queue))
return false;
// perform some initializations
queue->handle = baton->dpiQueueHandle;
baton->dpiQueueHandle = NULL;
queue->conn = conn;
queue->isJson = baton->isJson;
// create the dequeue options object
if (dpiQueue_getDeqOptions(queue->handle, &deqOptionsHandle) < 0)
return njsUtils_throwErrorDPI(env, baton->globals);
if (!njsUtils_genericNew(env, &njsClassDefAqDeqOptions,
baton->globals->jsAqDeqOptionsConstructor, &deqOptionsObj,
(void**) &deqOptions))
return false;
if (dpiDeqOptions_addRef(deqOptionsHandle) < 0)
return njsUtils_throwErrorDPI(env, baton->globals);
deqOptions->handle = deqOptionsHandle;
// create the enqueue options object
if (dpiQueue_getEnqOptions(queue->handle, &enqOptionsHandle) < 0)
return njsUtils_throwErrorDPI(env, baton->globals);
if (!njsUtils_genericNew(env, &njsClassDefAqEnqOptions,
baton->globals->jsAqEnqOptionsConstructor, &enqOptionsObj,
(void**) &enqOptions))
return false;
if (dpiEnqOptions_addRef(enqOptionsHandle) < 0)
return njsUtils_throwErrorDPI(env, baton->globals);
enqOptions->handle = enqOptionsHandle;
enqOptions->deliveryMode = DPI_MODE_MSG_PERSISTENT;
// define properties for the connection (to ensure that it is not garbage
// collected before the queue itself is) and for the dequeue and enqueue
// options objects (for convenience)
memset(descriptors, 0, sizeof(napi_property_descriptor) * 4);
descriptors[0].utf8name = "_connection";
NJS_CHECK_NAPI(env, napi_get_reference_value(env, baton->jsCallingObjRef,
&descriptors[0].value))
descriptors[1].utf8name = "deqOptions";
descriptors[1].value = deqOptionsObj;
descriptors[1].attributes = napi_enumerable;
descriptors[2].utf8name = "enqOptions";
descriptors[2].value = enqOptionsObj;
descriptors[2].attributes = napi_enumerable;
descriptors[3].utf8name = "name";
descriptors[3].attributes = napi_enumerable;
NJS_CHECK_NAPI(env, napi_create_string_utf8(env, baton->name,
baton->nameLength, &descriptors[3].value))
NJS_CHECK_NAPI(env, napi_define_properties(env, *queueObj, 4,
descriptors))
// acquire object type class, if needed
if (baton->dpiObjectTypeHandle && !njsDbObject_getSubClass(baton,
baton->dpiObjectTypeHandle, env, &temp,
&queue->payloadObjectType))
return false;
// add type properties
typeNum = baton->isJson ?
DPI_ORACLE_TYPE_JSON :
(queue->payloadObjectType) ? DPI_ORACLE_TYPE_OBJECT :
DPI_ORACLE_TYPE_RAW;
if (!njsUtils_addTypeProperties(env, *queueObj, "payloadType",
typeNum, queue->payloadObjectType))
return false;
return true;
}
//-----------------------------------------------------------------------------
// njsAqQueue_deq()
// Dequeue messages from an AQ queue.
//
// PARAMETERS
// - an array of one or more messages to dequeue
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_ASYNC(njsAqQueue_deq, 1, NULL)
{
NJS_CHECK_NAPI(env, napi_get_value_uint32(env, args[0],
&baton->numMsgProps))
return njsBaton_queueWork(baton, env, "Deq", njsAqQueue_deqAsync,
njsAqQueue_deqPostAsync, returnValue);
}
//-----------------------------------------------------------------------------
// njsAqQueue_deqAsync()
// Worker function for njsAqQueue_deqOne() & njsAqueue_deqMany()
//-----------------------------------------------------------------------------
static bool njsAqQueue_deqAsync(njsBaton* baton)
{
njsAqQueue *queue = (njsAqQueue*) baton->callingInstance;
baton->msgProps = calloc(baton->numMsgProps, sizeof(dpiMsgProps*));
if (!baton->msgProps)
return njsBaton_setErrorInsufficientMemory(baton);
if (baton->numMsgProps == 1) {
if (dpiQueue_deqOne(queue->handle, &baton->msgProps[0]) < 0)
return njsBaton_setErrorDPI(baton);
} else {
// deqMany case
if (dpiQueue_deqMany(queue->handle, &baton->numMsgProps,
baton->msgProps) < 0)
return njsBaton_setErrorDPI(baton);
}
return true;
}
//-----------------------------------------------------------------------------
// njsAqQueue_deqPostAsync()
// Defines the value returned to JS.
//-----------------------------------------------------------------------------
static bool njsAqQueue_deqPostAsync(njsBaton *baton, napi_env env,
napi_value *result)
{
njsAqQueue *queue = (njsAqQueue*) baton->callingInstance;
napi_value temp;
uint32_t i;
NJS_CHECK_NAPI(env, napi_create_array_with_length(env, baton->numMsgProps,
result))
for (i = 0; i < baton->numMsgProps; i++) {
if (!njsAqMessage_createFromHandle(baton, baton->msgProps[i], env,
queue, &temp))
return false;
baton->msgProps[i] = NULL;
NJS_CHECK_NAPI(env, napi_set_element(env, *result, i, temp))
}
return true;
}
//-----------------------------------------------------------------------------
// njsAqQueue_enq()
// Enqueue message(s) into an AQ queue.
//
// PARAMETERS
// - array of one or more messages to enqueue
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_ASYNC(njsAqQueue_enq, 1, NULL)
{
njsAqQueue *queue = (njsAqQueue*) baton->callingInstance;
napi_value message;
uint32_t i;
NJS_CHECK_NAPI(env, napi_get_array_length(env, args[0],
&baton->numMsgProps))
baton->msgProps = calloc(baton->numMsgProps, sizeof(dpiMsgProps*));
if (!baton->msgProps)
return njsBaton_setErrorInsufficientMemory(baton);
for (i = 0; i < baton->numMsgProps; i++) {
NJS_CHECK_NAPI(env, napi_get_element(env, args[0], i, &message))
if (!njsAqQueue_createMessage(baton, queue, env, message,
&baton->msgProps[i]))
return false;
}
return njsBaton_queueWork(baton, env, "Enq", njsAqQueue_enqAsync,
njsAqQueue_enqPostAsync, returnValue);
}
//-----------------------------------------------------------------------------
// njsAqQueue_enqAsync()
//
// Worker thread function for njsAqQueue_enqOne() & njsAqQueue_enqMany()
//-----------------------------------------------------------------------------
static bool njsAqQueue_enqAsync(njsBaton *baton)
{
njsAqQueue *queue = (njsAqQueue*) baton->callingInstance;
if (baton->numMsgProps == 1) {
// enqOne case
if (dpiQueue_enqOne(queue->handle, baton->msgProps[0]) < 0)
return njsBaton_setErrorDPI(baton);
} else {
// enqMany case
if (dpiQueue_enqMany(queue->handle, baton->numMsgProps,
baton->msgProps) < 0)
return njsBaton_setErrorDPI(baton);
}
return true;
}
//-----------------------------------------------------------------------------
// njsAqQueue_enqOnePostAsync()
// Defines the value returned to JS.
//-----------------------------------------------------------------------------
static bool njsAqQueue_enqPostAsync(njsBaton *baton, napi_env env,
napi_value *result)
{
njsAqQueue *queue = (njsAqQueue*) baton->callingInstance;
napi_value temp;
uint32_t i;
NJS_CHECK_NAPI(env, napi_create_array_with_length(env, baton->numMsgProps,
result))
for (i = 0; i < baton->numMsgProps; i++) {
if (!njsAqMessage_createFromHandle(baton, baton->msgProps[i], env,
queue, &temp))
return false;
baton->msgProps[i] = NULL;
NJS_CHECK_NAPI(env, napi_set_element(env, *result, i, temp))
}
return true;
}
//-----------------------------------------------------------------------------
// njsAqQueue_finalize()
// Invoked when the njsAqQueue object is garbage collected.
//-----------------------------------------------------------------------------
static void njsAqQueue_finalize(napi_env env, void *finalizeData,
void *finalizeHint)
{
njsAqQueue *queue = (njsAqQueue*) finalizeData;
if (queue->handle) {
dpiQueue_release(queue->handle);
queue->handle = NULL;
}
free(queue);
}