# Kyoto Cabinet

Kyoto Cabinet: a straightforward implementation of DBM

## Introduction

Kyoto Cabinet is a library of routines for managing a database. The database is a simple data file containing records, each is a pair of a key and a value. Every key and value is serial bytes with variable length. Both binary data and character string can be used as a key and a value. Each key must be unique within a database. There is neither concept of data tables nor data types. Records are organized in hash table or B+ tree.

The following access methods are provided to the database: storing a record with a key and a value, deleting a record by a key, retrieving a record by a key. Moreover, traversal access to every key are provided. These access methods are similar to ones of the original DBM (and its followers: NDBM and GDBM) library defined in the UNIX standard. Kyoto Cabinet is an alternative for the DBM because of its higher performance.

Each operation of the hash database has the time complexity of “O(1)”. Therefore, in theory, the performance is constant regardless of the scale of the database. In practice, the performance is determined by the speed of the main memory or the storage device. If the size of the database is less than the capacity of the main memory, the performance will seem on-memory speed, which is faster than std::map of STL. Of course, the database size can be greater than the capacity of the main memory and the upper limit is 8 exabytes. Even in that case, each operation needs only one or two seeking of the storage device.

Each operation of the B+ tree database has the time complexity of “O(log N)”. Therefore, in theory, the performance is logarithmic to the scale of the database. Although the performance of random access of the B+ tree database is slower than that of the hash database, the B+ tree database supports sequential access in order of the keys, which realizes forward matching search for strings and range search for integers. The performance of sequential access is much faster than that of random access.

This library wraps the polymorphic database of the C++ API. So, you can select the internal data structure by specifying the database name in runtime. This library is thread-safe on Ruby 1.9.x (YARV) though it is not thread-safe on Ruby 1.8.x.

## Installation

Install the latest version of Kyoto Cabinet beforehand and get the package of the Ruby binding of Kyoto Cabinet.

Enter the directory of the extracted package then perform installation.

    ruby extconf.rb
    make
    ruby test.rb
    su
    make install

The package `kyotocabinet’ should be loaded in each source file of application programs.

    require 'kyotocabinet'

All symbols of Kyoto Cabinet are defined in the module `KyotoCabinet’. You can access them without any prefix by including the module.

    include KyotoCabinet

An instance of the class `DB’ is used in order to handle a database. You can store, delete, and retrieve records with the instance.

## Example

The following code is a typical example to use a database.

    require 'kyotocabinet'
    include KyotoCabinet

    # create the database object
    db = DB::new

    # open the database
    unless db.open('casket.kch', DB::OWRITER | DB::OCREATE)
      STDERR.printf("open error: %s\n", db.error)
    end

    # store records
    unless db.set('foo', 'hop') and
        db.set('bar', 'step') and
        db.set('baz', 'jump')
      STDERR.printf("set error: %s\n", db.error)
    end

    # retrieve records
    value = db.get('foo')
    if value
      printf("%s\n", value)
    else
      STDERR.printf("get error: %s\n", db.error)
    end

    # traverse records
    cur = db.cursor
    cur.jump
    while rec = cur.get(true)
      printf("%s:%s\n", rec[0], rec[1])
    end
    cur.disable

    # close the database
    unless db.close
      STDERR.printf("close error: %s\n", db.error)
    end

The following code is a more complex example, which uses the Visitor pattern.

    require 'kyotocabinet'
    include KyotoCabinet

    # create the database object
    db = DB::new

    # open the database
    unless db.open('casket.kch', DB::OREADER)
      STDERR.printf("open error: %s\n", db.error)
    end

    # define the visitor
    class VisitorImpl < Visitor
      # call back function for an existing record
      def visit_full(key, value)
        printf("%s:%s\n", key, value)
        return NOP
      end
      # call back function for an empty record space
      def visit_empty(key)
        STDERR.printf("%s is missing\n", key)
        return NOP
      end
    end
    visitor = VisitorImpl::new

    # retrieve a record with visitor
    unless db.accept("foo", visitor, false) and
        db.accept("dummy", visitor, false)
      STDERR.printf("accept error: %s\n", db.error)
    end

    # traverse records with visitor
    unless db.iterate(visitor, false)
      STDERR.printf("iterate error: %s\n", db.error)
    end

    # close the database
    unless db.close
      STDERR.printf("close error: %s\n", db.error)
    end

The following code is also a complex example, which is suited to the Ruby style.

    require 'kyotocabinet'
    include KyotoCabinet

    # process the database by iterator
    DB::process('casket.kch') { |db|

      # set the encoding of external strings
      db.set_encoding('utf-8')

      # store records
      db['foo'] = 'hop';  # string is fundamental
      db[:bar] = 'step';  # symbol is also ok
      db[3] = 'jump';     # number is also ok

      # retrieve a record value
      printf("%s\n", db['foo'])

      # update records in transaction
      db.transaction {
        db['foo'] = 2.71828
        true
      }

      # multiply a record value
      db.accept('foo') { |key, value|
        value.to_f * 2
      }

      # traverse records by iterator
      db.each { |key, value|
        printf("%s:%s\n", key, value)
      }

      # upcase values by iterator
      db.iterate { |key, value|
        value.upcase
      }

      # traverse records by cursor
      db.cursor_process { |cur|
        cur.jump
        while cur.accept { |key, value|
            printf("%s:%s\n", key, value)
            Visitor::NOP
          }
          cur.step
        end
      }

    }

## License

    Copyright (C) 2009-2010 FAL Labs
    All rights reserved.

Kyoto Cabinet is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

Kyoto Cabinet is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

## Classes/Modules

- KyotoCabinet
- KyotoCabinet::Cursor
- KyotoCabinet::DB
- KyotoCabinet::Error
- KyotoCabinet::FileProcessor
- KyotoCabinet::Visitor

## Methods

- ::new — KyotoCabinet::Error
- ::new — KyotoCabinet::DB
- ::process — KyotoCabinet::DB
- #!= — KyotoCabinet::Error
- #== — KyotoCabinet::Error
- #[] — KyotoCabinet::DB
- #[]= — KyotoCabinet::DB
- #accept — KyotoCabinet::DB
- #accept — KyotoCabinet::Cursor
- #accept_bulk — KyotoCabinet::DB
- #add — KyotoCabinet::DB
- #append — KyotoCabinet::DB
- #atof — KyotoCabinet
- #atoi — KyotoCabinet
- #atoix — KyotoCabinet
- #begin_transaction — KyotoCabinet::DB
- #cas — KyotoCabinet::DB
- #check — KyotoCabinet::DB
- #clear — KyotoCabinet::DB
- #close — KyotoCabinet::DB
- #code — KyotoCabinet::Error
- #conv_str — KyotoCabinet
- #copy — KyotoCabinet::DB
- #count — KyotoCabinet::DB
- #cursor — KyotoCabinet::DB
- #cursor_process — KyotoCabinet::DB
- #db — KyotoCabinet::Cursor
- #delete — KyotoCabinet::DB
- #disable — KyotoCabinet::Cursor
- #dump_snapshot — KyotoCabinet::DB
- #each — KyotoCabinet::DB
- #each_key — KyotoCabinet::DB
- #each_value — KyotoCabinet::DB
- #end_transaction — KyotoCabinet::DB
- #error — KyotoCabinet::DB
- #error — KyotoCabinet::Cursor
- #fetch — KyotoCabinet::DB
- #get — KyotoCabinet::Cursor
- #get — KyotoCabinet::DB
- #get_bulk — KyotoCabinet::DB
- #get_key — KyotoCabinet::Cursor
- #get_value — KyotoCabinet::Cursor
- #hash_fnv — KyotoCabinet
- #hash_murmur — KyotoCabinet
- #increment — KyotoCabinet::DB
- #increment_double — KyotoCabinet::DB
- #inspect — KyotoCabinet::DB
- #inspect — KyotoCabinet::Error
- #inspect — KyotoCabinet::Cursor
- #iterate — KyotoCabinet::DB
- #jump — KyotoCabinet::Cursor
- #jump_back — KyotoCabinet::Cursor
- #length — KyotoCabinet::DB
- #levdist — KyotoCabinet
- #load_snapshot — KyotoCabinet::DB
- #match_prefix — KyotoCabinet::DB
- #match_regex — KyotoCabinet::DB
- #match_similar — KyotoCabinet::DB
- #merge — KyotoCabinet::DB
- #message — KyotoCabinet::Error
- #name — KyotoCabinet::Error
- #occupy — KyotoCabinet::DB
- #open — KyotoCabinet::DB
- #path — KyotoCabinet::DB
- #process — KyotoCabinet::FileProcessor
- #remove — KyotoCabinet::Cursor
- #remove — KyotoCabinet::DB
- #remove_bulk — KyotoCabinet::DB
- #replace — KyotoCabinet::DB
- #seize — KyotoCabinet::Cursor
- #seize — KyotoCabinet::DB
- #set — KyotoCabinet::DB
- #set — KyotoCabinet::Error
- #set_bulk — KyotoCabinet::DB
- #set_value — KyotoCabinet::Cursor
- #shift — KyotoCabinet::DB
- #size — KyotoCabinet::DB
- #status — KyotoCabinet::DB
- #step — KyotoCabinet::Cursor
- #step_back — KyotoCabinet::Cursor
- #store — KyotoCabinet::DB
- #synchronize — KyotoCabinet::DB
- #to_s — KyotoCabinet::Cursor
- #to_s — KyotoCabinet::Error
- #to_s — KyotoCabinet::DB
- #transaction — KyotoCabinet::DB
- #tune_encoding — KyotoCabinet::DB
- #tune_exception_rule — KyotoCabinet::DB
- #visit_empty — KyotoCabinet::Visitor
- #visit_full — KyotoCabinet::Visitor

Generated with the Darkfish Rdoc Generator 2.