-
10
JAN
2010Tokyo Cabinet's Key-Value Database Types
We've taken a good look at Tokyo Cabinet's Hash Database, but there's a lot more to the library than just that. Tokyo Cabinet supports three other kinds of databases. In addition, each database type accepts various tuning parameters that can be used to change its behavior. Each database type and setting involves different tradeoffs so you really have a lot of options for turning Tokyo Cabinet into exactly what you need. Let's look into some of those options now.
The B+Tree Database
Tokyo Cabinet's B+Tree Database is a little slower than the Hash Database we looked at before. That's its downside. However, giving up a little speed gains you several extra features that may just allow you to work smarter instead of faster.
The B+Tree Database is a more advanced form of the Hash Database. What that means is that all of the stuff I showed you in the last article still applies. You can set, read, and remove values by keys, iteration is supported, and you still have access to the neat options like adding to counters. With a B+Tree Database you get all of that and more.
-
1
JAN
2010Tokyo Cabinet as a Key-Value Store
Like most key-value stores, Tokyo Cabinet has a very
Hash
-like interface from Ruby (assuming you use Oklahoma Mixer). You can almost think of a Tokyo Cabinet database as aHash
that just happens to be stored in a file instead of memory. The advantage of that is that your data doesn't have to fit into memory. Luckily, you don't have to pay a big speed penalty to get this disk-backed storage. Tokyo Cabinet is pretty darn fast.Getting and Setting Keys
Let's have a look at the normal
Hash
-like methods as well as the file storage aspect:#!/usr/bin/env ruby -KU require "oklahoma_mixer" OklahomaMixer.open("data.tch") do |db| if db.size.zero? puts "Loading the database. Rerun to read back the data." db[:one] = 1 db[:two] = 2 db.update(:three => 3, :four => 4) db["users:1"] = "James" db["users:2"] = "Ruby" else puts "Reading data." %w[ db[:one] db["users:2"] - db.keys db.keys(:prefix\ =>\ "users:") db.keys(:limit\ =>\ 2) db.values - db.values_at(:one,\ :two) ].each do |command| puts(command == "-" ? "" : "#{command} = %p" % [eval(command)]) end end end
-
14
SEP
2009Using Key-Value Stores From Ruby
I've been playing with a few different key-value stores recently. My choices are pretty popular and you can find documentation for them. However, it can still be a bit of work to relate everything to Ruby specific usage, which is what I care about. Given that, here are my notes on the systems I've used.
Redis
- Setting up the Redis Server
- Using Redis as a Key-Value Store
- Lists and Sets in Redis
- Where Redis is a Good Fit
Tokyo Cabinet, Tokyo Tyrant, and Tokyo Dystopia
- Installing the Tokyo Software
- Tokyo Cabinet as a Key-Value Store
- Tokyo Cabinet's Key-Value Database Types
- Tokyo Cabinet's Tables
- Threads and Multiprocessing With Tokyo Cabinet
- Tokyo Tyrant as a Network Interface
- The Strengths of Tokyo Cabinet