handler_socket 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/ext/handler_socket.cc +186 -69
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.1
|
data/ext/handler_socket.cc
CHANGED
@@ -9,9 +9,9 @@ void parse_options(dena::config&, dena::socket_args&, VALUE);
|
|
9
9
|
|
10
10
|
VALUE hs_free(HandlerSocket* hs)
|
11
11
|
{
|
12
|
-
fprintf(stderr, "hs=%p\n", hs);
|
12
|
+
// fprintf(stderr, "hs=%p\n", hs);
|
13
13
|
if (hs) {
|
14
|
-
fprintf(stderr, "hs->ptr=%p\n", hs->ptr);
|
14
|
+
// fprintf(stderr, "hs->ptr=%p\n", hs->ptr);
|
15
15
|
if (hs->ptr) {
|
16
16
|
hs->ptr->close();
|
17
17
|
delete hs->ptr;
|
@@ -30,12 +30,12 @@ VALUE hs_initialize(VALUE self, VALUE options)
|
|
30
30
|
HandlerSocket* hs;
|
31
31
|
|
32
32
|
Data_Get_Struct(self, HandlerSocket, hs);
|
33
|
-
fprintf(stderr, "hs=%p\n", hs);
|
33
|
+
// fprintf(stderr, "hs=%p\n", hs);
|
34
34
|
if (!hs) {
|
35
35
|
hs = (HandlerSocket*)xmalloc(sizeof(HandlerSocket));
|
36
36
|
MEMZERO(hs, HandlerSocket, 1);
|
37
37
|
DATA_PTR(self) = hs;
|
38
|
-
fprintf(stderr, "hs=%p\n", hs);
|
38
|
+
// fprintf(stderr, "hs=%p\n", hs);
|
39
39
|
}
|
40
40
|
|
41
41
|
dena::config conf;
|
@@ -43,10 +43,10 @@ VALUE hs_initialize(VALUE self, VALUE options)
|
|
43
43
|
parse_options(conf, args, options);
|
44
44
|
|
45
45
|
dena::hstcpcli_ptr ptr = dena::hstcpcli_i::create(args);
|
46
|
-
fprintf(stderr, "ptr=%p\n", &ptr);
|
46
|
+
// fprintf(stderr, "ptr=%p\n", &ptr);
|
47
47
|
hs->ptr = ptr.get();
|
48
|
-
fprintf(stderr, "hs->ptr=%p\n", hs->ptr);
|
49
|
-
fprintf(stderr, "ptr.get()=%p\n", ptr.get());
|
48
|
+
// fprintf(stderr, "hs->ptr=%p\n", hs->ptr);
|
49
|
+
// fprintf(stderr, "ptr.get()=%p\n", ptr.get());
|
50
50
|
ptr.release();
|
51
51
|
|
52
52
|
return self;
|
@@ -116,16 +116,40 @@ VALUE hs_open_index(VALUE self, VALUE id, VALUE db, VALUE table, VALUE index, VA
|
|
116
116
|
size_t nflds = 0;
|
117
117
|
ptr->response_recv(nflds);
|
118
118
|
const int e = ptr->get_error_code();
|
119
|
-
fprintf(stderr, "errcode=%d\n", e);
|
119
|
+
// fprintf(stderr, "errcode=%d\n", e);
|
120
120
|
if (e >= 0) {
|
121
121
|
ptr->response_buf_remove();
|
122
122
|
}
|
123
|
-
fprintf(stderr, "errcode=%d\n", ptr->get_error_code());
|
123
|
+
// fprintf(stderr, "errcode=%d\n", ptr->get_error_code());
|
124
124
|
} while(0);
|
125
125
|
|
126
126
|
return INT2FIX(ptr->get_error_code());
|
127
127
|
}
|
128
128
|
|
129
|
+
VALUE ary_to_vector(VALUE ary, std::vector<dena::string_ref>& vec)
|
130
|
+
{
|
131
|
+
VALUE ret, val;
|
132
|
+
|
133
|
+
if (NIL_P(ary) || RARRAY_LEN(ary) == 0) {
|
134
|
+
return ary;
|
135
|
+
}
|
136
|
+
|
137
|
+
ret = rb_ary_new2(RARRAY_LEN(ary));
|
138
|
+
vec.reserve(RARRAY_LEN(ary));
|
139
|
+
|
140
|
+
for (size_t i=0; i<RARRAY_LEN(ary); i++) {
|
141
|
+
val = rb_ary_entry(ary, i);
|
142
|
+
if (FIXNUM_P(val)) {
|
143
|
+
val = rb_fix2str(val, 10);
|
144
|
+
}
|
145
|
+
StringValue(val);
|
146
|
+
vec.push_back(dena::string_ref(RSTRING_PTR(val), RSTRING_LEN(val)));
|
147
|
+
rb_ary_push(ret, val);
|
148
|
+
}
|
149
|
+
|
150
|
+
return ret;
|
151
|
+
}
|
152
|
+
|
129
153
|
VALUE hs_execute_single(int argc, VALUE *argv, VALUE self)
|
130
154
|
{
|
131
155
|
HandlerSocket* hs;
|
@@ -140,33 +164,19 @@ VALUE hs_execute_single(int argc, VALUE *argv, VALUE self)
|
|
140
164
|
|
141
165
|
if (!NIL_P(modop)) {
|
142
166
|
StringValue(modop);
|
143
|
-
Check_Type(modvals, T_ARRAY);
|
144
167
|
}
|
145
168
|
|
146
|
-
dena::string_ref op_ref;
|
147
|
-
std::vector<dena::string_ref> keyary;
|
169
|
+
dena::string_ref op_ref, modop_ref;
|
170
|
+
std::vector<dena::string_ref> keyary, modary;
|
148
171
|
|
149
172
|
op_ref = dena::string_ref(RSTRING_PTR(op), RSTRING_LEN(op));
|
150
|
-
|
151
|
-
|
152
|
-
for (size_t i=0; i<RARRAY_LEN(keys); i++) {
|
153
|
-
VALUE key = rb_ary_entry(keys, i);
|
154
|
-
StringValuePtr(key);
|
155
|
-
keyary.push_back(dena::string_ref(RSTRING_PTR(key), RSTRING_LEN(key)));
|
156
|
-
}
|
157
|
-
|
158
|
-
dena::string_ref modop_ref;
|
159
|
-
std::vector<dena::string_ref> modary;
|
173
|
+
keys = ary_to_vector(keys, keyary);
|
174
|
+
rb_gc_register_address(&keys);
|
160
175
|
|
161
176
|
if (!NIL_P(modop)) {
|
162
177
|
modop_ref = dena::string_ref(RSTRING_PTR(modop), RSTRING_LEN(modop));
|
163
|
-
|
164
|
-
|
165
|
-
for (size_t i=0; i<RARRAY_LEN(modvals); i++) {
|
166
|
-
VALUE key = rb_ary_entry(modvals, i);
|
167
|
-
// StringValue(key);
|
168
|
-
modary.push_back(dena::string_ref(RSTRING_PTR(key), RSTRING_LEN(key)));
|
169
|
-
}
|
178
|
+
modvals = ary_to_vector(modvals, modary);
|
179
|
+
rb_gc_register_address(&modvals);
|
170
180
|
}
|
171
181
|
|
172
182
|
ptr->request_buf_exec_generic(NUM2INT(id),
|
@@ -180,14 +190,20 @@ VALUE hs_execute_single(int argc, VALUE *argv, VALUE self)
|
|
180
190
|
return Qnil;
|
181
191
|
}
|
182
192
|
|
193
|
+
rb_gc_unregister_address(&keys);
|
194
|
+
|
195
|
+
if (!NIL_P(modop)) {
|
196
|
+
rb_gc_unregister_address(&modvals);
|
197
|
+
}
|
198
|
+
|
183
199
|
size_t nflds = 0;
|
184
200
|
ptr->response_recv(nflds);
|
185
|
-
fprintf(stderr, "nflds=%zu\n", nflds);
|
201
|
+
// fprintf(stderr, "nflds=%zu\n", nflds);
|
186
202
|
|
187
203
|
VALUE retval = rb_ary_new();
|
188
204
|
|
189
205
|
const int e = ptr->get_error_code();
|
190
|
-
fprintf(stderr, "e=%d nflds=%zu\n", e, nflds);
|
206
|
+
// fprintf(stderr, "e=%d nflds=%zu\n", e, nflds);
|
191
207
|
rb_ary_push(retval, FIX2INT(e));
|
192
208
|
|
193
209
|
if (e != 0) {
|
@@ -199,11 +215,11 @@ VALUE hs_execute_single(int argc, VALUE *argv, VALUE self)
|
|
199
215
|
arys = rb_ary_new();
|
200
216
|
const dena::string_ref *row = 0;
|
201
217
|
while ((row = ptr->get_next_row()) != 0) {
|
202
|
-
fprintf(stderr, "row=%p\n", row);
|
218
|
+
// fprintf(stderr, "row=%p\n", row);
|
203
219
|
ary = rb_ary_new2(nflds);
|
204
220
|
for (size_t i = 0; i < nflds; ++i) {
|
205
221
|
const dena::string_ref& v = row[i];
|
206
|
-
fprintf(stderr, "FLD %zu v=%s vbegin=%p\n", i, std::string(v.begin(), v.size()).c_str(), v.begin());
|
222
|
+
// fprintf(stderr, "FLD %zu v=%s vbegin=%p\n", i, std::string(v.begin(), v.size()).c_str(), v.begin());
|
207
223
|
if (v.begin() != 0) {
|
208
224
|
val = rb_str_new(v.begin(), v.size());
|
209
225
|
rb_ary_push(ary, val);
|
@@ -229,43 +245,144 @@ VALUE hs_execute_multi(int argc, VALUE *argv, VALUE self)
|
|
229
245
|
Data_Get_Struct(self, HandlerSocket, hs);
|
230
246
|
dena::hstcpcli_i *const ptr = hs->ptr;
|
231
247
|
|
232
|
-
|
233
|
-
}
|
248
|
+
VALUE exec_args, exec_arg;
|
234
249
|
|
235
|
-
VALUE
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
250
|
+
VALUE id, op, keys, limit, skip, modop, modvals;
|
251
|
+
int arg = rb_scan_args(argc, argv, "16", &id, &op, &keys, &limit, &skip, &modop, &modvals);
|
252
|
+
if (arg >= 5) {
|
253
|
+
VALUE tmp = rb_ary_new3(id, op, keys, limit, skip, modop, modvals);
|
254
|
+
exec_args = rb_ary_new3(tmp);
|
255
|
+
} else if (arg == 1) {
|
256
|
+
Check_Type(id, T_ARRAY);
|
257
|
+
exec_args = id;
|
258
|
+
} else {
|
259
|
+
rb_raise(rb_eArgError, "wrong number of arguments");
|
260
|
+
}
|
240
261
|
|
241
|
-
|
242
|
-
|
262
|
+
for (size_t i=0; i<RARRAY_LEN(exec_args); i++) {
|
263
|
+
exec_arg = rb_ary_entry(exec_args, i);
|
264
|
+
Check_Type(exec_arg, T_ARRAY);
|
243
265
|
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
266
|
+
id = rb_ary_entry(exec_arg, 0);
|
267
|
+
op = rb_ary_entry(exec_arg, 1);
|
268
|
+
keys = rb_ary_entry(exec_arg, 2);
|
269
|
+
limit = rb_ary_entry(exec_arg, 3);
|
270
|
+
skip = rb_ary_entry(exec_arg, 4);
|
271
|
+
modop = rb_ary_entry(exec_arg, 5);
|
272
|
+
modvals = rb_ary_entry(exec_arg, 6);
|
249
273
|
|
250
|
-
|
274
|
+
StringValue(op);
|
275
|
+
Check_Type(keys, T_ARRAY);
|
276
|
+
|
277
|
+
if (!NIL_P(modop)) {
|
278
|
+
StringValue(modop);
|
279
|
+
}
|
280
|
+
|
281
|
+
dena::string_ref op_ref, modop_ref;
|
282
|
+
std::vector<dena::string_ref> keyary, modary;
|
283
|
+
|
284
|
+
op_ref = dena::string_ref(RSTRING_PTR(op), RSTRING_LEN(op));
|
285
|
+
keys = ary_to_vector(keys, keyary);
|
286
|
+
rb_gc_register_address(&keys);
|
287
|
+
|
288
|
+
if (!NIL_P(modop)) {
|
289
|
+
modop_ref = dena::string_ref(RSTRING_PTR(modop), RSTRING_LEN(modop));
|
290
|
+
modvals = ary_to_vector(modvals, modary);
|
291
|
+
rb_gc_register_address(&modvals);
|
292
|
+
}
|
293
|
+
|
294
|
+
ptr->request_buf_exec_generic(NUM2INT(id),
|
295
|
+
op_ref,
|
296
|
+
&keyary[0], keyary.size(),
|
297
|
+
NUM2INT(limit), NUM2INT(skip),
|
298
|
+
modop_ref,
|
299
|
+
&modary[0], modary.size());
|
300
|
+
}
|
301
|
+
|
302
|
+
VALUE retvals, retval;
|
303
|
+
retvals = rb_ary_new();
|
304
|
+
|
305
|
+
if (ptr->request_send() < 0) {
|
306
|
+
retval = rb_ary_new();
|
307
|
+
|
308
|
+
const int e = ptr->get_error_code();
|
309
|
+
const std::string s = ptr->get_error();
|
310
|
+
rb_ary_push(retval, FIX2INT(e));
|
311
|
+
rb_ary_push(retval, rb_str_new2(s.c_str()));
|
312
|
+
|
313
|
+
rb_ary_push(retvals, retval);
|
314
|
+
return retvals;
|
315
|
+
}
|
316
|
+
|
317
|
+
for (size_t i=0; i<RARRAY_LEN(exec_args); i++) {
|
318
|
+
retval = rb_ary_new();
|
319
|
+
|
320
|
+
size_t nflds = 0;
|
321
|
+
const int e = ptr->response_recv(nflds);
|
322
|
+
rb_ary_push(retval, FIX2INT(e));
|
323
|
+
if (e != 0) {
|
324
|
+
const std::string s = ptr->get_error();
|
325
|
+
rb_ary_push(retval, rb_str_new2(s.c_str()));
|
326
|
+
} else {
|
327
|
+
VALUE arys, ary, val;
|
328
|
+
|
329
|
+
arys = rb_ary_new();
|
330
|
+
const dena::string_ref *row = 0;
|
331
|
+
while ((row = ptr->get_next_row()) != 0) {
|
332
|
+
// fprintf(stderr, "row=%p\n", row);
|
333
|
+
ary = rb_ary_new2(nflds);
|
334
|
+
for (size_t i = 0; i < nflds; ++i) {
|
335
|
+
const dena::string_ref& v = row[i];
|
336
|
+
// fprintf(stderr, "FLD %zu v=%s vbegin=%p\n", i, std::string(v.begin(), v.size()).c_str(), v.begin());
|
337
|
+
if (v.begin() != 0) {
|
338
|
+
val = rb_str_new(v.begin(), v.size());
|
339
|
+
rb_ary_push(ary, val);
|
340
|
+
} else {
|
341
|
+
rb_ary_push(ary, Qnil);
|
342
|
+
}
|
343
|
+
}
|
344
|
+
rb_ary_push(arys, ary);
|
345
|
+
}
|
346
|
+
rb_ary_push(retval, arys);
|
347
|
+
}
|
348
|
+
|
349
|
+
if (e >= 0) {
|
350
|
+
ptr->response_buf_remove();
|
351
|
+
}
|
352
|
+
|
353
|
+
rb_ary_push(retvals, retval);
|
354
|
+
|
355
|
+
if (e < 0) {
|
356
|
+
return retvals;
|
357
|
+
}
|
358
|
+
}
|
359
|
+
|
360
|
+
return retvals;
|
251
361
|
}
|
252
362
|
|
253
|
-
VALUE hs_execute_delete(int argc, VALUE *argv, VALUE self)
|
254
|
-
{
|
255
|
-
HandlerSocket* hs;
|
256
|
-
Data_Get_Struct(self, HandlerSocket, hs);
|
257
|
-
dena::hstcpcli_i *const ptr = hs->ptr;
|
258
363
|
|
259
|
-
|
364
|
+
VALUE hs_execute_update(VALUE self, VALUE id, VALUE op, VALUE keys, VALUE limit, VALUE skip, VALUE modvals)
|
365
|
+
{
|
366
|
+
VALUE argv[7] = {
|
367
|
+
id, op, keys, limit, skip, rb_str_new2("U"), modvals,
|
368
|
+
};
|
369
|
+
return hs_execute_single(7, argv, self);
|
260
370
|
}
|
261
371
|
|
262
|
-
VALUE
|
372
|
+
VALUE hs_execute_delete(VALUE self, VALUE id, VALUE op, VALUE keys, VALUE limit, VALUE skip)
|
263
373
|
{
|
264
|
-
|
265
|
-
|
266
|
-
|
374
|
+
VALUE argv[7] = {
|
375
|
+
id, op, keys, limit, skip, rb_str_new2("D"), Qnil,
|
376
|
+
};
|
377
|
+
return hs_execute_single(7, argv, self);
|
378
|
+
}
|
267
379
|
|
268
|
-
|
380
|
+
VALUE hs_execute_insert(VALUE self, VALUE id, VALUE fvals)
|
381
|
+
{
|
382
|
+
VALUE argv[5] = {
|
383
|
+
id, rb_str_new2("+"), fvals, 0, 0,
|
384
|
+
};
|
385
|
+
return hs_execute_single(5, argv, self);
|
269
386
|
}
|
270
387
|
|
271
388
|
void parse_options(dena::config& conf, dena::socket_args& args, VALUE options)
|
@@ -278,42 +395,42 @@ void parse_options(dena::config& conf, dena::socket_args& args, VALUE options)
|
|
278
395
|
if(val != Qnil)
|
279
396
|
{
|
280
397
|
conf["host"] = std::string(StringValuePtr(val));
|
281
|
-
fprintf(stderr, "host=%s\n", conf["host"].c_str());
|
398
|
+
// fprintf(stderr, "host=%s\n", conf["host"].c_str());
|
282
399
|
}
|
283
400
|
|
284
401
|
val = rb_hash_aref(options, ID2SYM(rb_intern("port")));
|
285
402
|
if(val != Qnil)
|
286
403
|
{
|
287
404
|
conf["port"] = std::string(StringValuePtr(val));
|
288
|
-
fprintf(stderr, "port=%s\n", conf["port"].c_str());
|
405
|
+
// fprintf(stderr, "port=%s\n", conf["port"].c_str());
|
289
406
|
}
|
290
407
|
|
291
408
|
val = rb_hash_aref(options, ID2SYM(rb_intern("timeout")));
|
292
409
|
if(val != Qnil)
|
293
410
|
{
|
294
411
|
conf["timeout"] = std::string(StringValuePtr(val));
|
295
|
-
fprintf(stderr, "timeout=%s\n", conf["timeout"].c_str());
|
412
|
+
// fprintf(stderr, "timeout=%s\n", conf["timeout"].c_str());
|
296
413
|
}
|
297
414
|
|
298
415
|
val = rb_hash_aref(options, ID2SYM(rb_intern("listen_backlog")));
|
299
416
|
if(val != Qnil)
|
300
417
|
{
|
301
418
|
conf["listen_backlog"] = std::string(StringValuePtr(val));
|
302
|
-
fprintf(stderr, "listen_backlog=%s\n", conf["listen_backlog"].c_str());
|
419
|
+
// fprintf(stderr, "listen_backlog=%s\n", conf["listen_backlog"].c_str());
|
303
420
|
}
|
304
421
|
|
305
422
|
val = rb_hash_aref(options, ID2SYM(rb_intern("sndbuf")));
|
306
423
|
if(val != Qnil)
|
307
424
|
{
|
308
425
|
conf["sndbuf"] = std::string(StringValuePtr(val));
|
309
|
-
fprintf(stderr, "sndbuf=%s\n", conf["sndbuf"].c_str());
|
426
|
+
// fprintf(stderr, "sndbuf=%s\n", conf["sndbuf"].c_str());
|
310
427
|
}
|
311
428
|
|
312
429
|
val = rb_hash_aref(options, ID2SYM(rb_intern("rcvbuf")));
|
313
430
|
if(val != Qnil)
|
314
431
|
{
|
315
432
|
conf["rcvbuf"] = std::string(StringValuePtr(val));
|
316
|
-
fprintf(stderr, "rcvbuf=%s\n", conf["rcvbuf"].c_str());
|
433
|
+
// fprintf(stderr, "rcvbuf=%s\n", conf["rcvbuf"].c_str());
|
317
434
|
}
|
318
435
|
|
319
436
|
args.set(conf);
|
@@ -337,9 +454,9 @@ extern "C" {
|
|
337
454
|
rb_define_method(rb_cHandlerSocket, "open_index", (VALUE(*)(...))hs_open_index, 5);
|
338
455
|
rb_define_method(rb_cHandlerSocket, "execute_single", (VALUE(*)(...))hs_execute_single, -1);
|
339
456
|
rb_define_method(rb_cHandlerSocket, "execute_multi", (VALUE(*)(...))hs_execute_multi, -1);
|
340
|
-
|
341
|
-
rb_define_method(rb_cHandlerSocket, "execute_update", (VALUE(*)(...))hs_execute_update,
|
342
|
-
rb_define_method(rb_cHandlerSocket, "execute_delete", (VALUE(*)(...))hs_execute_delete,
|
343
|
-
rb_define_method(rb_cHandlerSocket, "execute_insert", (VALUE(*)(...))hs_execute_insert,
|
457
|
+
rb_define_alias(rb_cHandlerSocket, "execute_find", "execute_single");
|
458
|
+
rb_define_method(rb_cHandlerSocket, "execute_update", (VALUE(*)(...))hs_execute_update, 6);
|
459
|
+
rb_define_method(rb_cHandlerSocket, "execute_delete", (VALUE(*)(...))hs_execute_delete, 5);
|
460
|
+
rb_define_method(rb_cHandlerSocket, "execute_insert", (VALUE(*)(...))hs_execute_insert, 2);
|
344
461
|
}
|
345
462
|
}
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: handler_socket
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- miyucy
|