Tagged Database — JDB

JDB

class tokyo.dystopia.JDB

Example:

from tokyo.dystopia import *

jdb = JDB()

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

# open the database
jdb.open("casket.tdj", JDBOWRITER | JDBOCREAT)

# store records
for key, value in [(1, ("hop", "step", "jump")),
                   (2, ("quick", "brown", "fox")),
                   (3, ("lazy", "dog"))]:
    jdb[key] = value

# retrieve one record
print(jdb[1])

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

# close the database
jdb.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 a sequence of str or unicode.
  • Python3: key must be int and value must be a sequence of bytes or str.

Values will always be returned as a tuple of UTF-8 encoded unicode objects.

On top of that key must always be > 0.

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

Tune a database.

Parameters:
  • ernum – the expected number of records to be stored. If specified as 0 or as a negative value, the default value (1000000) is used.
  • etnum – the expected number of tokens to be stored. If specified as 0 or as a negative value, the default value (1000000) is used.
  • iusiz – the unit size of each index file(?). If specified as 0 or as a negative value, the default value (536870912) is used.
  • opts – options, see JDB.tune() options.

Note

Tuning an open database is an invalid operation.

setcache(icsiz, lcnum)

Set the cache size.

Parameters:
  • icsiz – the size of the token cache. If specified as 0 or as a negative value, the default value (134217728) is used.
  • lcnum – the maximum number of cached leaf nodes. If specified as 0 or as a negative value, the default value (64 for writer or 1024 for reader) is used.

Note

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

setfwmmax(fwmmax)

Set the maximum number of forward matching expansion(?).

Parameter:fwmmax – the maximum number of forward matching expansion.

Note

Setting this on an open database is an invalid operation.

open(path, mode)

Open a database.

Parameters:
close()

Close the database.

Note

JDBs are closed when garbage-collected.

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

Copy the database directory.

Parameter:path – path to the destination directory.
get(key)
Return the value corresponding to key. Equivalent to jdb[key].
remove(key)
Delete a record from the database. Equivalent to del jdb[key].
put(key, value)
Store a record in the database. Equivalent to jdb[key] = value.
sync()
Flush modifications to the database file.
iterkeys()
Return an iterator over the database’s keys.
itervalues()
Return an iterator over the database’s values.
iteritems()
Return an iterator over the database’s items ((key, value) pairs).
search(expr[, mode])

Search a database, return a frozenset of keys whose value match the expressed condition.

Parameters:

Conditions can be expressed in two ways:

See also

‘Compound Expression of Search’ at Tokyo Dystopia documentation.

optimize()

Optimize a database.

Note

Optimizing a read only database is an invalid operation.

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

JDB.open() modes

tokyo.dystopia.JDBOREADER
Open a database in read-only mode.
tokyo.dystopia.JDBOWRITER
Open a database in read-write mode.

The following constants can only be combined with JDBOWRITER :

  • tokyo.dystopia.JDBOCREAT

    Create a new database file if it does not exists.

  • tokyo.dystopia.JDBOTRUNC

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

The following constants can be combined with either JDBOREADER or JDBOWRITER :

  • tokyo.dystopia.JDBONOLCK

    Opens the database file without file locking.

  • tokyo.dystopia.JDBOLCKNB

    Locking is performed without blocking.

JDB.tune() options

tokyo.dystopia.JDBTLARGE
The size of the database can be larger than 2GB.
tokyo.dystopia.JDBTDEFLATE
Each page is compressed with Deflate encoding.
tokyo.dystopia.JDBTBZIP
Each page is compressed with BZIP2 encoding.
tokyo.dystopia.JDBTTCBS
Each page is compressed with TCBS encoding.

JDB.search() modes

tokyo.dystopia.JDBSSUBSTR
expr in v for v in value
tokyo.dystopia.JDBSPREFIX
v.startswith(expr) for v in value
tokyo.dystopia.JDBSSUFFIX
v.endswith(expr) for v in value
tokyo.dystopia.JDBSFULL
expr in value

JDB.search() mini language

expr meaning
'expr'
expr in value
'expr1 expr2'
expr1 in value and expr2 in value
'"expr1 expr2"'
"expr1 expr2" in value
'[[*expr*]]'
expr in v for v in value
'[[expr*]]'
v.startswith(expr) for v in value
'[[*expr]]'
v.endswith(expr) for v in value

The expressions above can be combined with || and/or && (|| has a higher order of precedence).

Table Of Contents

Previous topic

Indexed Database — IDB

Next topic

Todo

This Page