Fixed-length Database — FDB

New in version 0.3.0.

FDB

class tokyocabinet.FDB

Example:

from tokyocabinet import *

fdb = FDB()

# if need be, you should call tune before open, ex. with default values:
fdb.tune(0, 0)

# open the database
fdb.open("casket.tcf", FDBOWRITER | FDBOCREAT)

# store records
for key, value in [(1, "hop"), (2, "step"), (3, "jump")]:
    fdb[key] = value

# retrieve one record
print(fdb[1])

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

# close the database
fdb.close()

Note

For all methods taking either a key argument or a pair (key, value):

  • Python2: key must be long/int and value must be str.
  • Python3: key must be int and value must be bytes.

On top of that key must always be > 0. See Special keys for exceptions to this rule.

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

Tune a database.

Parameters:
  • width – the max lenght (in bytes) of the value of each record. Values longer than this parameter will be truncated. If specified as 0 or as a negative value, the default value (255) is used.
  • size – the max size (in bytes) of the database file. It will be aligned to the system page size. If specified as 0 or as a negative value, the default value (268435456) is used.

See Note on tuning/optimizing a fixed-length database.

Note

Tuning an open database is an invalid operation.

open(path, mode)

Open a database.

Parameters:
close()

Close the database.

Note

FDBs 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 fdb[key].
remove(key)
Delete a record from the database. Equivalent to del fdb[key].
put(key, value)
Store a record in the database. Equivalent to fdb[key] = value.
putkeep(key, value)
Store a record in the database, unlike the standard forms (fdb[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.
sync()
Flush modifications to the database file.
range([lower=FDBIDMIN[, upper=FDBIDMAX[, max=-1]]])
Return a frozenset of keys. If given, max is the maximum number of keys to fetch, if omitted or specified as a negative value no limit is applied.
optimize([width=0[, size=0]])

Optimize a database.

Parameters:
  • width – the max lenght (in bytes) of the value of each record. Values longer than this parameter will be truncated. If specified as 0 or as a negative value, the current setting is kept.
  • size – the max size (in bytes) of the database file. It will be aligned to the system page size. If specified as 0 or as a negative value, the current setting is kept.

See Note on tuning/optimizing a fixed-length database.

Note

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

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:

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

    >>> fdb.addint(100, INT_MAX) # setting key 100 to INT_MAX
    2147483647
    >>> fdb[100]
    '\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 fdb[key] will not return a float.

New in version 0.5.0.

Methods keys(), values() and items() are not yet implemented (mainly because I didn’t settle on how to do it: should they return Iterable, Iterator, MappingView, etc.?). Any help would be greatly appreciated in this matter.

For the time being, for those of you who really need these methods, it’s trivial to implement them in python. Here is an example using generators:

from tokyocabinet import FDB as _FDB

class FDB(_FDB):

    def keys(self):
        return (key for key in self)

    def values(self):
        return (self[key] for key in self)

    def items(self):
        return ((key, self[key]) for key in self)
path
The path to the database file.
size
The size in bytes of the database file.

Note on tuning/optimizing a fixed-length database

The maximum length (and highest possible key) of a database is in direct relation to the width and size parameters and can be determined using the following function:

import os

PAGESIZE = os.sysconf("SC_PAGESIZE")

def fdb_max_len(width, size):
    diff = size & (PAGESIZE - 1)
    if diff > 0: # align to page size
        size = size + PAGESIZE - diff
    # 256 is the database header size
    return int((size - 256) / (width + 1))

FDB.open() modes

tokyocabinet.FDBOREADER
Open a database in read-only mode.
tokyocabinet.FDBOWRITER
Open a database in read-write mode.

The following constants can only be combined with FDBOWRITER :

  • tokyocabinet.FDBOCREAT

    Create a new database file if it does not exists.

  • tokyocabinet.FDBOTRUNC

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

  • tokyocabinet.FDBOTSYNC

    Sync the database file on every transaction.

The following constants can be combined with either FDBOREADER or FDBOWRITER :

  • tokyocabinet.FDBONOLCK

    Opens the database file without file locking.

  • tokyocabinet.FDBOLCKNB

    Locking is performed without blocking.

Special keys

tokyocabinet.FDBIDMIN(-1)
The lowest key currently in the database.
tokyocabinet.FDBIDPREV(-2)
FDBIDMIN - 1
tokyocabinet.FDBIDMAX(-3)
The highest key currently in the database.
tokyocabinet.FDBIDNEXT(-4)
FDBIDMAX + 1

Table Of Contents

Previous topic

On-memory Tree Database — NDB

Next topic

Table Database — TDB

This Page