Class: TokyoCabinet::TDB [Tokyo Cabinet]

Class | TokyoCabinet::TDB
In: | tokyocabinet-doc.rb
Parent: | Object

Table database is a file containing records composed of the primary keys and arbitrary columns and is handled with the table database API. Before operations to store or retrieve records, it is necessary to open a database file and connect the table database object to it. To avoid data missing or corruption, it is important to close every database file when it is no longer in use. It is forbidden for multible database objects in a process to open the same database at the same time.
Except for the interface below, methods compatible with the `Hash' class are also provided; `[]', `[]=', `store', `delete', `fetch', `has_key?', `clear', `length', `empty?', `each', `each_key', `each_value', and `keys'.

### Methods

adddouble addint close copy ecode errmsg fsiz fwmkeys genuid get iterinit iternext new open optimize out path put putcat putkeep rnum setcache setdfunit setindex setxmsiz sync tranabort tranbegin trancommit tune vanish vsiz

### Constants

ESUCCESS | = | 0 |  | error code: success
ETHREAD | = | 1 |  | error code: threading error
EINVALID | = | 2 |  | error code: invalid operation
ENOFILE | = | 3 |  | error code: file not found
ENOPERM | = | 4 |  | error code: no permission
EMETA | = | 5 |  | error code: invalid meta data
ERHEAD | = | 6 |  | error code: invalid record header
EOPEN | = | 7 |  | error code: open error
ECLOSE | = | 8 |  | error code: close error
ETRUNC | = | 9 |  | error code: trunc error
ESYNC | = | 10 |  | error code: sync error
ESTAT | = | 11 |  | error code: stat error
ESEEK | = | 12 |  | error code: seek error
EREAD | = | 13 |  | error code: read error
EWRITE | = | 14 |  | error code: write error
EMMAP | = | 15 |  | error code: mmap error
ELOCK | = | 16 |  | error code: lock error
EUNLINK | = | 17 |  | error code: unlink error
ERENAME | = | 18 |  | error code: rename error
EMKDIR | = | 19 |  | error code: mkdir error
ERMDIR | = | 20 |  | error code: rmdir error
EKEEP | = | 21 |  | error code: existing record
ENOREC | = | 22 |  | error code: no record found
EMISC | = | 9999 |  | error code: miscellaneous error
TLARGE | = | 1 << 0 |  | tuning option: use 64-bit bucket array
TDEFLATE | = | 1 << 1 |  | tuning option: compress each record with Deflate
TBZIP | = | 1 << 2 |  | tuning option: compress each record with BZIP2
TTCBS | = | 1 << 3 |  | tuning option: compress each record with TCBS
OREADER | = | 1 << 0 |  | open mode: open as a reader
OWRITER | = | 1 << 1 |  | open mode: open as a writer
OCREAT | = | 1 << 2 |  | open mode: writer creating
OTRUNC | = | 1 << 3 |  | open mode: writer truncating
ONOLCK | = | 1 << 4 |  | open mode: open without locking
OLCKNB | = | 1 << 5 |  | open mode: lock without blocking
OTSYNC | = | 1 << 6 |  | open mode: synchronize every transaction
ITLEXICAL | = | 0 |  | index type: lexical string
ITDECIMAL | = | 1 |  | index type: decimal string
ITTOKEN | = | 2 |  | index type: token inverted index
ITQGRAM | = | 3 |  | index type: q-gram inverted index
ITOPT | = | 9998 |  | index type: optimize
ITVOID | = | 9999 |  | index type: void
ITKEEP | = | 1 << 24 |  | index type: keep existing index

### Public Class methods

new()

Create a table database object.
The return value is the new table database object.

### Public Instance methods

adddouble(pkey, num)

Add a real number to a record.
`key' specifies the primary key.
`num' specifies the additional value.
If successful, the return value is the summation value, else, it is `nil'.
The additional value is stored as a decimal string value of a column whose name is “_num”. If no record corresponds, a new record with the additional value is stored.

addint(pkey, num)

Add an integer to a record.
`pkey' specifies the primary key.
`num' specifies the additional value.
If successful, the return value is the summation value, else, it is `nil'.
The additional value is stored as a decimal string value of a column whose name is “_num”. If no record corresponds, a new record with the additional value is stored.

close()

Close the database file.
If successful, the return value is true, else, it is false.
Update of a database is assured to be written when the database is closed. If a writer opens a database but does not close it appropriately, the database will be broken.

copy(path)

Copy the database file.
`path' specifies the path of the destination file. If it begins with `@', the trailing substring is executed as a command line.
If successful, the return value is true, else, it is false. False is returned if the executed command returns non-zero code.
The database file is assured to be kept synchronized and not modified while the copying or executing operation is in progress. So, this method is useful to create a backup file of the database file.

ecode()

Get the last happened error code.
The return value is the last happened error code.
The following error codes are defined: `TokyoCabinet::TDB::ESUCCESS' for success, `TokyoCabinet::TDB::ETHREAD' for threading error, `TokyoCabinet::TDB::EINVALID' for invalid operation, `TokyoCabinet::TDB::ENOFILE' for file not found, `TokyoCabinet::TDB::ENOPERM' for no permission, `TokyoCabinet::TDB::EMETA' for invalid meta data, `TokyoCabinet::TDB::ERHEAD' for invalid record header, `TokyoCabinet::TDB::EOPEN' for open error, `TokyoCabinet::TDB::ECLOSE' for close error, `TokyoCabinet::TDB::ETRUNC' for trunc error, `TokyoCabinet::TDB::ESYNC' for sync error, `TokyoCabinet::TDB::ESTAT' for stat error, `TokyoCabinet::TDB::ESEEK' for seek error, `TokyoCabinet::TDB::EREAD' for read error, `TokyoCabinet::TDB::EWRITE' for write error, `TokyoCabinet::TDB::EMMAP' for mmap error, `TokyoCabinet::TDB::ELOCK' for lock error, `TokyoCabinet::TDB::EUNLINK' for unlink error, `TokyoCabinet::TDB::ERENAME' for rename error, `TokyoCabinet::TDB::EMKDIR' for mkdir error, `TokyoCabinet::TDB::ERMDIR' for rmdir error, `TokyoCabinet::TDB::EKEEP' for existing record, `TokyoCabinet::TDB::ENOREC' for no record found, and `TokyoCabinet::TDB::EMISC' for miscellaneous error.

errmsg(ecode)

Get the message string corresponding to an error code.
`ecode' specifies the error code. If it is not defined or negative, the last happened error code is specified.
The return value is the message string of the error code.

fsiz()

Get the size of the database file.
The return value is the size of the database file or 0 if the object does not connect to any database file.

fwmkeys(prefix, max)

Get forward matching primary keys.
`prefix' specifies the prefix of the corresponding keys.
`max' specifies the maximum number of keys to be fetched. If it is not defined or negative, no limit is specified.
The return value is a list object of the keys of the corresponding records. This method does never fail. It returns an empty list even if no record corresponds.
Note that this method may be very slow because every key in the database is scanned.

genuid()

Generate a unique ID number.
The return value is the new unique ID number or -1 on failure.

get(pkey)

Retrieve a record.
`pkey' specifies the primary key.
If successful, the return value is a hash of the columns of the corresponding record. `nil' is returned if no record corresponds.

iterinit()

Initialize the iterator.
If successful, the return value is true, else, it is false.
The iterator is used in order to access the primary key of every record stored in a database.

iternext()

Get the next primary key of the iterator.
If successful, the return value is the next primary key, else, it is `nil'. `nil' is returned when no record is to be get out of the iterator.
It is possible to access every record by iteration of calling this method. It is allowed to update or remove records whose keys are fetched while the iteration. However, it is not assured if updating the database is occurred while the iteration. Besides, the order of this traversal access method is arbitrary, so it is not assured that the order of storing matches the one of the traversal access.

open(path, omode)

Open a database file.
`path' specifies the path of the database file.
`omode' specifies the connection mode: `TokyoCabinet::TDB::OWRITER' as a writer, `TokyoCabinet::TDB::OREADER' as a reader. If the mode is `TokyoCabinet::TDB::OWRITER', the following may be added by bitwise-or: `TokyoCabinet::TDB::OCREAT', which means it creates a new database if not exist, `TokyoCabinet::TDB::OTRUNC', which means it creates a new database regardless if one exists, `TokyoCabinet::TDB::OTSYNC', which means every transaction synchronizes updated contents with the device. Both of `TokyoCabinet::TDB::OREADER' and `TokyoCabinet::TDB::OWRITER' can be added to by bitwise-or: `TokyoCabinet::TDB::ONOLCK', which means it opens the database file without file locking, or `TokyoCabinet::TDB::OLCKNB', which means locking is performed without blocking. If it is not defined, `TokyoCabinet::TDB::OREADER' is specified.
If successful, the return value is true, else, it is false.

optimize(bnum, apow, fpow, opts)

Optimize the database file.
`bnum' specifies the number of elements of the bucket array. If it is not defined or not more than 0, the default value is specified. The default value is two times of the number of records.
`apow' specifies the size of record alignment by power of 2. If it is not defined or negative, the current setting is not changed.
`fpow' specifies the maximum number of elements of the free block pool by power of 2. If it is not defined or negative, the current setting is not changed.
`opts' specifies options by bitwise-or: `TokyoCabinet::TDB::TLARGE' specifies that the size of the database can be larger than 2GB by using 64-bit bucket array, `TokyoCabinet::TDB::TDEFLATE' specifies that each record is compressed with Deflate encoding, `TokyoCabinet::TDB::TBZIP' specifies that each record is compressed with BZIP2 encoding, `TokyoCabinet::TDB::TTCBS' specifies that each record is compressed with TCBS encoding. If it is not defined or 0xff, the current setting is not changed.
If successful, the return value is true, else, it is false.
This method is useful to reduce the size of the database file with data fragmentation by successive updating.

out(pkey)

Remove a record.
`pkey' specifies the primary key.
If successful, the return value is true, else, it is false.

path()

Get the path of the database file.
The return value is the path of the database file or `nil' if the object does not connect to any database file.

put(pkey, cols)

Store a record.
`pkey' specifies the primary key.
`cols' specifies a hash containing columns.
If successful, the return value is true, else, it is false.
If a record with the same key exists in the database, it is overwritten.

putcat(pkey, value)

Concatenate columns of the existing record.
`pkey' specifies the primary key.
`cols' specifies a hash containing columns.
If successful, the return value is true, else, it is false.
If there is no corresponding record, a new record is created.

putkeep(pkey, cols)

Store a new record.
`pkey' specifies the primary key.
`cols' specifies a hash containing columns.
If successful, the return value is true, else, it is false.
If a record with the same key exists in the database, this method has no effect.

rnum()

Get the number of records.
The return value is the number of records or 0 if the object does not connect to any database file.

setcache(rcnum, lcnum, ncnum)

Set the caching parameters.
`rcnum' specifies the maximum number of records to be cached. If it is not defined or not more than 0, the record cache is disabled. It is disabled by default.
`lcnum' specifies the maximum number of leaf nodes to be cached. If it is not defined or not more than 0, the default value is specified. The default value is 4096.
`ncnum' specifies the maximum number of non-leaf nodes to be cached. If it is not defined or not more than 0, the default value is specified. The default value is 512.
If successful, the return value is true, else, it is false.
Note that the caching parameters of the database should be set before the database is opened.

setdfunit(dfunit)

Set the unit step number of auto defragmentation.
`dfunit' specifie the unit step number. If it is not more than 0, the auto defragmentation is disabled. It is disabled by default.
If successful, the return value is true, else, it is false.
Note that the defragmentation parameters should be set before the database is opened.

setindex(name, type)

Set a column index.
`name' specifies the name of a column. If the name of an existing index is specified, the index is rebuilt. An empty string means the primary key.
`type' specifies the index type: `TokyoCabinet::TDB::ITLEXICAL' for lexical string, `TokyoCabinet::TDB::ITDECIMAL' for decimal string, `TokyoCabinet::TDB::ITTOKEN' for token inverted index, `TokyoCabinet::TDB::ITQGRAM' for q-gram inverted index. If it is `TokyoCabinet::TDB::ITOPT', the index is optimized. If it is `TokyoCabinet::TDB::ITVOID', the index is removed. If `TokyoCabinet::TDB::ITKEEP' is added by bitwise-or and the index exists, this method merely returns failure.
If successful, the return value is true, else, it is false.

setxmsiz(xmsiz)

Set the size of the extra mapped memory.
`xmsiz' specifies the size of the extra mapped memory. If it is not defined or not more than 0, the extra mapped memory is disabled. The default size is 67108864.
If successful, the return value is true, else, it is false.
Note that the mapping parameters should be set before the database is opened.

sync()

Synchronize updated contents with the file and the device.
If successful, the return value is true, else, it is false.
This method is useful when another process connects the same database file.

tranabort()

Abort the transaction.
If successful, the return value is true, else, it is false.
Update in the transaction is discarded when it is aborted. The state of the database is rollbacked to before transaction.

tranbegin()

Begin the transaction.
If successful, the return value is true, else, it is false.
The database is locked by the thread while the transaction so that only one transaction can be activated with a database object at the same time. Thus, the serializable isolation level is assumed if every database operation is performed in the transaction. All updated regions are kept track of by write ahead logging while the transaction. If the database is closed during transaction, the transaction is aborted implicitly.

trancommit()

Commit the transaction.
If successful, the return value is true, else, it is false.
Update in the transaction is fixed when it is committed successfully.

tune(bnum, apow, fpow, opts)

Set the tuning parameters.
`bnum' specifies the number of elements of the bucket array. If it is not defined or not more than 0, the default value is specified. The default value is 131071. Suggested size of the bucket array is about from 0.5 to 4 times of the number of all records to be stored.
`apow' specifies the size of record alignment by power of 2. If it is not defined or negative, the default value is specified. The default value is 4 standing for 2^4=16.
`fpow' specifies the maximum number of elements of the free block pool by power of 2. If it is not defined or negative, the default value is specified. The default value is 10 standing for 2^10=1024.
`opts' specifies options by bitwise-or: `TokyoCabinet::TDB::TLARGE' specifies that the size of the database can be larger than 2GB by using 64-bit bucket array, `TokyoCabinet::TDB::TDEFLATE' specifies that each record is compressed with Deflate encoding, `TokyoCabinet::TDB::TDBZIP' specifies that each record is compressed with BZIP2 encoding, `TokyoCabinet::TDB::TTCBS' specifies that each record is compressed with TCBS encoding. If it is not defined, no option is specified.
If successful, the return value is true, else, it is false. Note that the tuning parameters of the database should be set before the database is opened.

vanish()

Remove all records.
If successful, the return value is true, else, it is false.

vsiz(pkey)

Get the size of the value of a record.
`pkey' specifies the primary key.
If successful, the return value is the size of the value of the corresponding record, else, it is -1.