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