class LMDB::Database

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

Direct Known Subclasses

Defined in:

lmdb/database.cr

Constructors

Instance Method Summary

Instance methods inherited from module LMDB::Disposable

close close, closed? closed?

Constructor Detail

def self.new(environment : Environment, transaction : AbstractTransaction, flags : Flag = Flag::None) #

[View source]
def self.new(environment : Environment, name : String, transaction : AbstractTransaction, flags : Flag = Flag::None) #

[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

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


[View source]
def [](key) #

See #get


[View source]
def []=(key, value) #

See #put


[View source]
def []?(key) #

See #get?


[View source]
def clear #

Empty out the database.

This should happen within a Transaction.


[View source]
def 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.


[View source]
def cursor #

ditto


[View source]
def delete(key : String | Pointer(K) | Slice(K) | Array(K) | StaticArray(K, _)) forall K #

Deletes the items associated with the given key from self.


[View source]
def delete(key : K) forall K #

ditto


[View source]
def do_close #
Description copied from module LMDB::Disposable

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


[View source]
def each(&) #
Description copied from module Enumerable({LMDB::Value, LMDB::Value})

Must yield this collection's elements to the block.


[View source]
def each #
Description copied from module Iterable({LMDB::Value, LMDB::Value})

Must return an Iterator over the elements in this collection.


[View source]
def each_key(&) #

[View source]
def environment : Environment #

[View source]
def flags : Flag #

[View source]
def 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.


[View source]
def get(key : K) : Value forall K #

ditto


[View source]
def 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.


[View source]
def get?(key : K) : Value | Nil forall K #

ditto


[View source]
def 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.


[View source]
def put(key : String | Pointer(K) | Slice(K) | Array(K) | StaticArray(K, _), val : V, flags : PutFlags = PutFlags::None) forall K, V #

ditto


[View source]
def put(key : K, val : String | Pointer(V) | Slice(V) | Array(V) | StaticArray(V, _), flags : PutFlags = PutFlags::None) forall K, V #

ditto


[View source]
def put(key : K, val : V, flags : PutFlags = PutFlags::None) forall K, V #

ditto


[View source]
def size #

Returns the number of records in self.


[View source]
def stat #

Returns raw statistics about self.


[View source]
def to_unsafe : UInt32 #

[View source]