MDBX::
Database class
Superclass | rb_cObject |
Extended With |
|
The primary class for interacting with an MDBX
database.
Attributes
- deserializer RW
A Proc for automatically deserializing values. Defaults to
Marshal.load
.- options R
Options used when instantiating this database handle.
- path R
The path on disk of the database.
- serializer RW
A Proc for automatically serializing values. Defaults to
Marshal.dump
.
Public Class Methods
MDBX::Database.open( path, options ) => db
Open an existing (or create a new) mdbx database at filesystem path
. In block form, the database is automatically closed when the block exits.
MDBX::Database.open( path, options ) do |db| db[ 'key' ] = value end # closed!
Passing options modify various database behaviors. See the libmdbx documentation for detailed information.
Options
Unless otherwise mentioned, option keys are symbols, and values are boolean.
Attempt to coalesce items for the garbage collector, potentialy increasing the chance of unallocating storage earlier.
Skip compatibility checks when opening an in-use database with unknown or mismatched flag values.
Access is restricted to the first opening process. Other attempts to use this database (even in readonly mode) are denied.
Recycle garbage collected items via LIFO, instead of FIFO. Depending on underlying hardware (disk write-back cache), this could increase write performance.
Set the maximum number of “subdatabase” collections allowed. By default, collection support is disabled.
Set the maximum number of allocated simultaneous reader slots.
Set an upper boundary (in bytes) for the database map size. The default is 10485760 bytes.
Whe creating a new database, set permissions to this 4 digit octal number. Defaults to ‘0644`. Set to `0` to never automatically create a new file, only opening existing databases.
Skip initializing malloc’ed memory to zeroes before writing.
A system crash may sacrifice the last commit for a potentially large write performance increase. Database
integrity is maintained.
When creating a new database, don’t put the data and lock file under a dedicated subdirectory.
Disable all use of OS readahead. Potentially useful for random reads wunder low memory conditions. Default behavior is to dynamically choose when to use or omit readahead.
Parallelize read-only transactions across threads. Writes are always thread local. (See MDBX
documentation for details.)
Reject any write attempts while using this database handle.
Trade safety for speed for databases that fit within available memory. (See MDBX
documentation for details.)
Public Instance Methods
Return a single value for key
immediately.
Set a single value for key
. If the value is nil
, the key is removed.
Empty the current collection on disk. If collections are not enabled or the database handle is set to the top-level (main) db - this deletes *all records* from the database.
Cleanly close an opened database.
Predicate: return true if the database environment is closed.
Gets or sets the sub-database “collection” that read/write operations apply to. If a block is passed, the collection automatically reverts to the prior collection when it exits.
db.collection #=> (collection name, or nil if in main) db.collection( 'collection_name' ) #=> db db.collection( 'collection_name' ) do [ ... ] end # reverts to the previous collection name
Deletes the entry for the given key and returns its associated value. If no block is given and key is found, deletes the entry and returns the associated value. If no block given and key is not found, returns nil.
If a block is given and key is found, ignores the block, deletes the entry, and returns the associated value. If a block is given and key is not found, calls the block and returns the block’s return value.
Destroy a collection. You must be in the top level database to call this method.
Calls the block once for each key, returning self. A transaction must be opened prior to use.
Calls the block once for each key and value, returning self. A transaction must be opened prior to use.
Calls the block once for each value, returning self. A transaction must be opened prior to use.
Returns true
if the current collection has no data.
Returns the value for the given key, if found. If key is not found and no block was given, returns nil. If key is not found and a block was given, yields key to the block and returns the block’s return value.
Predicate: return true if a transaction (or snapshot) is currently open.
Returns true if the current collection contains key
.
Returns a new Array containing all keys in the collection.
Returns the count of keys in the currently selected collection.
Switch to the top-level collection.
Open the DB environment handle.
Returns a new Hash object containing the entries for the given keys. Any given keys that are not found are ignored.
Open a new mdbx read only snapshot. In block form, the snapshot is automatically closed when the block ends.
Return a hash of various metadata for the current database.
Return the entirety of database contents as an Array of array pairs.
Return the entirety of database contents as a Hash.
Open a new mdbx read/write transaction. In block form, the transaction is automatically committed when the block ends.
Raising a MDBX::Rollback
exception from within the block automatically rolls the transaction back.
Returns a new Array containing all values in the collection.
Returns a new Array containing values for the given keys
.
Protected Instance Methods
Yield and return the block, opening a snapshot first if there isn’t already a transaction in progress. Closes the snapshot if this method opened it.
Safely deserialize a value, closing any open transaction and re-raising if necessary.
Safely serialize a value, closing any open transaction and re-raising if necessary.