background preloader

Tokyocabinet

Facebook Twitter

Tokyotyrant

Clojure. Forum. Postrank. Tokyo Cabinet (Key-Value Stores Part 2) A few weeks ago I started a series of posts on Key-Value Stores with a general piece on the Key-Value Store concept, and why you should be using one. This week, I'm following it up with a focus on one of our favorite tools for the job here at Engine Yard: Tokyo Cabinet. Tokyo Cabinet was written by Mikio Hirabayashi, and was originally created for mixi -- the most popular social networking site in Japan. So it's proven in production already, and as far as we've seen -- nice and mature. If you're just beginning to experiment with Key-Value Stores, you've likely played with PStore, and while you probably found it interesting, you probably also ran into its shortcomings pretty early on. As a general diagnosis, I'd guess that you need something faster, more feature rich, and almost as importantly, something that leaves you with a vague tingly feeling after using it for the first time.

This brings me back to my original thought: I'd like to introduce you to Tokyo Cabinet. stockloadertc1.rb. Know Your Storage Options - Benchmarking Tokyo Cabinet « Markov. Tokyo Cabinet is what I’ve been focusing my interest on lately. It is a persistent and fast key-value store, and there is a choice of database engine implementations to use: hash, fixed-length array, B+ tree with a user specified ordering function and table database, which is based on hash. In-memory of file-based. The table engine is particularly interesting because it acts like a relational database without a schema – you are free to simply insert arbitrary data – with support for indexing and queries. TC is written in C with APIs for Perl, Ruby, Java, and Lua. The author is Mikio Hirabayashi, and Tokyo’s largest application is mixi.jp, Japan’s largest social network. There is also Tokyo Tyrant, the accompanying server.

Among the rest, it offers thread safe access, supports memcached protocol and asynchronous replication. The full results are available as a gist. Other notable resources: Mixi Engineers’ Blog » Inside Tokyo Cabinet その壱. 約半年間の沈黙を破ってOSSの世界に戻ってきつつあるmikioです。 先日、Tokyo Cabinet(以下「TC」と呼びます)というデータベースライブラリをリリースしました。 今回から数回に分けて、TCの設計と苦労話について連載してみます。 DBMとは TCは、いわゆるDBMの系譜のデータベースライブラリで、単純なハッシュテーブルをファイル上で永続化するだけの機能を提供します。 文字列だろうが数字だろうが、スカラとして表せるものは何でもハッシュに入れることができる(より複雑なデータ構造の場合にはスカラに直列化してから入れればOK)だけでなく、ハッシュ内のレコード数がどんなに多くなっても一瞬でレコードを格納したり取り出したりできるので、とにかくめっちゃ便利です。 しかしながら、普通のハッシュはメモリ上にデータを保持することを前提として実装されているので、プログラムが終了するとデータは消えてしまいますし、メモリ容量以上のデータをうまく扱うことができません。 そこで、DBM(DataBase Manager)の登場です。 DBM自体は「key/value」ペアの格納と削除と探索を行う機能を提供するだけですが、より複雑なロジックを司るハーネスをかぶせることで様々なシステムに進化します。 ハッシュテーブルの高速性 必ずしもDBMを使わなくっても、例えばCSVでもデータベースは実現できます。 1234560001,batara 1234560002,mikio 1234560003,penguin CSVファイルから特定のレコードを捜し出す際には、逐次探索を行います。 それに対して、DBMはハッシュテーブルを使って、データの探索や追加や削除を高速に行います。 ここまでは前置きで、ここからが本題です。

ハッシュ関数 キーが数値でなくて英字などの文字列であったり、さらには任意のバイナリコードであっても大丈夫なように、それらに一定の計算をして、特定の範囲に収まる数値を生成する必要があります。 付与の変域の中で値をできるだけ均一にバラけさせるために、ものすごくたくさんの手法が提案されています。 単純なファイルフォーマット ハッシュ値ごとにファイルを分けて保存するというのも悪くないアイデアですが、100万個とかのファイルを作るとなるとファイルシステムが大変なことになってしまいます。 次に各レコードのフォーマットを考えます。 Fundamental Specifications of Tokyo Cabinet Version 1. The Kumofs Project.