class

LMDB::Database

Inherits Iterable / Enumerable / LMDB::Disposable / Reference / Object

A Database is a key-value store, which is part of an Environment.

By default, each key maps to one value. However, a Database can be configured to allow duplicate keys, in which case one key will map to multiple values.

Keys are stored in a sorted order. The order can also be configured upon initialization.

Basic operations on a database are #put, #get and #delete records. One can also iterate through records using a Cursor.

All operations requires an active transaction opened in #environment. If no current transaction is found, operations will fail.

Example:

env = LMDB.new "databasedir"
txn = env.transaction
db = env.database "databasename"
db.put "key1", "value1"
db.put "key2", "value2"
db.get "key1" # => "value1"
txn.commit
env.close

Constructors

new(environment : Environment, transaction : AbstractTransaction, flags : Flag = Flag::None)
Source
new(environment : Environment, name : String, transaction : AbstractTransaction, flags : Flag = Flag::None)
Source

Instance methods

==(other : self)

Returns true if this reference is the same as other. Invokes same?.

Source
[](key)

See #get

Source
[]=(key, value)

See #put

Source
[]?(key)

See #get?

Source
clear

Empty out the database.

This should happen within a Transaction.

Source
cursor

Create and yields a AbstractCursor to iterate through self, closed when the block goes out of scope.

The created cursor is associated with the current transaction and self. It cannot be used after the database is closed, nor when the transaction has ended. A cursor in a Transaction can be closed before its transaction ends, and will otherwise be closed when its transaction ends. A cursor in a ReadOnlyTransaction must be closed explicitly, before or after its transaction ends. It can be reused with Cursor#renew before finally closing it.

Source
cursor

ditto

Source
delete(key : String | Pointer(K) | Slice(K) | Array(K) | StaticArray(K, _)) forall K

Deletes the items associated with the given key from self.

Source
delete(key : K) forall K

ditto

Source
do_close

Implementors overrides this method to perform resource cleanup If an exception is raised, the resource will not be marked as closed.

Source
each

Must yield this collection's elements to the block.

Source
each

Must return an Iterator over the elements in this collection.

Source
each_key
Source
environment
Source
flags
Source
get(key : String | Pointer(K) | Slice(K) | Array(K) | StaticArray(K, _)) : Value forall K

Retrieve the value associated with the given key from the database.

If the database supports duplicate keys (DUPSORT), the first value for the key is returned. See #cursor to retrieve all items from a given key.

Raises if the key is not in the database.

Source
get(key : K) : Value forall K

ditto

Source
get?(key : String | Pointer(K) | Slice(K) | Array(K) | StaticArray(K, _)) : Value | Nil forall K

Retrieve the value associated with the given key from the database.

If the database supports duplicate keys (DUPSORT), the first value for the key is returned. See #cursor to retrieve all items from a given key.

Returns nil if the key is not in the database.

Source
get?(key : K) : Value | Nil forall K

ditto

Source
put(key : String | Pointer(K) | Slice(K) | Array(K) | StaticArray(K, _), value : String | Pointer(V) | Slice(V) | Array(V) | StaticArray(V, _), flags : PutFlags = PutFlags::None) forall K, V

Stores the given key/value pair into self.

By default, a matching key replaces contents with given value.

Source
put(key : String | Pointer(K) | Slice(K) | Array(K) | StaticArray(K, _), val : V, flags : PutFlags = PutFlags::None) forall K, V

ditto

Source
put(key : K, val : String | Pointer(V) | Slice(V) | Array(V) | StaticArray(V, _), flags : PutFlags = PutFlags::None) forall K, V

ditto

Source
put(key : K, val : V, flags : PutFlags = PutFlags::None) forall K, V

ditto

Source
size

Returns the number of records in self.

Source
stat

Returns raw statistics about self.

Source
to_unsafe
Source

Nested types