aerospike_native 0.2.1 → 0.3.0

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: 7ac94d9b34b064822cc15dd0976ed7ced4b76c27
4
- data.tar.gz: 085fe5f23cb779c0ff29a7cb74d82ecef51de905
3
+ metadata.gz: cb0270c96b7bd4fa49f251d9b5fd406a21ae109b
4
+ data.tar.gz: 92c3cdb727dafa076735b779eb198011cb028058
5
5
  SHA512:
6
- metadata.gz: 95e3ce1b3fb8a94c61a88bb839790769f1f9112abc1ed7526c464e1c9cfb1195b13d8e4c0bdb40fa09d84c3dab6b289cc119f0f95b53d4f58b463409b2e18a9a
7
- data.tar.gz: 9384e2914162cbcc79aa1bb0e2bd850685e158c0617c59c0b8ab338da4d50782a794715779a69d113b8fc13ccb1d55c835593813fedc2b9752d67b7a3545a27f
6
+ metadata.gz: 27c8668efda00c0a467844f5f840fce63223dbbab69d71e53d8c9177c1bf6a4c23f69fb5366a3092a6b20eb90be95c372126fa1b4de1653533e3596a4957dcdf
7
+ data.tar.gz: 9d4fde1a9e2942ac4bfbe108cbc6babd807fdf37eef6d67e079cb797297458ab8404971716f7613d2bf1542c2c08ed3bbfdaf4df5b035be96521b492167dfde5
data/examples/batch.rb ADDED
@@ -0,0 +1,27 @@
1
+ require './common/common'
2
+
3
+ def main
4
+ Common::Common.run_example do |client, namespace, set, logger|
5
+ 20.times do |i|
6
+ client.put(AerospikeNative::Key.new(namespace, set, i), {'number' => i, 'key' => 'number', 'testbin' => i.to_s})
7
+ client.put(AerospikeNative::Key.new(namespace, set, "key#{i}"), {'number' => i, 'key' => 'string', 'testbin' => i.to_s})
8
+ end
9
+
10
+ keys = []
11
+ keys << AerospikeNative::Key.new(namespace, set, 1)
12
+ keys << AerospikeNative::Key.new(namespace, set, 10)
13
+ keys << AerospikeNative::Key.new(namespace, set, "key5")
14
+ keys << AerospikeNative::Key.new(namespace, set, "key26")
15
+
16
+ records = client.batch.get_exists(keys)
17
+ logger.info "fetched records metadata: #{records.inspect}"
18
+
19
+ records = client.batch.get(keys)
20
+ logger.info "fetched records with specified bins: #{records.inspect}"
21
+
22
+ records = client.batch.get(keys, bins)
23
+ logger.info "fetched records with all bins: #{records.inspect}"
24
+ end
25
+ end
26
+
27
+ main
@@ -5,6 +5,7 @@
5
5
  #include "record.h"
6
6
  #include "policy.h"
7
7
  #include "query.h"
8
+ #include "batch.h"
8
9
 
9
10
  VALUE AerospikeNativeClass;
10
11
 
@@ -14,6 +15,7 @@ void Init_aerospike_native()
14
15
  define_exception();
15
16
  define_logger();
16
17
  define_query();
18
+ define_batch();
17
19
  define_native_key();
18
20
  define_record();
19
21
  define_operation();
@@ -0,0 +1,213 @@
1
+ #include "batch.h"
2
+ #include "client.h"
3
+ #include "record.h"
4
+ #include <aerospike/aerospike_batch.h>
5
+
6
+ VALUE BatchClass;
7
+
8
+ VALUE batch_initialize(VALUE vSelf, VALUE vClient)
9
+ {
10
+ check_aerospike_client(vClient);
11
+ rb_iv_set(vSelf, "@client", vClient);
12
+ return vSelf;
13
+ }
14
+
15
+ bool batch_read_callback(const as_batch_read* results, uint32_t n, void* udata)
16
+ {
17
+ uint32_t i = 0;
18
+ VALUE vLogger;
19
+ char sMsg[1000];
20
+
21
+ vLogger = rb_cv_get(ClientClass, "@@logger");
22
+
23
+ for(i = 0; i < n; i++) {
24
+ switch(results[i].result) {
25
+ case AEROSPIKE_OK: {
26
+ VALUE vRecord = rb_record_from_c(&results[i].record, results[i].key);
27
+
28
+ if ( rb_block_given_p() ) {
29
+ rb_yield(vRecord);
30
+ } else {
31
+ VALUE *vArray = (VALUE*) udata;
32
+ rb_ary_push(*vArray, vRecord);
33
+ }
34
+ break;
35
+ }
36
+ case AEROSPIKE_ERR_RECORD_NOT_FOUND:
37
+ sprintf(sMsg, "Aerospike batch read record not found %d", i);
38
+ rb_funcall(vLogger, rb_intern("warn"), 1, rb_str_new2(sMsg));
39
+ break;
40
+ default:
41
+ sprintf(sMsg, "Aerospike batch read error %d", results[i].result);
42
+ rb_funcall(vLogger, rb_intern("error"), 1, rb_str_new2(sMsg));
43
+ }
44
+ }
45
+
46
+ return true;
47
+ }
48
+
49
+ /*
50
+ * call-seq:
51
+ * get(keys) -> Array
52
+ * get(keys, bins) -> Array
53
+ * get(keys, bins, policy_settings) -> Array
54
+ * get(keys, policy_settings) -> Array
55
+ * get(keys, ...) { |record| ... } -> Nil
56
+ *
57
+ * batch get records by keys with specified bins
58
+ */
59
+ VALUE batch_get(int argc, VALUE* vArgs, VALUE vSelf)
60
+ {
61
+ VALUE vKeys, vClient, vArray, vBins;
62
+
63
+ as_batch batch;
64
+ as_policy_batch policy;
65
+ as_key* key;
66
+ aerospike* ptr;
67
+ as_error err;
68
+
69
+ uint32_t n = 0, idx = 0, bins_idx = 0;
70
+
71
+ if (argc > 3 || argc < 1) { // there should only be 1, 2 or 3 arguments
72
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..3)", argc);
73
+ }
74
+
75
+ vKeys = vArgs[0];
76
+ Check_Type(vKeys, T_ARRAY);
77
+ as_policy_batch_init(&policy);
78
+
79
+ if (argc == 3) {
80
+ vBins = vArgs[1];
81
+ Check_Type(vBins, T_ARRAY);
82
+ bins_idx = RARRAY_LEN(vBins);
83
+
84
+ if (TYPE(vArgs[2]) != T_NIL) {
85
+ SET_POLICY(policy, vArgs[2]);
86
+ }
87
+ } else {
88
+ switch(TYPE(vArgs[1])) {
89
+ case T_NIL:
90
+ break;
91
+ case T_ARRAY:
92
+ vBins = vArgs[1];
93
+ bins_idx = RARRAY_LEN(vBins);
94
+ break;
95
+ case T_HASH: {
96
+ SET_POLICY(policy, vArgs[1]);
97
+ break;
98
+ }
99
+ default:
100
+ rb_raise(rb_eTypeError, "wrong argument type (expected Array or Hash)");
101
+ }
102
+ }
103
+
104
+ idx = RARRAY_LEN(vKeys);
105
+ as_batch_inita(&batch, idx);
106
+
107
+ for(n = 0; n < idx; n++) {
108
+ VALUE vKey = rb_ary_entry(vKeys, n);
109
+ Data_Get_Struct(vKey, as_key, key);
110
+ as_key_init_value(as_batch_keyat(&batch, n), key->ns, key->set, key->valuep);
111
+ }
112
+
113
+ vClient = rb_iv_get(vSelf, "@client");
114
+ Data_Get_Struct(vClient, aerospike, ptr);
115
+
116
+ vArray = rb_ary_new();
117
+ if (bins_idx > 0) {
118
+ char* sBins[bins_idx];
119
+ for(n = 0; n < bins_idx; n++) {
120
+ VALUE vEl = rb_ary_entry(vBins, n);
121
+ GET_STRING(vEl);
122
+ sBins[n] = StringValueCStr(vEl);
123
+ }
124
+ if (aerospike_batch_get_bins(ptr, &err, &policy, &batch, sBins, bins_idx, batch_read_callback, &vArray) != AEROSPIKE_OK) {
125
+ as_batch_destroy(&batch);
126
+ raise_aerospike_exception(err.code, err.message);
127
+ }
128
+ } else {
129
+ if (aerospike_batch_get(ptr, &err, &policy, &batch, batch_read_callback, &vArray) != AEROSPIKE_OK) {
130
+ as_batch_destroy(&batch);
131
+ raise_aerospike_exception(err.code, err.message);
132
+ }
133
+ }
134
+
135
+ as_batch_destroy(&batch);
136
+ if ( rb_block_given_p() ) {
137
+ return Qnil;
138
+ }
139
+
140
+ return vArray;
141
+ }
142
+
143
+ // TODO: implement batch read to customize bins for each key
144
+ // VALUE batch_read(int argc, VALUE* vArgs, VALUE vSelf);
145
+
146
+ /*
147
+ * call-seq:
148
+ * exists(keys) -> Array
149
+ * exists(keys, policy_settings) -> Array
150
+ * exists(keys, ...) { |record| ... } -> Nil
151
+ * exists(keys, policy_settings) { |record| ... } -> Nil
152
+ *
153
+ * batch get metatdata of records (ttl, gen)
154
+ */
155
+ VALUE batch_exists(int argc, VALUE* vArgs, VALUE vSelf)
156
+ {
157
+ VALUE vKeys, vClient, vArray;
158
+
159
+ as_batch batch;
160
+ as_policy_batch policy;
161
+ as_key* key;
162
+ aerospike* ptr;
163
+ as_error err;
164
+
165
+ uint32_t n = 0, idx = 0;
166
+
167
+ if (argc > 2 || argc < 1) { // there should only be 1 or 2 arguments
168
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
169
+ }
170
+
171
+ vKeys = vArgs[0];
172
+ Check_Type(vKeys, T_ARRAY);
173
+
174
+ as_policy_batch_init(&policy);
175
+ if (argc == 2 && TYPE(vArgs[1]) != T_NIL) {
176
+ SET_POLICY(policy, vArgs[1]);
177
+ }
178
+
179
+ idx = RARRAY_LEN(vKeys);
180
+ as_batch_inita(&batch, idx);
181
+
182
+ for(n = 0; n < idx; n++) {
183
+ VALUE vKey = rb_ary_entry(vKeys, n);
184
+ Data_Get_Struct(vKey, as_key, key);
185
+ as_key_init_value(as_batch_keyat(&batch, n), key->ns, key->set, key->valuep);
186
+ }
187
+
188
+ vClient = rb_iv_get(vSelf, "@client");
189
+ Data_Get_Struct(vClient, aerospike, ptr);
190
+
191
+ vArray = rb_ary_new();
192
+ if (aerospike_batch_exists(ptr, &err, &policy, &batch, batch_read_callback, &vArray) != AEROSPIKE_OK) {
193
+ as_batch_destroy(&batch);
194
+ raise_aerospike_exception(err.code, err.message);
195
+ }
196
+ as_batch_destroy(&batch);
197
+
198
+ if ( rb_block_given_p() ) {
199
+ return Qnil;
200
+ }
201
+
202
+ return vArray;
203
+ }
204
+
205
+ void define_batch()
206
+ {
207
+ BatchClass = rb_define_class_under(AerospikeNativeClass, "Batch", rb_cObject);
208
+ rb_define_method(BatchClass, "initialize", batch_initialize, 1);
209
+ rb_define_method(BatchClass, "get", batch_get, -1);
210
+ rb_define_method(BatchClass, "exists", batch_exists, -1);
211
+
212
+ rb_define_attr(BatchClass, "client", 1, 0);
213
+ }
@@ -0,0 +1,10 @@
1
+ #ifndef BATCH_H
2
+ #define BATCH_H
3
+
4
+ #include "aerospike_native.h"
5
+
6
+ RUBY_EXTERN VALUE BatchClass;
7
+ void define_batch();
8
+
9
+ #endif // BATCH_H
10
+
@@ -3,6 +3,7 @@
3
3
  #include "key.h"
4
4
  #include "record.h"
5
5
  #include "query.h"
6
+ #include "batch.h"
6
7
  #include <aerospike/as_key.h>
7
8
  #include <aerospike/as_operations.h>
8
9
  #include <aerospike/aerospike_key.h>
@@ -654,6 +655,14 @@ VALUE client_set_log_level(VALUE vSelf, VALUE vLevel)
654
655
  return rb_funcall(vLogger, rb_intern("set_level"), 1, vLevel);
655
656
  }
656
657
 
658
+ VALUE client_batch(VALUE vSelf)
659
+ {
660
+ VALUE vParams[1];
661
+
662
+ vParams[0] = vSelf;
663
+ return rb_class_new_instance(1, vParams, BatchClass);
664
+ }
665
+
657
666
  void define_client()
658
667
  {
659
668
  ClientClass = rb_define_class_under(AerospikeNativeClass, "Client", rb_cObject);
@@ -668,6 +677,7 @@ void define_client()
668
677
  rb_define_method(ClientClass, "create_index", client_create_index, -1);
669
678
  rb_define_method(ClientClass, "drop_index", client_drop_index, -1);
670
679
  rb_define_method(ClientClass, "query", client_query, 2);
680
+ rb_define_method(ClientClass, "batch", client_batch, 0);
671
681
 
672
682
  rb_cv_set(ClientClass, "@@logger", rb_class_new_instance(0, NULL, LoggerClass));
673
683
  rb_define_singleton_method(ClientClass, "set_logger", client_set_logger, 1);
@@ -21,6 +21,9 @@ VALUE rb_hash_keys(VALUE hash);
21
21
  VALUE vTimeout; \
22
22
  Check_Type(vSettings, T_HASH); \
23
23
  vTimeout = rb_hash_aref(vSettings, rb_str_new2("timeout")); \
24
+ if (TYPE(vTimeout) == T_NIL) { \
25
+ vTimeout = rb_hash_aref(vSettings, ID2SYM(rb_intern("timeout"))); \
26
+ } \
24
27
  if (TYPE(vTimeout) == T_FIXNUM) { \
25
28
  policy.timeout = NUM2UINT( vTimeout ); \
26
29
  }
@@ -10,10 +10,23 @@ VALUE exception_initialize(VALUE vSelf, VALUE vMessage)
10
10
  return vSelf;
11
11
  }
12
12
 
13
+ VALUE exception_inspect(VALUE vSelf)
14
+ {
15
+ VALUE vCode, vMsg;
16
+ char sMsg[200];
17
+
18
+ vCode = rb_iv_get(vSelf, "@code");
19
+ vMsg = rb_iv_get(vSelf, "@message");
20
+ sprintf(sMsg, "#<AerospikeNative::Exception: @code=%d, @message=\"%s\">", NUM2INT(vCode), StringValueCStr(vMsg));
21
+
22
+ return rb_str_new2(sMsg);
23
+ }
24
+
13
25
  void define_exception()
14
26
  {
15
27
  ExceptionClass = rb_define_class_under(AerospikeNativeClass, "Exception", rb_eStandardError);
16
28
  rb_define_method(ExceptionClass, "initialize", exception_initialize, 1);
29
+ rb_define_method(ExceptionClass, "inspect", exception_inspect, 0);
17
30
  rb_define_attr(ExceptionClass, "code", 1, 0);
18
31
  rb_define_attr(ExceptionClass, "message", 1, 0);
19
32
 
@@ -43,15 +43,18 @@ VALUE rb_record_from_c(as_record* record, as_key* key)
43
43
 
44
44
  vKeyParams[0] = rb_str_new2(current_key.ns);
45
45
  vKeyParams[1] = rb_str_new2(current_key.set);
46
+ vKeyParams[2] = Qnil;
46
47
 
47
- if (current_key.valuep == NULL) {
48
- vKeyParams[2] = Qnil;
49
- } else {
50
- char *str = current_key.value.string.value;
51
- if (str == NULL) {
52
- vKeyParams[2] = INT2NUM(current_key.value.integer.value);
53
- } else {
54
- vKeyParams[2] = rb_str_new2(str);
48
+ if (current_key.valuep != NULL) {
49
+ switch( as_val_type(current_key.valuep) ) {
50
+ case AS_INTEGER:
51
+ vKeyParams[2] = INT2NUM(as_integer_get(current_key.valuep));
52
+ break;
53
+ case AS_STRING:
54
+ if (current_key.value.string.len > 0) {
55
+ vKeyParams[2] = rb_str_new2(as_string_get(current_key.valuep));
56
+ }
57
+ break;
55
58
  }
56
59
  }
57
60
  vKeyParams[3] = rb_str_new(current_key.digest.value, AS_DIGEST_VALUE_SIZE);
@@ -69,12 +72,13 @@ VALUE rb_record_from_c(as_record* record, as_key* key)
69
72
  rb_hash_aset(vParams[1], rb_str_new2(bin.name), Qnil);
70
73
  break;
71
74
  case AS_INTEGER:
72
- rb_hash_aset(vParams[1], rb_str_new2(bin.name), LONG2NUM(bin.valuep->integer.value));
75
+ rb_hash_aset(vParams[1], rb_str_new2(bin.name), LONG2NUM(as_integer_get(bin.valuep)));
73
76
  break;
74
77
  case AS_STRING:
75
- rb_hash_aset(vParams[1], rb_str_new2(bin.name), rb_str_new2(bin.valuep->string.value));
78
+ rb_hash_aset(vParams[1], rb_str_new2(bin.name), rb_str_new2(as_string_get(bin.valuep)));
76
79
  break;
77
80
  case AS_BYTES: {
81
+ // as_bytes_get();
78
82
  VALUE vString = rb_str_new(bin.valuep->bytes.value, bin.valuep->bytes.size);
79
83
  rb_hash_aset(vParams[1], rb_str_new2(bin.name), rb_funcall(vMsgPackClass, rb_intern("unpack"), 1, vString));
80
84
  break;
@@ -1,3 +1,3 @@
1
1
  module AerospikeNative
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aerospike_native
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Ziablitskii
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-20 00:00:00.000000000 Z
11
+ date: 2015-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack
@@ -95,11 +95,14 @@ files:
95
95
  - README.md
96
96
  - Rakefile
97
97
  - aerospike_native.gemspec
98
+ - examples/batch.rb
98
99
  - examples/common/common.rb
99
100
  - examples/operate.rb
100
101
  - examples/query_and_index.rb
101
102
  - ext/aerospike_native/aerospike_native.c
102
103
  - ext/aerospike_native/aerospike_native.h
104
+ - ext/aerospike_native/batch.c
105
+ - ext/aerospike_native/batch.h
103
106
  - ext/aerospike_native/client.c
104
107
  - ext/aerospike_native/client.h
105
108
  - ext/aerospike_native/common.c