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)

#where_binsObject (readonly)

Instance Method Details

#exec(, vSelf) ⇒ Object



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

VALUE query_exec(int argc, VALUE* vArgs, VALUE vSelf)
{
    VALUE vNamespace;
    VALUE vSet;
    VALUE vArray;
    VALUE vClient;
    VALUE vWhere, vSelect, vOrder;
    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");
        }
    }

    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



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'ext/aerospike_native/query.c', line 82

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



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

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



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'ext/aerospike_native/query.c', line 125

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