GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages


Manual Reference Pages  -  TOKYOCABINET (3)

.ds Aq ’

NAME

TokyoCabinet - Perl Binding of Tokyo Cabinet

CONTENTS

SYNOPSYS



 use TokyoCabinet;



INTRODUCTION

Tokyo 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. There is neither concept of data tables nor data types. Records are organized in hash table, B+ tree, or fixed-length array.

As for database of hash table, each key must be unique within a database, so it is impossible to store two or more records with a key overlaps. 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, although the order is arbitrary. These access methods are similar to ones of DBM (or its followers: NDBM and GDBM) library defined in the UNIX standard. Tokyo Cabinet is an alternative for DBM because of its higher performance.

As for database of B+ tree, records whose keys are duplicated can be stored. Access methods of storing, deleting, and retrieving are provided as with the database of hash table. Records are stored in order by a comparison function assigned by a user. It is possible to access each record with the cursor in ascending or descending order. According to this mechanism, forward matching search for strings and range search for integers are realized.

As for database of fixed-length array, records are stored with unique natural numbers. It is impossible to store two or more records with a key overlaps. Moreover, the length of each record is limited by the specified length. Provided operations are the same as ones of hash database.

Table database is also provided as a variant of hash database. Each record is identified by the primary key and has a set of named columns. Although there is no concept of data schema, it is possible to search for records with complex conditions efficiently by using indices of arbitrary columns.

    Setting

Install the latest version of Tokyo Cabinet beforehand and get the package of the Perl binding of Tokyo Cabinet.

Enter the directory of the extracted package then perform installation.



 perl Makefile.PL
 make
 make test
 su
 make install



The package ‘TokyoCabinet’ should be loaded in each source file of application programs.



 use TokyoCabinet;



If you want to enable runtime assertion, set the variable ‘$TokyoCabinet::DEBUG’ to be true.



 $TokyoCabinet::DEBUG = 1;



EXAMPLE

The following code is an example to use a hash database.



 use TokyoCabinet;
 use strict;
 use warnings;

 # create the object
 my $hdb = TokyoCabinet::HDB->new();

 # open the database
 if(!$hdb->open("casket.tch", $hdb->OWRITER | $hdb->OCREAT)){
     my $ecode = $hdb->ecode();
     printf STDERR ("open error: %s\n", $hdb->errmsg($ecode));
 }

 # store records
 if(!$hdb->put("foo", "hop") ||
    !$hdb->put("bar", "step") ||
    !$hdb->put("baz", "jump")){
     my $ecode = $hdb->ecode();
     printf STDERR ("put error: %s\n", $hdb->errmsg($ecode));
 }

 # retrieve records
 my $value = $hdb->get("foo");
 if(defined($value)){
     printf("%s\n", $value);
 } else {
     my $ecode = $hdb->ecode();
     printf STDERR ("get error: %s\n", $hdb->errmsg($ecode));
 }

 # traverse records
 $hdb->iterinit();
 while(defined(my $key = $hdb->iternext())){
     my $value = $hdb->get($key);
     if(defined($value)){
         printf("%s:%s\n", $key, $value);
     }
 }

 # close the database
 if(!$hdb->close()){
     my $ecode = $hdb->ecode();
     printf STDERR ("close error: %s\n", $hdb->errmsg($ecode));
 }

 # tying usage
 my %hash;
 if(!tie(%hash, "TokyoCabinet::HDB", "casket.tch", TokyoCabinet::HDB::OWRITER)){
     printf STDERR ("tie error\n");
 }
 $hash{"quux"} = "touchdown";
 printf("%s\n", $hash{"quux"});
 while(my ($key, $value) = each(%hash)){
     printf("%s:%s\n", $key, $value);
 }
 untie(%hash);



The following code is an example to use a B+ tree database.



 use TokyoCabinet;
 use strict;
 use warnings;

 # create the object
 my $bdb = TokyoCabinet::BDB->new();

 # open the database
 if(!$bdb->open("casket.tcb", $bdb->OWRITER | $bdb->OCREAT)){
     my $ecode = $bdb->ecode();
     printf STDERR ("open error: %s\n", $bdb->errmsg($ecode));
 }

 # store records
 if(!$bdb->put("foo", "hop") ||
    !$bdb->put("bar", "step") ||
    !$bdb->put("baz", "jump")){
     my $ecode = $bdb->ecode();
     printf STDERR ("put error: %s\n", $bdb->errmsg($ecode));
 }

 # retrieve records
 my $value = $bdb->get("foo");
 if(defined($value)){
     printf("%s\n", $value);
 } else {
     my $ecode = $bdb->ecode();
     printf STDERR ("get error: %s\n", $bdb->errmsg($ecode));
 }

 # traverse records
 my $cur = TokyoCabinet::BDBCUR->new($bdb);
 $cur->first();
 while(defined(my $key = $cur->key())){
     my $value = $cur->val();
     if(defined($value)){
         printf("%s:%s\n", $key, $value);
     }
     $cur->next();
 }

 # close the database
 if(!$bdb->close()){
     my $ecode = $bdb->ecode();
     printf STDERR ("close error: %s\n", $bdb->errmsg($ecode));
 }

 # tying usage
 my %hash;
 if(!tie(%hash, "TokyoCabinet::BDB", "casket.tcb", TokyoCabinet::BDB::OWRITER)){
     printf STDERR ("tie error\n");
 }
 $hash{"quux"} = "touchdown";
 printf("%s\n", $hash{"quux"});
 while(my ($key, $value) = each(%hash)){
     printf("%s:%s\n", $key, $value);
 }
 untie(%hash);



The following code is an example to use a fixed-length database.



 use TokyoCabinet;
 use strict;
 use warnings;

 # create the object
 my $fdb = TokyoCabinet::FDB->new();

 # open the database
 if(!$fdb->open("casket.tcf", $fdb->OWRITER | $fdb->OCREAT)){
     my $ecode = $fdb->ecode();
     printf STDERR ("open error: %s\n", $fdb->errmsg($ecode));
 }

 # store records
 if(!$fdb->put(1, "one") ||
    !$fdb->put(12, "twelve") ||
    !$fdb->put(144, "one forty four")){
     my $ecode = $fdb->ecode();
     printf STDERR ("put error: %s\n", $fdb->errmsg($ecode));
 }

 # retrieve records
 my $value = $fdb->get(1);
 if(defined($value)){
     printf("%s\n", $value);
 } else {
     my $ecode = $fdb->ecode();
     printf STDERR ("get error: %s\n", $fdb->errmsg($ecode));
 }

 # traverse records
 $fdb->iterinit();
 while(defined(my $key = $fdb->iternext())){
     my $value = $fdb->get($key);
     if(defined($value)){
         printf("%s:%s\n", $key, $value);
     }
 }

 # close the database
 if(!$fdb->close()){
     my $ecode = $fdb->ecode();
     printf STDERR ("close error: %s\n", $fdb->errmsg($ecode));
 }

 # tying usage
 my %hash;
 if(!tie(%hash, "TokyoCabinet::FDB", "casket.tcf", TokyoCabinet::FDB::OWRITER)){
     printf STDERR ("tie error\n");
 }
 $hash{1728} = "seventeen twenty eight";
 printf("%s\n", $hash{1728});
 while(my ($key, $value) = each(%hash)){
     printf("%s:%s\n", $key, $value);
 }
 untie(%hash);



The following code is an example to use a table database.



 use TokyoCabinet;
 use strict;
 use warnings;

 # create the object
 my $tdb = TokyoCabinet::TDB->new();

 # open the database
 if(!$tdb->open("casket.tct", $tdb->OWRITER | $tdb->OCREAT)){
     my $ecode = $tdb->ecode();
     printf STDERR ("open error: %s\n", $tdb->errmsg($ecode));
 }

 # store a record
 my $pkey = $tdb->genuid();
 my $cols = { "name" => "mikio", "age" => "30", "lang" => "ja,en,c" };
 if(!$tdb->put($pkey, $cols)){
     my $ecode = $tdb->ecode();
     printf STDERR ("put error: %s\n", $tdb->errmsg($ecode));
 }

 # store another record
 $cols = { "name" => "falcon", "age" => "31", "lang" => "ja", "skill" => "cook,blog" };
 if(!$tdb->put("x12345", $cols)){
     my $ecode = $tdb->ecode();
     printf STDERR ("put error: %s\n", $tdb->errmsg($ecode));
 }

 # search for records
 my $qry = TokyoCabinet::TDBQRY->new($tdb);
 $qry->addcond("age", $qry->QCNUMGE, "20");
 $qry->addcond("lang", $qry->QCSTROR, "ja,en");
 $qry->setorder("name", $qry->QOSTRASC);
 $qry->setlimit(10);
 my $res = $qry->search();
 foreach my $rkey (@$res){
     my $rcols = $tdb->get($rkey);
     printf("name:%s\n", $rcols->{name});
 }

 # close the database
 if(!$tdb->close()){
     my $ecode = $tdb->ecode();
     printf STDERR ("close error: %s\n", $tdb->errmsg($ecode));
 }

 # tying usage
 my %hash;
 if(!tie(%hash, "TokyoCabinet::TDB", "casket.tct", TokyoCabinet::TDB::OWRITER)){
     printf STDERR ("tie error\n");
 }
 $hash{"joker"} = { "name" => "ozma", "lang" => "en", "skill" => "song,dance" };
 printf("%s\n", $hash{joker}->{name});
 while(my ($key, $value) = each(%hash)){
     printf("%s:%s\n", $key, $value->{name});
 }
 untie(%hash);



The following code is an example to use an abstract database.



 use TokyoCabinet;
 use strict;
 use warnings;

 # create the object
 my $adb = TokyoCabinet::ADB->new();

 # open the database
 if(!$adb->open("casket.tch")){
     printf STDERR ("open error\n");
 }

 # store records
 if(!$adb->put("foo", "hop") ||
    !$adb->put("bar", "step") ||
    !$adb->put("baz", "jump")){
     printf STDERR ("put error\n");
 }

 # retrieve records
 my $value = $adb->get("foo");
 if(defined($value)){
     printf("%s\n", $value);
 } else {
     printf STDERR ("get error\n");
 }

 # traverse records
 $adb->iterinit();
 while(defined(my $key = $adb->iternext())){
     my $value = $adb->get($key);
     if(defined($value)){
         printf("%s:%s\n", $key, $value);
     }
 }

 # close the database
 if(!$adb->close()){
     printf STDERR ("close error\n");
 }

 # tying usage
 my %hash;
 if(!tie(%hash, "TokyoCabinet::ADB", "casket.tch")){
     printf STDERR ("tie error\n");
 }
 $hash{"quux"} = "touchdown";
 printf("%s\n", $hash{"quux"});
 while(my ($key, $value) = each(%hash)){
     printf("%s:%s\n", $key, $value);
 }
 untie(%hash);



DESCRIPTION

    Class TokyoCabinet

The following functions are utilities to handle records by applications.
TokyoCabinet::VERSION()

Get the version information of Tokyo Cabinet.

The return value is the version information.

TokyoCabinet::atoi(str)

Convert a string to an integer.

The return value is the integer value.

TokyoCabinet::atof(str)

Convert a string to a real number.

The return value is the real number value.

TokyoCabinet::bercompress(aryref)

Serialize an array of nonnegative integers with BER encoding.

aryref’ specifies the reference to an array of nonnegative integers.

The return value is the reference to the serialized scalar.

TokyoCabinet::beruncompress(selref)

Deserialize a BER encoded scalar to an array.

selref’ specifies the reference to the BER encoded scalar.

The return value is the reference to the array of nonnegative integers.

TokyoCabinet::diffcompress(aryref)

Serialize an array of sorted nonnegative integers with difference BER encoding.

aryref’ specifies the reference to an array of sorted nonnegative integers.

The return value is the reference to the serialized scalar.

TokyoCabinet::diffuncompress(selref)

Deserialize a difference BER encoded scalar to an array.

selref’ specifies the reference to the BER encoded scalar.

The return value is the reference to the array of sorted nonnegative integers.

TokyoCabinet::strdistance(aref, bref, isutf)

Calculate the edit distance of two strings.

aref’ specifies the reference to a string.

bref’ specifies the reference to the other string.

isutf’ specifies whether the cost is calculated by Unicode character of UTF-8 strings.

The return value is the edit distance which is known as the Levenshtein distance.

    Class TokyoCabinet::HDB

Hash database is a file containing a hash table and is handled with the hash database API. Before operations to store or retrieve records, it is necessary to open a database file and connect the hash database object to it. The method ‘open’ is used to open a database file and the method ‘close’ is used to close the database file. 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.
$hdb = TokyoCabinet::HDB->new()

Create a hash database object.

The return value is the new hash database object.

$hdb->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.

$hdb->ecode()

Get the last happened error code.

The return value is the last happened error code.

The following error codes are defined: ‘$hdb->ESUCCESS’ for success, ‘$hdb->ETHREAD’ for threading error, ‘$hdb->EINVALID’ for invalid operation, ‘$hdb->ENOFILE’ for file not found, ‘$hdb->ENOPERM’ for no permission, ‘$hdb->EMETA’ for invalid meta data, ‘$hdb->ERHEAD’ for invalid record header, ‘$hdb->EOPEN’ for open error, ‘$hdb->ECLOSE’ for close error, ‘$hdb->ETRUNC’ for trunc error, ‘$hdb->ESYNC’ for sync error, ‘$hdb->ESTAT’ for stat error, ‘$hdb->ESEEK’ for seek error, ‘$hdb->EREAD’ for read error, ‘$hdb->EWRITE’ for write error, ‘$hdb->EMMAP’ for mmap error, ‘$hdb->ELOCK’ for lock error, ‘$hdb->EUNLINK’ for unlink error, ‘$hdb->ERENAME’ for rename error, ‘$hdb->EMKDIR’ for mkdir error, ‘$hdb->ERMDIR’ for rmdir error, ‘$hdb->EKEEP’ for existing record, ‘$hdb->ENOREC’ for no record found, and ‘$hdb->EMISC’ for miscellaneous error.

$hdb->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: ‘$hdb->TLARGE’ specifies that the size of the database can be larger than 2GB by using 64-bit bucket array, ‘$hdb->TDEFLATE’ specifies that each record is compressed with Deflate encoding, ‘$hdb->TBZIP’ specifies that each record is compressed with BZIP2 encoding, ‘$hdb->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.

$hdb->setcache(rcnum)

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.

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.

$hdb->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.

$hdb->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.

$hdb->open(path, omode)

Open a database file.

path’ specifies the path of the database file.

omode’ specifies the connection mode: ‘$hdb->OWRITER’ as a writer, ‘$hdb->OREADER’ as a reader. If the mode is ‘$hdb->OWRITER’, the following may be added by bitwise-or: ‘$hdb->OCREAT’, which means it creates a new database if not exist, ‘$hdb->OTRUNC’, which means it creates a new database regardless if one exists, ‘$hdb->OTSYNC’, which means every transaction synchronizes updated contents with the device. Both of ‘$hdb->OREADER’ and ‘$hdb->OWRITER’ can be added to by bitwise-or: ‘$hdb->ONOLCK’, which means it opens the database file without file locking, or ‘$hdb->OLCKNB’, which means locking is performed without blocking. If it is not defined, ‘$hdb->OREADER’ is specified.

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

$hdb->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.

$hdb->put(key, value)

Store a record.

key’ specifies the key.

value’ specifies the value.

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.

$hdb->putkeep(key, value)

Store a new record.

key’ specifies the key.

value’ specifies the value.

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.

$hdb->putcat(key, value)

Concatenate a value at the end of the existing record.

key’ specifies the key.

value’ specifies the value.

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

If there is no corresponding record, a new record is created.

$hdb->putasync(key, value)

Store a record in asynchronous fashion.

key’ specifies the key.

value’ specifies the value.

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. Records passed to this method are accumulated into the inner buffer and wrote into the file at a blast.

$hdb->out(key)

Remove a record.

key’ specifies the key.

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

$hdb->get(key)

Retrieve a record.

key’ specifies the key.

If successful, the return value is the value of the corresponding record. ‘undef’ is returned if no record corresponds.

$hdb->vsiz(key)

Get the size of the value of a record.

key’ specifies the key.

If successful, the return value is the size of the value of the corresponding record, else, it is -1.

$hdb->iterinit()

Initialize the iterator.

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

The iterator is used in order to access the key of every record stored in a database.

$hdb->iternext()

Get the next key of the iterator.

If successful, the return value is the next key, else, it is ‘undef’. ‘undef’ 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.

$hdb->fwmkeys(prefix, max)

Get forward matching 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 the reference to an array of the keys of the corresponding records. This method does never fail. It returns an empty array even if no record corresponds.

Note that this method may be very slow because every key in the database is scanned.

$hdb->addint(key, num)

Add an integer to a record.

key’ specifies the key.

num’ specifies the additional value.

If successful, the return value is the summation value, else, it is ‘undef’.

If the corresponding record exists, the value is treated as an integer and is added to. If no record corresponds, a new record of the additional value is stored. Because records are stored in binary format, they should be processed with the ‘unpack’ function with the ‘i’ operator after retrieval.

$hdb->adddouble(key, num)

Add a real number to a record.

key’ specifies the key.

num’ specifies the additional value.

If successful, the return value is the summation value, else, it is ‘undef’.

If the corresponding record exists, the value is treated as a real number and is added to. If no record corresponds, a new record of the additional value is stored. Because records are stored in binary format, they should be processed with the ‘unpack’ function with the ‘d’ operator after retrieval.

$hdb->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.

$hdb->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: ‘$hdb->TLARGE’ specifies that the size of the database can be larger than 2GB by using 64-bit bucket array, ‘$hdb->TDEFLATE’ specifies that each record is compressed with Deflate encoding, ‘$hdb->TBZIP’ specifies that each record is compressed with BZIP2 encoding, ‘$hdb->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.

$hdb->vanish()

Remove all records.

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

$hdb->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.

$hdb->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.

$hdb->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.

$hdb->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.

$hdb->path()

Get the path of the database file.

The return value is the path of the database file or ‘undef’ if the object does not connect to any database file.

$hdb->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.

$hdb->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.

    Tying functions of TokyoCabinet::HDB

tie(%hash, ‘‘TokyoCabinet::HDB’’, path, omode, bnum, apow, fpow, opts, rcnum)

Tie a hash variable to a hash database file.

path’ specifies the path of the database file.

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

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::HDB::TLARGE’ specifies that the size of the database can be larger than 2GB by using 64-bit bucket array, ‘TokyoCabinet::HDB::TDEFLATE’ specifies that each record is compressed with Deflate encoding, ‘TokyoCabinet::HDB::TBZIP’ specifies that each record is compressed with BZIP2 encoding, ‘TokyoCabinet::HDB::TTCBS’ specifies that each record is compressed with TCBS encoding. If it is not defined, no option is specified.

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.

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

untie(%hash)

Untie a hash variable from the database file.

The return value is always true.

$hash{key} = value

Store a record.

key’ specifies the key.

value’ specifies the value.

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.

delete($hash{key})

Remove a record.

key’ specifies the key.

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

$hash{key}

Retrieve a record.

key’ specifies the key.

If successful, the return value is the value of the corresponding record. ‘undef’ is returned if no record corresponds.

exists($hash{key})

Check whether a record corrsponding a key exists.

key’ specifies the key.

The return value is true if the record exists, else it is false.

$hash = ()

Remove all records.

The return value is always ‘undef’.

(the iterator)

The inner methods ‘FIRSTKEY’ and ‘NEXTKEY’ are also implemented so that you can use the tying functions ‘each’, ‘keys’, and so on.

    Class TokyoCabinet::BDB

B+ tree database is a file containing a B+ tree and is handled with the B+ tree database API. Before operations to store or retrieve records, it is necessary to open a database file and connect the B+ tree database object to it. The method ‘open’ is used to open a database file and the method ‘close’ is used to close the database file. 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.
$bdb = TokyoCabinet::BDB->new()

Create a B+ tree database object.

The return value is the new B+ tree database object.

$bdb->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.

$bdb->ecode()

Get the last happened error code.

The return value is the last happened error code.

The following error codes are defined: ‘$bdb->ESUCCESS’ for success, ‘$bdb->ETHREAD’ for threading error, ‘$bdb->EINVALID’ for invalid operation, ‘$bdb->ENOFILE’ for file not found, ‘$bdb->ENOPERM’ for no permission, ‘$bdb->EMETA’ for invalid meta data, ‘$bdb->ERHEAD’ for invalid record header, ‘$bdb->EOPEN’ for open error, ‘$bdb->ECLOSE’ for close error, ‘$bdb->ETRUNC’ for trunc error, ‘$bdb->ESYNC’ for sync error, ‘$bdb->ESTAT’ for stat error, ‘$bdb->ESEEK’ for seek error, ‘$bdb->EREAD’ for read error, ‘$bdb->EWRITE’ for write error, ‘$bdb->EMMAP’ for mmap error, ‘$bdb->ELOCK’ for lock error, ‘$bdb->EUNLINK’ for unlink error, ‘$bdb->ERENAME’ for rename error, ‘$bdb->EMKDIR’ for mkdir error, ‘$bdb->ERMDIR’ for rmdir error, ‘$bdb->EKEEP’ for existing record, ‘$bdb->ENOREC’ for no record found, and ‘$bdb->EMISC’ for miscellaneous error.

$bdb->setcmpfunc(cmp)

Set the custom comparison function.

cmp’ specifies the custom comparison function. It can be either the reference of a block or the name of a function.

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

The default comparison function compares keys of two records by lexical order. The constants ‘$bdb->CMPLEXICAL’ (dafault), ‘$bdb->CMPDECIMAL’, ‘$bdb->CMPINT32’, and ‘$bdb->CMPINT64’ are built-in. Note that the comparison function should be set before the database is opened. Moreover, user-defined comparison functions should be set every time the database is being opened.

$bdb->tune(lmemb, nmemb, bnum, apow, fpow, opts)

Set the tuning parameters.

lmemb’ specifies the number of members in each leaf page. If it is not defined or not more than 0, the default value is specified. The default value is 128.

nmemb’ specifies the number of members in each non-leaf page. If it is not defined or not more than 0, the default value is specified. The default value is 256.

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 32749. Suggested size of the bucket array is about from 1 to 4 times of the number of all pages 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^8=256.

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: ‘$bdb->TLARGE’ specifies that the size of the database can be larger than 2GB by using 64-bit bucket array, ‘$bdb->TDEFLATE’ specifies that each record is compressed with Deflate encoding, ‘$bdb->TBZIP’ specifies that each record is compressed with BZIP2 encoding, ‘$bdb->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.

$bdb->setcache(lcnum, ncnum)

Set the caching parameters.

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 1024.

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.

$bdb->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. It is disabled by default.

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

Note that the mapping parameters should be set before the database is opened.

$bdb->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.

$bdb->open(path, omode)

Open a database file.

path’ specifies the path of the database file.

omode’ specifies the connection mode: ‘$bdb->OWRITER’ as a writer, ‘$bdb->OREADER’ as a reader. If the mode is ‘$bdb->OWRITER’, the following may be added by bitwise-or: ‘$bdb->OCREAT’, which means it creates a new database if not exist, ‘$bdb->OTRUNC’, which means it creates a new database regardless if one exists, ‘$bdb->OTSYNC’, which means every transaction synchronizes updated contents with the device. Both of ‘$bdb->OREADER’ and ‘$bdb->OWRITER’ can be added to by bitwise-or: ‘$bdb->ONOLCK’, which means it opens the database file without file locking, or ‘$bdb->OLCKNB’, which means locking is performed without blocking. If it is not defined, ‘$bdb->OREADER’ is specified.

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

$bdb->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.

$bdb->put(key, value)

Store a record.

key’ specifies the key.

value’ specifies the value.

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.

$bdb->putkeep(key, value)

Store a new record.

key’ specifies the key.

value’ specifies the value.

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.

$bdb->putcat(key, value)

Concatenate a value at the end of the existing record.

key’ specifies the key.

value’ specifies the value.

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

If there is no corresponding record, a new record is created.

$bdb->putdup(key, value)

Store a record with allowing duplication of keys.

key’ specifies the key.

value’ specifies the value.

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

If a record with the same key exists in the database, the new record is placed after the existing one.

$bdb->putlist(key, values)

Store records with allowing duplication of keys.

key’ specifies the key.

values’ specifies the reference to an array of the values.

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

If a record with the same key exists in the database, the new records are placed after the existing one.

$bdb->out(key)

Remove a record.

key’ specifies the key.

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

If the key of duplicated records is specified, the first one is selected.

$bdb->outlist(key)

Remove records.

key’ specifies the key.

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

If the key of duplicated records is specified, all of them are removed.

$bdb->get(key)

Retrieve a record.

key’ specifies the key.

If successful, the return value is the value of the corresponding record. ‘undef’ is returned if no record corresponds.

If the key of duplicated records is specified, the first one is selected.

$bdb->getlist(key)

Retrieve records.

key’ specifies the key.

If successful, the return value is the reference to an array of the values of the corresponding records. ‘undef’ is returned if no record corresponds.

$bdb->vnum(key)

Get the number of records corresponding a key.

key’ specifies the key.

If successful, the return value is the number of the corresponding records, else, it is 0.

$bdb->vsiz(key)

Get the size of the value of a record.

key’ specifies the key.

If successful, the return value is the size of the value of the corresponding record, else, it is -1.

If the key of duplicated records is specified, the first one is selected.

$bdb->range(bkey, binc, ekey, einc, max)

Get keys of ranged records.

bkey’ specifies the key of the beginning border. If it is not defined, the first record is specified.

binc’ specifies whether the beginning border is inclusive or not. If it is not defined, false is specified.

ekey’ specifies the key of the ending border. If it is not defined, the last record is specified.

einc’ specifies whether the ending border is inclusive or not. If it is not defined, false is specified.

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 the reference to an array of the keys of the corresponding records. This method does never fail. It returns an empty array even if no record corresponds.

$bdb->fwmkeys(prefix, max)

Get forward matching 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 the reference to an array of the keys of the corresponding records. This method does never fail. It returns an empty array even if no record corresponds.

$bdb->addint(key, num)

Add an integer to a record.

key’ specifies the key.

num’ specifies the additional value.

If successful, the return value is the summation value, else, it is ‘undef’.

If the corresponding record exists, the value is treated as an integer and is added to. If no record corresponds, a new record of the additional value is stored. Because records are stored in binary format, they should be processed with the ‘unpack’ function with the ‘i’ operator after retrieval.

$bdb->adddouble(key, num)

Add a real number to a record.

key’ specifies the key.

num’ specifies the additional value.

If successful, the return value is the summation value, else, it is ‘undef’.

If the corresponding record exists, the value is treated as a real number and is added to. If no record corresponds, a new record of the additional value is stored. Because records are stored in binary format, they should be processed with the ‘unpack’ function with the ‘d’ operator after retrieval.

$bdb->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.

$bdb->optimize(lmemb, nmemb, bnum, apow, fpow, opts)

Optimize the database file.

lmemb’ specifies the number of members in each leaf page. If it is not defined or not more than 0, the current setting is not changed.

nmemb’ specifies the number of members in each non-leaf page. If it is not defined or not more than 0, the current setting is not changed.

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 pages.

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: ‘$bdb->TLARGE’ specifies that the size of the database can be larger than 2GB by using 64-bit bucket array, ‘$bdb->TDEFLATE’ specifies that each record is compressed with Deflate encoding, ‘$bdb->TBZIP’ specifies that each record is compressed with BZIP2 encoding, ‘$bdb->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.

$bdb->vanish()

Remove all records.

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

$bdb->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.

$bdb->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. Because all pages are cached on memory while the transaction, the amount of referred records is limited by the memory capacity. If the database is closed during transaction, the transaction is aborted implicitly.

$bdb->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.

$bdb->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.

$bdb->path()

Get the path of the database file.

The return value is the path of the database file or ‘undef’ if the object does not connect to any database file.

$bdb->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.

$bdb->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.

    Class TokyoCabinet::BDBCUR

$cur = TokyoCabinet::BDBCUR->new(bdb)

Create a cursor object.

bdb’ specifies the B+ tree database object.

The return value is the new cursor object.

Note that the cursor is available only after initialization with the ‘first’ or the ‘jump’ methods and so on. Moreover, the position of the cursor will be indefinite when the database is updated after the initialization of the cursor.

$cur->first()

Move the cursor to the first record.

If successful, the return value is true, else, it is false. False is returned if there is no record in the database.

$cur->last()

Move the cursor to the last record.

If successful, the return value is true, else, it is false. False is returned if there is no record in the database.

$cur->jump(key)

Move the cursor to the front of records corresponding a key.

key’ specifies the key.

If successful, the return value is true, else, it is false. False is returned if there is no record corresponding the condition.

The cursor is set to the first record corresponding the key or the next substitute if completely matching record does not exist.

$cur->prev()

Move the cursor to the previous record.

If successful, the return value is true, else, it is false. False is returned if there is no previous record.

$cur->next()

Move the cursor to the next record.

If successful, the return value is true, else, it is false. False is returned if there is no next record.

$cur->put(value, cpmode)

Insert a record around the cursor.

value’ specifies the value.

cpmode’ specifies detail adjustment: ‘$cur->CPCURRENT’, which means that the value of the current record is overwritten, ‘$cur->CPBEFORE’, which means that the new record is inserted before the current record, ‘$cur->CPAFTER’, which means that the new record is inserted after the current record.

If successful, the return value is true, else, it is false. False is returned when the cursor is at invalid position.

After insertion, the cursor is moved to the inserted record.

$cur->out()

Remove the record where the cursor is.

If successful, the return value is true, else, it is false. False is returned when the cursor is at invalid position.

After deletion, the cursor is moved to the next record if possible.

$cur->key()

Get the key of the record where the cursor is.

If successful, the return value is the key, else, it is ‘undef’. ’undef’ is returned when the cursor is at invalid position.

$cur->val()

Get the value of the record where the cursor is.

If successful, the return value is the value, else, it is ‘undef’. ’undef’ is returned when the cursor is at invalid position.

    Tying functions of TokyoCabinet::BDB

tie(%hash, ‘‘TokyoCabinet::BDB’’, path, omode, lmemb, nmemb, bnum, apow, fpow, opts, lcnum, ncnum)

Tie a hash variable to a B+ tree database file.

path’ specifies the path of the database file.

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

lmemb’ specifies the number of members in each leaf page. If it is not defined or not more than 0, the default value is specified. The default value is 128.

nmemb’ specifies the number of members in each non-leaf page. If it is not defined or not more than 0, the default value is specified. The default value is 256.

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 32749.

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^8=256.

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::BDB::TLARGE’ specifies that the size of the database can be larger than 2GB by using 64-bit bucket array, ‘TokyoCabinet::BDB::TDEFLATE’ specifies that each record is compressed with Deflate encoding, ‘TokyoCabinet::BDB::TBZIP’ specifies that each record is compressed with BZIP2 encoding, ‘TokyoCabinet::BDB::TTCBS’ specifies that each record is compressed with TCBS encoding. If it is not defined, no option is specified.

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.

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.

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

untie(%hash)

Untie a hash variable from the database file.

The return value is always true.

$hash{key} = value

Store a record.

key’ specifies the key.

value’ specifies the value.

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.

delete($hash{key})

Remove a record.

key’ specifies the key.

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

$hash{key}

Retrieve a record.

key’ specifies the key.

If successful, the return value is the value of the corresponding record. ‘undef’ is returned if no record corresponds.

exists($hash{key})

Check whether a record corrsponding a key exists.

key’ specifies the key.

The return value is true if the record exists, else it is false.

$hash = ()

Remove all records.

The return value is always ‘undef’.

(the iterator)

The inner methods ‘FIRSTKEY’ and ‘NEXTKEY’ are also implemented so that you can use the tying functions ‘each’, ‘keys’, and so on.

    Class TokyoCabinet::FDB

Fixed-length database is a file containing an array of fixed-length elements and is handled with the fixed-length database API. Before operations to store or retrieve records, it is necessary to open a database file and connect the fixed-length database object to it. The method ‘open’ is used to open a database file and the method ‘close’ is used to close the database file. 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.
$fdb = TokyoCabinet::FDB->new()

Create a fixed-length database object.

The return value is the new fixed-length database object.

$fdb->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.

$fdb->ecode()

Get the last happened error code.

The return value is the last happened error code.

The following error codes are defined: ‘$fdb->ESUCCESS’ for success, ‘$fdb->ETHREAD’ for threading error, ‘$fdb->EINVALID’ for invalid operation, ‘$fdb->ENOFILE’ for file not found, ‘$fdb->ENOPERM’ for no permission, ‘$fdb->EMETA’ for invalid meta data, ‘$fdb->ERHEAD’ for invalid record header, ‘$fdb->EOPEN’ for open error, ‘$fdb->ECLOSE’ for close error, ‘$fdb->ETRUNC’ for trunc error, ‘$fdb->ESYNC’ for sync error, ‘$fdb->ESTAT’ for stat error, ‘$fdb->ESEEK’ for seek error, ‘$fdb->EREAD’ for read error, ‘$fdb->EWRITE’ for write error, ‘$fdb->EMMAP’ for mmap error, ‘$fdb->ELOCK’ for lock error, ‘$fdb->EUNLINK’ for unlink error, ‘$fdb->ERENAME’ for rename error, ‘$fdb->EMKDIR’ for mkdir error, ‘$fdb->ERMDIR’ for rmdir error, ‘$fdb->EKEEP’ for existing record, ‘$fdb->ENOREC’ for no record found, and ‘$fdb->EMISC’ for miscellaneous error.

$fdb->tune(width, limsiz);

Set the tuning parameters.

width’ specifies the width of the value of each record. If it is not defined or not more than 0, the default value is specified. The default value is 255.

limsiz’ specifies the limit size of the database file. If it is not defined or not more than 0, the default value is specified. The default value is 268435456.

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.

$fdb->open(path, omode)

Open a database file.

path’ specifies the path of the database file.

omode’ specifies the connection mode: ‘$fdb->OWRITER’ as a writer, ‘$fdb->OREADER’ as a reader. If the mode is ‘$fdb->OWRITER’, the following may be added by bitwise-or: ‘$fdb->OCREAT’, which means it creates a new database if not exist, ‘$fdb->OTRUNC’, which means it creates a new database regardless if one exists. Both of ‘$fdb->OREADER’ and ‘$fdb->OWRITER’ can be added to by bitwise-or: ‘$fdb->ONOLCK’, which means it opens the database file without file locking, or ‘$fdb->OLCKNB’, which means locking is performed without blocking. If it is not defined, ‘$fdb->OREADER’ is specified.

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

$fdb->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.

$fdb->put(key, value)

Store a record.

key’ specifies the key. It should be more than 0. If it is min, the minimum ID number of existing records is specified. If it is prev, the number less by one than the minimum ID number of existing records is specified. If it is max, the maximum ID number of existing records is specified. If it is next, the number greater by one than the maximum ID number of existing records is specified.

value’ specifies the value.

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.

$fdb->putkeep(key, value)

Store a new record.

key’ specifies the key. It should be more than 0. If it is min, the minimum ID number of existing records is specified. If it is prev, the number less by one than the minimum ID number of existing records is specified. If it is max, the maximum ID number of existing records is specified. If it is next, the number greater by one than the maximum ID number of existing records is specified.

value’ specifies the value.

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.

$fdb->putcat(key, value)

Concatenate a value at the end of the existing record.

key’ specifies the key. It should be more than 0. If it is min, the minimum ID number of existing records is specified. If it is prev, the number less by one than the minimum ID number of existing records is specified. If it is max, the maximum ID number of existing records is specified. If it is next, the number greater by one than the maximum ID number of existing records is specified.

value’ specifies the value.

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

If there is no corresponding record, a new record is created.

$fdb->out(key)

Remove a record.

key’ specifies the key. It should be more than 0. If it is min, the minimum ID number of existing records is specified. If it is max, the maximum ID number of existing records is specified.

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

$fdb->get(key)

Retrieve a record.

key’ specifies the key. It should be more than 0. If it is min, the minimum ID number of existing records is specified. If it is max, the maximum ID number of existing records is specified.

If successful, the return value is the value of the corresponding record. ‘undef’ is returned if no record corresponds.

$fdb->vsiz(key)

Get the size of the value of a record.

key’ specifies the key. It should be more than 0. If it is min, the minimum ID number of existing records is specified. If it is max, the maximum ID number of existing records is specified.

If successful, the return value is the size of the value of the corresponding record, else, it is -1.

$fdb->iterinit()

Initialize the iterator.

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

The iterator is used in order to access the key of every record stored in a database.

$fdb->iternext()

Get the next key of the iterator.

If successful, the return value is the next key, else, it is ‘undef’. ‘undef’ 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. The order of this traversal access method is ascending of the ID number.

$fdb->range(interval, max)

Get keys with an interval notation.

interval’ specifies the interval notation.

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 the reference to an array of the keys of the corresponding records. This method does never fail. It returns an empty array even if no record corresponds.

$fdb->addint(key, num)

Add an integer to a record.

key’ specifies the key. It should be more than 0. If it is min, the minimum ID number of existing records is specified. If it is prev, the number less by one than the minimum ID number of existing records is specified. If it is max, the maximum ID number of existing records is specified. If it is next, the number greater by one than the maximum ID number of existing records is specified.

num’ specifies the additional value.

If successful, the return value is the summation value, else, it is ‘undef’.

If the corresponding record exists, the value is treated as an integer and is added to. If no record corresponds, a new record of the additional value is stored. Because records are stored in binary format, they should be processed with the ‘unpack’ function with the ‘i’ operator after retrieval.

$fdb->adddouble(key, num)

Add a real number to a record.

key’ specifies the key. It should be more than 0. If it is min, the minimum ID number of existing records is specified. If it is prev, the number less by one than the minimum ID number of existing records is specified. If it is max, the maximum ID number of existing records is specified. If it is next, the number greater by one than the maximum ID number of existing records is specified.

num’ specifies the additional value.

If successful, the return value is the summation value, else, it is ‘undef’.

If the corresponding record exists, the value is treated as a real number and is added to. If no record corresponds, a new record of the additional value is stored. Because records are stored in binary format, they should be processed with the ‘unpack’ function with the ‘d’ operator after retrieval.

$fdb->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.

$fdb->optimize(width, limsiz)

Optimize the database file.

width’ specifies the width of the value of each record. If it is not defined or not more than 0, the current setting is not changed.

limsiz’ specifies the limit size of the database file. If it is not defined or not more than 0, the current setting is not changed.

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

$fdb->vanish()

Remove all records.

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

$fdb->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.

$fdb->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.

$fdb->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.

$fdb->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.

$fdb->path()

Get the path of the database file.

The return value is the path of the database file or ‘undef’ if the object does not connect to any database file.

$fdb->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.

$fdb->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.

    Tying functions of TokyoCabinet::FDB

tie(%hash, ‘‘TokyoCabinet::FDB’’, path, omode, width, limsiz)

Tie a hash variable to a hash database file.

path’ specifies the path of the database file.

omode’ specifies the connection mode: ‘TokyoCabinet::FDB::OWRITER’ as a writer, ‘TokyoCabinet::FDB::OREADER’ as a reader. If the mode is ‘TokyoCabinet::FDB::OWRITER’, the following may be added by bitwise-or: ‘TokyoCabinet::FDB::OCREAT’, which means it creates a new database if not exist, ‘TokyoCabinet::FDB::OTRUNC’, which means it creates a new database regardless if one exists. Both of ‘TokyoCabinet::FDB::OREADER’ and ‘TokyoCabinet::FDB::OWRITER’ can be added to by bitwise-or: ‘TokyoCabinet::FDB::ONOLCK’, which means it opens the database file without file locking, or ‘TokyoCabinet::FDB::OLCKNB’, which means locking is performed without blocking. If it is not defined, ‘TokyoCabinet::FDB::OREADER’ is specified.

width’ specifies the width of the value of each record. If it is not defined or not more than 0, the default value is specified. The default value is 255.

limsiz’ specifies the limit size of the database file. If it is not defined or not more than 0, the default value is specified. The default value is 268435456.

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

untie(%hash)

Untie a hash variable from the database file.

The return value is always true.

$hash{key} = value

Store a record.

key’ specifies the key. It should be more than 0. If it is min, the minimum ID number of existing records is specified. If it is prev, the number less by one than the minimum ID number of existing records is specified. If it is max, the maximum ID number of existing records is specified. If it is next, the number greater by one than the maximum ID number of existing records is specified.

value’ specifies the value.

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.

delete($hash{key})

Remove a record.

key’ specifies the key. It should be more than 0. If it is min, the minimum ID number of existing records is specified. If it is max, the maximum ID number of existing records is specified.

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

$hash{key}

Retrieve a record.

key’ specifies the key. It should be more than 0. If it is min, the minimum ID number of existing records is specified. If it is max, the maximum ID number of existing records is specified.

If successful, the return value is the value of the corresponding record. ‘undef’ is returned if no record corresponds.

exists($hash{key})

Check whether a record corrsponding a key exists.

key’ specifies the key. It should be more than 0. If it is min, the minimum ID number of existing records is specified. If it is max, the maximum ID number of existing records is specified.

The return value is true if the record exists, else it is false.

$hash = ()

Remove all records.

The return value is always ‘undef’.

(the iterator)

The inner methods ‘FIRSTKEY’ and ‘NEXTKEY’ are also implemented so that you can use the tying functions ‘each’, ‘keys’, and so on.

    Class TokyoCabinet::TDB

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. The method ‘open’ is used to open a database file and the method ‘close’ is used to close the database file. 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.
$tdb = TokyoCabinet::TDB->new()

Create a table database object.

The return value is the new table database object.

$tdb->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.

$tdb->ecode()

Get the last happened error code.

The return value is the last happened error code.

The following error codes are defined: ‘$tdb->ESUCCESS’ for success, ‘$tdb->ETHREAD’ for threading error, ‘$tdb->EINVALID’ for invalid operation, ‘$tdb->ENOFILE’ for file not found, ‘$tdb->ENOPERM’ for no permission, ‘$tdb->EMETA’ for invalid meta data, ‘$tdb->ERHEAD’ for invalid record header, ‘$tdb->EOPEN’ for open error, ‘$tdb->ECLOSE’ for close error, ‘$tdb->ETRUNC’ for trunc error, ‘$tdb->ESYNC’ for sync error, ‘$tdb->ESTAT’ for stat error, ‘$tdb->ESEEK’ for seek error, ‘$tdb->EREAD’ for read error, ‘$tdb->EWRITE’ for write error, ‘$tdb->EMMAP’ for mmap error, ‘$tdb->ELOCK’ for lock error, ‘$tdb->EUNLINK’ for unlink error, ‘$tdb->ERENAME’ for rename error, ‘$tdb->EMKDIR’ for mkdir error, ‘$tdb->ERMDIR’ for rmdir error, ‘$tdb->EKEEP’ for existing record, ‘$tdb->ENOREC’ for no record found, and ‘$tdb->EMISC’ for miscellaneous error.

$tdb->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: ‘$tdb->TLARGE’ specifies that the size of the database can be larger than 2GB by using 64-bit bucket array, ‘$tdb->TDEFLATE’ specifies that each record is compressed with Deflate encoding, ‘$tdb->TBZIP’ specifies that each record is compressed with BZIP2 encoding, ‘$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.

$tdb->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.

$tdb->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.

$tdb->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.

$tdb->open(path, omode)

Open a database file.

path’ specifies the path of the database file.

omode’ specifies the connection mode: ‘$tdb->OWRITER’ as a writer, ‘$tdb->OREADER’ as a reader. If the mode is ‘$tdb->OWRITER’, the following may be added by bitwise-or: ‘$tdb->OCREAT’, which means it creates a new database if not exist, ‘$tdb->OTRUNC’, which means it creates a new database regardless if one exists, ‘$tdb->OTSYNC’, which means every transaction synchronizes updated contents with the device. Both of ‘$tdb->OREADER’ and ‘$tdb->OWRITER’ can be added to by bitwise-or: ‘$tdb->ONOLCK’, which means it opens the database file without file locking, or ‘$tdb->OLCKNB’, which means locking is performed without blocking. If it is not defined, ‘$tdb->OREADER’ is specified.

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

$tdb->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.

$tdb->put(pkey, cols)

Store a record.

pkey’ specifies the primary key.

cols’ specifies the reference to 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.

$tdb->putkeep(pkey, cols)

Store a new record.

pkey’ specifies the primary key.

cols’ specifies the reference to 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.

$tdb->putcat(pkey, cols)

Concatenate columns of the existing record.

pkey’ specifies the primary key.

cols’ specifies the reference to 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.

$tdb->out(pkey)

Remove a record.

pkey’ specifies the primary key.

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

$tdb->get(pkey)

Retrieve a record.

pkey’ specifies the primary key.

If successful, the return value is the reference to a hash of the columns of the corresponding record. ‘undef’ is returned if no record corresponds.

$tdb->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.

$tdb->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.

$tdb->iternext()

Get the next primary key of the iterator.

If successful, the return value is the next primary key, else, it is ‘undef’. ‘undef’ 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.

$tdb->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 the reference to an array of the keys of the corresponding records. This method does never fail. It returns an empty array even if no record corresponds.

Note that this method may be very slow because every key in the database is scanned.

$tdb->addint(pkey, num)

Add an integer to a record.

pkey’ specifies primary key.

num’ specifies the additional value.

If successful, the return value is the summation value, else, it is ‘undef’.

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.

$tdb->adddouble(pkey, num)

Add a real number to a record.

pkey’ specifies primary key.

num’ specifies the additional value.

If successful, the return value is the summation value, else, it is ‘undef’.

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.

$tdb->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.

$tdb->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: ‘$tdb->TLARGE’ specifies that the size of the database can be larger than 2GB by using 64-bit bucket array, ‘$tdb->TDEFLATE’ specifies that each record is compressed with Deflate encoding, ‘$tdb->TBZIP’ specifies that each record is compressed with BZIP2 encoding, ‘$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.

$tdb->vanish()

Remove all records.

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

$tdb->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.

$tdb->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.

$tdb->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.

$tdb->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.

$tdb->path()

Get the path of the database file.

The return value is the path of the database file or ‘undef’ if the object does not connect to any database file.

$tdb->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.

$tdb->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.

$tdb->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: ‘$tdb->ITLEXICAL’ for lexical string, ‘$tdb->ITDECIMAL’ for decimal string, ‘$tdb->ITTOKEN’ for token inverted index, ‘$tdb->ITQGRAM’ for q-gram inverted index. If it is ‘$tdb->ITOPT’, the index is optimized. If it is ‘$tdb->ITVOID’, the index is removed. If ‘$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.

$tdb->genuid()

Generate a unique ID number.

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

    Class TokyoCabinet::TDBQRY

$qry = TokyoCabinet::TDBQRY->new(tdb)

Create a query object.

tdb’ specifies the table database object.

The return value is the new query object.

$qry->addcond(name, op, expr)

Add a narrowing condition.

name’ specifies the name of a column. An empty string means the primary key.

op’ specifies an operation type: ‘$qry->QCSTREQ’ for string which is equal to the expression, ‘$qry->QCSTRINC’ for string which is included in the expression, ‘$qry->QCSTRBW’ for string which begins with the expression, ‘$qry->QCSTREW’ for string which ends with the expression, ‘$qry->QCSTRAND’ for string which includes all tokens in the expression, ‘$qry->QCSTROR’ for string which includes at least one token in the expression, ‘$qry->QCSTROREQ’ for string which is equal to at least one token in the expression, ‘$qry->QCSTRRX’ for string which matches regular expressions of the expression, ‘$qry->QCNUMEQ’ for number which is equal to the expression, ‘$qry->QCNUMGT’ for number which is greater than the expression, ‘$qry->QCNUMGE’ for number which is greater than or equal to the expression, ‘$qry->QCNUMLT’ for number which is less than the expression, ‘$qry->QCNUMLE’ for number which is less than or equal to the expression, ‘$qry->QCNUMBT’ for number which is between two tokens of the expression, ‘$qry->QCNUMOREQ’ for number which is equal to at least one token in the expression, ‘$qry->QCFTSPH’ for full-text search with the phrase of the expression, ‘$qry->QCFTSAND’ for full-text search with all tokens in the expression, ‘$qry->QCFTSOR’ for full-text search with at least one token in the expression, ‘$qry->QCFTSEX’ for full-text search with the compound expression. All operations can be flagged by bitwise-or: ‘$qry->QCNEGATE’ for negation, ‘$qry->QCNOIDX’ for using no index.

expr’ specifies an operand exression.

The return value is always ‘undef’.

$qry->setorder(name, type)

Set the order of the result.

name’ specifies the name of a column. An empty string means the primary key.

type’ specifies the order type: ‘$qry->QOSTRASC’ for string ascending, ‘$qry->QOSTRDESC’ for string descending, ‘$qry->QONUMASC’ for number ascending, ‘$qry->QONUMDESC’ for number descending. If it is not defined, ‘$qry->QOSTRASC’ is specified.

The return value is always ‘undef’.

$qry->setlimit(max, skip)

Set the maximum number of records of the result.

max’ specifies the maximum number of records of the result. If it is not defined or negative, no limit is specified.

skip’ specifies the number of skipped records of the result. If it is not defined or not more than 0, no record is skipped.

The return value is always ‘undef’.

$qry->search()

Execute the search.

The return value is the reference to an array of the primary keys of the corresponding records. This method does never fail. It returns an empty array even if no record corresponds.

$qry->searchout()

Remove each corresponding record.

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

$qry->proc(proc)

Process each corresponding record.

proc’ specifies the iterator function called for each record. It can be either the reference of a block or the name of a function. The function receives two parameters. The first parameter is the primary key. The second parameter is the reference to a hash containing columns. It returns flags of the post treatment by bitwise-or: ‘$qry->QPPUT’ to modify the record, ‘$qry->QPOUT’ to remove the record, ‘$qry->QPSTOP’ to stop the iteration.

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

$qry->hint()

Get the hint string.

The return value is the hint string.

$qry->metasearch(others, type)

Retrieve records with multiple query objects and get the set of the result.

others’ specifies the reference to an array of the query objects except for the self object.

type’ specifies a set operation type: ‘$qry->MSUNION’ for the union set, ‘$qry->MSISECT’ for the intersection set, ‘$qry->MSDIFF’ for the difference set. If it is not defined, ‘$qry->MSUNION’ is specified.

The return value is the reference to an array of the primary keys of the corresponding records. This method does never fail. It returns an empty array even if no record corresponds.

If the first query object has the order setting, the result array is sorted by the order.

$qry->kwic(cols, name, width, opts)

Generate keyword-in-context strings.

cols’ specifies the reference to a hash containing columns.

name’ specifies the name of a column. If it is not defined, the first column of the query is specified.

width’ specifies the width of strings picked up around each keyword. If it is not defined or negative, the whole text is picked up.

opts’ specifies options by bitwise-or: ‘$qry->KWMUTAB’ specifies that each keyword is marked up between two tab characters, ‘$qry->KWMUCTRL’ specifies that each keyword is marked up by the STX (0x02) code and the ETX (0x03) code, ‘$qry->KWMUBRCT’ specifies that each keyword is marked up by the two square brackets, ‘$qry->KWNOOVER’ specifies that each context does not overlap, ‘$qry->KWPULEAD’ specifies that the lead string is picked up forcibly. If it is not defined, no option is specified.

The return value is the reference to an array of strings around keywords.

    Tying functions of TokyoCabinet::TDB

tie(%hash, ‘‘TokyoCabinet::TDB’’, path, omode, bnum, apow, fpow, opts, rcnum, lcnum, ncnum)

Tie a hash variable to a table 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.

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::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, no option is specified.

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 2048.

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.

untie(%hash)

Untie a hash variable from the database file.

The return value is always true.

$hash{pkey} = cols

Store a record.

pkey’ specifies primary key.

cols’ specifies the reference to 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.

delete($hash{pkey})

Remove a record.

pkey’ specifies primary key.

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

$hash{pkey}

Retrieve a record.

pkey’ specifies primary key.

If successful, the return value is the reference to a hash of the columns of the corresponding record. ‘undef’ is returned if no record corresponds.

exists($hash{pkey})

Check whether a record corrsponding a key exists.

pkey’ specifies primary key.

The return value is true if the record exists, else it is false.

$hash = ()

Remove all records.

The return value is always ‘undef’.

(the iterator)

The inner methods ‘FIRSTKEY’ and ‘NEXTKEY’ are also implemented so that you can use the tying functions ‘each’, ‘keys’, and so on.

    Class TokyoCabinet::ADB

Abstract database is a set of interfaces to use on-memory hash database, on-memory tree database, hash database, B+ tree database, fixed-length database, and table database with the same API. Before operations to store or retrieve records, it is necessary to connect the abstract database object to the concrete one. The method ‘open’ is used to open a concrete database and the method ‘close’ is used to close the database. To avoid data missing or corruption, it is important to close every database instance 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.
$adb = TokyoCabinet::ADB->new()

Create an abstract database object.

The return value is the new abstract database object.

$adb->open(name)

Open a database.

name’ specifies the name of the database. If it is *, the database will be an on-memory hash database. If it is +, the database will be an on-memory tree database. If its suffix is .tch, the database will be a hash database. If its suffix is .tcb, the database will be a B+ tree database. If its suffix is .tcf, the database will be a fixed-length database. If its suffix is .tct, the database will be a table database. Otherwise, this method fails. Tuning parameters can trail the name, separated by #. Each parameter is composed of the name and the value, separated by =. On-memory hash database supports bnum, capnum, and capsiz. On-memory tree database supports capnum and capsiz. Hash database supports mode, bnum, apow, fpow, opts, rcnum, and xmsiz. B+ tree database supports mode, lmemb, nmemb, bnum, apow, fpow, opts, lcnum, ncnum, and xmsiz. Fixed-length database supports mode, width, and limsiz. Table database supports mode, bnum, apow, fpow, opts, rcnum, lcnum, ncnum, xmsiz, and idx.

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

The tuning parameter capnum specifies the capacity number of records. capsiz specifies the capacity size of using memory. Records spilled the capacity are removed by the storing order. mode can contain w of writer, r of reader, c of creating, t of truncating, e of no locking, and f of non-blocking lock. The default mode is relevant to wc. opts can contains l of large option, d of Deflate option, b of BZIP2 option, and t of TCBS option. idx specifies the column name of an index and its type separated by :. For example, casket.tch#bnum=1000000#opts=ld means that the name of the database file is casket.tch, and the bucket number is 1000000, and the options are large and Deflate.

$adb->close()

Close the database.

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.

$adb->put(key, value)

Store a record.

key’ specifies the key.

value’ specifies the value.

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.

$adb->putkeep(key, value)

Store a new record.

key’ specifies the key.

value’ specifies the value.

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.

$adb->putcat(key, value)

Concatenate a value at the end of the existing record.

key’ specifies the key.

value’ specifies the value.

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

If there is no corresponding record, a new record is created.

$adb->out(key)

Remove a record.

key’ specifies the key.

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

$adb->get(key)

Retrieve a record.

key’ specifies the key.

If successful, the return value is the value of the corresponding record. ‘undef’ is returned if no record corresponds.

$adb->vsiz(key)

Get the size of the value of a record.

key’ specifies the key.

If successful, the return value is the size of the value of the corresponding record, else, it is -1.

$adb->iterinit()

Initialize the iterator.

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

The iterator is used in order to access the key of every record stored in a database.

$adb->iternext()

Get the next key of the iterator.

If successful, the return value is the next key, else, it is ‘undef’. ‘undef’ 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.

$adb->fwmkeys(prefix, max)

Get forward matching 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 the reference to an array of the keys of the corresponding records. This method does never fail. It returns an empty array even if no record corresponds.

Note that this method may be very slow because every key in the database is scanned.

$adb->addint(key, num)

Add an integer to a record.

key’ specifies the key.

num’ specifies the additional value.

If successful, the return value is the summation value, else, it is ‘undef’.

If the corresponding record exists, the value is treated as an integer and is added to. If no record corresponds, a new record of the additional value is stored. Because records are stored in binary format, they should be processed with the ‘unpack’ function with the ‘i’ operator after retrieval.

$adb->adddouble(key, num)

Add a real number to a record.

key’ specifies the key.

num’ specifies the additional value.

If successful, the return value is the summation value, else, it is ‘undef’.

If the corresponding record exists, the value is treated as a real number and is added to. If no record corresponds, a new record of the additional value is stored. Because records are stored in binary format, they should be processed with the ‘unpack’ function with the ‘d’ operator after retrieval.

$adb->sync()

Synchronize updated contents with the file and the device.

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

$adb->optimize(params)

Optimize the storage.

params’ specifies the string of the tuning parameters, which works as with the tuning of parameters the method ‘open’. If it is not defined, it is not used.

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

$adb->vanish()

Remove all records.

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

$adb->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.

$adb->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.

$adb->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.

$adb->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.

$adb->path()

Get the path of the database file.

The return value is the path of the database file or ‘undef’ if the object does not connect to any database instance. * stands for on-memory hash database. + stands for on-memory tree database.

$adb->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 instance.

$adb->size()

Get the size of the database.

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

$adb->misc(name, args)

Call a versatile function for miscellaneous operations.

name’ specifies the name of the function.

args’ specifies the reference to an array of arguments. If it is not defined, no argument is specified.

If successful, the return value is the reference to an array of the result. ‘undef’ is returned on failure.

    Tying functions of TokyoCabinet::ADB

tie(%hash, ‘‘TokyoCabinet::ADB’’, name)

Tie a hash variable to an abstract database instance.

name’ specifies the name of the database. If it is *, the database will be an on-memory hash database. If it is +, the database will be an on-memory tree database. If its suffix is .tch, the database will be a hash database. If its suffix is .tcb, the database will be a B+ tree database. If its suffix is .tcf, the database will be a fixed-length database. If its suffix is .tct, the database will be a table database. Otherwise, this method fails. Tuning parameters can trail the name, separated by #. Each parameter is composed of the name and the value, separated by =. On-memory hash database supports bnum, capnum, and capsiz. On-memory tree database supports capnum and capsiz. Hash database supports mode, bnum, apow, fpow, opts, rcnum, and xmsiz. B+ tree database supports mode, lmemb, nmemb, bnum, apow, fpow, opts, lcnum, ncnum, and xmsiz. Fixed-length database supports mode, width, and limsiz. Table database supports mode, bnum, apow, fpow, opts, rcnum, lcnum, ncnum, xmsiz, and idx.

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

untie(%hash)

Untie a hash variable from the database.

The return value is always true.

$hash{key} = value

Store a record.

key’ specifies the key.

value’ specifies the value.

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.

delete($hash{key})

Remove a record.

key’ specifies the key.

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

$hash{key}

Retrieve a record.

key’ specifies the key.

If successful, the return value is the value of the corresponding record. ‘undef’ is returned if no record corresponds.

exists($hash{key})

Check whether a record corrsponding a key exists.

key’ specifies the key.

The return value is true if the record exists, else it is false.

$hash = ()

Remove all records.

The return value is always ‘undef’.

(the iterator)

The inner methods ‘FIRSTKEY’ and ‘NEXTKEY’ are also implemented so that you can use the tying functions ‘each’, ‘keys’, and so on.

LICENSE



 Copyright (C) 2006-2010 FAL Labs
 All rights reserved.



Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License or any later version. Tokyo 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with Tokyo Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

Search for    or go to Top of page |  Section 3 |  Main Index


perl v5.20.3 TOKYOCABINET (3) 2010-07-19

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.