Skip to content

sled

stable

Embedded key-value database backed by sled, supporting persistent storage, prefix scans, range queries, atomic batches, and named sub-trees.

use plugin sled::{SledDb.new, SledDb.insert, SledDb.get, …}
24 functions Database
/ filter jk navigate Esc clear
Functions (24)
  1. SledDb.new Open or create a sled database at a path
  2. SledDb.insert Insert a key-value pair
  3. SledDb.get Get a value by key (returns bytes)
  4. SledDb.get_string Get a value by key (returns string)
  5. SledDb.remove Delete a key, returns true if it existed
  6. SledDb.contains Check if a key exists
  7. SledDb.len Get the number of entries
  8. SledDb.is_empty Check if the database is empty
  9. SledDb.iter_keys Iterate all keys as strings
  10. SledDb.scan_prefix Scan all entries with a key prefix
  11. SledDb.range Scan entries within a key range
  12. SledDb.batch Apply multiple insert/remove ops atomically
  13. SledDb.flush Flush pending writes to disk
  14. SledDb.clear Remove all entries
  15. SledDb.tree_names List all named trees
  16. SledDb.size_on_disk Get the approximate disk usage in bytes
  17. SledDb.generate_id Generate a unique monotonic integer ID
  18. SledDb.open_tree Open a named sub-tree
  19. SledDb.drop_tree Drop a named sub-tree
  20. SledTree.insert Insert into a named tree
  21. SledTree.get Get from a named tree
  22. SledTree.remove Remove from a named tree
  23. SledTree.flush Flush a named tree to disk
  24. SledTree.len Get entry count of a named tree

Open or create a sled database at a path

Opens or creates a sled database at path. The database is persisted to disk automatically.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")

Insert a key-value pair

Inserts value (bytes or string) under key. Overwrites any existing value.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
db.insert("user:1", "Alice")
db.insert("user:2", "Bob")

Get a value by key (returns bytes)

Returns the raw bytes stored under key, or nil if the key does not exist.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
db.insert("config", "debug=true")
let raw = db.get("config")

Get a value by key (returns string)

Returns the value under key decoded as a UTF-8 string, or nil if absent. Use this instead of get when values are known to be text.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
db.insert("greeting", "hello")
let val = db.get_string("greeting")
print(val)

Delete a key, returns true if it existed

Removes the entry for key. Returns true if the key existed, false otherwise.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
db.insert("temp", "value")
let removed = db.remove("temp")
print("removed: {removed}")

Check if a key exists

Returns true if key exists in the database.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
db.insert("flag", "1")
if db.contains("flag") {
  print("flag is set")
}

Get the number of entries

Returns the number of key-value pairs currently stored.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
print("entries: {db.len()}")

Check if the database is empty

Returns true if the database contains no entries.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
print(db.is_empty())

Iterate all keys as strings

Returns a table of all keys in the database as strings.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
db.insert("a", "1")
db.insert("b", "2")
let keys = db.iter_keys()
for _, k in keys {
  print(k)
}

Scan all entries with a key prefix

Returns all entries whose keys start with prefix. Each entry is a table with key (string) and value (bytes).

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
db.insert("user:1", "Alice")
db.insert("user:2", "Bob")
db.insert("config", "x")
let users = db.scan_prefix("user:")
for _, entry in users {
  print("{entry["key"]}")
}

Scan entries within a key range

Returns all entries with keys in the range [start, end). Each entry has key and value.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
db.insert("a", "1")
db.insert("b", "2")
db.insert("c", "3")
let slice = db.range("a", "c")
for _, entry in slice {
  print(entry["key"])
}

Apply multiple insert/remove ops atomically

Applies a list of insert and remove operations atomically. Each op is a table with op ("insert" or "remove"), key, and optionally value.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
db.batch([
  #{"op": "insert", "key": "x", "value": "10"},
  #{"op": "insert", "key": "y", "value": "20"},
  #{"op": "remove", "key": "old"}
])

Flush pending writes to disk

Forces all pending writes to be flushed to disk immediately.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
db.insert("key", "value")
db.flush()

Remove all entries

Removes all entries from the database.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
db.clear()

List all named trees

Returns a table of the names of all trees (including the default tree) in the database.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
let names = db.tree_names()
for _, name in names {
  print(name)
}

Get the approximate disk usage in bytes

Returns the approximate number of bytes the database occupies on disk.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
print("disk usage: {db.size_on_disk()} bytes")

Generate a unique monotonic integer ID

Generates and returns a unique, monotonically increasing integer ID. Useful for auto-incrementing primary keys.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
let id = db.generate_id()
db.insert("record:{id}", "data")

Open a named sub-tree

Opens a named sub-tree within the database. Each tree is an independent key-value namespace.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
let sessions = db.open_tree("sessions")
sessions.insert("tok_abc", "user:42")
print(sessions.get_string("tok_abc"))

Drop a named sub-tree

Drops the named tree and all its entries. Returns true if the tree existed.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
let dropped = db.drop_tree("old_cache")
print("dropped: {dropped}")

Insert into a named tree

Inserts a key-value pair into this named tree.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
let tree = db.open_tree("cache")
tree.insert("page:home", "<html>...</html>")

Get from a named tree

Returns the raw bytes for key in this tree, or nil.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
let tree = db.open_tree("cache")
let val = tree.get("page:home")

Remove from a named tree

Removes key from this tree. Returns true if it existed.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
let tree = db.open_tree("cache")
tree.remove("page:home")

Flush a named tree to disk

Flushes this tree's pending writes to disk.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
let tree = db.open_tree("cache")
tree.flush()

Get entry count of a named tree

Returns the number of entries in this named tree.

use plugin sled::{SledDb}

let db = SledDb.new("./mydata")
let tree = db.open_tree("cache")
print("cached pages: {tree.len()}")
enespt-br