-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtokyotyrant.rb
1324 lines (1317 loc) · 41.3 KB
/
tokyotyrant.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
106
107
108
109
110
111
112
113
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
192
193
194
195
196
197
198
199
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
233
234
235
236
237
238
239
240
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
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
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
445
446
447
448
449
450
451
452
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
510
511
512
513
514
515
516
517
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
584
585
586
587
588
589
590
591
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#--
# Pure Ruby interface of Tokyo Cabinet
# Copyright (C) 2006-2008 Mikio Hirabayashi
# This file is part of Tokyo Cabinet.
# Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of
# the GNU Lesser General Public License as published by the Free Software Foundation; either
# version 2.1 of the License or any later version. Tokyo Cabinet is distributed in the hope
# that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details.
# You should have received a copy of the GNU Lesser General Public License along with Tokyo
# Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA.
#++
#:include:overview.rd
require "socket"
module TokyoTyrant
# Remote database is a set of interfaces to use an abstract database of Tokyo Cabinet, mediated by a server of Tokyo Tyrant. Before operations to store or retrieve records, it is necessary to connect the remote database object to the server. The method `open' is used to open a database connection and the method `close' is used to close the connection.%%
class RDB
#--------------------------------
# constants
#--------------------------------
public
# error code: success
ESUCCESS = 0
# error code: invalid operation
EINVALID = 1
# error code: host not found
ENOHOST = 2
# error code: connection refused
EREFUSED = 3
# error code: send error
ESEND = 4
# error code: recv error
ERECV = 5
# error code: existing record
EKEEP = 6
# error code: no record found
ENOREC = 7
# error code: miscellaneous error
EMISC = 9999
# scripting extension option: record locking
XOLCKREC = 1 << 0
# scripting extension option: global locking
XOLCKGLB = 1 << 1
# versatile function option: omission of the update log
MONOULOG = 1 << 0
#--------------------------------
# public methods
#--------------------------------
public
# Create a remote database object.%%
# The return value is the new remote database object.%%
def initialize()
@ecode = ESUCCESS
@enc = nil
@sock = nil
end
# Get the message string corresponding to an error code.%%
# `<i>ecode</i>' specifies the error code. If it is not defined or negative, the last happened error code is specified.%%
# The return value is the message string of the error code.%%
def errmsg(ecode = nil)
ecode = @ecode if !ecode
if ecode == ESUCCESS
return "success"
elsif ecode == EINVALID
return "invalid operation"
elsif ecode == ENOHOST
return "host not found"
elsif ecode == EREFUSED
return "connection refused"
elsif ecode == ESEND
return "send error"
elsif ecode == ERECV
return "recv error"
elsif ecode == EKEEP
return "existing record"
elsif ecode == ENOREC
return "no record found"
elsif ecode == EMISC
return "miscellaneous error"
end
return "unknown"
end
# Get the last happened error code.%%
# The return value is the last happened error code.%%
# The following error code is defined: `TokyoTyrant::RDB::ESUCCESS' for success, `TokyoTyrant::RDB::EINVALID' for invalid operation, `TokyoTyrant::RDB::ENOHOST' for host not found, `TokyoTyrant::RDB::EREFUSED' for connection refused, `TokyoTyrant::RDB::ESEND' for send error, `TokyoTyrant::RDB::ERECV' for recv error, `TokyoTyrant::RDB::EKEEP' for existing record, `TokyoTyrant::RDB::ENOREC' for no record found, `TokyoTyrant::RDB::EMISC' for miscellaneous error.%%
def ecode()
return @ecode
end
# Open a remote database connection.%%
# `<i>host</i>' specifies the name or the address of the server.%%
# `<i>port</i>' specifies the port number. If it is not defined or not more than 0, UNIX domain socket is used and the path of the socket file is specified by the host parameter.%%
# If successful, the return value is true, else, it is false.%%
def open(host, port = 0)
host = _argstr(host)
port = _argnum(port)
if @sock
@ecode = EINVALID
return false
end
if port > 0
begin
info = TCPSocket.gethostbyname(host)
rescue Exception
@ecode = ENOHOST
return false
end
begin
sock = TCPSocket.open(info[3], port)
rescue Exception
@ecode = EREFUSED
return false
end
begin
sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true)
rescue Exception
end
else
begin
sock = UNIXSocket.open(host)
rescue Exception
@ecode = EREFUSED
return false
end
end
if sock.respond_to?(:set_encoding)
sock.set_encoding("ASCII-8BIT")
end
@sock = sock
return true
end
# Close the database connection.%%
# If successful, the return value is true, else, it is false.%%
def close()
if !@sock
@ecode = EINVALID
return false
end
begin
@sock.close
rescue Exception
@ecode = EMISC
@sock = nil
return false
end
@sock = nil
return true
end
# Store a record.%%
# `<i>key</i>' specifies the key.%%
# `<i>value</i>' specifies the value.%%
# If successful, the return value is true, else, it is false.%%
# If a record with the same key exists in the database, it is overwritten.%%
def put(key, value)
key = _argstr(key)
value = _argstr(value)
if !@sock
@ecode = EINVALID
return false
end
sbuf = [0xC8, 0x10, key.length, value.length].pack("CCNN")
sbuf += key + value
if !_send(sbuf)
@ecode = ESEND
return false
end
code = _recvcode
if code == -1
@ecode = ERECV
return false
end
if code != 0
@ecode = EMISC
return false
end
return true
end
# Store a new record.%%
# `<i>key</i>' specifies the key.%%
# `<i>value</i>' specifies the value.%%
# If successful, the return value is true, else, it is false.%%
# If a record with the same key exists in the database, this method has no effect.%%
def putkeep(key, value)
key = _argstr(key)
value = _argstr(value)
if !@sock
@ecode = EINVALID
return false
end
sbuf = [0xC8, 0x11, key.length, value.length].pack("CCNN")
sbuf += key + value
if !_send(sbuf)
@ecode = ESEND
return false
end
code = _recvcode
if code == -1
@ecode = ERECV
return false
end
if code != 0
@ecode = EKEEP
return false
end
return true
end
# Concatenate a value at the end of the existing record.%%
# `<i>key</i>' specifies the key.%%
# `<i>value</i>' specifies the value.%%
# If successful, the return value is true, else, it is false.%%
# If there is no corresponding record, a new record is created.%%
def putcat(key, value)
key = _argstr(key)
value = _argstr(value)
if !@sock
@ecode = EINVALID
return false
end
sbuf = [0xC8, 0x12, key.length, value.length].pack("CCNN")
sbuf += key + value
if !_send(sbuf)
@ecode = ESEND
return false
end
code = _recvcode
if code == -1
@ecode = ERECV
return false
end
if code != 0
@ecode = EMISC
return false
end
return true
end
# Concatenate a value at the end of the existing record and shift it to the left.%%
# `<i>key</i>' specifies the key.%%
# `<i>value</i>' specifies the value.%%
# `<i>width</i>' specifies the width of the record.%%
# If successful, the return value is true, else, it is false.%%
# If there is no corresponding record, a new record is created.%%
def putshl(key, value, width = 0)
key = _argstr(key)
value = _argstr(value)
width = _argnum(width)
if !@sock
@ecode = EINVALID
return false
end
sbuf = [0xC8, 0x13, key.length, value.length, width].pack("CCNNN")
sbuf += key + value
if !_send(sbuf)
@ecode = ESEND
return false
end
code = _recvcode
if code == -1
@ecode = ERECV
return false
end
if code != 0
@ecode = EMISC
return false
end
return true
end
# Store a record without response from the server.%%
# `<i>key</i>' specifies the key.%%
# `<i>value</i>' specifies the value.%%
# If successful, the return value is true, else, it is false.%%
# If a record with the same key exists in the database, it is overwritten.%%
def putnr(key, value)
key = _argstr(key)
value = _argstr(value)
if !@sock
@ecode = EINVALID
return false
end
sbuf = [0xC8, 0x18, key.length, value.length].pack("CCNN")
sbuf += key + value
if !_send(sbuf)
@ecode = ESEND
return false
end
return true
end
# Remove a record.%%
# `<i>key</i>' specifies the key.%%
# If successful, the return value is true, else, it is false.%%
def out(key)
key = _argstr(key)
if !@sock
@ecode = EINVALID
return false
end
sbuf = [0xC8, 0x20, key.length].pack("CCN")
sbuf += key
if !_send(sbuf)
@ecode = ESEND
return false
end
code = _recvcode
if code == -1
@ecode = ERECV
return false
end
if code != 0
@ecode = ENOREC
return false
end
return true
end
# Retrieve a record.%%
# `<i>key</i>' specifies the key.%%
# If successful, the return value is the value of the corresponding record. `nil' is returned if no record corresponds.%%
def get(key)
key = _argstr(key)
sbuf = [0xC8, 0x30, key.length].pack("CCN")
sbuf += key
if !_send(sbuf)
@ecode = ESEND
return nil
end
code = _recvcode
if code == -1
@ecode = ERECV
return nil
end
if code != 0
@ecode = ENOREC
return nil
end
vsiz = _recvint32
if vsiz < 0
@ecode = ERECV
return nil
end
vbuf = _recv(vsiz)
if !vbuf
@ecode = ERECV
return nil
end
return _retstr(vbuf)
end
# Retrieve records.%%
# `<i>recs</i>' specifies a hash containing the retrieval keys. As a result of this method, keys existing in the database have the corresponding values and keys not existing in the database are removed.%%
# If successful, the return value is the number of retrieved records or -1 on failure.%%
def mget(recs)
raise ArgumentError if !recs.is_a?(Hash)
if !@sock
@ecode = EINVALID
return -1
end
rnum = 0
sbuf = ""
recs.each_pair do |key, value|
key = _argstr(key)
sbuf += [key.length].pack("N") + key
rnum += 1
end
sbuf = [0xC8, 0x31, rnum].pack("CCN") + sbuf
if !_send(sbuf)
@ecode = ESEND
return -1
end
code = _recvcode
rnum = _recvint32
if code == -1
@ecode = ERECV
return -1
end
if code != 0
@ecode = ENOREC
return -1
end
if rnum < 0
@ecode = ERECV
return -1
end
recs.clear
for i in 1..rnum
ksiz = _recvint32()
vsiz = _recvint32()
if ksiz < 0 || vsiz < 0
@ecode = ERECV
return -1
end
kbuf = _recv(ksiz)
vbuf = _recv(vsiz)
if !kbuf || !vbuf
@ecode = ERECV
return -1
end
recs[kbuf] = _retstr(vbuf)
end
return rnum
end
# Get the size of the value of a record.%%
# `<i>key</i>' specifies the key.%%
# If successful, the return value is the size of the value of the corresponding record, else, it is -1.%%
def vsiz(key)
key = _argstr(key)
if !@sock
@ecode = EINVALID
return -1
end
sbuf = [0xC8, 0x38, key.length].pack("CCN")
sbuf += key
if !_send(sbuf)
@ecode = ESEND
return -1
end
code = _recvcode
if code == -1
@ecode = ERECV
return -1
end
if code != 0
@ecode = ENOREC
return -1
end
return _recvint32
end
# Initialize the iterator.%%
# If successful, the return value is true, else, it is false.%%
# The iterator is used in order to access the key of every record stored in a database.%%
def iterinit()
if !@sock
@ecode = EINVALID
return false
end
sbuf = [0xC8, 0x50].pack("CC")
if !_send(sbuf)
@ecode = ESEND
return false
end
code = _recvcode
if code == -1
@ecode = ERECV
return false
end
if code != 0
@ecode = EMISC
return false
end
return true
end
# Get the next key of the iterator.%%
# If successful, the return value is the next key, else, it is `nil'. `nil' is returned when no record is to be get out of the iterator.%%
# It is possible to access every record by iteration of calling this method. It is allowed to update or remove records whose keys are fetched while the iteration. However, it is not assured if updating the database is occurred while the iteration. Besides, the order of this traversal access method is arbitrary, so it is not assured that the order of storing matches the one of the traversal access.%%
def iternext()
if !@sock
@ecode = EINVALID
return nil
end
sbuf = [0xC8, 0x51].pack("CC")
if !_send(sbuf)
@ecode = ESEND
return nil
end
code = _recvcode
if code == -1
@ecode = ERECV
return nil
end
if code != 0
@ecode = ENOREC
return nil
end
vsiz = _recvint32
if vsiz < 0
@ecode = ERECV
return nil
end
vbuf = _recv(vsiz)
if !vbuf
@ecode = ERECV
return nil
end
return _retstr(vbuf)
end
# Get forward matching keys.%%
# `<i>prefix</i>' specifies the prefix of the corresponding keys.%%
# `<i>max</i>' specifies the maximum number of keys to be fetched. If it is not defined or negative, no limit is specified.%%
# The return value is an array of the keys of the corresponding records. This method does never fail and return an empty array even if no record corresponds.%%
# Note that this method may be very slow because every key in the database is scanned.%%
def fwmkeys(prefix, max = -1)
prefix = _argstr(prefix)
max = _argnum(max)
if !@sock
@ecode = EINVALID
return Array.new
end
sbuf = [0xC8, 0x58, prefix.length, max].pack("CCNN")
sbuf += prefix
if !_send(sbuf)
@ecode = ESEND
return Array.new
end
code = _recvcode
if code == -1
@ecode = ERECV
return Array.new
end
if code != 0
@ecode = ENOREC
return Array.new
end
knum = _recvint32
if knum < 0
@ecode = ERECV
return Array.new
end
keys = Array.new
for i in 1..knum
ksiz = _recvint32()
if ksiz < 0
@ecode = ERECV
return Array.new
end
kbuf = _recv(ksiz)
if !kbuf
@ecode = ERECV
return Array.new
end
keys.push(_retstr(kbuf))
end
return keys
end
# Add an integer to a record.%%
# `<i>key</i>' specifies the key.%%
# `<i>num</i>' specifies the additional value. If it is not defined, 0 is specified.%%
# If successful, the return value is the summation value, else, it is `nil'.%%
# If the corresponding record exists, the value is treated as an integer and is added to. If no record corresponds, a new record of the additional value is stored. Because records are stored in binary format, they should be processed with the `unpack' function with the `i' operator after retrieval.%%
def addint(key, num = 0)
key = _argstr(key)
num = _argnum(num)
if !@sock
@ecode = EINVALID
return nil
end
sbuf = [0xC8, 0x60, key.length, num].pack("CCNN")
sbuf += key
if !_send(sbuf)
@ecode = ESEND
return nil
end
code = _recvcode
if code == -1
@ecode = ERECV
return nil
end
if code != 0
@ecode = EKEEP
return nil
end
return _recvint32
end
# Add a real number to a record.%%
# `<i>key</i>' specifies the key.%%
# `<i>num</i>' specifies the additional value. If it is not defined, 0 is specified.%%
# If successful, the return value is the summation value, else, it is `nil'.%%
# If the corresponding record exists, the value is treated as a real number and is added to. If no record corresponds, a new record of the additional value is stored. Because records are stored in binary format, they should be processed with the `unpack' function with the `d' operator after retrieval.%%
def adddouble(key, num)
key = _argstr(key)
num = _argnum(num)
if !@sock
@ecode = EINVALID
return nil
end
integ = num.truncate
fract = ((num - integ) * 1000000000000).truncate
sbuf = [0xC8, 0x61, key.length].pack("CCN")
sbuf += _packquad(integ) + _packquad(fract) + key
if !_send(sbuf)
@ecode = ESEND
return nil
end
code = _recvcode
if code == -1
@ecode = ERECV
return nil
end
if code != 0
@ecode = EKEEP
return nil
end
integ = _recvint64()
fract = _recvint64()
return integ + fract / 1000000000000.0
end
# Call a function of the script language extension.%%
# `<i>name</i>' specifies the function name.%%
# `<i>key</i>' specifies the key. If it is not defined, an empty string is specified.%%
# `<i>value</i>' specifies the value. If it is not defined, an empty string is specified.%%
# `<i>opts</i>' specifies options by bitwise-or: `TokyoTyrant::RDB::XOLCKREC' for record locking, `TokyoTyrant::RDB::XOLCKGLB' for global locking. If it is not defined, no option is specified.%%
# If successful, the return value is the value of the response or `nil' on failure.%%
def ext(name, key = "", value = "", opts = 0)
name = _argstr(name)
key = _argstr(key)
value = _argstr(value)
opts = _argnum(opts)
if !@sock
@ecode = EINVALID
return nil
end
sbuf = [0xC8, 0x68, name.length, opts, key.length, value.length].pack("CCNNNN")
sbuf += name + key + value
if !_send(sbuf)
@ecode = ESEND
return nil
end
code = _recvcode
if code == -1
@ecode = ERECV
return nil
end
if code != 0
@ecode = EMISC
return nil
end
vsiz = _recvint32
if vsiz < 0
@ecode = ERECV
return nil
end
vbuf = _recv(vsiz)
if !vbuf
@ecode = ERECV
return nil
end
return _retstr(vbuf)
end
# Synchronize updated contents with the file and the device.%%
# If successful, the return value is true, else, it is false.%%
def sync()
if !@sock
@ecode = EINVALID
return false
end
sbuf = [0xC8, 0x70].pack("CC")
if !_send(sbuf)
@ecode = ESEND
return false
end
code = _recvcode
if code == -1
@ecode = ERECV
return false
end
if code != 0
@ecode = EMISC
return false
end
return true
end
# Remove all records.%%
# If successful, the return value is true, else, it is false.%%
def vanish()
if !@sock
@ecode = EINVALID
return false
end
sbuf = [0xC8, 0x71].pack("CC")
if !_send(sbuf)
@ecode = ESEND
return false
end
code = _recvcode
if code == -1
@ecode = ERECV
return false
end
if code != 0
@ecode = EMISC
return false
end
return true
end
# Copy the database file.%%
# `<i>path</i>' specifies the path of the destination file. If it begins with `@', the trailing substring is executed as a command line.%%
# If successful, the return value is true, else, it is false. False is returned if the executed command returns non-zero code.%%
# The database file is assured to be kept synchronized and not modified while the copying or executing operation is in progress. So, this method is useful to create a backup file of the database file.%%
def copy(path)
path = _argstr(path)
if !@sock
@ecode = EINVALID
return false
end
sbuf = [0xC8, 0x72, path.length].pack("CCN")
sbuf += path
if !_send(sbuf)
@ecode = ESEND
return false
end
code = _recvcode
if code == -1
@ecode = ERECV
return false
end
if code != 0
@ecode = EMISC
return false
end
return true
end
# Get the number of records.%%
# The return value is the number of records or 0 if the object does not connect to any database server.%%
def rnum()
if !@sock
@ecode = EINVALID
return 0
end
sbuf = [0xC8, 0x80].pack("CC")
if !_send(sbuf)
@ecode = ESEND
return 0
end
code = _recvcode
if code == -1
@ecode = ERECV
return 0
end
if code != 0
@ecode = EMISC
return 0
end
return _recvint64
end
# Get the size of the database.%%
# The return value is the size of the database or 0 if the object does not connect to any database server.%%
def size()
if !@sock
@ecode = EINVALID
return 0
end
sbuf = [0xC8, 0x81].pack("CC")
if !_send(sbuf)
@ecode = ESEND
return 0
end
code = _recvcode
if code == -1
@ecode = ERECV
return 0
end
if code != 0
@ecode = EMISC
return 0
end
return _recvint64
end
# Get the status string of the database server.%%
# The return value is the status message of the database or `nil' if the object does not connect to any database server. The message format is TSV. The first field of each line means the parameter name and the second field means the value.%%
def stat()
if !@sock
@ecode = EINVALID
return nil
end
sbuf = [0xC8, 0x88].pack("CC")
if !_send(sbuf)
@ecode = ESEND
return nil
end
code = _recvcode
if code == -1
@ecode = ERECV
return nil
end
if code != 0
@ecode = ENOREC
return nil
end
ssiz = _recvint32
if ssiz < 0
@ecode = ERECV
return nil
end
sbuf = _recv(ssiz)
if !sbuf
@ecode = ERECV
return nil
end
return _retstr(sbuf)
end
# Call a versatile function for miscellaneous operations.%%
# `<i>name</i>' specifies the name of the function. All databases support "putlist", "outlist", and "getlist". "putlist" is to store records. It receives keys and values one after the other, and returns an empty list. "outlist" is to remove records. It receives keys, and returns an empty array. "getlist" is to retrieve records. It receives keys, and returns keys and values of corresponding records one after the other. Table database supports "setindex", "search", and "genuid".%%
# `<i>args</i>' specifies an array containing arguments. If it is not defined, no argument is specified.%%
# `<i>opts</i>' specifies options by bitwise-or: `TokyoTyrant::RDB::MONOULOG' for omission of the update log. If it is not defined, no option is specified.%%
# If successful, the return value is an array of the result. `nil' is returned on failure.%%
def misc(name, args = [], opts = 0)
name = _argstr(name)
args = Array.new if !args.is_a?(Array)
opts = _argnum(opts)
if !@sock
@ecode = EINVALID
return nil
end
sbuf = [0xC8, 0x90, name.length, opts, args.size].pack("CCNNN")
sbuf += name
args.each do |arg|
arg = _argstr(arg)
sbuf += [arg.length].pack("N") + arg
end
if !_send(sbuf)
@ecode = ESEND
return nil
end
code = _recvcode
rnum = _recvint32
if code == -1
@ecode = ERECV
return nil
end
if code != 0
@ecode = EMISC
return nil
end
res = Array.new
for i in 1..rnum
esiz = _recvint32
if esiz < 0
@ecode = ERECV
return nil
end
ebuf = _recv(esiz)
if !ebuf
@ecode = ERECV
return nil
end
res.push(_retstr(ebuf))
end
return res
end
#--------------------------------
# aliases and iterators
#--------------------------------
public
# Hash-compatible method.%%
# Alias of `put'.%%
def store(key, value)
return put(key, value)
end
# Hash-compatible method.%%
# Alias of `out'.%%
def delete(key)
return out(key)
end
# Hash-compatible method.%%
# Alias of `get'.%%
def fetch(key)
return out(key)
end
# Hash-compatible method.%%
# Check existence of a key.%%
def has_key?(key)
return vsiz(key) >= 0
end
# Hash-compatible method.%%
# Check existence of a value.%%
def has_value?(value)
return nil if !iterinit
while tkey = iternext
tvalue = get(tkey)
break if !tvalue
return true if value == tvalue
end
return false
end
# Hash-compatible method.%%
# Alias of `vanish'.%%
def clear
return vanish
end
# Hash-compatible method.%%
# Alias of `rnum'.%%
def length
return rnum
end
# Hash-compatible method.%%
# Alias of `rnum > 0'.%%
def empty?
return rnum > 0
end
# Hash-compatible method.%%
# Alias of `put'.%%
def []=(key, value)
return put(key, value)
end
# Hash-compatible method.%%
# Alias of `get'.%%
def [](key)
return get(key)
end
# Hash-compatible method.%%
# Iterator of pairs of the key and the value.%%
def each
return nil if !iterinit
while key = iternext
value = get(key)
break if !value
yield(key, value)
end
return nil
end
alias each_pair each
# Hash-compatible method.%%
# Iterator of the keys.%%
def each_keys
return nil if !iterinit
while key = iternext
yield(key)
end
return nil
end
# Hash-compatible method.%%
# Iterator of the values.%%
def each_values
return nil if !iterinit
while key = iternext
value = get(key)
break if !value
yield(value)
end
return nil
end
# Hash-compatible method.%%
# Get an array of all keys.%%
def keys
tkeys = Array.new
return tkeys if !iterinit
while key = iternext
tkeys.push(key)
end
return tkeys
end
# Hash-compatible method.%%
# Get an array of all keys.%%
def values
tvals = Array.new
return tvals if !iterinit
while key = iternext
value = get(key)
break if !value
tvals.push(value)
end
return tvals
end
#--------------------------------
# private methods
#--------------------------------
private
# Get a string argument.%%
def _argstr(obj)
case obj
when Numeric
obj = obj.to_s
when Symbol
obj = obj.to_s
when String
else
raise ArgumentError
end
if obj.respond_to?(:force_encoding)
obj = obj.dup
obj.force_encoding("ASCII-8BIT")
end
return obj
end
# Get a numeric argument.%%
def _argnum(obj)
case obj
when String
obj = obj.to_i
when Numeric
else
raise ArgumentError
end
return obj
end
# Get a normalized string to be returned
def _retstr(str)
if str.respond_to?(:force_encoding)
if @enc
str.force_encoding(@enc)
elsif Encoding.default_internal
str.force_encoding(Encoding.default_internal)
else
str.force_encoding("UTF-8")
end
end
return str
end
# Send a series of data.%%
def _send(buf)