rufus-tokyo 0.1.7 → 0.1.8

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