Class: Rufus::Tokyo::Table
- Inherits:
-
Object
- Object
- Rufus::Tokyo::Table
- Includes:
- CabinetLibMixin, HashMethods, TokyoContainerMixin
- Defined in:
- lib/rufus/tokyo/cabinet/table.rb
Overview
A ‘table’ a table database.
http://alpha.mixi.co.jp/blog/?p=290
http://tokyocabinet.sourceforge.net/spex-en.html#tctdbapi
A short example :
require 'rubygems'
require 'rufus/tokyo/cabinet/table'
t = Rufus::Tokyo::Table.new('table.tdb', :create, :write)
# '.tdb' suffix is a must
t['pk0'] = { 'name' => 'alfred', 'age' => '22' }
t['pk1'] = { 'name' => 'bob', 'age' => '18' }
t['pk2'] = { 'name' => 'charly', 'age' => '45' }
t['pk3'] = { 'name' => 'doug', 'age' => '77' }
t['pk4'] = { 'name' => 'ephrem', 'age' => '32' }
p t.query { |q|
q.add_condition 'age', :numge, '32'
q.order_by 'age'
q.limit 2
}
# => [ {"name"=>"ephrem", :pk=>"pk4", "age"=>"32"},
# {"name"=>"charly", :pk=>"pk2", "age"=>"45"} ]
t.close
Constant Summary
Constants included from TokyoContainerMixin
Rufus::Tokyo::TokyoContainerMixin::OCREAT, Rufus::Tokyo::TokyoContainerMixin::OLCKNB, Rufus::Tokyo::TokyoContainerMixin::ONOLCK, Rufus::Tokyo::TokyoContainerMixin::OREADER, Rufus::Tokyo::TokyoContainerMixin::OTRUNC, Rufus::Tokyo::TokyoContainerMixin::OTSYNC, Rufus::Tokyo::TokyoContainerMixin::OWRITER
Instance Attribute Summary
Attributes included from HashMethods
Instance Method Summary collapse
-
#[]=(pk, h_or_a) ⇒ Object
Inserts a record in the table db.
-
#clear ⇒ Object
Removes all records in this table database.
-
#close ⇒ Object
Closes the table (and frees the datastructure allocated for it), returns true in case of success.
-
#delete(k) ⇒ Object
Removes an entry in the table.
-
#do_query(&block) ⇒ Object
Prepares and runs a query, returns a ResultSet instance (takes care of freeing the query structure).
-
#generate_unique_id ⇒ Object
(also: #genuid)
Generates a unique id (in the context of this Table instance).
-
#initialize(*args) ⇒ Table
constructor
Creates a Table instance (creates or opens it depending on the args).
-
#keys ⇒ Object
Returns an array of all the primary keys in the table.
-
#pointer ⇒ Object
Returns the actual pointer to the Tokyo Cabinet table.
-
#prepare_query(&block) ⇒ Object
Prepares a query instance (block is optional).
-
#query(&block) ⇒ Object
Prepares and runs a query, returns an array of hashes (all Ruby) (takes care of freeing the query and the result set structures).
-
#size ⇒ Object
Returns the number of records in this table db.
-
#tabbed_put(pk, *args) ⇒ Object
Accepts a variable number of arguments, at least two.
Methods included from HashMethods
#[], #default, #default=, #each, #merge, #merge!, #to_a, #to_h, #values
Methods included from TokyoContainerMixin
#compute_open_mode, #params_to_h
Constructor Details
#initialize(*args) ⇒ Table
Creates a Table instance (creates or opens it depending on the args)
For example,
t = Rufus::Tokyo::Table.new('table.tdb', :create, :write)
# '.tdb' suffix is a must
will create the table.tdb (or simply open it if already present) and make sure we have write access to it. Note that the suffix (.tdc) is relevant to Tokyo Cabinet, using another will result in a Tokyo Cabinet error.
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rufus/tokyo/cabinet/table.rb', line 85 def initialize (*args) path = args.first # car params = args[1..-1] # cdr mode = compute_open_mode(params) @db = self.lib.tctdbnew (lib.tctdbopen(@db, path, compute_open_mode(params)) == 1 ) || raise_error end |
Instance Method Details
#[]=(pk, h_or_a) ⇒ Object
Inserts a record in the table db
table['pk0'] = [ 'name', 'fred', 'age', '45' ]
table['pk1'] = { 'name' => 'jeff', 'age' => '46' }
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/rufus/tokyo/cabinet/table.rb', line 146 def []= (pk, h_or_a) return tabbed_put(pk, *h_or_a) if h_or_a.is_a?(Array) pklen = lib.strlen(pk) m = Rufus::Tokyo::Map.from_h(h_or_a) r = lib.tctdbput(@db, pk, pklen, m.pointer) m.free (r == 1) || raise_error h_or_a end |
#clear ⇒ Object
Removes all records in this table database
179 180 181 |
# File 'lib/rufus/tokyo/cabinet/table.rb', line 179 def clear (lib.tctdbvanish(@db) == 1) || raise_error end |
#close ⇒ Object
Closes the table (and frees the datastructure allocated for it), returns true in case of success.
101 102 103 104 105 |
# File 'lib/rufus/tokyo/cabinet/table.rb', line 101 def close result = lib.tctdbclose(@db) lib.tctdbdel(@db) (result == 1) end |
#delete(k) ⇒ Object
Removes an entry in the table
(might raise an error if the delete itself failed, but returns nil if there was no entry for the given key)
169 170 171 172 173 174 |
# File 'lib/rufus/tokyo/cabinet/table.rb', line 169 def delete (k) v = self[k] return nil unless v (lib.tctdbout2(@db, k) == 1) || raise_error v end |
#do_query(&block) ⇒ Object
Prepares and runs a query, returns a ResultSet instance (takes care of freeing the query structure)
225 226 227 228 229 230 |
# File 'lib/rufus/tokyo/cabinet/table.rb', line 225 def do_query (&block) q = prepare_query(&block) rs = q.run q.free rs end |
#generate_unique_id ⇒ Object Also known as: genuid
Generates a unique id (in the context of this Table instance)
110 111 112 |
# File 'lib/rufus/tokyo/cabinet/table.rb', line 110 def generate_unique_id lib.tctdbgenuid(@db) end |
#keys ⇒ Object
Returns an array of all the primary keys in the table
198 199 200 201 202 203 |
# File 'lib/rufus/tokyo/cabinet/table.rb', line 198 def keys a = [] lib.tctdbiterinit(@db) while (k = (lib.tctdbiternext2(@db) rescue nil)); a << k; end a end |
#pointer ⇒ Object
Returns the actual pointer to the Tokyo Cabinet table
246 247 248 |
# File 'lib/rufus/tokyo/cabinet/table.rb', line 246 def pointer @db end |
#prepare_query(&block) ⇒ Object
Prepares a query instance (block is optional)
215 216 217 218 219 |
# File 'lib/rufus/tokyo/cabinet/table.rb', line 215 def prepare_query (&block) q = TableQuery.new(self) block.call(q) if block q end |
#query(&block) ⇒ Object
Prepares and runs a query, returns an array of hashes (all Ruby) (takes care of freeing the query and the result set structures)
236 237 238 239 240 241 |
# File 'lib/rufus/tokyo/cabinet/table.rb', line 236 def query (&block) rs = do_query(&block) a = rs.to_a rs.free a end |
#size ⇒ Object
Returns the number of records in this table db
208 209 210 |
# File 'lib/rufus/tokyo/cabinet/table.rb', line 208 def size lib.tctdbrnum(@db) end |
#tabbed_put(pk, *args) ⇒ Object
Accepts a variable number of arguments, at least two. First one is the primary key of the record, the others are the columns.
One can also directly write
table['one'] = [ 'name', 'toto', 'age', '33' ]
table['two'] = [ 'name', 'fred', 'age', '45' ]
instead of
table.tabbed_put('one', 'name', 'toto', 'age', '33')
table.tabbed_put('two', 'name', 'fred', 'age', '45')
beware : inserting an array uses a tab separator…
131 132 133 134 135 136 137 138 |
# File 'lib/rufus/tokyo/cabinet/table.rb', line 131 def tabbed_put (pk, *args) cols = args.collect { |e| e.to_s }.join("\t") (lib.tctdbput3(@db, pk, cols) == 1) || raise_error args end |