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
|
# File 'ext/aerospike_native/query.c', line 222
VALUE query_exec(int argc, VALUE* vArgs, VALUE vSelf)
{
VALUE vNamespace;
VALUE vSet;
VALUE vArray;
VALUE vClient;
VALUE vWhere, vSelect, vOrder;
VALUE vUdfModule;
VALUE vWhereKeys, vOrderKeys;
aerospike *ptr;
as_error err;
as_policy_query policy;
as_query query;
int n = 0;
int where_idx = 0, select_idx = 0, order_idx = 0;
if (argc > 1) { // there should only be 0 or 1 arguments
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
}
vNamespace = rb_iv_get(vSelf, "@namespace");
vSet = rb_iv_get(vSelf, "@set");
vWhere = rb_iv_get(vSelf, "@where_bins");
switch(TYPE(vWhere)) {
case T_NIL:
break;
case T_HASH:
vWhereKeys = rb_hash_keys(vWhere);
where_idx = RARRAY_LEN(vWhereKeys);
break;
default:
rb_raise(rb_eTypeError, "wrong argument type for where (expected Hash or Nil)");
}
vSelect = rb_iv_get(vSelf, "@select_bins");
switch(TYPE(vSelect)) {
case T_NIL:
break;
case T_ARRAY:
select_idx = RARRAY_LEN(vSelect);
break;
default:
rb_raise(rb_eTypeError, "wrong argument type for select (expected Array or Nil)");
}
vOrder = rb_iv_get(vSelf, "@order_bins");
switch(TYPE(vOrder)) {
case T_NIL:
break;
case T_HASH:
vOrderKeys = rb_hash_keys(vOrder);
order_idx = RARRAY_LEN(vOrderKeys);
break;
default:
rb_raise(rb_eTypeError, "wrong argument type for order (expected Hash or Nil)");
}
as_policy_query_init(&policy);
if (argc == 1 && TYPE(vArgs[0]) != T_NIL) {
SET_POLICY(policy, vArgs[0]);
}
vClient = rb_iv_get(vSelf, "@client");
Data_Get_Struct(vClient, aerospike, ptr);
as_query_init(&query, StringValueCStr(vNamespace), StringValueCStr(vSet));
as_query_select_inita(&query, select_idx);
for(n = 0; n < select_idx; n++) {
VALUE vBinName;
vBinName = rb_ary_entry(vSelect, n);
as_query_select(&query, StringValueCStr(vBinName));
}
as_query_orderby_inita(&query, order_idx);
for(n = 0; n < order_idx; n++) {
VALUE vBinName;
VALUE vCondition;
vBinName = rb_ary_entry(vOrderKeys, n);
vCondition = rb_hash_aref(vOrder, vBinName);
as_query_orderby(&query, StringValueCStr(vBinName), NUM2INT(vCondition));
}
as_query_where_inita(&query, where_idx);
for(n = 0; n < where_idx; n++) {
VALUE vMin = Qnil, vMax = Qnil, vBinName;
VALUE vCondition;
vBinName = rb_ary_entry(vWhereKeys, n);
vCondition = rb_hash_aref(vWhere, vBinName);
switch(TYPE(vCondition)) {
case T_ARRAY:
vMin = rb_ary_entry(vCondition, 0);
vMax = rb_ary_entry(vCondition, 1);
break;
default:
vMin = vCondition;
}
switch(TYPE(vMin)) {
case T_FIXNUM:
switch(TYPE(vMax)) {
case T_NIL:
as_query_where(&query, StringValueCStr(vBinName), as_integer_equals(FIX2LONG(vMin)));
break;
case T_FIXNUM:
as_query_where(&query, StringValueCStr(vBinName), as_integer_range(FIX2LONG(vMin), FIX2LONG(vMax)));
break;
default:
rb_raise(rb_eArgError, "Incorrect condition");
}
break;
case T_STRING:
Check_Type(vMax, T_NIL);
as_query_where(&query, StringValueCStr(vBinName), as_string_equals(StringValueCStr(vMin)));
break;
default:
rb_raise(rb_eArgError, "Incorrect condition");
}
}
vUdfModule = rb_iv_get(vSelf, "@udf_module");
switch(TYPE(vUdfModule)) {
case T_NIL:
break;
case T_STRING: {
VALUE vUdfFunction = rb_iv_get(vSelf, "@udf_function");
as_query_apply(&query, StringValueCStr(vUdfModule), StringValueCStr(vUdfFunction), NULL);
break;
}
default:
rb_raise(rb_eTypeError, "wrong argument type for udf module (expected String or Nil)");
}
vArray = rb_ary_new();
if (aerospike_query_foreach(ptr, &err, &policy, &query, query_callback, &vArray) != AEROSPIKE_OK) {
as_query_destroy(&query);
raise_aerospike_exception(err.code, err.message);
}
as_query_destroy(&query);
if ( rb_block_given_p() ) {
return Qnil;
}
return vArray;
}
|