tokyo-cabinet snippets

How to install Tokyo Tyrant, Tokyo Cabinet and rufus-tokyo

Tagged ruby, tokyo-cabinet, key-value, database, tokyo-tyrant  Languages ruby
git clone git://github.com/etrepum/tokyo-cabinet.git
git clone git://github.com/etrepum/tokyo-tyrant.git


cd tokyo-cabinet/
./configure
make
sudo make install

cd ..

cd tokyo-tyrant/
./configure
make
sudo make install

ttserver -port 45001 data.tch

sudo gem install rufus-tokyo

cat <<EOF >>test.rb

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


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

db = Rufus::Tokyo::Tyrant.new('localhost', 45001)
 
 seconds = Benchmark.realtime do
  for i in 1..1000000
    db['nada' + i.to_s] = 'surf' + i.to_s
  end
end

puts seconds
db.close

EOF

ruby test.rb

Gotchas

One gotcha I learned was that you should only use strings not symbols. If you use symbols you'll get this weird error:

Invalid String value

Another gotcha is that Tokyo Cabinet will lock a database. If you try to open the same database from another process it will wait for the other process to close the database.

Debian

On Debian you need to install bzlib before installing tokyo-cabinet:

apt-get install libbz2-dev

Stringify a Ruby Hash

Tagged hash, tokyo-cabinet, stringify, ruby  Languages ruby
class Hash
  def stringify
    inject({}) do |options, (key, value)|
      options[key.to_s] = value.to_s
      options
    end
  end

  def stringify!
    each do |key, value|
      delete(key)
      store(key.to_s, value.to_s)
    end
  end
end