jackowayed-rufus-tokyo 0.1.13.1 → 0.1.13.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,102 @@
1
+ #--
2
+ # Copyright (c) 2009, John Mettraux, jmettraux@gmail.com
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ #
22
+ # Made in Japan.
23
+ #++
24
+
25
+
26
+ module Rufus
27
+ module Tokyo
28
+
29
+ #
30
+ # Some constants shared by table implementations.
31
+ #
32
+ module QueryConstants
33
+
34
+ OPERATORS = {
35
+
36
+ # strings...
37
+
38
+ :streq => 0, # string equality
39
+ :eq => 0,
40
+ :eql => 0,
41
+ :equals => 0,
42
+
43
+ :strinc => 1, # string include
44
+ :inc => 1, # string include
45
+ :includes => 1, # string include
46
+
47
+ :strbw => 2, # string begins with
48
+ :bw => 2,
49
+ :starts_with => 2,
50
+ :strew => 3, # string ends with
51
+ :ew => 3,
52
+ :ends_with => 3,
53
+
54
+ :strand => 4, # string which include all the tokens in the given exp
55
+ :and => 4,
56
+
57
+ :stror => 5, # string which include at least one of the tokens
58
+ :or => 5,
59
+
60
+ :stroreq => 6, # string which is equal to at least one token
61
+
62
+ :strorrx => 7, # string which matches the given regex
63
+ :regex => 7,
64
+ :matches => 7,
65
+
66
+ # numbers...
67
+
68
+ :numeq => 8, # equal
69
+ :numequals => 8,
70
+ :numgt => 9, # greater than
71
+ :gt => 9,
72
+ :numge => 10, # greater or equal
73
+ :ge => 10,
74
+ :gte => 10,
75
+ :numlt => 11, # greater or equal
76
+ :lt => 11,
77
+ :numle => 12, # greater or equal
78
+ :le => 12,
79
+ :lte => 12,
80
+ :numbt => 13, # a number between two tokens in the given exp
81
+ :bt => 13,
82
+ :between => 13,
83
+
84
+ :numoreq => 14 # number which is equal to at least one token
85
+ }
86
+
87
+ TDBQCNEGATE = 1 << 24
88
+ TDBQCNOIDX = 1 << 25
89
+
90
+ DIRECTIONS = {
91
+ :strasc => 0,
92
+ :strdesc => 1,
93
+ :asc => 0,
94
+ :desc => 1,
95
+ :numasc => 2,
96
+ :numdesc => 3
97
+ }
98
+ end
99
+
100
+ end
101
+ end
102
+
@@ -0,0 +1,74 @@
1
+ #--
2
+ # Copyright (c) 2009, John Mettraux, jmettraux@gmail.com
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ #
22
+ # Made in Japan.
23
+ #++
24
+
25
+
26
+ module Rufus
27
+ module Tokyo
28
+
29
+ #
30
+ # A mixin for structures to respond to tranbegin, trancommit and tranabort
31
+ #
32
+ module Transactions
33
+
34
+ #
35
+ # Transaction in a block.
36
+ #
37
+ # table.transaction do
38
+ # table['pk0'] => { 'name' => 'Fred', 'age' => '40' }
39
+ # table['pk1'] => { 'name' => 'Brooke', 'age' => '76' }
40
+ # table.abort if weather.bad?
41
+ # end
42
+ #
43
+ # (This is a table example, a classical cabinet won't accept a hash
44
+ # as a value for its entries).
45
+ #
46
+ # If an error or an abort is trigger withing the transaction, it's rolled
47
+ # back. If the block executes successfully, it gets commited.
48
+ #
49
+ def transaction
50
+
51
+ return unless block_given?
52
+
53
+ begin
54
+ tranbegin
55
+ yield
56
+ trancommit
57
+ rescue Exception => e
58
+ tranabort
59
+ end
60
+ end
61
+
62
+ #
63
+ # Aborts the enclosing transaction
64
+ #
65
+ # See #transaction
66
+ #
67
+ def abort
68
+ raise "abort transaction !"
69
+ end
70
+ end
71
+
72
+ end
73
+ end
74
+
@@ -0,0 +1,59 @@
1
+ #--
2
+ # Copyright (c) 2009, John Mettraux, jmettraux@gmail.com
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ #
22
+ # Made in Japan.
23
+ #++
24
+
25
+
26
+ module Rufus
27
+ module Tokyo
28
+
29
+ #
30
+ # #stat and #compte_ext_opts, that's all.
31
+ #
32
+ module TyrantCommons
33
+
34
+ # Returns a hash of information about the Tokyo Tyrant database (or table)
35
+ # at the other end of the connection.
36
+ #
37
+ def stat
38
+
39
+ do_stat.split("\n").inject({}) { |r, l|
40
+ kv = l.split("\t")
41
+ r[kv.first] = kv.last
42
+ r
43
+ }
44
+ end
45
+
46
+ # Computes the :int option for the "ext" function (triggering
47
+ # tyrant embedded lua functions)
48
+ #
49
+ def compute_ext_opts (opts)
50
+
51
+ r = 0
52
+ r = r | 1 if opts[:record_locking]
53
+ r = r | 2 if opts[:global_locking]
54
+ r
55
+ end
56
+ end
57
+ end
58
+ end
59
+
@@ -0,0 +1,35 @@
1
+ #--
2
+ # Copyright (c) 2009, John Mettraux, jmettraux@gmail.com
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ #
22
+ # Made in Japan.
23
+ #++
24
+
25
+
26
+ require 'rufus/tokyo'
27
+
28
+
29
+ module Rufus::Tokyo
30
+ end
31
+
32
+ require 'rufus/tokyo/tyrant/lib'
33
+ require 'rufus/tokyo/tyrant/abstract'
34
+ require 'rufus/tokyo/tyrant/table'
35
+
@@ -0,0 +1,138 @@
1
+ #--
2
+ # Copyright (c) 2009, John Mettraux, jmettraux@gmail.com
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ #
22
+ # Made in Japan.
23
+ #++
24
+
25
+
26
+ require 'rufus/tokyo/ttcommons'
27
+
28
+
29
+ module Rufus::Tokyo
30
+
31
+ #
32
+ # Connecting to a 'classic' tyrant server remotely
33
+ #
34
+ # require 'rufus/tokyo/tyrant'
35
+ # t = Rufus::Tokyo::Tyrant.new('127.0.0.1', 44001)
36
+ # t['toto'] = 'blah blah'
37
+ # t['toto'] # => 'blah blah'
38
+ #
39
+ class Tyrant < Cabinet
40
+
41
+ include TyrantCommons
42
+
43
+ attr_reader :host, :port
44
+
45
+ # Connects to a given Tokyo Tyrant server.
46
+ #
47
+ # Note that if the port is not specified, the host parameter is expected
48
+ # to hold the path to a unix socket (not a TCP socket).
49
+ #
50
+ # (You can start a unix socket listening Tyrant with :
51
+ #
52
+ # ttserver -host /tmp/tyrant_socket -port 0 data.tch
53
+ #
54
+ # and then connect to it with rufus-tokyo via :
55
+ #
56
+ # require 'rufus/tokyo/tyrant'
57
+ # db = Rufus::Tokyo::Tyrant.new('/tmp/tyrant_socket')
58
+ # db['a'] = 'alpha'
59
+ # db.close
60
+ # )
61
+ #
62
+ # To connect to a classic TCP bound Tyrant (port 44001) :
63
+ #
64
+ # t = Rufus::Tokyo::Tyrant.new('127.0.0.1', 44001)
65
+ #
66
+ def initialize (host, port=0)
67
+
68
+ @db = lib.tcrdbnew
69
+
70
+ @host = host
71
+ @port = port
72
+
73
+ (lib.tcrdbopen(@db, host, port) == 1) ||
74
+ raise("couldn't connect to tyrant at #{host}:#{port}")
75
+
76
+ if self.stat['type'] == 'table'
77
+
78
+ self.close
79
+
80
+ raise ArgumentError.new(
81
+ "tyrant at #{host}:#{port} is a table, " +
82
+ "use Rufus::Tokyo::TyrantTable instead to access it.")
83
+ end
84
+ end
85
+
86
+ # Using the tyrant lib
87
+ #
88
+ def lib
89
+ TyrantLib
90
+ end
91
+
92
+ # isn't that a bit dangerous ? it creates a file on the server...
93
+ #
94
+ # DISABLED.
95
+ #
96
+ def copy (target_path)
97
+ #@db.copy(target_path)
98
+ raise 'not allowed to create files on the server'
99
+ end
100
+
101
+ # Calls a lua embedded function
102
+ # (http://tokyocabinet.sourceforge.net/tyrantdoc/#luaext)
103
+ #
104
+ # Options are :global_locking and :record_locking
105
+ #
106
+ # Returns the return value of the called function.
107
+ #
108
+ # Nil is returned in case of failure.
109
+ #
110
+ def ext (func_name, key, value, opts={})
111
+
112
+ lib.tcrdbext2(
113
+ @db, func_name.to_s, compute_ext_opts(opts), key.to_s, value.to_s
114
+ ) rescue nil
115
+ end
116
+
117
+ # Tyrant databases DO NOT support the 'defrag' call. Calling this method
118
+ # will raise an exception.
119
+ #
120
+ undef_method(:defrag)
121
+
122
+ protected
123
+
124
+ def do_call_misc (function, list_pointer)
125
+
126
+ lib.tcrdbmisc(@db, function, 0, list_pointer)
127
+ # opts always to 0 for now
128
+ end
129
+
130
+ # Returns the raw stat string from the Tyrant server.
131
+ #
132
+ def do_stat
133
+
134
+ lib.tcrdbstat(@db)
135
+ end
136
+ end
137
+ end
138
+
@@ -0,0 +1,149 @@
1
+ #--
2
+ # Copyright (c) 2009, John Mettraux, jmettraux@gmail.com
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ #
22
+ # Made in Japan.
23
+ #++
24
+
25
+
26
+ module Rufus::Tokyo
27
+
28
+ #
29
+ # The libtokyocabinet.so methods get bound to this module
30
+ #
31
+ module TyrantLib #:nodoc#
32
+
33
+ extend FFI::Library
34
+
35
+ #
36
+ # find Tokyo Tyrant lib
37
+
38
+ paths = Array(ENV['TOKYO_TYRANT_LIB'] || %w{
39
+ /usr/lib/libtokyotyrant.so
40
+ /opt/local/lib/libtokyotyrant.dylib
41
+ /opt/local/lib/libtokyotyrant.so
42
+ /usr/local/lib/libtokyotyrant.dylib
43
+ /usr/local/lib/libtokyotyrant.so
44
+ })
45
+
46
+ begin
47
+
48
+ ffi_lib(*paths)
49
+
50
+ rescue LoadError => le
51
+ raise(
52
+ "didn't find Tokyo Tyrant libs on your system. " +
53
+ "Please install Tokyo Tyrant (http://tokyocabinet.sf.net) " +
54
+ "(see also http://openwferu.rubyforge.org/tokyo.html)"
55
+ )
56
+ end
57
+
58
+
59
+ class << self
60
+ alias :attfunc :attach_function
61
+ end
62
+
63
+ #
64
+ # tcrdb functions
65
+
66
+ attfunc :tcrdbnew, [], :pointer
67
+
68
+ attfunc :tcrdbstat, [ :pointer ], :string
69
+
70
+ attfunc :tcrdbopen, [ :pointer, :string, :int ], :int
71
+ attfunc :abs_close, :tcrdbclose, [ :pointer ], :int
72
+
73
+ attfunc :abs_del, :tcrdbdel, [ :pointer ], :void
74
+
75
+ attfunc :abs_rnum, :tcrdbrnum, [ :pointer ], :uint64
76
+ attfunc :abs_size, :tcrdbsize, [ :pointer ], :uint64
77
+
78
+ attfunc :abs_put2, :tcrdbput2, [ :pointer, :string, :string ], :int
79
+ attfunc :abs_get2, :tcrdbget2, [ :pointer, :string ], :string
80
+ attfunc :abs_out2, :tcrdbout2, [ :pointer, :string ], :int
81
+
82
+ attfunc :abs_iterinit, :tcrdbiterinit, [ :pointer ], :int
83
+ attfunc :abs_iternext2, :tcrdbiternext2, [ :pointer ], :string
84
+
85
+ attfunc :abs_vanish, :tcrdbvanish, [ :pointer ], :int
86
+
87
+ attfunc :abs_sync, :tcrdbsync, [ :pointer ], :int
88
+ attfunc :abs_copy, :tcrdbcopy, [ :pointer, :string ], :int
89
+
90
+ attfunc :abs_fwmkeys2, :tcrdbfwmkeys2, [ :pointer, :string, :int ], :pointer
91
+ attfunc :tcrdbmisc, [ :pointer, :string, :int, :pointer ], :pointer
92
+
93
+ attfunc :tcrdbext2, [ :pointer, :string, :int, :string, :string ], :string
94
+
95
+ attfunc :addint, :tcrdbaddint, [ :pointer, :string, :int, :int ], :int
96
+ attfunc :adddouble, :tcrdbadddouble, [ :pointer, :string, :int, :double ], :double
97
+
98
+ #
99
+ # table functions
100
+
101
+ attfunc :tab_close, :tcrdbclose, [ :pointer ], :int
102
+
103
+ attfunc :tab_genuid, :tcrdbtblgenuid, [ :pointer ], :int64
104
+
105
+ attfunc :tab_get, :tcrdbtblget, [ :pointer, :string, :int ], :pointer
106
+
107
+ attfunc :tab_iterinit, :tcrdbiterinit, [ :pointer ], :int
108
+ attfunc :tab_iternext2, :tcrdbiternext2, [ :pointer ], :string
109
+
110
+ attfunc :tab_put, :tcrdbtblput, [ :pointer, :string, :int, :pointer ], :int
111
+
112
+ attfunc :tab_out, :tcrdbtblout, [ :pointer, :string, :int ], :int
113
+
114
+ attfunc :tab_ecode, :tcrdbecode, [ :pointer ], :int
115
+ attfunc :tab_errmsg, :tcrdberrmsg, [ :int ], :string
116
+
117
+ attfunc :tab_del, :tcrdbdel, [ :pointer ], :void
118
+
119
+ attfunc :tab_rnum, :tcrdbrnum, [ :pointer ], :uint64
120
+
121
+ attfunc :tab_vanish, :tcrdbvanish, [ :pointer ], :int
122
+
123
+ attfunc :tab_setindex, :tcrdbtblsetindex, [ :pointer, :string, :int ], :int
124
+
125
+ attfunc :tab_fwmkeys2, :tcrdbfwmkeys2, [ :pointer, :string, :int ], :pointer
126
+
127
+ #
128
+ # qry functions
129
+
130
+ attfunc :qry_new, :tcrdbqrynew, [ :pointer ], :pointer
131
+ attfunc :qry_del, :tcrdbqrydel, [ :pointer ], :void
132
+
133
+ attfunc :qry_addcond, :tcrdbqryaddcond, [ :pointer, :string, :int, :string ], :void
134
+ attfunc :qry_setorder, :tcrdbqrysetorder, [ :pointer, :string, :int ], :void
135
+
136
+ begin
137
+ attfunc :qry_setmax, :tcrdbqrysetmax, [ :pointer, :int ], :void
138
+ rescue FFI::NotFoundError => nfe
139
+ attfunc :qry_setlimit, :tcrdbqrysetlimit, [ :pointer, :int, :int ], :void
140
+ end
141
+
142
+ attfunc :qry_search, :tcrdbqrysearch, [ :pointer ], :pointer
143
+
144
+ begin # since TC 1.4.21
145
+ attfunc :qry_count, :tcrdbqrysearchcount, [ :pointer ], :int
146
+ rescue FFI::NotFoundError => nfe
147
+ end
148
+ end
149
+ end