leveldb
stableIn-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 Plugin snippets can be edited in the Playground, but native capabilities are executed locally with the Zolo CLI.
Functions (15)
- LevelDB.new Create a new LevelDB store instance
- LevelDB.put Insert or update a key-value pair
- LevelDB.get Retrieve bytes by key
- LevelDB.get_string Retrieve a UTF-8 string value by key
- LevelDB.delete Remove a key-value pair
- LevelDB.has Check if a key exists
- LevelDB.keys List all keys in sorted order
- LevelDB.keys_range List keys within a lexicographic range
- LevelDB.prefix_keys List keys matching a prefix
- LevelDB.values List all values as bytes
- LevelDB.count Count all stored entries
- LevelDB.clear Remove all entries
- LevelDB.to_table Export all entries as a table
- LevelDB.batch_put Insert multiple entries at once
- LevelDB.batch_delete Delete multiple keys at once
From import to result
- 01Import only what you use
- 02Copy the smallest example
- 03Run it with the Zolo CLI
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"])