RocksDB - Perl extension for RocksDB
use RocksDB; my $db = RocksDB->new('/path/to/db', { create_if_missing => 1 }); # put, get and delete $db->put(foo => 'bar'); my $val = $db->get('foo'); $db->delete('foo'); # batch $db->update(sub { my $batch = shift; # RocksDB::WriteBatch object $batch->put(foo => 'bar'); $batch->delete('bar'); }); # or manually my $batch = RocksDB::WriteBatch->new; $batch->put(foo => 'bar'); $batch->delete('bar'); $db->write($batch); # iteration my $iter = $db->new_iterator->seek_to_first; while (my ($key, $value) = $iter->each) { printf "%s: %s\n", $key, $value; } # in reverse order $iter->seek_to_last; while (my ($key, $value) = $iter->reverse_each) { printf "%s: %s\n", $key, $value; } # The tie interface tie my %db, 'RocksDB', '/path/to/db', { create_if_missing => 1 };
RocksDB is a Perl extension for RocksDB.
RocksDB is an embeddable persistent key-value store for fast storage.
See http://rocksdb.org/ for more details.
This distribution bundles the rocksdb source tree, so you don't need to have rocksdb.
If rocksdb already installed, the installer figures out it.
rocksdb depends on some environment. See vendor/rocksdb/INSTALL.md before installation.
Currently rocksdb supports Linux and OS X only.
RocksDB->new($name :Str[, $options :HashRef]) :RocksDB
Open the database with the specified $name and returns a new RocksDB object.
RocksDB->open($name :Str[, $options :HashRef]) :RocksDB
Alias for new.
new
$db->get($key :Str[, $read_options :HashRef]) :Maybe[Str]
If the database contains an entry for $key returns the corresponding value. If there is no entry for $key returns undef.
$db->get_multi(@keys :(Str) [, $read_options :HashRef]) :HashRef
Retrieve several values associated with @keys. @keys should be an array of scalars.
Returns reference to hash, where $href->{$key} holds corresponding value.
$db->put($key :Str, $value :Str[, $write_options :HashRef]) :Undef
Set the database entry for $key to $value.
$db->put_multi($key_values :HashRef [, $write_options :HashRef]) :Undef
Set the database entry for $key_values.
$db->delete($key :Str[, $write_options :HashRef]) :Undef
Remove the database entry (if any) for $key.
$db->remove($key :Str[, $write_options :HashRef]) :Undef
Alias for remove.
remove
$db->exists($key :Str) :Bool
Return true if the $key is present, false otherwise.
$db->key_may_exist($key :Str[, $value_ref :ScalarRef, $read_options :HashRef]) :Bool
If the key definitely does not exist in the database, then this method returns false, else true. If the caller wants to obtain value when the key is found in memory, a ScalarRef for $value_ref must be passed. This check is potentially lighter-weight than invoking $db->get(). One way to make this lighter weight is to avoid doing any IOs. Default implementation here returns true and not set $value.
$db->merge($key :Str, $value :Str[, $write_options :HashRef]) :Undef
Merge the database entry for $key with $value. The semantics of this operation is determined by the user provided merge_operator when opening DB.
$db->write($batch :RocksDB::WriteBatch[, $write_options :HashRef]) :Undef
Apply the specified updates to the database.
$db->update($callback :CodeRef[, $write_options :HashRef]) :Undef
Call $db->write by callback style.
$db->update(sub { my $batch = shift; }); # apply $batch
$db->new_iterator([$read_options :HashRef]) :RocksDB::Iterator
Return a RocksDB::Iterator over the contents of the database. The result of $db->new_iterator() is initially invalid (caller must call one of the seek methods on the iterator before using it).
$db->get_snapshot() :RocksDB::Snapshot
Return a handle to the current DB state. Iterators created with this handle will all observe a stable snapshot of the current DB state.
$db->get_approximate_size($from :Str, $to :Str) :Int
Return the approximate file system space used by keys in "$from .. $to".
Note that the returned sizes measure file system space usage, so if the user data compresses by a factor of ten, the returned sizes will be one-tenth the size of the corresponding user data size.
The results may not include the sizes of recently written data.
$db->get_property($property :Str) :Str
DB implementations can export properties about their state via this method. If $property is a valid property understood by this DB implementation, returns its current value. Otherwise returns undef.
Valid property names include:
"rocksdb.num-files-at-level<N>" - return the number of files at level <N>, where <N> is an ASCII representation of a level number (e.g. "0"). "rocksdb.stats" - returns a multi-line string that describes statistics about the internal operation of the DB. "rocksdb.sstables" - returns a multi-line string that describes all of the sstables that make up the db contents.
$db->compact_range($begin :Maybe[Str], $end :Maybe[Str] [, $compact_options :HashRef]) :Undef
Compact the underlying storage for the key range [begin,end]. In particular, deleted and overwritten versions are discarded, and the data is rearranged to reduce the cost of operations needed to access the data. This operation should typically only be invoked by users who understand the underlying implementation.
begin == undef is treated as a key before all keys in the database. end == undef is treated as a key after all keys in the database. Therefore the following call will compact the entire database:
$db->compact_range;
$compact_options might be:
Defaults to true.
Defaults to -1.
$db->number_levels() :Int
Number of levels used for this DB.
$db->max_mem_compaction_level() :Int
Maximum level to which a new compacted memtable is pushed if it does not create overlap.
$db->level0_stop_write_trigger() :Int
Number of files in level-0 that would stop writes.
$db->get_name() :Str
Get DB name -- the exact same name that was provided as an argument to RocksDB->new()
$db->flush([$flush_options :HashRef]) :Undef
Flush all mem-table data.
$db->disable_file_deletions() :Undef
Prevent file deletions. Compactions will continue to occur, but no obsolete files will be deleted. Calling this multiple times have the same effect as calling it once.
$db->enable_file_deletions() :Undef
Allow compactions to delete obselete files.
$db->get_sorted_wal_files() :(HashRef, ...)
Retrieve the sorted list of all wal files with earliest file first.
( { # log file's pathname relative to the main db dir 'path_name' => '/000003.log', # Primary identifier for log file. # This is directly proportional to creation time of the log file 'log_number' => 157, # Log file can be either alive or archived 'type' => 'alive', # or 'archived' # Starting sequence number of writebatch written in this log file 'start_sequence' => 85, # Size of log file on disk in Bytes 'size_file_bytes' => 28, }, ... )
$db->get_latest_sequence_number() :Int
The sequence number of the most recent transaction.
$db->get_updates_since($seq_number :Int) :RocksDB::TransactionLogIterator
Sets iter to an iterator that is positioned at a write-batch containing seq_number. If the sequence number is non existent, it returns an iterator at the first available seq_no after the requested seq_no. Must set WAL_ttl_seconds or WAL_size_limit_MB to large values to use this api, else the WAL files will get cleared aggressively and the iterator might keep getting invalid before an update is read.
$db->delete_file($name :Str) :Undef
Delete the file name from the db directory and update the internal state to reflect that. Supports deletion of sst and log files only. 'name' must be path relative to the db directory. eg. 000001.sst, /archive/000003.log
$db->get_live_files_meta_data() :(HashRef, ...)
Returns a list of all table files with their level, start key and end key.
( { # Name of the file 'name' => '/000140.sst', # Level at which this file resides. 'level' => 1 # File size in bytes. 'size' => 339, # Smallest user defined key in the file. 'smallestkey' => 'bar', # Largest user defined key in the file. 'largestkey' => 'foo', # smallest seqno in file 'smallest_seqno' => '0', # largest seqno in file 'largest_seqno' => '0', }, ... )
$db->get_statistics() :RocksDB::Statistics
Returns a RocksDB::Statistics object.
It's necessary to specify the 'enable_statistics' option when openning the DB.
$db->get_db_identity() :Str
Returns the globally unique ID created at database creation time.
RocksDB->destroy_db($name :Str) :Undef
Destroy the contents of the specified database. Be very careful using this method.
RocksDB->repair_db($name :Str) :Undef
If a DB cannot be opened, you may attempt to call this method to resurrect as much of the contents of the database as possible. Some data may be lost, so be careful when calling this function on a database that contains important information.
RocksDB->major_version() :Int
Returns the major version of rocksdb.
RocksDB->minor_version() :Int
Returns the minor version of rocksdb.
The following options are supported.
For details, see the documentation for RocksDB itself.
Call DBOptions.IncreaseParallelism(). Value will be ignored.
Call Options.PrepareForBulkLoad(). Value will be ignored.
Call ColumnFamilyOptions.OptimizeForPointLookup() with given value.
Call ColumnFamilyOptions.OptimizeLevelStyleCompaction() with given value.
Call ColumnFamilyOptions.OptimizeUniversalStyleCompaction() with given value.
Defaults to false. If true, call rocksdb::DB::OpenForReadOnly().
Defaults to undef. See RocksDB::Comparator.
Defaults to undef. See RocksDB::MergeOperator, RocksDB::AssociativeMergeOperator.
Defaults to undef. See RocksDB::CompactionFilter.
Defaults to false.
Defaults to 4MB.
Defaults to 2.
Defaults to 1.
Defaults to 1000.
Defaults to 0.
Defaults to 'snappy'. It can be specified using the following arguments.
snappy zlib bzip2 lz4 lz4hc none
['snappy', 'zlib', 'zlib', 'bzip2', 'lz4', 'lz4hc' ...]
Defaults to undef. See RocksDB::SliceTransform, RocksDB::FixedPrefixTransform.
Defaults to 7.
Defaults to 4.
Defaults to 8.
Defaults to 12.
Defaults to 2MB.
Defaults to 10MB.
Defaults to 10.
Defaults to target_file_size_base * 25.
Defaults to false. See RocksDB::Statistics.
Defaults to "".
Defaults to 21600000000 (6 hours).
Defaults to 0 (disabled).
Defaults to MAX_INT.
Defaults to 3600 (1 hour).
Defaults to 'normal'. It can be specified using the following arguments.
normal none sequential willneed
Defaults to 'level'. It can be specified using the following arguments.
level universal fifo
See 'Universal compaction options' section below.
See 'FIFO compaction options' section below.
Defaults to 10000, if inplace_update_support = true, else 0.
Defaults to 0 (disable).
See 'Block-based table options' section below.
See 'Plain table options' section below.
See 'Cuckoo table options' section below.
Defaults to 'binary_search'. It can be specified using the following arguments.
binary_search hash_search
Defaults to 'crc32c'. It can be specified using the following arguments.
no_checksum crc32c xxhash
Defaults to undef. See RocksDB::Cache, RocksDB::LRUCache.
Defaults to 4K.
Defaults to 16.
Defaults to undef. See RocksDB::FilterPolicy, RocksDB::BloomFilterPolicy.
Defaults to 0 (variable length).
Defaults to 0.75.
Defaults to 'plain'. It can be specified using the following arguments.
plain prefix
Defaults to 0.9.
Defaults to 100.
Defaults to 5.
Defaults to UINT_MAX.
Defaults to 200.
Defaults to 'total_size'. It can be specified using the following arguments.
total_size similar_size
Defaults to 1GB.
Defaults to undef. See RocksDB::Snapshot.
Defaults to 'read_all'. It can be specified using the following arguments.
read_all block_cache
http://rocksdb.org/
Jiro Nishiguchi <jiro@cpan.org>
Copyright (c) 2013, Jiro Nishiguchi All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Jiro Nishiguchi. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
See also vendor/rocksdb/LICENSE for bundled RocksDB sources.
To install RocksDB, copy and paste the appropriate command in to your terminal.
cpanm
cpanm RocksDB
CPAN shell
perl -MCPAN -e shell install RocksDB
For more information on module installation, please visit the detailed CPAN module installation guide.