fjall (fjall v0.1.0)
View SourceFjall embedded key-value database for Erlang.
This module provides a unified API that dispatches on tuple tags.
Examples
Non-transactional
{ok, Db} = fjall:open("./mydb"),
{ok, Ks} = fjall:keyspace(Db, <<"default">>),
ok = fjall:insert(Ks, <<"key">>, <<"value">>),
{ok, <<"value">>} = fjall:get(Ks, <<"key">>),
ok = fjall:remove(Ks, <<"key">>).Optimistic Transaction
{ok, Db} = fjall:open("./mydb", [{optimistic, true}]),
{ok, Ks} = fjall:keyspace(Db, <<"default">>),
{ok, Tx} = fjall:write_tx(Db),
ok = fjall:insert(Tx, Ks, <<"key">>, <<"value">>),
{ok, <<"value">>} = fjall:get(Tx, Ks, <<"key">>),
ok = fjall:commit(Tx).
Summary
Types
Opaque handle to a write batch.
Compression type for journal entries.
Configuration option for database creation.
Opaque handle to a database (plain or optimistic transactional).
Iterator direction.
Opaque handle to an iterator.
Opaque handle to a keyspace (plain or optimistic transactional).
Keyspace configuration option.
Persist mode for database persistence.
Range boundary type.
Result type for operations that don't return a value on success.
Result type for operations that return a value on success.
Result type for operations that return a value on success, an error, or a sentinal.
Opaque handle to a database snapshot.
Opaque handle to a write transaction.
Functions
Returns the approximate number of key-value pairs in the keyspace.
Creates a write batch for atomic multi-keyspace writes.
Clears the entire keyspace.
Returns a list of all remaining key-value pairs from the iterator.
Returns a list of up to N key-value pairs from the iterator.
Returns a list of all remaining keys from the iterator.
Returns a list of up to N keys from the iterator.
Returns a list of all remaining values from the iterator.
Returns a list of up to N values from the iterator.
Commits a batch or transaction.
Returns true if Key exists in the keyspace.
Destroys the iterator. Call this to release resources without waiting for GC.
Returns the approximate disk space used by the keyspace in bytes.
Returns the first key-value pair in the keyspace.
Returns the value for Key, or not_found if it doesn't exist.
Returns the value for Key within a transaction or snapshot.
Inserts or updates Key with Value.
Inserts or updates Key with Value within a batch or transaction.
Returns true if the batch contains no operations.
Creates an iterator over all key-value pairs in the keyspace.
Creates an iterator over keys matching Prefix.
Creates an iterator over keys in the range {Start, End}.
Opens or creates a keyspace with Name.
Opens or creates a keyspace with Name and Opts.
Returns the last key-value pair in the keyspace.
For batches, returns the number of operations. For plain keyspaces, returns the exact number of key-value pairs (O(n)).
Returns the next key-value pair from the iterator, or done if exhausted.
Opens a database at Path with default options.
Opens a database at Path with Options.
Returns the filesystem path to the keyspace directory.
Persists the database journal with the given Mode.
Removes Key from the keyspace.
Removes Key from the keyspace within a batch or transaction.
Rolls back a transaction, discarding all changes.
Returns the size of the value for Key in bytes.
Creates a point-in-time snapshot for consistent reads.
Removes and returns the value for Key. Only available on transactional keyspaces.
Creates an optimistic write transaction.
Types
-opaque batch()
Opaque handle to a write batch.
-type compression() :: lz4 | none.
Compression type for journal entries.
See CompressionType in the Rust documentation.
-type config_option() :: {cache_size, pos_integer()} | {journal_compression, compression()} | {manual_journal_persist, boolean()} | {max_cached_files, pos_integer()} | {max_journaling_size, pos_integer()} | {temporary, boolean()} | {worker_threads, pos_integer()}.
Configuration option for database creation.
Supported options:
{cache_size, pos_integer()}- Total block cache size in bytes. Higher values improve read performance but consume more memory. Default: 32MB{journal_compression, compression()}- Compression type for large values written to the journal. Seecompression/0. Default:lz4{manual_journal_persist, boolean()}- Iftrue, journal persistence is manual and must be triggered explicitly. Default:false{max_cached_files, pos_integer()}- Maximum number of cached file descriptors. Default: 150 (macOS), 900 (Linux), 400 (Windows){max_journaling_size, pos_integer()}- Maximum write-ahead log (journal) size in bytes. Older journals are cleaned up as needed. Default: 512MB{temporary, boolean()}- Iftrue, the database is temporary and will be deleted when closed. Default:false{worker_threads, pos_integer()}- Number of worker threads for background maintenance (flushing and compaction). Default: min(CPU cores, 4)
See DatabaseBuilder in the Rust documentation for configuration methods.
-opaque db()
Opaque handle to a database (plain or optimistic transactional).
-type direction() :: forward | reverse.
Iterator direction.
Specifies the order in which key-value pairs are returned:
forward- Iterate from first to last (ascending key order)reverse- Iterate from last to first (descending key order)
-opaque iter()
Opaque handle to an iterator.
-opaque ks()
Opaque handle to a keyspace (plain or optimistic transactional).
-type ks_option() :: {manual_journal_persist, boolean()} | {max_memtable_size, pos_integer()} | {expect_point_read_hits, boolean()}.
Keyspace configuration option.
Supported options:
{manual_journal_persist, boolean()}- Iftrue, journal persistence is manual and must be triggered explicitly. Default:false{max_memtable_size, pos_integer()}- Maximum in-memory buffer size in bytes. Recommended range: 8-64 MiB. Default: 64 MiB{expect_point_read_hits, boolean()}- Iftrue, disables last-level bloom filters for ~90% size reduction. Use when point reads typically succeed. Default:false
See KeyspaceCreateOptions in the Rust documentation.
-type persist_mode() :: buffer | sync_data | sync_all.
Persist mode for database persistence.
Determines the durability guarantee when persisting a database:
buffer- Flush to OS buffers only. Data survives application crash but not power loss or OS crash. See PersistMode::Buffer.sync_data- Flush with fdatasync. Ensures data is written to disk, suitable for most file systems. See PersistMode::SyncData.sync_all- Flush with fsync. Strongest guarantee, ensuring both data and metadata are written to disk. See PersistMode::SyncAll.
See PersistMode in the Rust documentation.
-type range() :: inclusive | exclusive.
Range boundary type.
Specifies whether the end boundary of a range is included:
inclusive- End boundary is included in the range[Start, End]exclusive- End boundary is excluded from the range[Start, End)
-type result() :: ok | {error, Reason :: term()}.
Result type for operations that don't return a value on success.
-type result(T) :: {ok, T} | {error, Reason :: term()}.
Result type for operations that return a value on success.
-type result(T, S) :: {ok, T} | {error, Reason :: term()} | S.
Result type for operations that return a value on success, an error, or a sentinal.
-opaque snapshot()
Opaque handle to a database snapshot.
-opaque tx()
Opaque handle to a write transaction.
Functions
-spec approximate_len(ks()) -> non_neg_integer().
Returns the approximate number of key-value pairs in the keyspace.
Creates a write batch for atomic multi-keyspace writes.
Clears the entire keyspace.
Returns a list of all remaining key-value pairs from the iterator.
-spec collect(iter(), pos_integer()) -> {ok, [{binary(), binary()}]} | {error, term()}.
Returns a list of up to N key-value pairs from the iterator.
Returns a list of all remaining keys from the iterator.
-spec collect_keys(iter(), pos_integer()) -> {ok, [binary()]} | {error, term()}.
Returns a list of up to N keys from the iterator.
Returns a list of all remaining values from the iterator.
-spec collect_values(iter(), pos_integer()) -> {ok, [binary()]} | {error, term()}.
Returns a list of up to N values from the iterator.
Commits a batch or transaction.
Returns true if Key exists in the keyspace.
-spec destroy(iter()) -> ok.
Destroys the iterator. Call this to release resources without waiting for GC.
-spec disk_space(ks()) -> non_neg_integer().
Returns the approximate disk space used by the keyspace in bytes.
Returns the first key-value pair in the keyspace.
Returns the value for Key, or not_found if it doesn't exist.
-spec get(tx(), ks(), Key :: binary()) -> result(binary()); (snapshot(), ks(), Key :: binary()) -> result(binary(), not_found).
Returns the value for Key within a transaction or snapshot.
Inserts or updates Key with Value.
-spec insert(batch(), ks(), Key :: binary(), Value :: binary()) -> result(); (tx(), ks(), Key :: binary(), Value :: binary()) -> result().
Inserts or updates Key with Value within a batch or transaction.
Returns true if the batch contains no operations.
Creates an iterator over all key-value pairs in the keyspace.
Creates an iterator over keys matching Prefix.
Creates an iterator over keys in the range {Start, End}.
Opens or creates a keyspace with Name.
Opens or creates a keyspace with Name and Opts.
Returns the last key-value pair in the keyspace.
-spec len(batch() | ks()) -> non_neg_integer() | result(non_neg_integer()).
For batches, returns the number of operations. For plain keyspaces, returns the exact number of key-value pairs (O(n)).
Returns the next key-value pair from the iterator, or done if exhausted.
-spec open(Path :: file:name_all()) -> result(db()).
Opens a database at Path with default options.
-spec open(Path :: file:name_all(), Options :: [config_option() | {optimistic, boolean()}]) -> result(db()).
Opens a database at Path with Options.
Returns the filesystem path to the keyspace directory.
-spec persist(db(), Mode :: persist_mode()) -> result().
Persists the database journal with the given Mode.
Removes Key from the keyspace.
-spec remove(batch(), ks(), Key :: binary()) -> result(); (tx(), ks(), Key :: binary()) -> result().
Removes Key from the keyspace within a batch or transaction.
Rolls back a transaction, discarding all changes.
-spec size_of(ks(), Key :: binary()) -> result(non_neg_integer()).
Returns the size of the value for Key in bytes.
Creates a point-in-time snapshot for consistent reads.
Removes and returns the value for Key. Only available on transactional keyspaces.
Creates an optimistic write transaction.