class LMDB::Database
- LMDB::Database
- Reference
- Object
Overview
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
Included Modules
- Enumerable({LMDB::Value, LMDB::Value})
- Iterable({LMDB::Value, LMDB::Value})
- LMDB::Disposable
Direct Known Subclasses
Defined in:
lmdb/database.crConstructors
- .new(environment : Environment, transaction : AbstractTransaction, flags : Flag = Flag::None)
- .new(environment : Environment, name : String, transaction : AbstractTransaction, flags : Flag = Flag::None)
Instance Method Summary
-
#==(other : self)
Returns
true
if this reference is the same as other. -
#[](key)
See
#get
-
#[]=(key, value)
See
#put
-
#[]?(key)
See
#get?
-
#clear
Empty out the database.
-
#cursor(&)
Create and yields a
AbstractCursor
to iterate throughself
, closed when the block goes out of scope. -
#cursor
ditto
-
#delete(key : String | Pointer(K) | Slice(K) | Array(K) | StaticArray(K, _)) forall K
Deletes the items associated with the given key from
self
. -
#delete(key : K) forall K
ditto
-
#do_close
Implementors overrides this method to perform resource cleanup If an exception is raised, the resource will not be marked as closed.
-
#each(&)
Must yield this collection's elements to the block.
-
#each
Must return an
Iterator
over the elements in this collection. - #each_key(&)
- #environment : Environment
- #flags : Flag
-
#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.
-
#get(key : K) : Value forall K
ditto
-
#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.
-
#get?(key : K) : Value | Nil forall K
ditto
-
#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
. -
#put(key : String | Pointer(K) | Slice(K) | Array(K) | StaticArray(K, _), val : V, flags : PutFlags = PutFlags::None) forall K, V
ditto
-
#put(key : K, val : String | Pointer(V) | Slice(V) | Array(V) | StaticArray(V, _), flags : PutFlags = PutFlags::None) forall K, V
ditto
-
#put(key : K, val : V, flags : PutFlags = PutFlags::None) forall K, V
ditto
-
#size
Returns the number of records in
self
. -
#stat
Returns raw statistics about
self
. - #to_unsafe : UInt32
Constructor Detail
Instance Method Detail
Returns true
if this reference is the same as other. Invokes same?
.
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.
Deletes the items associated with the given key from self
.
Implementors overrides this method to perform resource cleanup If an exception is raised, the resource will not be marked as closed.
Must yield this collection's elements to the block.
Must return an Iterator
over the elements in this collection.
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.
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.
Stores the given key/value pair into self
.
By default, a matching key replaces contents with given value.
ditto