Class: AerospikeNative::Client

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#newAerospikeNative::Client #new(hosts) ⇒ AerospikeNative::Client

initialize new client, use => …, ‘port’ => … for each hosts element

Overloads:



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

VALUE client_initialize(int argc, VALUE* argv, VALUE self)
{
    VALUE ary = Qnil;
    aerospike *ptr;
    as_config config;
    as_error err;
    long idx = 0, n = 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);
    }

    if (argc == 1) {
        ary = argv[0];
    }

    switch (TYPE(ary)) {
    case T_NIL:
    case T_ARRAY:
        break;
    default:
        /* raise exception */
        Check_Type(ary, T_ARRAY);
        break;
    }
    Data_Get_Struct(self, aerospike, ptr);

    as_config_init(&config);

    if (TYPE(ary) == T_ARRAY) {
        idx = RARRAY_LEN(ary);
        for(n = 0; n < idx; n++) {
            VALUE vHost, vPort;
            VALUE hash = rb_ary_entry(ary, n);
            vHost = rb_hash_aref(hash, rb_str_new2("host"));
            if (TYPE(vHost) == T_NIL) {
                vHost = rb_hash_aref(hash, ID2SYM( rb_intern("host") ));
            }
            vPort = rb_hash_aref(hash, rb_str_new2("port"));
            if (TYPE(vPort) == T_NIL) {
                vPort = rb_hash_aref(hash, ID2SYM( rb_intern("port") ));
            }
            as_config_add_host(&config, StringValueCStr(vHost), NUM2UINT(vPort));
        }
    }

    if (idx == 0) {
        as_config_add_host(&config, "127.0.0.1", 3000);
    }

    aerospike_init(ptr, &config);

    if ( aerospike_connect(ptr, &err) != AEROSPIKE_OK ) {
        raise_aerospike_exception(err.code, err.message);
    }
    return self;
}

Class Method Details

.set_log_level(vLevel) ⇒ Object



649
650
651
652
653
654
655
# File 'ext/aerospike_native/client.c', line 649

VALUE client_set_log_level(VALUE vSelf, VALUE vLevel)
{
    VALUE vLogger = rb_cv_get(vSelf, "@@logger");
    Check_Type(vLevel, T_SYMBOL);

    return rb_funcall(vLogger, rb_intern("set_level"), 1, vLevel);
}

.set_logger(vNewLogger) ⇒ Object



641
642
643
644
645
646
647
# File 'ext/aerospike_native/client.c', line 641

VALUE client_set_logger(VALUE vSelf, VALUE vNewLogger)
{
    VALUE vLogger = rb_cv_get(vSelf, "@@logger");
    rb_iv_set(vLogger, "@internal", vNewLogger);

    return vLogger;
}

Instance Method Details

#create_index(, vSelf) ⇒ Object



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'ext/aerospike_native/client.c', line 518

VALUE client_create_index(int argc, VALUE* vArgs, VALUE vSelf)
{
    VALUE vNamespace, vSet, vBinName, vIndexName;

    aerospike *ptr;
    as_error err;
    as_policy_info policy;
    as_index_task task;
    bool is_integer = true;

    if (argc > 5 || argc < 4) {  // there should only be 4 or 5 arguments
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 4..5)", argc);
    }

    vNamespace = vArgs[0];
    Check_Type(vNamespace, T_STRING);

    vSet = vArgs[1];
    Check_Type(vSet, T_STRING);

    vBinName = vArgs[2];
    Check_Type(vBinName, T_STRING);

    vIndexName = vArgs[3];
    Check_Type(vIndexName, T_STRING);

    if (argc == 5 && TYPE(vArgs[4]) != T_NIL) {
        VALUE vType = Qnil;
        SET_INFO_POLICY(policy, vArgs[4]);
        vType = rb_hash_aref(vArgs[4], rb_str_new2("type"));
        if (TYPE(vType) == T_FIXNUM) {
            switch(FIX2INT(vType)) {
            case INDEX_NUMERIC:
                is_integer = true;
                break;
            case INDEX_STRING:
                is_integer = false;
                break;
            default:
                rb_raise(rb_eArgError, "Incorrect index type");
            }
        }
    }

    Data_Get_Struct(vSelf, aerospike, ptr);

    if (is_integer) {
        if (aerospike_index_integer_create(ptr, &err, &policy, StringValueCStr(vNamespace), StringValueCStr(vSet), StringValueCStr(vBinName), StringValueCStr(vIndexName)) != AEROSPIKE_OK) {
            raise_aerospike_exception(err.code, err.message);
        }
    } else {
        if (aerospike_index_string_create(ptr, &err, &policy, StringValueCStr(vNamespace), StringValueCStr(vSet), StringValueCStr(vBinName), StringValueCStr(vIndexName)) != AEROSPIKE_OK) {
            raise_aerospike_exception(err.code, err.message);
        }
    }

    task.as = ptr;
    strcpy(task.ns, StringValueCStr(vNamespace));
    strcpy(task.name, StringValueCStr(vIndexName));
    task.done = false;
    if (aerospike_index_create_wait(&err, &task, 1000) != AEROSPIKE_OK) {
        raise_aerospike_exception(err.code, err.message);
    }

    return (task.done ? Qtrue : Qfalse);
}

#drop_index(, vSelf) ⇒ Object



592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'ext/aerospike_native/client.c', line 592

VALUE client_drop_index(int argc, VALUE* vArgs, VALUE vSelf)
{
    VALUE vNamespace, vIndexName;

    aerospike *ptr;
    as_error err;
    as_policy_info policy;

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

    vNamespace = vArgs[0];
    Check_Type(vNamespace, T_STRING);

    vIndexName = vArgs[1];
    Check_Type(vIndexName, T_STRING);

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

    Data_Get_Struct(vSelf, aerospike, ptr);

    if (aerospike_index_remove(ptr, &err, &policy, StringValueCStr(vNamespace), StringValueCStr(vIndexName)) != AEROSPIKE_OK) {
        raise_aerospike_exception(err.code, err.message);
    }

    return Qtrue;
}

#exists?(, vSelf) ⇒ Object



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

VALUE client_exists(int argc, VALUE* vArgs, VALUE vSelf)
{
    VALUE vKey;

    aerospike *ptr;
    as_key* key;
    as_error err;
    as_policy_read policy;
    as_record* record = NULL;
    as_status status;

    if (argc > 2 || argc < 1) {  // there should only be 1 or 2 arguments
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
    }

    vKey = vArgs[0];
    check_aerospike_key(vKey);

    if (argc == 2 && TYPE(vArgs[1]) != T_NIL) {
        SET_READ_POLICY(policy, vArgs[1]);
    }

    Data_Get_Struct(vSelf, aerospike, ptr);
    Data_Get_Struct(vKey, as_key, key);

    status = aerospike_key_exists(ptr, &err, &policy, key, &record);
    if (status != AEROSPIKE_OK && status != AEROSPIKE_ERR_RECORD_NOT_FOUND) {
        as_record_destroy(record);
        raise_aerospike_exception(err.code, err.message);
    }
    as_record_destroy(record);

    if (status == AEROSPIKE_ERR_RECORD_NOT_FOUND) {
        return Qfalse;
    }

    return Qtrue;
}

#get(, vSelf) ⇒ Object



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

VALUE client_get(int argc, VALUE* vArgs, VALUE vSelf)
{
    VALUE vKey;

    aerospike *ptr;
    as_key* key;
    as_error err;
    as_record* record = NULL;
    as_policy_read policy;

    if (argc > 2 || argc < 1) {  // there should only be 1 or 2 arguments
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
    }

    vKey = vArgs[0];
    check_aerospike_key(vKey);

    as_policy_read_init(&policy);

    if (argc == 2 && TYPE(vArgs[1]) != T_NIL) {
        SET_READ_POLICY(policy, vArgs[1]);
    }

    Data_Get_Struct(vSelf, aerospike, ptr);
    Data_Get_Struct(vKey, as_key, key);

    if (aerospike_key_get(ptr, &err, &policy, key, &record) != AEROSPIKE_OK) {
        as_record_destroy(record);
        raise_aerospike_exception(err.code, err.message);
    }

    return rb_record_from_c(record, key);
}

#operate(, vSelf) ⇒ Object



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

VALUE client_operate(int argc, VALUE* vArgs, VALUE vSelf)
{
    VALUE vKey;
    VALUE vOperations;
    long idx = 0, n = 0;
    bool isset_read = false;

    aerospike *ptr;
    as_operations ops;
    as_key* key;
    as_error err;
    as_policy_operate policy;
    as_record* record = NULL;

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

    vKey = vArgs[0];
    check_aerospike_key(vKey);

    vOperations = vArgs[1];
    Check_Type(vOperations, T_ARRAY);
    as_policy_operate_init(&policy);

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

    idx = RARRAY_LEN(vOperations);
    if (idx == 0) {
        return Qfalse;
    }

    Data_Get_Struct(vSelf, aerospike, ptr);
    as_operations_inita(&ops, idx);

   for(n = 0; n < idx; n++) {
        VALUE operation = rb_ary_entry(vOperations, n);
        int op_type = NUM2INT( rb_iv_get(operation, "@op_type") );
        VALUE bin_name = rb_iv_get(operation, "@bin_name");
        VALUE bin_value = rb_iv_get(operation, "@bin_value");

        switch( op_type ) {
        case OPERATION_WRITE:
            switch( TYPE(bin_value) ) {
            case T_NIL: {
                as_record rec;
                as_record_inita(&rec, 1);
                as_record_set_nil(&rec, StringValueCStr( bin_name ));
                ops.binops.entries[ops.binops.size].op = AS_OPERATOR_WRITE;
                ops.binops.entries[ops.binops.size].bin = rec.bins.entries[0];
                ops.binops.size++;
                break;
            }
            case T_STRING:
                as_operations_add_write_str(&ops, StringValueCStr( bin_name ), StringValueCStr( bin_value ));
                break;
            case T_FIXNUM:
                as_operations_add_write_int64(&ops, StringValueCStr( bin_name ), NUM2LONG( bin_value ));
                break;
//            case T_ARRAY:
//            case T_HASH:
//                rb_raise(rb_eTypeError, "wrong argument type for bin value (hashes and arrays not supported yet)");
//                break;
            default: {
                VALUE vBytes = rb_funcall(bin_value, rb_intern("to_msgpack"), 0);
                int strSize = RSTRING_LEN(vBytes);
                as_operations_add_write_raw(&ops, StringValueCStr(bin_name), StringValuePtr(vBytes), strSize);
                break;
            }
            }

            break;
        case OPERATION_READ:
            isset_read = true;
            as_operations_add_read(&ops, StringValueCStr( bin_name ));
            break;
        case OPERATION_INCREMENT:
            as_operations_add_incr(&ops, StringValueCStr( bin_name ), NUM2INT( bin_value ));
            break;
        case OPERATION_APPEND:
            Check_Type(bin_value, T_STRING);
            as_operations_add_append_str(&ops, StringValueCStr( bin_name ), StringValueCStr( bin_value ));
            break;
        case OPERATION_PREPEND:
            Check_Type(bin_value, T_STRING);
            as_operations_add_prepend_str(&ops, StringValueCStr( bin_name ), StringValueCStr( bin_value ));
            break;
        case OPERATION_TOUCH:
            isset_read = true;
            as_operations_add_touch(&ops);
            break;
        default:
            rb_raise(rb_eArgError, "Incorrect operation type");
            break;
        }
    }

    Data_Get_Struct(vKey, as_key, key);

    if (isset_read) {
        if (aerospike_key_operate(ptr, &err, &policy, key, &ops, &record) != AEROSPIKE_OK) {
            as_operations_destroy(&ops);
            as_record_destroy(record);
            raise_aerospike_exception(err.code, err.message);
        }

        as_operations_destroy(&ops);

        return rb_record_from_c(record, key);
    } else {
        if (aerospike_key_operate(ptr, &err, &policy, key, &ops, NULL) != AEROSPIKE_OK) {
            as_operations_destroy(&ops);
            raise_aerospike_exception(err.code, err.message);
        }

        as_operations_destroy(&ops);
        return Qtrue;
    }
}

#put(, vSelf) ⇒ Object



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

VALUE client_put(int argc, VALUE* vArgs, VALUE vSelf)
{
    VALUE vKey;
    VALUE vBins;
    VALUE vHashKeys;

    aerospike *ptr;
    as_key* key;
    as_error err;
    as_record record;
    as_policy_write policy;

    int idx = 0, n = 0;

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

    vKey = vArgs[0];
    check_aerospike_key(vKey);

    vBins = vArgs[1];
    Check_Type(vBins, T_HASH);

    as_policy_write_init(&policy);

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

    idx = RHASH_SIZE(vBins);
    if (idx == 0) {
        return Qfalse;
    }

    Data_Get_Struct(vSelf, aerospike, ptr);
    as_record_inita(&record, idx);
    vHashKeys = rb_hash_keys(vBins);

    for(n = 0; n < idx; n++) {
        VALUE bin_name = rb_ary_entry(vHashKeys, n);
        VALUE bin_value = rb_hash_aref(vBins, bin_name);

        Check_Type(bin_name, T_STRING);

        switch( TYPE(bin_value) ) {
        case T_NIL:
            as_record_set_nil(&record, StringValueCStr(bin_name));
            break;
        case T_STRING:
            as_record_set_str(&record, StringValueCStr(bin_name), StringValueCStr(bin_value));
            break;
        case T_FIXNUM:
            as_record_set_int64(&record, StringValueCStr(bin_name), NUM2LONG(bin_value));
            break;
//        case T_ARRAY:
//        case T_HASH:
//            rb_raise(rb_eTypeError, "wrong argument type for bin value (hashes and arrays not supported yet)");
//            break;
        default: {
            VALUE vBytes = rb_funcall(bin_value, rb_intern("to_msgpack"), 0);
            int strSize = RSTRING_LEN(vBytes);
            as_record_set_raw(&record, StringValueCStr(bin_name), StringValuePtr(vBytes), strSize);
            break;
        }
        }
     }

    Data_Get_Struct(vKey, as_key, key);

    if (aerospike_key_put(ptr, &err, &policy, key, &record) != AEROSPIKE_OK) {
        as_record_destroy(&record);
        raise_aerospike_exception(err.code, err.message);
    }

    as_record_destroy(&record);
    return Qtrue;
}

#query(namespace, set) ⇒ AerospikeNative::Query

Instantiate new query



630
631
632
633
634
635
636
637
638
639
# File 'ext/aerospike_native/client.c', line 630

VALUE client_query(VALUE vSelf, VALUE vNamespace, VALUE vSet)
{
    VALUE vParams[3];

    vParams[0] = vSelf;
    vParams[1] = vNamespace;
    vParams[2] = vSet;

    return rb_class_new_instance(3, vParams, QueryClass);
}

#remove(, vSelf) ⇒ Object



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

VALUE client_remove(int argc, VALUE* vArgs, VALUE vSelf)
{
    VALUE vKey;

    aerospike *ptr;
    as_key* key;
    as_error err;
    as_policy_remove policy;

    if (argc > 2 || argc < 1) {  // there should only be 1 or 2 arguments
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
    }

    vKey = vArgs[0];
    check_aerospike_key(vKey);

    if (argc == 2 && TYPE(vArgs[2]) != T_NIL) {
        SET_REMOVE_POLICY(policy, vArgs[2]);
    }

    Data_Get_Struct(vSelf, aerospike, ptr);
    Data_Get_Struct(vKey, as_key, key);

    if (aerospike_key_remove(ptr, &err, &policy, key) != AEROSPIKE_OK) {
        raise_aerospike_exception(err.code, err.message);
    }

    return Qtrue;
}

#select(, vSelf) ⇒ Object



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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'ext/aerospike_native/client.c', line 453

VALUE client_select(int argc, VALUE* vArgs, VALUE vSelf)
{
    VALUE vKey;
    VALUE vArray;

    aerospike *ptr;
    as_key* key;
    as_error err;
    as_policy_read policy;
    as_record* record = NULL;
    long n = 0, idx = 0;

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

    vKey = vArgs[0];
    check_aerospike_key(vKey);
    vArray = vArgs[1];

    Check_Type(vArray, T_ARRAY);
    idx = RARRAY_LEN(vArray);
    if (idx == 0) {
        return Qfalse;
    }

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

    Data_Get_Struct(vSelf, aerospike, ptr);
    Data_Get_Struct(vKey, as_key, key);
    const char* bins[idx];

    for(n = 0; n < idx; n++) {
        VALUE bin_name = rb_ary_entry(vArray, n);

        Check_Type(bin_name, T_STRING);
        char* name = StringValueCStr( bin_name );
        bins[n] = malloc(strlen(name) * sizeof(char) + 1);
        memset(bins[n], '\0', strlen(name) + 1);
        strcpy(bins[n], name);
    }

    if (aerospike_key_select(ptr, &err, &policy, key, bins, &record) != AEROSPIKE_OK) {
        for(n = 0; n < idx; n++) {
            free(bins[n]);
        }
        as_record_destroy(record);
        raise_aerospike_exception(err.code, err.message);
    }
    for(n = 0; n < idx; n++) {
        free(bins[n]);
    }

    return rb_record_from_c(record, key);
}