Skip to content

leveldb

stable

In-memory ordered key-value store with a LevelDB-inspired API, supporting byte and string values, range queries, prefix scans, and batch operations.

use plugin leveldb::{LevelDB.new, LevelDB.put, LevelDB.get, …}
15 functions Database
/ filter jk navigate Esc clear
Functions (15)
  1. LevelDB.new Create a new LevelDB store instance
  2. LevelDB.put Insert or update a key-value pair
  3. LevelDB.get Retrieve bytes by key
  4. LevelDB.get_string Retrieve a UTF-8 string value by key
  5. LevelDB.delete Remove a key-value pair
  6. LevelDB.has Check if a key exists
  7. LevelDB.keys List all keys in sorted order
  8. LevelDB.keys_range List keys within a lexicographic range
  9. LevelDB.prefix_keys List keys matching a prefix
  10. LevelDB.values List all values as bytes
  11. LevelDB.count Count all stored entries
  12. LevelDB.clear Remove all entries
  13. LevelDB.to_table Export all entries as a table
  14. LevelDB.batch_put Insert multiple entries at once
  15. LevelDB.batch_delete Delete multiple keys at once

Create a new LevelDB store instance

Creates a new in-memory ordered key-value store. Keys are maintained in sorted (BTree) order, making range and prefix queries efficient.

use plugin leveldb::{LevelDB}

let db = LevelDB.new()

Insert or update a key-value pair

Inserts or updates a key with a string or bytes value. String values are stored as UTF-8 bytes internally.

use plugin leveldb::{LevelDB}

let db = LevelDB.new()
db.put("user:1", "Alice")
db.put("user:2", "Bob")

Retrieve bytes by key

Retrieves the raw bytes stored at key. Returns nil if the key does not exist. Use get_string when you know the value is valid UTF-8.

let raw = db.get("user:1")

Retrieve a UTF-8 string value by key

Retrieves the value at key as a UTF-8 string. Returns nil if the key does not exist. Errors if the stored bytes are not valid UTF-8.

use plugin leveldb::{LevelDB}

let db = LevelDB.new()
db.put("name", "Alice")
let name = db.get_string("name")
print("Hello, {name}")

Remove a key-value pair

Removes the entry at key. Does nothing if the key does not exist.

db.delete("user:2")

Check if a key exists

Returns true if key exists in the store.

use plugin leveldb::{LevelDB}

let db = LevelDB.new()
db.put("token", "abc")
if db.has("token") {
  print("Token found: {db.get_string("token")}")
}

List all keys in sorted order

Returns all keys in sorted lexicographic order as an array.

let all_keys = db.keys()
print("Keys: {all_keys}")

List keys within a lexicographic range

Returns all keys in the inclusive range [start, end] in sorted order. Useful for scanning a subset of entries, such as all keys with a common prefix.

use plugin leveldb::{LevelDB}

let db = LevelDB.new()
db.put("user:1", "Alice")
db.put("user:2", "Bob")
db.put("user:3", "Carol")
let range_keys = db.keys_range("user:1", "user:2")

List keys matching a prefix

Returns all keys that start with prefix, in sorted order.

let user_keys = db.prefix_keys("user:")

List all values as bytes

Returns all stored values as bytes, in key-sorted order.

let all_values = db.values()

Count all stored entries

Returns the total number of entries in the store.

let n = db.count()
print("Entries: {n}")

Remove all entries

Removes all entries from the store, leaving it empty.

db.clear()

Export all entries as a table

Exports all entries as a table mapping string keys to byte values. Useful for serializing the store's contents.

let snapshot = db.to_table()

Insert multiple entries at once

Inserts multiple entries at once. Each entry in the entries table must be a table with key (string) and value (string or bytes) fields.

use plugin leveldb::{LevelDB}

let db = LevelDB.new()
db.batch_put([
  #{"key": "a", "value": "1"},
  #{"key": "b", "value": "2"},
  #{"key": "c", "value": "3"}
])
print("Count: {db.count()}")

Delete multiple keys at once

Deletes multiple keys at once. The keys argument is a table (array) of key strings.

db.batch_delete(["a", "b"])
enespt-br