In Files

Parent

Class Index [+]

Quicksearch

TokyoTyrant::RDB

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.%%

Constants

ESUCCESS

error code: success

EINVALID

error code: invalid operation

ENOHOST

error code: host not found

EREFUSED

error code: connection refused

ESEND

error code: send error

ERECV

error code: recv error

EKEEP

error code: existing record

ENOREC

error code: no record found

EMISC

error code: miscellaneous error

XOLCKREC

scripting extension option: record locking

XOLCKGLB

scripting extension option: global locking

MONOULOG

versatile function option: omission of the update log

Public Class Methods

new() click to toggle source

Create a remote database object.%% The return value is the new remote database object.%%

    # File tokyotyrant.rb, line 58
58:     def initialize()
59:       @ecode = ESUCCESS
60:       @enc = nil
61:       @sock = nil
62:     end

Public Instance Methods

[](key) click to toggle source

Hash-compatible method.%% Alias of `get’.%%

     # File tokyotyrant.rb, line 936
936:     def [](key)
937:       return get(key)
938:     end
[]=(key, value) click to toggle source

Hash-compatible method.%% Alias of `put’.%%

     # File tokyotyrant.rb, line 931
931:     def []=(key, value)
932:       return put(key, value)
933:     end
adddouble(key, num) click to toggle source

Add a real number to a record.%% `key’ specifies the key.%% `num’ 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.%%

     # File tokyotyrant.rb, line 572
572:     def adddouble(key, num)
573:       key = _argstr(key)
574:       num = _argnum(num)
575:       if !@sock
576:         @ecode = EINVALID
577:         return nil
578:       end
579:       integ = num.truncate
580:       fract = ((num - integ) * 1000000000000).truncate
581:       sbuf = [0xC8, 0x61, key.length].pack("CCN")
582:       sbuf += _packquad(integ) + _packquad(fract) + key
583:       if !_send(sbuf)
584:         @ecode = ESEND
585:         return nil
586:       end
587:       code = _recvcode
588:       if code == 1
589:         @ecode = ERECV
590:         return nil
591:       end
592:       if code != 0
593:         @ecode = EKEEP
594:         return nil
595:       end
596:       integ = _recvint64()
597:       fract = _recvint64()
598:       return integ + fract / 1000000000000.0
599:     end
addint(key, num = 0) click to toggle source

Add an integer to a record.%% `key’ specifies the key.%% `num’ 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.%%

     # File tokyotyrant.rb, line 543
543:     def addint(key, num = 0)
544:       key = _argstr(key)
545:       num = _argnum(num)
546:       if !@sock
547:         @ecode = EINVALID
548:         return nil
549:       end
550:       sbuf = [0xC8, 0x60, key.length, num].pack("CCNN")
551:       sbuf += key
552:       if !_send(sbuf)
553:         @ecode = ESEND
554:         return nil
555:       end
556:       code = _recvcode
557:       if code == 1
558:         @ecode = ERECV
559:         return nil
560:       end
561:       if code != 0
562:         @ecode = EKEEP
563:         return nil
564:       end
565:       return _recvint32
566:     end
clear() click to toggle source

Hash-compatible method.%% Alias of `vanish’.%%

     # File tokyotyrant.rb, line 916
916:     def clear
917:       return vanish
918:     end
close() click to toggle source

Close the database connection.%% If successful, the return value is true, else, it is false.%%

     # File tokyotyrant.rb, line 142
142:     def close()
143:       if !@sock
144:         @ecode = EINVALID
145:         return false
146:       end
147:       begin
148:         @sock.close
149:       rescue Exception
150:         @ecode = EMISC
151:         @sock = nil
152:         return false
153:       end
154:       @sock = nil
155:       return true
156:     end
copy(path) click to toggle source

Copy the database file.%% `path’ 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.%%

     # File tokyotyrant.rb, line 718
718:     def copy(path)
719:       path = _argstr(path)
720:       if !@sock
721:         @ecode = EINVALID
722:         return false
723:       end
724:       sbuf = [0xC8, 0x73, path.length].pack("CCN")
725:       sbuf += path
726:       if !_send(sbuf)
727:         @ecode = ESEND
728:         return false
729:       end
730:       code = _recvcode
731:       if code == 1
732:         @ecode = ERECV
733:         return false
734:       end
735:       if code != 0
736:         @ecode = EMISC
737:         return false
738:       end
739:       return true
740:     end
delete(key) click to toggle source

Hash-compatible method.%% Alias of `out’.%%

     # File tokyotyrant.rb, line 890
890:     def delete(key)
891:       return out(key)
892:     end
each() click to toggle source

Hash-compatible method.%% Iterator of pairs of the key and the value.%%

     # File tokyotyrant.rb, line 941
941:     def each
942:       return nil if !iterinit
943:       while key = iternext
944:         value = get(key)
945:         break if !value
946:         yield(key, value)
947:       end
948:       return nil
949:     end
Also aliased as: each_pair
each_keys() click to toggle source

Hash-compatible method.%% Iterator of the keys.%%

     # File tokyotyrant.rb, line 953
953:     def each_keys
954:       return nil if !iterinit
955:       while key = iternext
956:         yield(key)
957:       end
958:       return nil
959:     end
each_pair() click to toggle source
Alias for: each
each_values() click to toggle source

Hash-compatible method.%% Iterator of the values.%%

     # File tokyotyrant.rb, line 962
962:     def each_values
963:       return nil if !iterinit
964:       while key = iternext
965:         value = get(key)
966:         break if !value
967:         yield(value)
968:       end
969:       return nil
970:     end
ecode() click to toggle source

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.%%

    # File tokyotyrant.rb, line 92
92:     def ecode()
93:       return @ecode
94:     end
empty?() click to toggle source

Hash-compatible method.%% Alias of `rnum < 1’.%%

     # File tokyotyrant.rb, line 926
926:     def empty?
927:       return rnum < 1
928:     end
errmsg(ecode = nil) click to toggle source

Get the message string corresponding to an error code.%% `ecode’ 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.%%

    # File tokyotyrant.rb, line 66
66:     def errmsg(ecode = nil)
67:       ecode = @ecode if !ecode
68:       if ecode == ESUCCESS
69:         return "success"
70:       elsif ecode == EINVALID
71:         return "invalid operation"
72:       elsif ecode == ENOHOST
73:         return "host not found"
74:       elsif ecode == EREFUSED
75:         return "connection refused"
76:       elsif ecode == ESEND
77:         return "send error"
78:       elsif ecode == ERECV
79:         return "recv error"
80:       elsif ecode == EKEEP
81:         return "existing record"
82:       elsif ecode == ENOREC
83:         return "no record found"
84:       elsif ecode == EMISC
85:         return "miscellaneous error"
86:       end
87:       return "unknown"
88:     end
ext(name, key = "", value = "", opts = 0) click to toggle source

Call a function of the script language extension.%% `name’ specifies the function name.%% `key’ specifies the key. If it is not defined, an empty string is specified.%% `value’ specifies the value. If it is not defined, an empty string is specified.%% `opts’ 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.%%

     # File tokyotyrant.rb, line 606
606:     def ext(name, key = "", value = "", opts = 0)
607:       name = _argstr(name)
608:       key = _argstr(key)
609:       value = _argstr(value)
610:       opts = _argnum(opts)
611:       if !@sock
612:         @ecode = EINVALID
613:         return nil
614:       end
615:       sbuf = [0xC8, 0x68, name.length, opts, key.length, value.length].pack("CCNNNN")
616:       sbuf += name + key + value
617:       if !_send(sbuf)
618:         @ecode = ESEND
619:         return nil
620:       end
621:       code = _recvcode
622:       if code == 1
623:         @ecode = ERECV
624:         return nil
625:       end
626:       if code != 0
627:         @ecode = EMISC
628:         return nil
629:       end
630:       vsiz = _recvint32
631:       if vsiz < 0
632:         @ecode = ERECV
633:         return nil
634:       end
635:       vbuf = _recv(vsiz)
636:       if !vbuf
637:         @ecode = ERECV
638:         return nil
639:       end
640:       return _retstr(vbuf)
641:     end
fetch(key) click to toggle source

Hash-compatible method.%% Alias of `get’.%%

     # File tokyotyrant.rb, line 895
895:     def fetch(key)
896:       return get(key)
897:     end
fwmkeys(prefix, max = -1) click to toggle source

Get forward matching keys.%% `prefix’ specifies the prefix of the corresponding keys.%% `max’ 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. It returns an empty array even if no record corresponds.%% Note that this method may be very slow because every key in the database is scanned.%%

     # File tokyotyrant.rb, line 495
495:     def fwmkeys(prefix, max = 1)
496:       prefix = _argstr(prefix)
497:       max = _argnum(max)
498:       if !@sock
499:         @ecode = EINVALID
500:         return Array::new
501:       end
502:       sbuf = [0xC8, 0x58, prefix.length, max].pack("CCNN")
503:       sbuf += prefix
504:       if !_send(sbuf)
505:         @ecode = ESEND
506:         return Array::new
507:       end
508:       code = _recvcode
509:       if code == 1
510:         @ecode = ERECV
511:         return Array::new
512:       end
513:       if code != 0
514:         @ecode = ENOREC
515:         return Array::new
516:       end
517:       knum = _recvint32
518:       if knum < 0
519:         @ecode = ERECV
520:         return Array::new
521:       end
522:       keys = Array::new
523:       for i in 1..knum
524:         ksiz = _recvint32()
525:         if ksiz < 0
526:           @ecode = ERECV
527:           return Array::new
528:         end
529:         kbuf = _recv(ksiz)
530:         if !kbuf
531:           @ecode = ERECV
532:           return Array::new
533:         end
534:         keys.push(_retstr(kbuf))
535:       end
536:       return keys
537:     end
get(key) click to toggle source

Retrieve a record.%% `key’ specifies the key.%% If successful, the return value is the value of the corresponding record. `nil’ is returned if no record corresponds.%%

     # File tokyotyrant.rb, line 324
324:     def get(key)
325:       key = _argstr(key)
326:       sbuf = [0xC8, 0x30, key.length].pack("CCN")
327:       sbuf += key
328:       if !_send(sbuf)
329:         @ecode = ESEND
330:         return nil
331:       end
332:       code = _recvcode
333:       if code == 1
334:         @ecode = ERECV
335:         return nil
336:       end
337:       if code != 0
338:         @ecode = ENOREC
339:         return nil
340:       end
341:       vsiz = _recvint32
342:       if vsiz < 0
343:         @ecode = ERECV
344:         return nil
345:       end
346:       vbuf = _recv(vsiz)
347:       if !vbuf
348:         @ecode = ERECV
349:         return nil
350:       end
351:       return _retstr(vbuf)
352:     end
has_key?(key) click to toggle source

Hash-compatible method.%% Check existence of a key.%%

     # File tokyotyrant.rb, line 900
900:     def has_key?(key)
901:       return vsiz(key) >= 0
902:     end
has_value?(value) click to toggle source

Hash-compatible method.%% Check existence of a value.%%

     # File tokyotyrant.rb, line 905
905:     def has_value?(value)
906:       return nil if !iterinit
907:       while tkey = iternext
908:         tvalue = get(tkey)
909:         break if !tvalue
910:         return true if value == tvalue
911:       end
912:       return false
913:     end
iterinit() click to toggle source

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.%%

     # File tokyotyrant.rb, line 435
435:     def iterinit()
436:       if !@sock
437:         @ecode = EINVALID
438:         return false
439:       end
440:       sbuf = [0xC8, 0x50].pack("CC")
441:       if !_send(sbuf)
442:         @ecode = ESEND
443:         return false
444:       end
445:       code = _recvcode
446:       if code == 1
447:         @ecode = ERECV
448:         return false
449:       end
450:       if code != 0
451:         @ecode = EMISC
452:         return false
453:       end
454:       return true
455:     end
iternext() click to toggle source

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.%%

     # File tokyotyrant.rb, line 459
459:     def iternext()
460:       if !@sock
461:         @ecode = EINVALID
462:         return nil
463:       end
464:       sbuf = [0xC8, 0x51].pack("CC")
465:       if !_send(sbuf)
466:         @ecode = ESEND
467:         return nil
468:       end
469:       code = _recvcode
470:       if code == 1
471:         @ecode = ERECV
472:         return nil
473:       end
474:       if code != 0
475:         @ecode = ENOREC
476:         return nil
477:       end
478:       vsiz = _recvint32
479:       if vsiz < 0
480:         @ecode = ERECV
481:         return nil
482:       end
483:       vbuf = _recv(vsiz)
484:       if !vbuf
485:         @ecode = ERECV
486:         return nil
487:       end
488:       return _retstr(vbuf)
489:     end
keys() click to toggle source

Hash-compatible method.%% Get an array of all keys.%%

     # File tokyotyrant.rb, line 973
973:     def keys
974:       tkeys = Array::new
975:       return tkeys if !iterinit
976:       while key = iternext
977:         tkeys.push(key)
978:       end
979:       return tkeys
980:     end
length() click to toggle source

Hash-compatible method.%% Alias of `rnum’.%%

     # File tokyotyrant.rb, line 921
921:     def length
922:       return rnum
923:     end
mget(recs) click to toggle source

Retrieve records.%% `recs’ 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.%%

     # File tokyotyrant.rb, line 356
356:     def mget(recs)
357:       raise ArgumentError if !recs.is_a?(Hash)
358:       if !@sock
359:         @ecode = EINVALID
360:         return 1
361:       end
362:       rnum = 0
363:       sbuf = ""
364:       recs.each_pair do |key, value|
365:         key = _argstr(key)
366:         sbuf += [key.length].pack("N") + key
367:         rnum += 1
368:       end
369:       sbuf = [0xC8, 0x31, rnum].pack("CCN") + sbuf
370:       if !_send(sbuf)
371:         @ecode = ESEND
372:         return 1
373:       end
374:       code = _recvcode
375:       rnum = _recvint32
376:       if code == 1
377:         @ecode = ERECV
378:         return 1
379:       end
380:       if code != 0
381:         @ecode = ENOREC
382:         return 1
383:       end
384:       if rnum < 0
385:         @ecode = ERECV
386:         return 1
387:       end
388:       recs.clear
389:       for i in 1..rnum
390:         ksiz = _recvint32()
391:         vsiz = _recvint32()
392:         if ksiz < 0 || vsiz < 0
393:           @ecode = ERECV
394:           return 1
395:         end
396:         kbuf = _recv(ksiz)
397:         vbuf = _recv(vsiz)
398:         if !kbuf || !vbuf
399:           @ecode = ERECV
400:           return 1
401:         end
402:         recs[kbuf] = _retstr(vbuf)
403:       end
404:       return rnum
405:     end
misc(name, args = [], opts = 0) click to toggle source

Call a versatile function for miscellaneous operations.%% `name’ 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”.%% `args’ specifies an array containing arguments. If it is not defined, no argument is specified.%% `opts’ 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.%%

     # File tokyotyrant.rb, line 835
835:     def misc(name, args = [], opts = 0)
836:       name = _argstr(name)
837:       args = Array::new if !args.is_a?(Array)
838:       opts = _argnum(opts)
839:       if !@sock
840:         @ecode = EINVALID
841:         return nil
842:       end
843:       sbuf = [0xC8, 0x90, name.length, opts, args.size].pack("CCNNN")
844:       sbuf += name
845:       args.each do |arg|
846:         arg = _argstr(arg)
847:         sbuf += [arg.length].pack("N") + arg
848:       end
849:       if !_send(sbuf)
850:         @ecode = ESEND
851:         return nil
852:       end
853:       code = _recvcode
854:       rnum = _recvint32
855:       if code == 1
856:         @ecode = ERECV
857:         return nil
858:       end
859:       if code != 0
860:         @ecode = EMISC
861:         return nil
862:       end
863:       res = Array::new
864:       for i in 1..rnum
865:         esiz = _recvint32
866:         if esiz < 0
867:           @ecode = ERECV
868:           return nil
869:         end
870:         ebuf = _recv(esiz)
871:         if !ebuf
872:           @ecode = ERECV
873:           return nil
874:         end
875:         res.push(_retstr(ebuf))
876:       end
877:       return res
878:     end
open(host, port = 0, timeout = 0) click to toggle source

Open a remote database connection.%% `host’ specifies the name or the address of the server.%% `port’ 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.%% `timeout’ specifies the timeout of each query in seconds. If it is not defined or not more than 0, the timeout is not specified. If successful, the return value is true, else, it is false.%%

     # File tokyotyrant.rb, line 100
100:     def open(host, port = 0, timeout = 0)
101:       host = _argstr(host)
102:       port = _argnum(port)
103:       timeout = _argnum(timeout)
104:       if @sock
105:         @ecode = EINVALID
106:         return false
107:       end
108:       if port > 0
109:         begin
110:           info = TCPSocket::gethostbyname(host)
111:         rescue Exception
112:           @ecode = ENOHOST
113:           return false
114:         end
115:         begin
116:           sock = TCPSocket::open(info[3], port)
117:         rescue Exception
118:           @ecode = EREFUSED
119:           return false
120:         end
121:         begin
122:           sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true)
123:         rescue Exception
124:         end
125:       else
126:         begin
127:           sock = UNIXSocket::open(host)
128:         rescue Exception
129:           @ecode = EREFUSED
130:           return false
131:         end
132:       end
133:       if sock.respond_to?(:set_encoding)
134:         sock.set_encoding("ASCII-8BIT")
135:       end
136:       @sock = sock
137:       @tout = timeout
138:       return true
139:     end
optimize(params = nil) click to toggle source

Optimize the storage.%% `params’ specifies the string of the tuning parameters. If it is not defined, it is not used.%% If successful, the return value is true, else, it is false.%%

     # File tokyotyrant.rb, line 668
668:     def optimize(params = nil)
669:       params = params ? _argstr(params) : ""
670:       if !@sock
671:         @ecode = EINVALID
672:         return false
673:       end
674:       sbuf = [0xC8, 0x71, params.length].pack("CCN")
675:       sbuf += params
676:       if !_send(sbuf)
677:         @ecode = ESEND
678:         return false
679:       end
680:       code = _recvcode
681:       if code == 1
682:         @ecode = ERECV
683:         return false
684:       end
685:       if code != 0
686:         @ecode = EMISC
687:         return false
688:       end
689:       return true
690:     end
out(key) click to toggle source

Remove a record.%% `key’ specifies the key.%% If successful, the return value is true, else, it is false.%%

     # File tokyotyrant.rb, line 298
298:     def out(key)
299:       key = _argstr(key)
300:       if !@sock
301:         @ecode = EINVALID
302:         return false
303:       end
304:       sbuf = [0xC8, 0x20, key.length].pack("CCN")
305:       sbuf += key
306:       if !_send(sbuf)
307:         @ecode = ESEND
308:         return false
309:       end
310:       code = _recvcode
311:       if code == 1
312:         @ecode = ERECV
313:         return false
314:       end
315:       if code != 0
316:         @ecode = ENOREC
317:         return false
318:       end
319:       return true
320:     end
put(key, value) click to toggle source

Store a record.%% `key’ specifies the key.%% `value’ 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.%%

     # File tokyotyrant.rb, line 162
162:     def put(key, value)
163:       key = _argstr(key)
164:       value = _argstr(value)
165:       if !@sock
166:         @ecode = EINVALID
167:         return false
168:       end
169:       sbuf = [0xC8, 0x10, key.length, value.length].pack("CCNN")
170:       sbuf += key + value
171:       if !_send(sbuf)
172:         @ecode = ESEND
173:         return false
174:       end
175:       code = _recvcode
176:       if code == 1
177:         @ecode = ERECV
178:         return false
179:       end
180:       if code != 0
181:         @ecode = EMISC
182:         return false
183:       end
184:       return true
185:     end
putcat(key, value) click to toggle source

Concatenate a value at the end of the existing record.%% `key’ specifies the key.%% `value’ 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.%%

     # File tokyotyrant.rb, line 220
220:     def putcat(key, value)
221:       key = _argstr(key)
222:       value = _argstr(value)
223:       if !@sock
224:         @ecode = EINVALID
225:         return false
226:       end
227:       sbuf = [0xC8, 0x12, key.length, value.length].pack("CCNN")
228:       sbuf += key + value
229:       if !_send(sbuf)
230:         @ecode = ESEND
231:         return false
232:       end
233:       code = _recvcode
234:       if code == 1
235:         @ecode = ERECV
236:         return false
237:       end
238:       if code != 0
239:         @ecode = EMISC
240:         return false
241:       end
242:       return true
243:     end
putkeep(key, value) click to toggle source

Store a new record.%% `key’ specifies the key.%% `value’ 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.%%

     # File tokyotyrant.rb, line 191
191:     def putkeep(key, value)
192:       key = _argstr(key)
193:       value = _argstr(value)
194:       if !@sock
195:         @ecode = EINVALID
196:         return false
197:       end
198:       sbuf = [0xC8, 0x11, key.length, value.length].pack("CCNN")
199:       sbuf += key + value
200:       if !_send(sbuf)
201:         @ecode = ESEND
202:         return false
203:       end
204:       code = _recvcode
205:       if code == 1
206:         @ecode = ERECV
207:         return false
208:       end
209:       if code != 0
210:         @ecode = EKEEP
211:         return false
212:       end
213:       return true
214:     end
putnr(key, value) click to toggle source

Store a record without response from the server.%% `key’ specifies the key.%% `value’ 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.%%

     # File tokyotyrant.rb, line 280
280:     def putnr(key, value)
281:       key = _argstr(key)
282:       value = _argstr(value)
283:       if !@sock
284:         @ecode = EINVALID
285:         return false
286:       end
287:       sbuf = [0xC8, 0x18, key.length, value.length].pack("CCNN")
288:       sbuf += key + value
289:       if !_send(sbuf)
290:         @ecode = ESEND
291:         return false
292:       end
293:       return true
294:     end
putshl(key, value, width = 0) click to toggle source

Concatenate a value at the end of the existing record and shift it to the left.%% `key’ specifies the key.%% `value’ specifies the value.%% `width’ 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.%%

     # File tokyotyrant.rb, line 250
250:     def putshl(key, value, width = 0)
251:       key = _argstr(key)
252:       value = _argstr(value)
253:       width = _argnum(width)
254:       if !@sock
255:         @ecode = EINVALID
256:         return false
257:       end
258:       sbuf = [0xC8, 0x13, key.length, value.length, width].pack("CCNNN")
259:       sbuf += key + value
260:       if !_send(sbuf)
261:         @ecode = ESEND
262:         return false
263:       end
264:       code = _recvcode
265:       if code == 1
266:         @ecode = ERECV
267:         return false
268:       end
269:       if code != 0
270:         @ecode = EMISC
271:         return false
272:       end
273:       return true
274:     end
rnum() click to toggle source

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.%%

     # File tokyotyrant.rb, line 743
743:     def rnum()
744:       if !@sock
745:         @ecode = EINVALID
746:         return 0
747:       end
748:       sbuf = [0xC8, 0x80].pack("CC")
749:       if !_send(sbuf)
750:         @ecode = ESEND
751:         return 0
752:       end
753:       code = _recvcode
754:       if code == 1
755:         @ecode = ERECV
756:         return 0
757:       end
758:       if code != 0
759:         @ecode = EMISC
760:         return 0
761:       end
762:       rv = _recvint64
763:       if rv < 0
764:         @ecode = ERECV
765:         return 0
766:       end
767:       return rv
768:     end
size() click to toggle source

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.%%

     # File tokyotyrant.rb, line 771
771:     def size()
772:       if !@sock
773:         @ecode = EINVALID
774:         return 0
775:       end
776:       sbuf = [0xC8, 0x81].pack("CC")
777:       if !_send(sbuf)
778:         @ecode = ESEND
779:         return 0
780:       end
781:       code = _recvcode
782:       if code == 1
783:         @ecode = ERECV
784:         return 0
785:       end
786:       if code != 0
787:         @ecode = EMISC
788:         return 0
789:       end
790:       rv = _recvint64
791:       if rv < 0
792:         @ecode = ERECV
793:         return 0
794:       end
795:       return rv
796:     end
stat() click to toggle source

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.%%

     # File tokyotyrant.rb, line 799
799:     def stat()
800:       if !@sock
801:         @ecode = EINVALID
802:         return nil
803:       end
804:       sbuf = [0xC8, 0x88].pack("CC")
805:       if !_send(sbuf)
806:         @ecode = ESEND
807:         return nil
808:       end
809:       code = _recvcode
810:       if code == 1
811:         @ecode = ERECV
812:         return nil
813:       end
814:       if code != 0
815:         @ecode = ENOREC
816:         return nil
817:       end
818:       ssiz = _recvint32
819:       if ssiz < 0
820:         @ecode = ERECV
821:         return nil
822:       end
823:       sbuf = _recv(ssiz)
824:       if !sbuf
825:         @ecode = ERECV
826:         return nil
827:       end
828:       return _retstr(sbuf)
829:     end
store(key, value) click to toggle source

Hash-compatible method.%% Alias of `put’.%%

     # File tokyotyrant.rb, line 885
885:     def store(key, value)
886:       return put(key, value)
887:     end
sync() click to toggle source

Synchronize updated contents with the file and the device.%% If successful, the return value is true, else, it is false.%%

     # File tokyotyrant.rb, line 644
644:     def sync()
645:       if !@sock
646:         @ecode = EINVALID
647:         return false
648:       end
649:       sbuf = [0xC8, 0x70].pack("CC")
650:       if !_send(sbuf)
651:         @ecode = ESEND
652:         return false
653:       end
654:       code = _recvcode
655:       if code == 1
656:         @ecode = ERECV
657:         return false
658:       end
659:       if code != 0
660:         @ecode = EMISC
661:         return false
662:       end
663:       return true
664:     end
values() click to toggle source

Hash-compatible method.%% Get an array of all keys.%%

     # File tokyotyrant.rb, line 983
983:     def values
984:       tvals = Array::new
985:       return tvals if !iterinit
986:       while key = iternext
987:         value = get(key)
988:         break if !value
989:         tvals.push(value)
990:       end
991:       return tvals
992:     end
vanish() click to toggle source

Remove all records.%% If successful, the return value is true, else, it is false.%%

     # File tokyotyrant.rb, line 693
693:     def vanish()
694:       if !@sock
695:         @ecode = EINVALID
696:         return false
697:       end
698:       sbuf = [0xC8, 0x72].pack("CC")
699:       if !_send(sbuf)
700:         @ecode = ESEND
701:         return false
702:       end
703:       code = _recvcode
704:       if code == 1
705:         @ecode = ERECV
706:         return false
707:       end
708:       if code != 0
709:         @ecode = EMISC
710:         return false
711:       end
712:       return true
713:     end
vsiz(key) click to toggle source

Get the size of the value of a record.%% `key’ specifies the key.%% If successful, the return value is the size of the value of the corresponding record, else, it is -1.%%

     # File tokyotyrant.rb, line 409
409:     def vsiz(key)
410:       key = _argstr(key)
411:       if !@sock
412:         @ecode = EINVALID
413:         return 1
414:       end
415:       sbuf = [0xC8, 0x38, key.length].pack("CCN")
416:       sbuf += key
417:       if !_send(sbuf)
418:         @ecode = ESEND
419:         return 1
420:       end
421:       code = _recvcode
422:       if code == 1
423:         @ecode = ERECV
424:         return 1
425:       end
426:       if code != 0
427:         @ecode = ENOREC
428:         return 1
429:       end
430:       return _recvint32
431:     end

Private Instance Methods

_argnum(obj) click to toggle source

Get a numeric argument.%%

      # File tokyotyrant.rb, line 1015
1015:     def _argnum(obj)
1016:       case obj
1017:       when String
1018:         obj = obj.to_i
1019:       when Numeric
1020:       else
1021:         raise ArgumentError
1022:       end
1023:       return obj
1024:     end
_argstr(obj) click to toggle source

Get a string argument.%%

      # File tokyotyrant.rb, line 998
 998:     def _argstr(obj)
 999:       case obj
1000:       when Numeric
1001:         obj = obj.to_s
1002:       when Symbol
1003:         obj = obj.to_s
1004:       when String
1005:       else
1006:         raise ArgumentError
1007:       end
1008:       if obj.respond_to?(:force_encoding)
1009:         obj = obj.dup
1010:         obj.force_encoding("ASCII-8BIT")
1011:       end
1012:       return obj
1013:     end
_packquad(num) click to toggle source

Pack an int64 value.%%

      # File tokyotyrant.rb, line 1098
1098:     def _packquad(num)
1099:       high = (num / (1 << 32)).truncate
1100:       low = num % (1 << 32)
1101:       return [high, low].pack("NN")
1102:     end
_recv(len) click to toggle source

Receive a series of data.%%

      # File tokyotyrant.rb, line 1054
1054:     def _recv(len)
1055:       return "" if len < 1
1056:       begin
1057:         return nil if @tout > 0 && !IO::select([ @sock ], nil, nil, @tout)
1058:         str = @sock.recv(len)
1059:         return nil if str.length < 1
1060:         len -= str.length
1061:         while len > 0
1062:           return nil if @tout > 0 && !IO::select([ @sock ], nil, nil, @tout)
1063:           tstr = @sock.recv(len, 0)
1064:           if tstr.length < 1
1065:             tstr = @sock.recv(len, 0)
1066:             return nil if tstr.length < 1
1067:           end
1068:           len -= tstr.length
1069:           str += tstr
1070:         end
1071:         return str
1072:       rescue Exception
1073:         return nil
1074:       end
1075:     end
_recvcode() click to toggle source

Receive a byte code.%%

      # File tokyotyrant.rb, line 1077
1077:     def _recvcode()
1078:       rbuf = _recv(1)
1079:       return 1 if !rbuf
1080:       return rbuf.unpack("C")[0]
1081:     end
_recvint32() click to toggle source

Receive an int32 number.%%

      # File tokyotyrant.rb, line 1083
1083:     def _recvint32()
1084:       rbuf = _recv(4)
1085:       return 1 if !rbuf
1086:       num = rbuf.unpack("N")[0]
1087:       return [num].pack("l").unpack("l")[0]
1088:     end
_recvint64() click to toggle source

Receive an int64 number.%%

      # File tokyotyrant.rb, line 1090
1090:     def _recvint64()
1091:       rbuf = _recv(8)
1092:       return 1 if !rbuf
1093:       high, low = rbuf.unpack("NN")
1094:       num = (high << 32) + low
1095:       return [num].pack("q").unpack("q")[0]
1096:     end
_retstr(str) click to toggle source

Get a normalized string to be returned

      # File tokyotyrant.rb, line 1026
1026:     def _retstr(str)
1027:       if str.respond_to?(:force_encoding)
1028:         if @enc
1029:           str.force_encoding(@enc)
1030:         elsif Encoding.default_internal
1031:           str.force_encoding(Encoding::default_internal)
1032:         else
1033:           str.force_encoding("UTF-8")
1034:         end
1035:       end
1036:       return str
1037:     end
_send(buf) click to toggle source

Send a series of data.%%

      # File tokyotyrant.rb, line 1039
1039:     def _send(buf)
1040:       begin
1041:         while true
1042:           return false if @tout > 0 && !IO::select(nil, [ @sock ], nil, @tout)
1043:           rv = @sock.send(buf, 0)
1044:           return false if rv < 1
1045:           break if rv == buf.length
1046:           buf = buf[rv,buf.length]
1047:         end
1048:       rescue Exception
1049:         return false
1050:       end
1051:       return true
1052:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.