Home | Trees | Indices | Help |
|
---|
|
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 works on Python 2.x (2.6 or later) only. Python 3.x requires another dedicated package.
Install the latest version of Kyoto Cabinet beforehand and get the package of the Python binding of Kyoto Cabinet.
Enter the directory of the extracted package then perform installation. If your system has another command except for the "python2.7" command, edit the Makefile beforehand.:
make make check su make install
Symbols of the module `kyotocabinet' should be included in each source file of application programs.:
import 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.
The following code is a typical example to use a database.:
from kyotocabinet import * import sys # create the database object db = DB() # open the database if not db.open("casket.kch", DB.OWRITER | DB.OCREATE): print >>sys.stderr, "open error: " + str(db.error()) # store records if not db.set("foo", "hop") or not db.set("bar", "step") or not db.set("baz", "jump"): print >>sys.stderr, "set error: " + str(db.error()) # retrieve records value = db.get("foo") if value: print value else: print >>sys.stderr, "get error: " + str(db.error()) # traverse records cur = db.cursor() cur.jump() while True: rec = cur.get(True) if not rec: break print rec[0] + ":" + rec[1] cur.disable() # close the database if not db.close(): print >>sys.stderr, "close error: " + str(db.error())
The following code is a more complex example, which uses the Visitor pattern.:
from kyotocabinet import * import sys # create the database object db = DB() # open the database if not db.open("casket.kch", DB.OREADER): print >>sys.stderr, "open error: " + str(db.error()) # define the visitor class VisitorImpl(Visitor): # call back function for an existing record def visit_full(self, key, value): print "%s:%s" % (key, value) return self.NOP # call back function for an empty record space def visit_empty(self, key): print >>sys.stderr, "%s is missing" % key return self.NOP visitor = VisitorImpl() # retrieve a record with visitor if not db.accept("foo", visitor, False) or not db.accept("dummy", visitor, False): print >>sys.stderr, "accept error: " + str(db.error()) # traverse records with visitor if not db.iterate(visitor, False): print >>sys.stderr, "iterate error: " + str(db.error()) # close the database if not db.close(): print >>sys.stderr, "close error: " + str(db.error())
The following code is also a complex example, which is more suited to the Ruby style.:
from kyotocabinet import * import sys # define the functor def dbproc(db): # store records db['foo'] = b'step'; # string is fundamental db[u'bar'] = 'hop'; # unicode is also ok db[3] = 'jump'; # number is also ok # retrieve a record value print db['foo'] # update records in transaction def tranproc(): db['foo'] = 2.71828 return True db.transaction(tranproc) # multiply a record value def mulproc(key, value): return float(value) * 2 db.accept('foo', mulproc) # traverse records by iterator for key in db: print "%s:%s" % (key, db[key]) # upcase values by iterator def upproc(key, value): return value.upper() db.iterate(upproc) # traverse records by cursor def curproc(cur): cur.jump() def printproc(key, value): print "%s:%s" % (key, value) return Visitor.NOP while cur.accept(printproc): cur.step() db.cursor_process(curproc) # process the database by the functor DB.process(dbproc, 'casket.kch')
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 | |
Error Error data. |
|
Visitor Interface to access a record. |
|
FileProcessor Interface to process the database file. |
|
Cursor Interface of cursor to indicate a record. |
|
DB Interface of database abstraction. |
Functions | |||
|
|||
|
|||
|
|||
|
|||
|
|||
|
Variables | |
VERSION =
The version information. |
|
__package__ = None
|
Function Details |
Convert any object to a string.
|
Convert a string to an integer.
|
Convert a string with a metric prefix to an integer.
|
Convert a string to a real number.
|
Get the hash value of a string by MurMur hashing.
|
Get the hash value of a string by FNV hashing.
|
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Sat Mar 10 18:51:32 2012 | http://epydoc.sourceforge.net |