# 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.

As the API is based on object-oriented design, the hash database and the the B+ tree database have same methods which inherited from the upper abstract class. Beside them, seven kinds of databases are provided under the same base class. The prototype hash database is powered by the standard container of std::unordered_map. The prototype tree database is powered by the standard container of std::map. The stash database is powered by the original implementation of naive hash map saving memory. The cache hash database is powered by the original implementation of doubly-linked hash map with LRU deletion algorithm. The cache tree database is powered by the cache hash database and provides B+ tree mechanism. The directory hash database is powered by the directory mechanism of the file system and stores records as respective files in a directory. The directory tree database is powered by the directory hash database and provides B+ tree mechanism. All databases have practical utility methods related to transaction and cursor. Programs for command line interface are also included in the package.

All databases have practical utility methods related to transaction and cursor. Programs for command line interface are also included in the package.

The following classes are most important. If you are new to Kyoto Cabinet, learn the polymorphic database first.

- kyotocabinet::BasicDB -- common interface of concrete databases
- kyotocabinet::ProtoHashDB -- on-memory hash database based on std::unordered_map
- kyotocabinet::ProtoTreeDB -- on-memory hash database based on std::map
- kyotocabinet::StashDB -- economical on-memory hash database for cache.
- kyotocabinet::CacheDB -- on-memory hash database for cache with LRU deletion
- kyotocabinet::GrassDB -- on-memory tree database for cache in order
- kyotocabinet::HashDB -- file hash database, which implements hash table on a file
- kyotocabinet::TreeDB -- file tree database, which implements B+ tree on a file
- kyotocabinet::DirDB -- directory hash database, which handles respective files in a directory
- kyotocabinet::ForestDB -- directory tree database, which implements B+ tree on a directory
- kyotocabinet::TextDB -- plain text database, which treats a text file as a database
- kyotocabinet::PolyDB -- polymorphic database, dynamic binding of the above databases

- kyotocabinet::MapReduce -- MapReduce framework to process records in each database
- kyotocabinet::IndexDB -- wrapper for efficient appending operations to each database

- kclangc.h -- C language binding of the polymorphic database

See the project homepage ( http://fallabs.com/kyotocabinet/ ) for details.

## Example

The following code is an example to use a polymorphic database.

    #include <kcpolydb.h>

    using namespace std;
    using namespace kyotocabinet;

    // main routine
    int main(int argc, char** argv) {

      // create the database object
      PolyDB db;

      // open the database
      if (!db.open("casket.kch", PolyDB::OWRITER | PolyDB::OCREATE)) {
        cerr << "open error: " << db.error().name() << endl;
      }

      // store records
      if (!db.set("foo", "hop") ||
          !db.set("bar", "step") ||
          !db.set("baz", "jump")) {
        cerr << "set error: " << db.error().name() << endl;
      }

      // retrieve a record
      string value;
      if (db.get("foo", &value)) {
        cout << value << endl;
      } else {
        cerr << "get error: " << db.error().name() << endl;
      }

      // traverse records
      DB::Cursor* cur = db.cursor();
      cur->jump();
      string ckey, cvalue;
      while (cur->get(&ckey, &cvalue, true)) {
        cout << ckey << ":" << cvalue << endl;
      }
      delete cur;

      // close the database
      if (!db.close()) {
        cerr << "close error: " << db.error().name() << endl;
      }

      return 0;
    }

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

    #include <kcpolydb.h>

    using namespace std;
    using namespace kyotocabinet;

    // main routine
    int main(int argc, char** argv) {

      // create the database object
      PolyDB db;

      // open the database
      if (!db.open("casket.kch", PolyDB::OREADER)) {
        cerr << "open error: " << db.error().name() << endl;
      }

      // define the visitor
      class VisitorImpl : public DB::Visitor {
        // call back function for an existing record
        const char* visit_full(const char* kbuf, size_t ksiz,
                               const char* vbuf, size_t vsiz, size_t *sp) {
          cout << string(kbuf, ksiz) << ":" << string(vbuf, vsiz) << endl;
          return NOP;
        }
        // call back function for an empty record space
        const char* visit_empty(const char* kbuf, size_t ksiz, size_t *sp) {
          cerr << string(kbuf, ksiz) << " is missing" << endl;
          return NOP;
        }
      } visitor;

      // retrieve a record with visitor
      if (!db.accept("foo", 3, &visitor, false) ||
          !db.accept("dummy", 5, &visitor, false)) {
        cerr << "accept error: " << db.error().name() << endl;
      }

      // traverse records with visitor
      if (!db.iterate(&visitor, false)) {
        cerr << "iterate error: " << db.error().name() << endl;
      }

      // close the database
      if (!db.close()) {
        cerr << "close error: " << db.error().name() << endl;
      }

      return 0;
    }

The following code is an example of word counting with the MapReduce framework.

    #include <kcpolydb.h>
    #include <kcdbext.h>

    using namespace std;
    using namespace kyotocabinet;

    // main routine
    int main(int argc, char** argv) {

      // create the database object
      PolyDB db;

      // open the database
      if (!db.open()) {
        cerr << "open error: " << db.error().name() << endl;
      }

      // store records
      db.set("1", "this is a pen");
      db.set("2", "what a beautiful pen this is");
      db.set("3", "she is beautiful");

      // define the mapper and the reducer
      class MapReduceImpl : public MapReduce {
        // call back function of the mapper
        bool map(const char* kbuf, size_t ksiz, const char* vbuf, size_t vsiz) {
          vector<string> words;
          strsplit(string(vbuf, vsiz), ' ', &words);
          for (vector<string>::iterator it = words.begin();
               it != words.end(); it++) {
            emit(it->data(), it->size(), "", 0);
          }
          return true;
        }
        // call back function of the reducer
        bool reduce(const char* kbuf, size_t ksiz, ValueIterator* iter) {
          size_t count = 0;
          const char* vbuf;
          size_t vsiz;
          while ((vbuf = iter->next(&vsiz)) != NULL) {
            count++;
          }
          cout << string(kbuf, ksiz) << ": " << count << endl;
          return true;
        }
      } mr;

      // execute the MapReduce process
      if (!mr.execute(&db)) {
        cerr << "MapReduce error: " << db.error().name() << endl;
      }

      // close the database
      if (!db.close()) {
        cerr << "close error: " << db.error().name() << endl;
      }

      return 0;
    }

The C language binding is also provided as a wrapper of the polymorphic database API. The following code is an example.

    #include <kclangc.h>

    /* call back function for an existing record */
    const char* visitfull(const char* kbuf, size_t ksiz,
                          const char* vbuf, size_t vsiz, size_t *sp, void* opq) {
      fwrite(kbuf, 1, ksiz, stdout);
      printf(":");
      fwrite(vbuf, 1, vsiz, stdout);
      printf("\n");
      return KCVISNOP;
    }

    /* call back function for an empty record space */
    const char* visitempty(const char* kbuf, size_t ksiz, size_t *sp, void* opq) {
      fwrite(kbuf, 1, ksiz, stdout);
      printf(" is missing\n");
      return KCVISNOP;
    }

    /* main routine */
    int main(int argc, char** argv) {
      KCDB* db;
      KCCUR* cur;
      char *kbuf, *vbuf;
      size_t ksiz, vsiz;
      const char *cvbuf;

      /* create the database object */
      db = kcdbnew();

      /* open the database */
      if (!kcdbopen(db, "casket.kch", KCOWRITER | KCOCREATE)) {
        fprintf(stderr, "open error: %s\n", kcecodename(kcdbecode(db)));
      }

      /* store records */
      if (!kcdbset(db, "foo", 3, "hop", 3) ||
          !kcdbset(db, "bar", 3, "step", 4) ||
          !kcdbset(db, "baz", 3, "jump", 4)) {
        fprintf(stderr, "set error: %s\n", kcecodename(kcdbecode(db)));
      }

      /* retrieve a record */
      vbuf = kcdbget(db, "foo", 3, &vsiz);
      if (vbuf) {
        printf("%s\n", vbuf);
        kcfree(vbuf);
      } else {
        fprintf(stderr, "get error: %s\n", kcecodename(kcdbecode(db)));
      }

      /* traverse records */
      cur = kcdbcursor(db);
      kccurjump(cur);
      while ((kbuf = kccurget(cur, &ksiz, &cvbuf, &vsiz, 1)) != NULL) {
        printf("%s:%s\n", kbuf, cvbuf);
        kcfree(kbuf);
      }
      kccurdel(cur);

      /* retrieve a record with visitor */
      if (!kcdbaccept(db, "foo", 3, visitfull, visitempty, NULL, 0) ||
          !kcdbaccept(db, "dummy", 5, visitfull, visitempty, NULL, 0)) {
        fprintf(stderr, "accept error: %s\n", kcecodename(kcdbecode(db)));
      }

      /* traverse records with visitor */
      if (!kcdbiterate(db, visitfull, NULL, 0)) {
        fprintf(stderr, "iterate error: %s\n", kcecodename(kcdbecode(db)));
      }

      /* close the database */
      if (!kcdbclose(db)) {
        fprintf(stderr, "close error: %s\n", kcecodename(kcdbecode(db)));
      }

      /* delete the database object */
      kcdbdel(db);

      return 0;
    }