cabyrant¶
cabyrant adds a few methods to the ruby binding of TokyoCabinet and TokyoTyrant targeting to interact easier and faster with Cabinet and Tyrant databases.
Dependencies¶
- The official ruby binding of TokyoCabinet
Download the latest version: tokyocabinet-ruby-1.29.tar.gz
and install as described in the relative API documentation
In brief:
$ fetch http://1978th.net/tokyocabinet/rubypkg/tokyocabinet-ruby-1.29.tar.gz $ tar -zxvf tokyocabinet-ruby-1.29.tar.gz $ cd tokyocabinet-ruby-1.29 $ ruby extconf.rb $ make $ su #root permission required to install % make install
You can also see this video , although the careo/ruby-tokyocabinet git repository is outdated.
- The official ruby binding of TokyoTyrant
Download the latest version: tokyotyrant-ruby-1.13.tar.gz
and install as described in the relative API documentation
In brief:
$ fetch http://tokyocabinet.sourceforge.net/tyrantrubypkg/tokyotyrant-ruby-1.13.tar.gz $ tar -zxvf tokyotyrant-ruby-1.13.tar.gz $ cd tokyotyrant-ruby-1.13 $ su #root permission required to install % ruby install.rb
Installation¶
To install just clone the git repository to your preferred location and create a file named cabyrant.rb on your ruby's site_ruby directory that requires the module.
In my case cabyrant.rb is located at /usr/local/lib/ruby/site_ruby/1.8/cabyrant.rb contains the following single line:
require '/home/vtypal/git/cabyrant/lib/cabyrant'
Usage¶
At the moment 'cabyrant' supports only table-like TokyoCabinet and TokyoTyrant databases. Hash and B+Tree implementations with cabyrant is in my todo list.
Some examples:
require 'cabyrant' t=Cabyrant.connect "table:capital_cities.tct"
Insert a series of hash records at "capital_cities.tct".
t.mput({"continent"=>"Europe", "city"=>"Athens", "population"=>3686371, "country"=>"Greece"},
{"continent"=>"Europe", "city"=>"Rome", "population"=>3700000, "country"=>"Italy"},
{"continent"=>"Europe", "city"=>"Paris", "population"=>2203817, "country"=>"France"},
{"continent"=>"Europe", "city"=>"Madrid", "population"=>3213271, "country"=>"Spain"},
{"continent"=>"Europe", "city"=>"Budapest", "population"=>1702297, "country"=>"Hungary"})
all : returns an array with all the hash records. Iterate as you like over it
t.all.each { |hi| hi.each_key do |i| ; print hi[i], "\t"; end ; print "\n" }
Europe Athens Greece 3686371
Europe Rome Italy 3700000
Europe Paris France 2203817
Europe Madrid Spain 3213271
Europe Budapest Hungary 1702297
query : returns a TDBQRY Cabinet or RDBQRY Tyrant object
q=Cabyrant.query(t)
qry :
Let's suppose that we want to start a query for all capital cities with a population > 2500000
q.qry(:type=>"n", :col=>"population", :op=>">", :xpr=>2500000) #q.qry is a TokyoCabinet::TDBQRY object
q.search returns the pkeys of the saved hash records that matches the specifics of the query.
Note that "search" is an instance method of the original API. You can use other instance methods of TokyoCabinet::TDBQRY (for example setlimit(max,skip) to paginate, ... )
res : returns an array with the corresponding hash records that matches the specifics of the previous query (acts like the previous "all" method, but for the query). res accepts as argument an array of hash keys (note that q.search is exaclty ["1","2","4"]). Again iterate as you like over it.
t.res(q.search).each { |hi| hi.each_key do |i| ; print hi[i], "\t"; end ; print "\n" }
Europe Athens Greece 3686371
Europe Rome Italy 3700000
Europe Madrid Spain 3213271
ord : Order in ascending/descending mode by a specific column. If you apply it after a query, the "ord" method apply in cascade. Otherwise creates (and returns) a fresh TokyoCabinet::TDBQRY object. (for opts specifics please see the source)
q.ord(:type => "n", :col=>"population", :scd=>"v")
q.search #=> ["2","1","4"]
t.res(q.search).each { |hi| hi.each_key do |i| ; print hi[i], "\t"; end ; print "\n" }
Europe Madrid Spain 3213271
Europe Athens Greece 3686371
Europe Rome Italy 3700000
To update, delete and merge records i use the original offered by the ruby-tokyocabinet API. For example to delete the specific records after a query:
q.search.each {|i| t.out(i) } #delete specific records after a query
Don't forget to close your database
t.close
License¶
Copyright (c) 2009 Vaggelis Typaldos <vtypal@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.