Class: AerospikeNative::Query

Inherits:
Object
  • Object
show all
Defined in:
ext/aerospike_native/query.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vClient, vNamespace, vSet) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'ext/aerospike_native/query.c', line 8

VALUE query_initialize(VALUE vSelf, VALUE vClient, VALUE vNamespace, VALUE vSet)
{
    Check_Type(vNamespace, T_STRING);
    Check_Type(vSet, T_STRING);
    check_aerospike_client(vClient);

    rb_iv_set(vSelf, "@client", vClient);
    rb_iv_set(vSelf, "@namespace", vNamespace);
    rb_iv_set(vSelf, "@set", vSet);

    return vSelf;
}

Instance Attribute Details

#clientObject (readonly)

#namespaceObject (readonly)

#order_binsObject (readonly)

#select_binsObject (readonly)

#setObject (readonly)

#udf_arglistObject (readonly)

#udf_functionObject (readonly)

#udf_moduleObject (readonly)

#where_binsObject (readonly)

Instance Method Details

#apply(, vSelf) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'ext/aerospike_native/query.c', line 146

VALUE query_apply(int argc, VALUE* vArgs, VALUE vSelf)
{
    if (argc < 2 || argc > 3) {  // there should only be 2 or 3 arguments
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 2..3)", argc);
    }

    Check_Type(vArgs[0], T_STRING);
    Check_Type(vArgs[1], T_STRING);
    rb_iv_set(vSelf, "@udf_module", vArgs[0]);
    rb_iv_set(vSelf, "@udf_function", vArgs[1]);

    if (argc == 3 && TYPE(vArgs[2]) != T_NIL) {
        Check_Type(vArgs[2], T_ARRAY);
    }

    return vSelf;
}

#exec(, vSelf) ⇒ Object



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;
}

#order(vHash) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'ext/aerospike_native/query.c', line 89

VALUE query_order(VALUE vSelf, VALUE vHash)
{
    VALUE vBins;
    Check_Type(vHash, T_HASH);

    vBins = rb_iv_get(vSelf, "@order_bins");
    if(TYPE(vBins) == T_NIL) {
        vBins = rb_hash_new();
    }
    rb_hash_foreach(vHash, query_order_hash_foreach, vBins);
    rb_iv_set(vSelf, "@order_bins", vBins);
    return vSelf;
}

#select(, vSelf) ⇒ Object



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
# File 'ext/aerospike_native/query.c', line 28

VALUE query_select(int argc, VALUE* vArgs, VALUE vSelf)
{
    VALUE vBins;
    int i;

    if (argc == 0) {  // there should be greater than 0
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..n)", argc);
    }

    vBins = rb_iv_get(vSelf, "@select_bins");
    if(TYPE(vBins) == T_NIL) {
        vBins = rb_ary_new();
    }
    if (argc == 1) {
        int idx;
        VALUE vArray = vArgs[0];
        Check_Type(vArray, T_ARRAY);
        idx = RARRAY_LEN(vArray);
        for(i = 0; i < idx; i++) {
            VALUE vString = rb_ary_entry(vArray, i);
            GET_STRING(vString);
            rb_ary_push(vBins, vString);
        }
    } else {
        for(i = 0; i < argc; i++) {
            VALUE vString = vArgs[i];
            GET_STRING(vString);
            rb_ary_push(vBins, vString);
        }
    }

    rb_iv_set(vSelf, "@select_bins", vBins);
    return vSelf;
}

#where(vHash) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'ext/aerospike_native/query.c', line 132

VALUE query_where(VALUE vSelf, VALUE vHash)
{
    VALUE vBins;
    Check_Type(vHash, T_HASH);

    vBins = rb_iv_get(vSelf, "@where_bins");
    if(TYPE(vBins) == T_NIL) {
        vBins = rb_hash_new();
    }
    rb_hash_foreach(vHash, query_where_hash_foreach, vBins);
    rb_iv_set(vSelf, "@where_bins", vBins);
    return vSelf;
}