aerospike_native 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 61a277355cea7b57701e87d449cc21d05d933f17
4
- data.tar.gz: b7e0a9780c63ac5b07ab380da0cfbadef654bbe5
3
+ metadata.gz: 55fd092a56f2b28d9fff6ab918d7aae3f19324e3
4
+ data.tar.gz: 687914c31d0f71e661d0453434136ef1b75f33ab
5
5
  SHA512:
6
- metadata.gz: 16f5e09b60d5e74ab9a4438575f38c00953d728e147f96d7b6d8726a59dc8b597e6fb506a7e5fba68061d8a4567c66157f9e753765f5e8f218148a1c7fd1c580
7
- data.tar.gz: 1662b1718efc07a1c93859a734f5e114dddfb9def810a306a78485acdaf4d1c16ac53f2ed9f6953bc66d8a9f5f4d896ec1207bc563b85f6a0a3e21e61b92110e
6
+ metadata.gz: 0e0b41edd06ad7599ed15866119471672f080358dab8346c94fe3bdabe5dad75eac914aa8026c0c406255582780565242f8aabb275eb3e6c27856ce0ac1e24f3
7
+ data.tar.gz: 15595c8b755ba4d88ef78342d4d277060c93f8c5ae78fc37b09087332f194f1e85f1649b54e4259f59da30ee278db79c5c3ce7188fbc5c22a1daa8d56cb13f68
@@ -1,8 +1,8 @@
1
- //#include <iostream>
2
1
  #include "aerospike_native.h"
3
2
  #include "client.h"
4
3
  #include "key.h"
5
4
  #include "operation.h"
5
+ #include "record.h"
6
6
 
7
7
  VALUE AerospikeNativeClass;
8
8
 
@@ -11,5 +11,6 @@ void Init_aerospike_native()
11
11
  AerospikeNativeClass = rb_define_module("AerospikeNative");
12
12
  define_client();
13
13
  define_native_key();
14
+ define_record();
14
15
  define_operation();
15
16
  }
@@ -1,5 +1,7 @@
1
1
  #include "client.h"
2
2
  #include "operation.h"
3
+ #include "key.h"
4
+ #include "record.h"
3
5
  #include <aerospike/as_key.h>
4
6
  #include <aerospike/as_operations.h>
5
7
  #include <aerospike/aerospike_key.h>
@@ -25,6 +27,23 @@ static VALUE client_allocate(VALUE klass)
25
27
  return obj;
26
28
  }
27
29
 
30
+ static int keys_i(VALUE key, VALUE value, VALUE ary)
31
+ {
32
+ rb_ary_push(ary, key);
33
+ return ST_CONTINUE;
34
+ }
35
+
36
+ static VALUE
37
+ rb_hash_keys(VALUE hash)
38
+ {
39
+ VALUE ary;
40
+
41
+ ary = rb_ary_new_capa(RHASH_SIZE(hash));
42
+ rb_hash_foreach(hash, keys_i, ary);
43
+
44
+ return ary;
45
+ }
46
+
28
47
  VALUE client_initialize(int argc, VALUE* argv, VALUE self)
29
48
  {
30
49
  VALUE ary = Qnil;
@@ -34,7 +53,7 @@ VALUE client_initialize(int argc, VALUE* argv, VALUE self)
34
53
  long idx = 0, n = 0;
35
54
 
36
55
  if (argc > 1) { // there should only be 0 or 1 arguments
37
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
56
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
38
57
  }
39
58
 
40
59
  if (argc == 1) {
@@ -78,6 +97,176 @@ VALUE client_initialize(int argc, VALUE* argv, VALUE self)
78
97
  return self;
79
98
  }
80
99
 
100
+ VALUE client_put(int argc, VALUE* vArgs, VALUE vSelf)
101
+ {
102
+ VALUE vKey;
103
+ VALUE vBins;
104
+ VALUE vSettings;
105
+ VALUE vHashKeys;
106
+
107
+ aerospike *ptr;
108
+ as_key key;
109
+ as_error err;
110
+ as_record record;
111
+ as_policy_write policy;
112
+
113
+ VALUE vNamespace, vSet, vKeyValue;
114
+
115
+ int idx = 0, n = 0;
116
+
117
+ if (argc > 3 || argc < 2) { // there should only be 2 or 3 arguments
118
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 2..3)", argc);
119
+ }
120
+
121
+ vKey = vArgs[0];
122
+ check_aerospike_key(vKey);
123
+
124
+ vBins = vArgs[1];
125
+ Check_Type(vBins, T_HASH);
126
+
127
+ as_policy_write_init(&policy);
128
+
129
+ if (argc == 3) {
130
+ vSettings = vArgs[2];
131
+
132
+ switch (TYPE(vSettings)) {
133
+ case T_NIL:
134
+ break;
135
+ case T_HASH: {
136
+ VALUE vTimeout = rb_hash_aref(vSettings, rb_str_new2("timeout"));
137
+ if (TYPE(vTimeout) == T_FIXNUM) {
138
+ policy.timeout = NUM2UINT( vTimeout );
139
+ }
140
+ break;
141
+ }
142
+ default:
143
+ /* raise exception */
144
+ Check_Type(vSettings, T_HASH);
145
+ break;
146
+ }
147
+ }
148
+
149
+ idx = RHASH_SIZE(vBins);
150
+ if (idx == 0) {
151
+ return Qfalse;
152
+ }
153
+
154
+ Data_Get_Struct(vSelf, aerospike, ptr);
155
+ as_record_inita(&record, idx);
156
+ vHashKeys = rb_hash_keys(vBins);
157
+
158
+ for(n = 0; n < idx; n++) {
159
+ VALUE bin_name = rb_ary_entry(vHashKeys, n);
160
+ VALUE bin_value = rb_hash_aref(vBins, bin_name);
161
+
162
+ Check_Type(bin_name, T_STRING);
163
+
164
+ switch( TYPE(bin_value) ) {
165
+ case T_STRING:
166
+ as_record_set_str(&record, StringValueCStr(bin_name), StringValueCStr(bin_value));
167
+ break;
168
+ case T_FIXNUM:
169
+ as_record_set_int64(&record, StringValueCStr(bin_name), NUM2LONG(bin_value));
170
+ break;
171
+ default:
172
+ rb_raise(rb_eArgError, "Incorrect input type");
173
+ break;
174
+ }
175
+ }
176
+
177
+ vNamespace = rb_iv_get(vKey, "@namespace");
178
+ vSet = rb_iv_get(vKey, "@set");
179
+ vKeyValue = rb_iv_get(vKey, "@value");
180
+ as_key_init_str(&key, StringValueCStr( vNamespace ), StringValueCStr( vSet ), StringValueCStr( vKeyValue ));
181
+
182
+ if (aerospike_key_put(ptr, &err, &policy, &key, &record) != AEROSPIKE_OK) {
183
+ printf("error\n");
184
+ fprintf(stderr, "err(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line);
185
+ }
186
+
187
+ return Qtrue;
188
+ }
189
+
190
+ VALUE client_get(int argc, VALUE* vArgs, VALUE vSelf)
191
+ {
192
+ VALUE vKey;
193
+ VALUE vSettings;
194
+ VALUE vParams[4];
195
+
196
+ aerospike *ptr;
197
+ as_key key;
198
+ as_error err;
199
+ as_record* record = NULL;
200
+ as_policy_read policy;
201
+ as_bin bin;
202
+
203
+ VALUE vNamespace, vSet, vKeyValue;
204
+ int n = 0;
205
+
206
+ if (argc > 2 || argc < 1) { // there should only be 1 or 2 arguments
207
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
208
+ }
209
+
210
+ vKey = vArgs[0];
211
+ check_aerospike_key(vKey);
212
+
213
+ as_policy_read_init(&policy);
214
+
215
+ if (argc == 2) {
216
+ vSettings = vArgs[1];
217
+
218
+ switch (TYPE(vSettings)) {
219
+ case T_NIL:
220
+ break;
221
+ case T_HASH: {
222
+ VALUE vTimeout = rb_hash_aref(vSettings, rb_str_new2("timeout"));
223
+ if (TYPE(vTimeout) == T_FIXNUM) {
224
+ policy.timeout = NUM2UINT( vTimeout );
225
+ }
226
+ break;
227
+ }
228
+ default:
229
+ /* raise exception */
230
+ Check_Type(vSettings, T_HASH);
231
+ break;
232
+ }
233
+ }
234
+
235
+ Data_Get_Struct(vSelf, aerospike, ptr);
236
+ vNamespace = rb_iv_get(vKey, "@namespace");
237
+ vSet = rb_iv_get(vKey, "@set");
238
+ vKeyValue = rb_iv_get(vKey, "@value");
239
+ as_key_init_str(&key, StringValueCStr( vNamespace ), StringValueCStr( vSet ), StringValueCStr( vKeyValue ));
240
+
241
+ if (aerospike_key_get(ptr, &err, &policy, &key, &record) != AEROSPIKE_OK) {
242
+ printf("error\n");
243
+ fprintf(stderr, "err(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line);
244
+ }
245
+ rb_iv_set(vKey, "@digest", rb_str_new( record->key.digest.value, AS_DIGEST_VALUE_SIZE));
246
+
247
+ vParams[0] = vKey;
248
+ vParams[1] = rb_hash_new();
249
+ vParams[2] = UINT2NUM(record->gen);
250
+ vParams[3] = UINT2NUM(record->ttl);
251
+
252
+ for(n = 0; n < record->bins.size; n++) {
253
+ bin = record->bins.entries[n];
254
+ switch( as_val_type(bin.valuep) ) {
255
+ case AS_INTEGER:
256
+ rb_hash_aset(vParams[1], rb_str_new2(bin.name), LONG2NUM(bin.valuep->integer.value));
257
+ break;
258
+ case AS_STRING:
259
+ rb_hash_aset(vParams[1], rb_str_new2(bin.name), rb_str_new2(bin.valuep->string.value));
260
+ break;
261
+ case AS_UNDEF:
262
+ default:
263
+ break;
264
+ }
265
+ }
266
+
267
+ return rb_class_new_instance(4, vParams, RecordClass);
268
+ }
269
+
81
270
  VALUE client_operate(int argc, VALUE* argv, VALUE vSelf)
82
271
  {
83
272
  VALUE vKey;
@@ -89,27 +278,34 @@ VALUE client_operate(int argc, VALUE* argv, VALUE vSelf)
89
278
  as_operations ops;
90
279
  as_key key;
91
280
  as_error err;
281
+ as_policy_operate policy;
92
282
 
93
283
  VALUE vNamespace, vSet, vKeyValue;
94
284
 
95
285
  if (argc > 3 || argc < 2) { // there should only be 2 or 3 arguments
96
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 3)", argc);
286
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 2..3)", argc);
97
287
  }
98
288
 
99
289
  vKey = argv[0];
100
- // char* name = rb_obj_classname(vKey);
101
- printf("class name: %s\n", rb_obj_classname(vKey));
290
+ check_aerospike_key(vKey);
102
291
 
103
292
  vOperations = argv[1];
104
293
  Check_Type(vOperations, T_ARRAY);
294
+ as_policy_operate_init(&policy);
105
295
 
106
296
  if (argc == 3) {
107
297
  vSettings = argv[2];
108
298
 
109
299
  switch (TYPE(vSettings)) {
110
300
  case T_NIL:
111
- case T_HASH:
112
301
  break;
302
+ case T_HASH: {
303
+ VALUE vTimeout = rb_hash_aref(vSettings, rb_str_new2("timeout"));
304
+ if (TYPE(vTimeout) == T_FIXNUM) {
305
+ policy.timeout = NUM2UINT( vTimeout );
306
+ }
307
+ break;
308
+ }
113
309
  default:
114
310
  /* raise exception */
115
311
  Check_Type(vSettings, T_HASH);
@@ -125,9 +321,7 @@ VALUE client_operate(int argc, VALUE* argv, VALUE vSelf)
125
321
  Data_Get_Struct(vSelf, aerospike, ptr);
126
322
  as_operations_inita(&ops, idx);
127
323
 
128
- printf("prepare operations\n");
129
-
130
- for(n = 0; n < idx; n++) {
324
+ for(n = 0; n < idx; n++) {
131
325
  VALUE operation = rb_ary_entry(vOperations, n);
132
326
  int op_type = NUM2INT( rb_iv_get(operation, "@op_type") );
133
327
  VALUE bin_name = rb_iv_get(operation, "@bin_name");
@@ -135,39 +329,47 @@ VALUE client_operate(int argc, VALUE* argv, VALUE vSelf)
135
329
 
136
330
  switch( op_type ) {
137
331
  case WRITE:
138
- printf("write\n");
139
- as_operations_add_write_int64(&ops, StringValueCStr( bin_name ), NUM2INT( bin_value ));
140
- // switch( TYPE(bin_value) ) {
141
- // case T_STRING:
142
- // as_operations_add_write_str(&ops, StringValueCStr( bin_name ), StringValueCStr( bin_value ));
143
- // break;
144
- // case T_FIXNUM:
145
- // as_operations_add_write_int64(&ops, StringValueCStr( bin_name ), NUM2INT( bin_value ));
146
- // break;
147
- // }
332
+ switch( TYPE(bin_value) ) {
333
+ case T_STRING:
334
+ as_operations_add_write_str(&ops, StringValueCStr( bin_name ), StringValueCStr( bin_value ));
335
+ break;
336
+ case T_FIXNUM:
337
+ as_operations_add_write_int64(&ops, StringValueCStr( bin_name ), NUM2LONG( bin_value ));
338
+ break;
339
+ }
148
340
 
341
+ break;
342
+ case READ:
149
343
  break;
150
344
  case INCREMENT:
151
- printf("increment\n");
152
345
  as_operations_add_incr(&ops, StringValueCStr( bin_name ), NUM2INT( bin_value ));
153
346
  break;
347
+ case APPEND:
348
+ Check_Type(bin_value, T_STRING);
349
+ as_operations_add_append_str(&ops, StringValueCStr( bin_name ), StringValueCStr( bin_value ));
350
+ break;
351
+ case PREPEND:
352
+ Check_Type(bin_value, T_STRING);
353
+ as_operations_add_prepend_str(&ops, StringValueCStr( bin_name ), StringValueCStr( bin_value ));
354
+ break;
355
+ case TOUCH:
356
+ as_operations_add_touch(&ops);
357
+ break;
358
+ default:
359
+ rb_raise(rb_eArgError, "Incorrect operation type");
360
+ break;
154
361
  }
155
362
  }
156
363
 
157
- printf("test\n");
158
- printf("prepare key\n");
159
-
160
364
  vNamespace = rb_iv_get(vKey, "@namespace");
161
365
  vSet = rb_iv_get(vKey, "@set");
162
366
  vKeyValue = rb_iv_get(vKey, "@value");
163
367
  as_key_init_str(&key, StringValueCStr( vNamespace ), StringValueCStr( vSet ), StringValueCStr( vKeyValue ));
164
368
 
165
- printf("execute\n");
166
- if (aerospike_key_operate(ptr, &err, NULL, &key, &ops, NULL) != AEROSPIKE_OK) {
369
+ if (aerospike_key_operate(ptr, &err, &policy, &key, &ops, NULL) != AEROSPIKE_OK) {
167
370
  printf("error\n");
168
371
  fprintf(stderr, "err(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line);
169
372
  }
170
- printf("finish\n");
171
373
 
172
374
  as_operations_destroy(&ops);
173
375
 
@@ -176,12 +378,8 @@ VALUE client_operate(int argc, VALUE* argv, VALUE vSelf)
176
378
 
177
379
  // GET
178
380
  // PUT
179
- // OPERATE
180
381
 
181
382
  // Record
182
- // Key
183
- // Bin
184
- // Policy
185
383
 
186
384
  void define_client()
187
385
  {
@@ -189,4 +387,6 @@ void define_client()
189
387
  rb_define_alloc_func(ClientClass, client_allocate);
190
388
  rb_define_method(ClientClass, "initialize", client_initialize, -1);
191
389
  rb_define_method(ClientClass, "operate", client_operate, -1);
390
+ rb_define_method(ClientClass, "put", client_put, -1);
391
+ rb_define_method(ClientClass, "get", client_get, -1);
192
392
  }
@@ -15,6 +15,15 @@ VALUE key_initialize(VALUE vSelf, VALUE vNamespace, VALUE vSet, VALUE vValue)
15
15
  return vSelf;
16
16
  }
17
17
 
18
+ void check_aerospike_key(VALUE vKey)
19
+ {
20
+ char sKeyName[] = "AerospikeNative::Key";
21
+
22
+ if (strcmp(sKeyName, rb_obj_classname(vKey)) != 0) {
23
+ rb_raise(rb_eArgError, "Incorrect type (expected %s)", sKeyName);
24
+ }
25
+ }
26
+
18
27
  void define_native_key()
19
28
  {
20
29
  KeyClass = rb_define_class_under(AerospikeNativeClass, "Key", rb_cObject);
@@ -5,6 +5,7 @@
5
5
 
6
6
  extern VALUE KeyClass;
7
7
  void define_native_key();
8
+ void check_aerospike_key(VALUE vKey);
8
9
 
9
10
  #endif // KEY_H
10
11
 
@@ -23,6 +23,30 @@ VALUE operation_write(VALUE vSelf, VALUE vBinName, VALUE vBinValue)
23
23
  return rb_class_new_instance(3, vArgs, vSelf);
24
24
  }
25
25
 
26
+ VALUE operation_append(VALUE vSelf, VALUE vBinName, VALUE vBinValue)
27
+ {
28
+ VALUE vArgs[3];
29
+
30
+ Check_Type(vBinValue, T_STRING);
31
+
32
+ vArgs[0] = INT2NUM(APPEND);
33
+ vArgs[1] = vBinName;
34
+ vArgs[2] = vBinValue;
35
+ return rb_class_new_instance(3, vArgs, vSelf);
36
+ }
37
+
38
+ VALUE operation_prepend(VALUE vSelf, VALUE vBinName, VALUE vBinValue)
39
+ {
40
+ VALUE vArgs[3];
41
+
42
+ Check_Type(vBinValue, T_STRING);
43
+
44
+ vArgs[0] = INT2NUM(PREPEND);
45
+ vArgs[1] = vBinName;
46
+ vArgs[2] = vBinValue;
47
+ return rb_class_new_instance(3, vArgs, vSelf);
48
+ }
49
+
26
50
  VALUE operation_increment(VALUE vSelf, VALUE vBinName, VALUE vBinValue)
27
51
  {
28
52
  VALUE vArgs[3];
@@ -40,6 +64,8 @@ void define_operation()
40
64
  OperationClass = rb_define_class_under(AerospikeNativeClass, "Operation", rb_cObject);
41
65
  rb_define_method(OperationClass, "initialize", operation_initialize, 3);
42
66
  rb_define_singleton_method(OperationClass, "write", operation_write, 2);
67
+ rb_define_singleton_method(OperationClass, "append", operation_append, 2);
68
+ rb_define_singleton_method(OperationClass, "prepend", operation_prepend, 2);
43
69
  rb_define_singleton_method(OperationClass, "increment", operation_increment, 2);
44
70
  rb_define_attr(OperationClass, "op_type", 1, 0);
45
71
  rb_define_attr(OperationClass, "bin_name", 1, 0);
@@ -0,0 +1,33 @@
1
+ #include "record.h"
2
+ #include "key.h"
3
+
4
+ VALUE RecordClass;
5
+
6
+ VALUE record_initialize(VALUE vSelf, VALUE vKey, VALUE vBins, VALUE vGen, VALUE vTtl)
7
+ {
8
+ check_aerospike_key(vKey);
9
+ Check_Type(vBins, T_HASH);
10
+ Check_Type(vGen, T_FIXNUM);
11
+ Check_Type(vTtl, T_FIXNUM);
12
+
13
+ rb_iv_set(vSelf, "@key", vKey);
14
+ rb_iv_set(vSelf, "@bins", vBins);
15
+ rb_iv_set(vSelf, "@gen", vGen);
16
+ rb_iv_set(vSelf, "@ttl", vTtl);
17
+
18
+ return vSelf;
19
+ }
20
+
21
+
22
+ void define_record()
23
+ {
24
+ RecordClass = rb_define_class_under(AerospikeNativeClass, "Record", rb_cObject);
25
+ rb_define_method(RecordClass, "initialize", record_initialize, 4);
26
+ rb_define_attr(RecordClass, "key", 1, 0);
27
+ rb_define_attr(RecordClass, "bins", 1, 0);
28
+ rb_define_attr(RecordClass, "gen", 1, 0);
29
+ rb_define_attr(RecordClass, "ttl", 1, 0);
30
+ }
31
+
32
+
33
+
@@ -0,0 +1,10 @@
1
+ #ifndef RECORD_H
2
+ #define RECORD_H
3
+
4
+ #include "aerospike_native.h"
5
+
6
+ extern VALUE RecordClass;
7
+ void define_record();
8
+
9
+ #endif // RECORD_H
10
+
@@ -1,3 +1,3 @@
1
1
  module AerospikeNative
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aerospike_native
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Ziablitskii
@@ -93,6 +93,8 @@ files:
93
93
  - ext/aerospike_native/lib/.keep
94
94
  - ext/aerospike_native/operation.c
95
95
  - ext/aerospike_native/operation.h
96
+ - ext/aerospike_native/record.c
97
+ - ext/aerospike_native/record.h
96
98
  - lib/aerospike_native.rb
97
99
  - lib/aerospike_native/version.rb
98
100
  homepage: https://github.com/rainlabs/aerospike_native