Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 306 lines (180 sloc) 6.551 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

= rufus-tokyo

ruby-ffi based interface to Tokyo Cabinet and Tokyo Tyrant.

The 'abstract' and the 'table' API are covered for now.


== installation

  sudo gem install rufus-tokyo

  (see after 'usage' for how to install Tokyo Cabinet (and Tyrant) if required)


== Rufus::Edo

Note : Rufus::Tokyo focuses on leveraging Hirabayashi-san's C libraries via ruby-ffi, but the gem rufus-tokyo also contains Rufus::Edo which wraps the Tokyo Cabinet/Tyrant author's [native] C bindings :

  http://github.com/jmettraux/rufus-tokyo/tree/master/lib/rufus/edo


== usage

hereafter TC references Tokyo Cabinet, while TT references Tokyo Tyrant.

the rdoc is at http://rufus.rubyforge.org/rufus-tokyo/


== version [mis]match

this version of rufus-tokyo works with

  ruby 1.8.6
  ruby 1.9.1p0
  jruby 1.1.6
  jruby 1.2.0

  TC 1.4.9 and TT 1.1.16
  TC 1.4.10 and TT 1.1.17 (adds the offset arg to query.limit)
  TC 1.4.11 and TT 1.1.18

  tc-ruby 1.20 and tt-ruby 1.3 (Rufus::Edo)
  tc-ruby 1.21 and tt-ruby 1.4 (Rufus::Edo)


=== TC Abstract API

http://tokyocabinet.sourceforge.net/spex-en.html#tcadbapi

to create a hash (file named 'data.tch')

  require 'rubygems'
  require 'rufus/tokyo'

  db = Rufus::Tokyo::Cabinet.new('data.tch')

  db['nada'] = 'surf'

  p db['nada'] # => 'surf'
  p db['lost'] # => nil

  5000.times { |i| db[i.to_s] = "x" }

  p db.inject { |r, (k, v)| k } # => 4999

  db.close


=== TC Table API

http://tokyocabinet.sourceforge.net/spex-en.html#tctdbapi

  require 'rubygems'
  require 'rufus/tokyo'

  t = Rufus::Tokyo::Table.new('table.tct')

  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'
  }
    # => [ {"name"=>"ephrem", :pk=>"pk4", "age"=>"32"},
    # {"name"=>"charly", :pk=>"pk2", "age"=>"45"} ]

  t.close

Note that the Tokyo Cabinet Table API does support transactions :

  p t.size
    # => 0

  t.transaction do
    t['pk0'] = { 'name' => 'alfred', 'age' => '22' }
    t['pk1'] = { 'name' => 'bob', 'age' => '18' }
    t.abort
  end

  p t.size
    # => 0


=== TT remote db

http://tokyocabinet.sourceforge.net/tyrantdoc/

to start a ttserver (backed by a hash), on the command line

  ttserver -port 45001 data.tch


then, in Ruby :

  require 'rubygems'
  require 'rufus/tokyo/tyrant'

  db = Rufus::Tokyo::Tyrant.new('localhost', 45001)

  db['nada'] = 'surf'

  p db['nada'] # => 'surf'
  p db['lost'] # => nil

  db.close


Rufus::Tokyo::Tyrant instances have a #stat method :

  puts db.stat.inject('') { |s, (k, v)| s << "#{k} => #{v}\n" }
    # =>
    # pid => 7566
    # loadavg => 0.398438
    # size => 528736
    # rnum => 0
    # time => 1234764065.305923
    # sid => 898521513
    # type => hash
    # bigend => 0
    # ru_sys => 3.398698
    # version => 1.1.15
    # ru_user => 2.155215
    # ru_real => 3218.451152
    # fd => 7


Note that it's also OK to make a Tokyo Tyrant server listen on a unix socket :

  ttserver -host /tmp/ttsocket -port 0 data.tch

and then :

  require 'rubygems'
  require 'rufus/tokyo/tyrant'
  db = Rufus::Tokyo::Tyrant.new('/tmp/ttsocket')
  db['a'] = 'alpha'
  db.close


=== TT remote table

to start a ttserver (backed by a table), on the command line :

  ttserver -port 45002 data.tct


then, in Ruby, much like a local table :

  require 'rubygems'
  require 'rufus/tokyo/tyrant'

  t = Rufus::Tokyo::TyrantTable.new('localhost', 45002)

  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'
  }
    # => [ {"name"=>"ephrem", :pk=>"pk4", "age"=>"32"},
    # {"name"=>"charly", :pk=>"pk2", "age"=>"45"} ]

  t.close


Rufus::Tokyo::TyrantTable instances have a #stat method :

  puts t.stat.inject('') { |s, (k, v)| s << "#{k} => #{v}\n" }
    # =>
    # pid => 7569
    # loadavg => 0.295410
    # size => 935792
    # rnum => 0
    # time => 1234764228.942014
    # sid => 1027604232
    # type => table
    # bigend => 0
    # ru_sys => 5.966750
    # version => 1.1.15
    # ru_user => 2.601947
    # ru_real => 3382.084479
    # fd => 10


Note that it's also OK to make a Tokyo Tyrant server listen on a unix socket :

  ttserver -host /tmp/tttsocket -port 0 data.tct

and then :

  require 'rubygems'
  require 'rufus/tokyo/tyrant'
  t = Rufus::Tokyo::TyrantTable.new('/tmp/tttsocket')
  t['customer0'] = { 'name' => 'Heike no Kyomori', 'age' => '75' }
  t.close


== rdoc

more in the rdoc

  http://rufus.rubyforge.org/rufus-tokyo/
  http://rufus.rubyforge.org/rufus-tokyo/classes/Rufus/Tokyo/Cabinet.html
  http://rufus.rubyforge.org/rufus-tokyo/classes/Rufus/Tokyo/Table.html
  http://rufus.rubyforge.org/rufus-tokyo/classes/Rufus/Tokyo/Tyrant.html
  http://rufus.rubyforge.org/rufus-tokyo/classes/Rufus/Tokyo/TyrantTable.html

don't hesitate to "man ttserver" on the command line.

or directly in the source

  http://github.com/jmettraux/rufus-tokyo/blob/master/lib/rufus/tokyo/cabinet/abstract.rb
  http://github.com/jmettraux/rufus-tokyo/blob/master/lib/rufus/tokyo/cabinet/table.rb
  http://github.com/jmettraux/rufus-tokyo/blob/master/lib/rufus/tokyo/tyrant/abstract.rb
  http://github.com/jmettraux/rufus-tokyo/blob/master/lib/rufus/tokyo/tyrant/table.rb


== Tokyo Cabinet / Tyrant install

a compilation of notes is available at :

  http://openwferu.rubyforge.org/tokyo.html


== dependencies

the ruby gem 'ffi'


== mailing list

On the rufus-ruby list :

  http://groups.google.com/group/rufus-ruby


== issue tracker

  http://rubyforge.org/tracker/?atid=18584&group_id=4812&func=browse


== irc

irc.freenode.net #ruote


== source

http://github.com/jmettraux/rufus-tokyo

  git clone git://github.com/jmettraux/rufus-tokyo.git


== credits

many thanks to the author of Tokyo Cabinet, Mikio Hirabayashi, and to the authors of ruby-ffi

  http://tokyocabinet.sourceforge.net
  http://kenai.com/projects/ruby-ffi


== authors

John Mettraux, jmettraux@gmail.com, http://jmettraux.wordpress.com
Zev Blut, http://www.iknow.co.jp/users/zev


== the rest of Rufus

http://rufus.rubyforge.org


== license

MIT

Something went wrong with that request. Please try again.