Hash Database — HDB

HDB

class tokyo.cabinet.HDB

Example:

from tokyo.cabinet import *

hdb = HDB()

# if need be, you should call tune/setcache/setxmsiz/setdfunit before
# open, ex. with default values:
hdb.tune(0, -1, -1, 0)
hdb.setcache(0)
hdb.setxmsiz(0)
hdb.setdfunit(0)

# open the database
hdb.open("casket.tch", HDBOWRITER | HDBOCREAT)

# store records
for key, value in [("foo", "hop"), ("bar", "step"), ("baz", "jump")]:
    hdb[key] = value

# retrieve one record
print(hdb["foo"])

# traverse records
for key in hdb:
    print(key, hdb[key])

# close the database
hdb.close()

Note

For all methods taking either a key argument or a pair (key, value), key and value must be either str (Python2) or bytes (Python3).

len(hdb)
Return the number of records in the database hdb.
hdb[key]
Return the value of hdb‘s record corresponding to key. Raises KeyError if key is not in the database.
hdb[key] = value
Set hdb[key] to value.
del hdb[key]
Remove hdb[key] from hdb. Raises KeyError if key is not in the database.
key in hdb
Return True if hdb has a key key, else False.
key not in hdb
Equivalent to not key in hdb.
iter(hdb)
Return an iterator over the keys of the database.
tune(bnum, apow, fpow, opts)

Tune a database.

Parameters:
  • bnum – the number of elements in a bucket array. If specified as 0 or as a negative value, the default value (131071) is used.
  • apow – (power of 2) TODO. If specified as a negative value, the default value (4) is used (means: 2**4).
  • fpow – (power of 2) TODO. If specified as a negative value, the default value (10) is used (means 2**10).
  • opts – options, see HDB.tune()/HDB.optimize() options.

Note

Tuning an open database is an invalid operation.

setcache(rcnum)

Set the cache size.

Parameter:rcnum – the maximum number of records to be cached. If specified as 0 or as a negative value, caching is disabled (default).

Note

Setting the cache size on an open database is an invalid operation.

setxmsiz(xmsiz)

Set the extra mapped memory size.

Parameter:xmsiz – the amount of extra mapped memory (in what unit?). If specified as 0 or as a negative value, the extra mapped memory is disabled. Default is 67108864 (unit?).

Note

Setting the extra memory size on an open database is an invalid operation.

setdfunit(dfunit)

Set auto defragmentation’s unit step number.

Parameter:dfunit – the unit step number(?). If specified as 0 or as a negative value, auto defragmentation is disabled (default).

Note

Setting this on an open database is an invalid operation.

open(path, mode)

Open a database.

Parameters:
close()

Close the database.

Note

HDBs are closed when garbage-collected.

clear()
Remove all records from the database.
copy(path)

Copy the database file.

Parameter:path – path to the destination file.
begin()
Begin a transaction.
commit()
Commit a transaction.
abort()
Abort a transaction.
get(key)

Return the value corresponding to key. Equivalent to hdb[key].

New in version 0.2.0.

remove(key)

Delete a record from the database. Equivalent to del hdb[key].

New in version 0.2.0.

put(key, value)
Store a record in the database. Equivalent to hdb[key] = value.
putkeep(key, value)
Store a record in the database, unlike the standard forms (hdb[key] = value or put()), this method raises KeyError if key is already in the database.
putcat(key, value)
Concatenate a value at the end of an existing one. If there is no corresponding record, a new record is stored.
putasync(key, value)
Store a record in the database in an asynchronous fashion. Records passed to this method are accumulated into an inner buffer and written to the file when synced.
addint(key, num)

Store an int in the database. If key is not in the database, this method stores num in the database and returns it. If key is already in the database, then it will add num to its current value and return the result. If key exists but its value cannot be treated as an int this method raises KeyError.

Note

  • The returned value will wrap around INT_MAX and INT_MIN. Example:

    >>> hdb.addint('id', INT_MAX) # setting 'id' to INT_MAX
    2147483647
    >>> hdb.addint('id', 1) # adding 1 to 'id' returns INT_MIN
    -2147483648
    >>>
    
  • Trying to access a value set with addint() using get() or hdb[key] will not return an int. It will instead return the internal binary representation of the value. Example:

    >>> hdb.addint('id', INT_MAX) # setting 'id' to INT_MAX
    2147483647
    >>> hdb['id']
    '\xff\xff\xff\x7f'
    >>>
    

New in version 0.5.0.

adddouble(key, num)

Store a float in the database. If key is not in the database, this method stores num in the database and returns it. If key is already in the database, then it will add num to its current value and return the result. If key exists but its value cannot be treated as a float this method raises KeyError.

Note

Trying to access a value set with adddouble() using get() or hdb[key] will not return a float.

New in version 0.5.0.

sync()
Flush modifications to the database file.
iterkeys()

Return an iterator over the database’s keys.

New in version 0.6.1.

itervalues()

Return an iterator over the database’s values.

New in version 0.6.1.

iteritems()

Return an iterator over the database’s items ((key, value) pairs).

New in version 0.6.1.

searchkeys(prefix[, max])
Return a frozenset of keys starting with prefix. If given, max is the maximum number of keys to fetch, if omitted or specified as a negative value no limit is applied.
optimize([bnum=0[, apow=-1[, fpow=-1[, opts=255]]]])

Optimize a database.

Parameters:
  • bnum – the number of elements in a bucket array. If specified as 0 or as a negative value, the default value (twice the number of records) is used.
  • apow – (power of 2) TODO. If specified as a negative value, the current setting is kept.
  • fpow – (power of 2) TODO. If specified as a negative value, the current setting is kept.
  • opts – options, see HDB.tune()/HDB.optimize() options. If specified as 255 (UINT8_MAX), the current setting is kept.

Note

Optimizing a read only database, or during a transaction, is an invalid operation.

path
The path to the database file.
size
The size in bytes of the database file.

HDB.open() modes

tokyo.cabinet.HDBOREADER
Open a database in read-only mode.
tokyo.cabinet.HDBOWRITER
Open a database in read-write mode.

The following constants can only be combined with HDBOWRITER :

  • tokyo.cabinet.HDBOCREAT

    Create a new database file if it does not exists.

  • tokyo.cabinet.HDBOTRUNC

    Create a new database file even if one already exists (truncates existing file).

  • tokyo.cabinet.HDBOTSYNC

    Sync the database file on every transaction.

The following constants can be combined with either HDBOREADER or HDBOWRITER :

  • tokyo.cabinet.HDBONOLCK

    Opens the database file without file locking.

  • tokyo.cabinet.HDBOLCKNB

    Locking is performed without blocking.

HDB.tune()/HDB.optimize() options

tokyo.cabinet.HDBTLARGE
The size of the database can be larger than 2GB.
tokyo.cabinet.HDBTDEFLATE
Each record is compressed with Deflate encoding.
tokyo.cabinet.HDBTBZIP
Each record is compressed with BZIP2 encoding.
tokyo.cabinet.HDBTTCBS
Each record is compressed with TCBS encoding.

Table Of Contents

Previous topic

tokyo.cabinet — Python Tokyo Cabinet interface.

Next topic

On-memory Hash Database — MDB

This Page