Class KyotoCabinet::DB
In: kyotocabinet.rb
Parent: Object

Interface of database abstraction.

Methods

[]   []=   accept   accept_bulk   add   append   begin_transaction   cas   clear   close   copy   count   cursor   cursor_process   delete   dump_snapshot   each   each_key   each_value   end_transaction   error   fetch   get   get_bulk   increment   increment_double   inspect   iterate   length   load_snapshot   match_prefix   match_regex   merge   new   open   path   process   remove   remove_bulk   replace   set   set_bulk   shift   size   status   store   synchronize   to_s   transaction   tune_encoding   tune_exception_rule  

Constants

GEXCEPTIONAL = 1 << 0   generic mode: exceptional mode
GCONCURRENT = 1 << 1   generic mode: concurrent mode
OREADER = 1 << 0   open mode: open as a reader
OWRITER = 1 << 1   open mode: open as a writer
OCREATE = 1 << 2   open mode: writer creating
OTRUNCATE = 1 << 3   open mode: writer truncating
OAUTOTRAN = 1 << 4   open mode: auto transaction
OAUTOSYNC = 1 << 5   open mode: auto synchronization
ONOLOCK = 1 << 6   open mode: open without locking
OTRYLOCK = 1 << 7   open mode: lock without blocking
ONOREPAIR = 1 << 8   open mode: open without auto repair
MSET = 0   merge mode: overwrite the existing value
MADD = 1   merge mode: keep the existing value
MREPLACE = 2   merge mode: modify the existing record only
MAPPEND = 3   merge mode: append the new value

Public Class methods

Create a database object.

  • @param opts the optional features by bitwise-or: DB::GEXCEPTIONAL for the exceptional mode, DB::GCONCURRENT for the concurrent mode.
  • @return the database object.
  • @note The exceptional mode means that fatal errors caused by methods are reported by exceptions raised. The concurrent mode means that database operations by multiple threads are performed concurrently without the giant VM lock. However, it has a side effect that such methods with call back of Ruby code as DB#accept, DB#accept_bulk, DB#iterate, DB#each, DB#each_key, DB#each_value, Cursor#accept are disabled.

Process a database by the block parameter.

  • @param path the same to the one of the open method.
  • @param mode the same to the one of the open method.
  • @param opts the optional features by bitwise-or: DB::GCONCURRENT for the concurrent mode.
  • @block a block including operations for the database. The block receives the database object. The database is opened with the given parameters before the block and is closed implicitly after the block.
  • @return nil on success, or an error object on failure.

Public Instance methods

An alias of DB#get.

Accept a visitor to a record.

  • @param key the key.
  • @param visitor a visitor object which implements the Visitor interface. If it is omitted, the block parameter is evaluated.
  • @param writable true for writable operation, or false for read-only operation.
  • @block If it is specified, the block is called as the visitor.
  • @return true on success, or false on failure.
  • @note The operation for each record is performed atomically and other threads accessing the same record are blocked. To avoid deadlock, any explicit database operation must not be performed in this method.

Accept a visitor to multiple records at once.

  • @param keys specifies an array of the keys.
  • @param visitor a visitor object which implements the Visitor interface, or a function object which receives the key and the value.
  • @param writable true for writable operation, or false for read-only operation.
  • @return true on success, or false on failure.
  • @note The operations for specified records are performed atomically and other threads accessing the same records are blocked. To avoid deadlock, any explicit database operation must not be performed in this method.

Add a record.

  • @param key the key.
  • @param value the value.
  • @return true on success, or false on failure.
  • @note If no record corresponds to the key, a new record is created. If the corresponding record exists, the record is not modified and false is returned.

Append the value of a record.

  • @param key the key.
  • @param value the value.
  • @return true on success, or false on failure.
  • @note If no record corresponds to the key, a new record is created. If the corresponding record exists, the given value is appended at the end of the existing value.

Begin transaction.

  • @param hard true for physical synchronization with the device, or false for logical synchronization with the file system.
  • @return true on success, or false on failure.

Perform compare-and-swap.

  • @param key the key.
  • @param oval the old value. nil means that no record corresponds.
  • @param nval the new value. nil means that the record is removed.
  • @return true on success, or false on failure.

Remove all records.

  • @return true on success, or false on failure.

Close the database file.

  • @return true on success, or false on failure.

Create a copy of the database file.

  • @param dest the path of the destination file.
  • @return true on success, or false on failure.

Get the number of records.

  • @return the number of records, or -1 on failure.

Create a cursor object.

  • @return the return value is the created cursor object. Each cursor should be disabled with the Cursor#disable method when it is no longer in use.

Process a cursor by the block parameter.

  • @block a block including operations for the cursor. The cursor is disabled implicitly after the block.
  • @return always nil.

Dump records into a snapshot file.

  • @param dest the name of the destination file.
  • @return true on success, or false on failure.

Process each records with a iterator block.

  • @block the iterator to receive the key and the value of each record.
  • @return true on success, or false on failure.

Process the key of each record with a iterator block.

  • @block the iterator to receive the key of each record.
  • @return true on success, or false on failure.

Process the value of each record with a iterator block.

  • @block the iterator to receive the value of each record.
  • @return true on success, or false on failure.

End transaction.

  • @param commit true to commit the transaction, or false to abort the transaction.
  • @return true on success, or false on failure.

Get the last happened error.

  • @return the last happened error.

Retrieve the value of a record.

  • @param key the key.
  • @return the value of the corresponding record, or nil on failure.

Retrieve records at once.

  • @param keys an array of the keys of the records to retrieve.
  • @param atomic true to perform all operations atomically, or false for non-atomic operations.
  • @return a hash of retrieved records, or nil on failure.

Add a number to the numeric integer value of a record.

  • @param key the key.
  • @param num the additional number.
  • @return the result value, or nil on failure.
  • @note If no record corresponds to the key, a new record is created with the initial value set by the additional value. The value is serialized as an 8-byte binary integer in big-endian order, not a decimal string. If existing value is not 8-byte, this method fails.

Add a number to the numeric double value of a record.

  • @param key the key.
  • @param num the additional number.
  • @return the result value, or nil on failure.
  • @note If no record corresponds to the key, a new record is created with the initial value set by the additional value. The value is serialized as an 16-byte binary fixed-point number in big-endian order, not a decimal string. If existing value is not 16-byte, this method fails.

Get the inspection string.

  • @return the inspection string.

Iterate to accept a visitor for each record.

  • @param visitor a visitor object which implements the Visitor interface. If it is omitted, the block parameter is evaluated.
  • @param writable true for writable operation, or false for read-only operation.
  • @block If it is specified, the block is called as the visitor.
  • @return true on success, or false on failure.
  • @note The whole iteration is performed atomically and other threads are blocked. To avoid deadlock, any explicit database operation must not be performed in this method.

An alias of DB#count.

Load records from a snapshot file.

  • @param src the name of the source file.
  • @return true on success, or false on failure.

Get keys matching a prefix string.

  • @param prefix the prefix string.
  • @param max the maximum number to retrieve. If it is negative, no limit is specified.
  • @return an array of matching keys, or nil on failure.

Get keys matching a regular expression string.

  • @param regex the regular expression string.
  • @param max the maximum number to retrieve. If it is negative, no limit is specified.
  • @return an array of matching keys, or nil on failure.

Merge records from other databases.

  • @param srcary an array of the source detabase objects.
  • @param mode the merge mode. DB::MSET to overwrite the existing value, DB::MADD to keep the existing value, DB::MAPPEND to append the new value.
  • @return true on success, or false on failure.

Open a database file.

  • @param path the path of a database file. If it is “-”, the database will be a prototype hash database. If it is “+”, the database will be a prototype tree database. If it is “:”, the database will be a stash database. If it is “*”, the database will be a cache hash database. If it is “%”, the database will be a cache tree database. If its suffix is “.kch”, the database will be a file hash database. If its suffix is “.kct”, the database will be a file tree database. If its suffix is “.kcd”, the database will be a directory hash database. If its suffix is “.kcf”, the database will be a directory tree database. Otherwise, this function fails. Tuning parameters can trail the name, separated by “#”. Each parameter is composed of the name and the value, separated by “=”. If the “type” parameter is specified, the database type is determined by the value in “-”, “+”, “:”, “*”, “%”, “kch”, “kct”, “kcd”, and “kcf”. All database types support the logging parameters of “log”, “logkinds”, and “logpx”. The prototype hash database and the prototype tree database do not support any other tuning parameter. The stash database supports “bnum”. The cache hash database supports “opts”, “bnum”, “zcomp”, “capcnt”, “capsiz”, and “zkey”. The cache tree database supports all parameters of the cache hash database except for capacity limitation, and supports “psiz”, “rcomp”, “pccap” in addition. The file hash database supports “apow”, “fpow”, “opts”, “bnum”, “msiz”, “dfunit”, “zcomp”, and “zkey”. The file tree database supports all parameters of the file hash database and “psiz”, “rcomp”, “pccap” in addition. The directory hash database supports “opts”, “zcomp”, and “zkey”. The directory tree database supports all parameters of the directory hash database and “psiz”, “rcomp”, “pccap” in addition.
  • @param mode the connection mode. DB::OWRITER as a writer, DB::OREADER as a reader. The following may be added to the writer mode by bitwise-or: DB::OCREATE, which means it creates a new database if the file does not exist, DB::OTRUNCATE, which means it creates a new database regardless if the file exists, DB::OAUTOTRAN, which means each updating operation is performed in implicit transaction, DB::OAUTOSYNC, which means each updating operation is followed by implicit synchronization with the file system. The following may be added to both of the reader mode and the writer mode by bitwise-or: DB::ONOLOCK, which means it opens the database file without file locking, DB::OTRYLOCK, which means locking is performed without blocking, DB::ONOREPAIR, which means the database file is not repaired implicitly even if file destruction is detected.
  • @return true on success, or false on failure.
  • @note The tuning parameter “log” is for the original “tune_logger“ and the value specifies the path of the log file, or “-” for the standard output, or “+” for the standard error. “logkinds” specifies kinds of logged messages and the value can be “debug”, “info”, “warn”, or “error”. “logpx” specifies the prefix of each log message. “opts” is for “tune_options“ and the value can contain “s” for the small option, “l” for the linear option, and “c” for the compress option. “bnum” corresponds to “tune_bucket“. “zcomp” is for “tune_compressor“ and the value can be “zlib” for the ZLIB raw compressor, “def” for the ZLIB deflate compressor, “gz” for the ZLIB gzip compressor, “lzo” for the LZO compressor, “lzma” for the LZMA compressor, or “arc” for the Arcfour cipher. “zkey” specifies the cipher key of the compressor. “capcnt” is for “cap_count“. “capsiz” is for “cap_size“. “psiz” is for “tune_page“. “rcomp” is for “tune_comparator“ and the value can be “lex” for the lexical comparator, “dec” for the decimal comparator, “lexdesc” for the lexical descending comparator, or “decdesc” for the decimal descending comparator. “pccap” is for “tune_page_cache“. “apow” is for “tune_alignment“. “fpow” is for “tune_fbp“. “msiz” is for “tune_map“. “dfunit” is for “tune_defrag“. Every opened database must be closed by the PolyDB::close method when it is no longer in use. It is not allowed for two or more database objects in the same process to keep their connections to the same database file at the same time.

Get the path of the database file.

  • @return the path of the database file, or nil on failure.

Remove a record.

  • @param key the key.
  • @return true on success, or false on failure.
  • @note If no record corresponds to the key, false is returned.

Remove records at once.

  • @param keys an array of the keys of the records to remove.
  • @param atomic true to perform all operations atomically, or false for non-atomic operations.
  • @return the number of removed records, or -1 on failure.

Replace the value of a record.

  • @param key the key.
  • @param value the value.
  • @return true on success, or false on failure.
  • @note If no record corresponds to the key, no new record is created and false is returned. If the corresponding record exists, the value is modified.

Set the value of a record.

  • @param key the key.
  • @param value the value.
  • @return true on success, or false on failure.
  • @note If no record corresponds to the key, a new record is created. If the corresponding record exists, the value is overwritten.

Store records at once.

  • @param recs a hash of the records to store.
  • @param atomic true to perform all operations atomically, or false for non-atomic operations.
  • @return the number of stored records, or -1 on failure.

Remove the first record.

  • @return a pair of the key and the value of the removed record, or nil on failure.

Get the size of the database file.

  • @return the size of the database file in bytes, or -1 on failure.

Get the miscellaneous status information.

  • @return a hash of the status information, or nil on failure.

Synchronize updated contents with the file and the device.

  • @param hard true for physical synchronization with the device, or false for logical synchronization with the file system.
  • @param proc a postprocessor object which implements the FileProcessor interface. If it is nil, no postprocessing is performed.
  • @block If it is specified, the block is called for postprocessing.
  • @return true on success, or false on failure.

Get the string expression.

  • @return the string expression.

Perform entire transaction by the block parameter.

  • @param hard true for physical synchronization with the device, or false for logical synchronization with the file system.
  • @block a block including operations during transaction. If the block returns true, the transaction is committed. If the block returns false or an exception is thrown, the transaction is aborted.
  • @return true on success, or false on failure.

Set the encoding of external strings.

  • @param enc the name of the encoding or its encoding object.
  • @note The default encoding of external strings is Encoding::ASCII_8BIT.
  • @return true on success, or false on failure.

Set the rule about throwing exception.

  • @param codes an array of error codes. If each method occurs an error corresponding to one of the specified codes, the error is thrown as an exception.
  • @return true on success, or false on failure.