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,159 @@
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 'tokyocabinet'
27
+
28
+ require 'rufus/edo/error'
29
+ require 'rufus/edo/tabcore'
30
+ require 'rufus/tokyo/config'
31
+
32
+
33
+ module Rufus::Edo
34
+
35
+ #
36
+ # Rufus::Edo::Table wraps Hirabayashi-san's Ruby bindings for Tokyo Cabinet
37
+ # tables.
38
+ #
39
+ # This class has the exact same methods as Rufus::Tokyo::Table. It's faster
40
+ # though. The advantage of Rufus::Tokyo::Table lies in that in runs on
41
+ # Ruby 1.8, 1.9 and JRuby.
42
+ #
43
+ # You need to have Hirabayashi-san's binding installed to use this
44
+ # Rufus::Edo::Table :
45
+ #
46
+ # http://github.com/jmettraux/rufus-tokyo/tree/master/lib/rufus/edo
47
+ #
48
+ # Example usage :
49
+ #
50
+ # require 'rufus/edo'
51
+ # db = Rufus::Edo::Table.new('data.tct')
52
+ # db['customer1'] = { 'name' => 'Taira no Kyomori', 'age' => '55' }
53
+ # # ...
54
+ # db.close
55
+ #
56
+ class Table
57
+
58
+ include Rufus::Tokyo::CabinetConfig
59
+ include Rufus::Edo::TableCore
60
+
61
+ # Initializes and open a table.
62
+ #
63
+ # db = Rufus::Edo::Table.new('data.tct')
64
+ # # or
65
+ # db = Rufus::Edo::Table.new('data', :type => :table)
66
+ # # or
67
+ # db = Rufus::Edo::Table.new('data')
68
+ #
69
+ # == parameters
70
+ #
71
+ # There are two ways to pass parameters at the opening of a db :
72
+ #
73
+ # db = Rufus::Edo::Table.new('data.tct#opts=ld#mode=w') # or
74
+ # db = Rufus::Edo::Table.new('data.tct', :opts => 'ld', :mode => 'w')
75
+ #
76
+ # === mode
77
+ #
78
+ # * :mode a set of chars ('r'ead, 'w'rite, 'c'reate, 't'runcate,
79
+ # 'e' non locking, 'f' non blocking lock), default is 'wc'
80
+ #
81
+ # === other parameters
82
+ #
83
+ # * :opts a set of chars ('l'arge, 'd'eflate, 'b'zip2, 't'cbs)
84
+ # (usually empty or something like 'ld' or 'lb')
85
+ #
86
+ # * :bnum number of elements of the bucket array
87
+ # * :apow size of record alignment by power of 2 (defaults to 4)
88
+ # * :fpow maximum number of elements of the free block pool by
89
+ # power of 2 (defaults to 10)
90
+ #
91
+ # * :rcnum specifies the maximum number of records to be cached.
92
+ # If it is not more than 0, the record cache is disabled.
93
+ # It is disabled by default.
94
+ # * :lcnum specifies the maximum number of leaf nodes to be cached.
95
+ # If it is not more than 0, the default value is specified.
96
+ # The default value is 2048.
97
+ # * :ncnum specifies the maximum number of non-leaf nodes to be
98
+ # cached. If it is not more than 0, the default value is
99
+ # specified. The default value is 512.
100
+ #
101
+ # * :dfunit unit step number. If it is not more than 0,
102
+ # the auto defragmentation is disabled. (Since TC 1.4.21)
103
+ #
104
+ # = NOTE :
105
+ #
106
+ # On reopening a file, Cabinet will tend to stick to the parameters as
107
+ # set when the file was opened. To change that, have a look at the
108
+ # man pages of the various command line tools coming with Tokyo Cabinet.
109
+ #
110
+ def initialize (path, params={})
111
+
112
+ conf = determine_conf(path, params, :table)
113
+
114
+ @db = TokyoCabinet::TDB.new
115
+
116
+ #
117
+ # tune
118
+
119
+ @db.tune(conf[:bnum], conf[:apow], conf[:fpow], conf[:opts])
120
+
121
+ #
122
+ # set cache
123
+
124
+ @db.setcache(conf[:rcnum], conf[:lcnum], conf[:ncnum])
125
+
126
+ #
127
+ # set xmsiz
128
+
129
+ @db.setxmsiz(conf[:xmsiz])
130
+
131
+ #
132
+ # set dfunit (TC > 1.4.21)
133
+
134
+ @db.setdfunit(conf[:dfunit]) if @db.respond_to?(:setdfunit)
135
+
136
+ #
137
+ # open
138
+
139
+ @path = conf[:path]
140
+
141
+ @db.open(@path, conf[:mode]) || raise_error
142
+ end
143
+
144
+ # Returns the path to this table.
145
+ #
146
+ def path
147
+
148
+ @path
149
+ end
150
+
151
+ protected
152
+
153
+ def table_query_class #:nodoc#
154
+
155
+ TokyoCabinet::TDBQRY
156
+ end
157
+ end
158
+ end
159
+
@@ -0,0 +1,36 @@
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 Edo
28
+
29
+ #
30
+ # A common error class
31
+ #
32
+ class EdoError < RuntimeError; end
33
+
34
+ end
35
+ end
36
+
@@ -0,0 +1,4 @@
1
+
2
+ require 'rufus/edo/ntyrant/abstract'
3
+ require 'rufus/edo/ntyrant/table'
4
+
@@ -0,0 +1,170 @@
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 'tokyotyrant' # gem install careo-tokyotyrant
27
+
28
+ require 'rufus/edo/error'
29
+ require 'rufus/edo/cabcore'
30
+ require 'rufus/tokyo/ttcommons'
31
+
32
+
33
+ module Rufus::Edo
34
+
35
+ #
36
+ # Connecting to a 'classic' Tokyo Tyrant server remotely
37
+ #
38
+ # require 'rufus/edo/ntyrant'
39
+ # t = Rufus::Edo::NetTyrant.new('127.0.0.1', 44001)
40
+ # t['toto'] = 'blah blah'
41
+ # t['toto'] # => 'blah blah'
42
+ #
43
+ class NetTyrant
44
+
45
+ include Rufus::Edo::CabinetCore
46
+ include Rufus::Tokyo::TyrantCommons
47
+
48
+ attr_reader :host, :port
49
+
50
+ # Connects to a given Tokyo Tyrant server.
51
+ #
52
+ # Note that if the port is not specified, the host parameter is expected
53
+ # to hold the path to a unix socket (not a TCP socket).
54
+ #
55
+ # (You can start a unix socket listening Tyrant with :
56
+ #
57
+ # ttserver -host /tmp/tyrant_socket -port 0 data.tch
58
+ #
59
+ # and then connect to it with rufus-tokyo via :
60
+ #
61
+ # require 'rufus/edo/ntyrant'
62
+ # db = Rufus::Edo::NetTyrant.new('/tmp/tyrant_socket')
63
+ # db['a'] = 'alpha'
64
+ # db.close
65
+ # )
66
+ #
67
+ # To connect to a classic TCP bound Tyrant (port 44001) :
68
+ #
69
+ # t = Rufus::Edo::NetTyrant.new('127.0.0.1', 44001)
70
+ #
71
+ def initialize (host, port=0)
72
+
73
+ @host = host
74
+ @port = port
75
+
76
+ @db = TokyoTyrant::RDB.new
77
+ @db.open(host, port) || raise_error
78
+
79
+ if self.stat['type'] == 'table'
80
+
81
+ @db.close
82
+
83
+ raise ArgumentError.new(
84
+ "tyrant at #{host}:#{port} is a table, " +
85
+ "use Rufus::Edo::NetTyrantTable instead to access it.")
86
+ end
87
+ end
88
+
89
+ # Returns the 'weight' of the db (in bytes)
90
+ #
91
+ def weight
92
+
93
+ self.stat['size']
94
+ end
95
+
96
+ # isn't that a bit dangerous ? it creates a file on the server...
97
+ #
98
+ # DISABLED.
99
+ #
100
+ def copy (target_path)
101
+
102
+ #@db.copy(target_path)
103
+ raise 'not allowed to create files on the server'
104
+ end
105
+
106
+ # Copies the current cabinet to a new file.
107
+ #
108
+ # Does it by copying each entry afresh to the target file. Spares some
109
+ # space, hence the 'compact' label...
110
+ #
111
+ def compact_copy (target_path)
112
+
113
+ raise NotImplementedError.new('not creating files locally')
114
+ end
115
+
116
+ # Deletes all the entries whose keys begin with the given prefix
117
+ #
118
+ def delete_keys_with_prefix (prefix)
119
+
120
+ @db.misc('outlist', @db.fwmkeys(prefix, -1)) # -1 for no limits
121
+ nil
122
+ end
123
+
124
+ # Given a list of keys, returns a Hash { key => value } of the
125
+ # matching entries (in one sweep).
126
+ #
127
+ def lget (keys)
128
+
129
+ Hash[*@db.misc('getlist', keys)]
130
+ end
131
+
132
+ # Merges the given hash into this Tyrant and returns self.
133
+ #
134
+ def merge! (hash)
135
+
136
+ @db.misc('putlist', hash.inject([]) { |l, (k, v)| l << k; l << v; l })
137
+ self
138
+ end
139
+ alias :lput :merge!
140
+
141
+ # Given a list of keys, deletes all the matching entries (in one sweep).
142
+ #
143
+ def ldelete (keys)
144
+
145
+ @db.misc('outlist', keys)
146
+ nil
147
+ end
148
+
149
+ # Calls a lua embedded function
150
+ # (http://tokyocabinet.sourceforge.net/tyrantdoc/#luaext)
151
+ #
152
+ # Options are :global_locking and :record_locking
153
+ #
154
+ # Returns the return value of the called function.
155
+ #
156
+ # Nil is returned in case of failure.
157
+ #
158
+ def ext (func_name, key, value, opts={})
159
+
160
+ @db.ext(func_name.to_s, key.to_s, value.to_s, compute_ext_opts(opts))
161
+ end
162
+
163
+ protected
164
+
165
+ def do_stat #:nodoc#
166
+ @db.stat
167
+ end
168
+ end
169
+ end
170
+
@@ -0,0 +1,142 @@
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 'tokyotyrant'
27
+
28
+ require 'rufus/edo/error'
29
+ require 'rufus/edo/tabcore'
30
+ require 'rufus/tokyo/ttcommons'
31
+
32
+
33
+ module Rufus::Edo
34
+
35
+ #
36
+ # A Tokyo Cabinet table, but remote...
37
+ #
38
+ # require 'rufus/edo/ntyrant'
39
+ # t = Rufus::Edo::NetTyrant.new('127.0.0.1', 44001)
40
+ # t['toto'] = { 'name' => 'toto the first', 'age' => '34' }
41
+ # t['toto']
42
+ # # => { 'name' => 'toto the first', 'age' => '34' }
43
+ #
44
+ # NOTE : The advantage of this class is that it leverages the TokyoTyrant.rb
45
+ # provided by Hirabayashi-san. It's pure Ruby, it's slow but works everywhere
46
+ # without the need for Tokyo Cabinet and Tyrant C libraries.
47
+ #
48
+ class NetTyrantTable
49
+
50
+ include Rufus::Edo::TableCore
51
+ include Rufus::Tokyo::TyrantCommons
52
+
53
+ attr_reader :host, :port
54
+
55
+ #
56
+ # Connects to the Tyrant table listening at the given host and port.
57
+ #
58
+ # You start such a Tyrant with :
59
+ #
60
+ # ttserver -port 44502 data.tct
61
+ #
62
+ # and then :
63
+ #
64
+ # require 'rufus/edo/ntyrant'
65
+ # t = Rufus::Edo::NetTyrantTable.new('127.0.0.1', 44502)
66
+ # t['client0'] = { 'name' => 'Heike no Kyomori', 'country' => 'jp' }
67
+ # t.close
68
+ #
69
+ #
70
+ # You can start a Tokyo Tyrant and make it listen to a unix socket (not TCP)
71
+ # with :
72
+ #
73
+ # ttserver -host /tmp/table_socket -port 0 data.tct
74
+ #
75
+ # then :
76
+ #
77
+ # require 'rufus/edo/ntyrant'
78
+ # t = Rufus::Edo::NetTyrantTable.new('/tmp/table_socket')
79
+ # t['client0'] = { 'name' => 'Theodore Roosevelt', 'country' => 'usa' }
80
+ # t.close
81
+ #
82
+ def initialize (host, port=0)
83
+
84
+ @host = host
85
+ @port = port
86
+
87
+ @db = TokyoTyrant::RDBTBL.new
88
+ @db.open(host, port) || raise_error
89
+
90
+ if self.stat['type'] != 'table'
91
+
92
+ @db.close
93
+
94
+ raise ArgumentError.new(
95
+ "tyrant at #{host}:#{port} is not a table, " +
96
+ "use Rufus::Edo::NetTyrant instead to access it.")
97
+ end
98
+ end
99
+
100
+ # This implementation of lget relies on the mget provided by
101
+ # Hirabayashi-san in his native ruby library (all the other rufus
102
+ # tables do multiple gets for now)
103
+ #
104
+ def lget (keys)
105
+
106
+ h = keys.inject({}) { |h, k| h[k] = nil; h }
107
+ r = @db.mget(h)
108
+
109
+ raise 'lget failure' if r == -1
110
+ h
111
+ end
112
+
113
+ def transaction #:nodoc#
114
+ raise NoMethodError.new("NetTyrant : transactions not supported")
115
+ end
116
+ def abort #:nodoc#
117
+ raise NoMethodError.new("NetTyrant : transactions not supported")
118
+ end
119
+ def tranbegin #:nodoc#
120
+ raise NoMethodError.new("NetTyrant : transactions not supported")
121
+ end
122
+ def trancommit #:nodoc#
123
+ raise NoMethodError.new("NetTyrant : transactions not supported")
124
+ end
125
+ def tranabort #:nodoc#
126
+ raise NoMethodError.new("NetTyrant : transactions not supported")
127
+ end
128
+
129
+ protected
130
+
131
+ def table_query_class #:nodoc#
132
+
133
+ TokyoTyrant::RDBQRY
134
+ end
135
+
136
+ def do_stat #:nodoc#
137
+
138
+ @db.stat
139
+ end
140
+ end
141
+ end
142
+