shelf/bag

Types

An open persistent bag table with typed keys and values.

pub opaque type PBag(k, v)

Values

pub fn close(table: PBag(k, v)) -> Result(Nil, shelf.ShelfError)

Close the table, saving all data to disk.

pub fn delete_all(
  from table: PBag(k, v),
) -> Result(Nil, shelf.ShelfError)

Delete all entries (keeps the table open).

pub fn delete_key(
  from table: PBag(k, v),
  key key: k,
) -> Result(Nil, shelf.ShelfError)

Delete all values for the given key.

pub fn delete_object(
  from table: PBag(k, v),
  key key: k,
  value value: v,
) -> Result(Nil, shelf.ShelfError)

Delete a specific key-value pair.

Only the exact matching pair is removed. Other values for the same key are preserved.

pub fn fold(
  over table: PBag(k, v),
  from initial: acc,
  with fun: fn(acc, k, v) -> acc,
) -> Result(acc, shelf.ShelfError)

Fold over all entries. Order is unspecified.

pub fn insert(
  into table: PBag(k, v),
  key key: k,
  value value: v,
) -> Result(Nil, shelf.ShelfError)

Insert a key-value pair. Duplicate key-value pairs are ignored.

pub fn insert_list(
  into table: PBag(k, v),
  entries entries: List(#(k, v)),
) -> Result(Nil, shelf.ShelfError)

Insert multiple key-value pairs.

pub fn lookup(
  from table: PBag(k, v),
  key key: k,
) -> Result(List(v), shelf.ShelfError)

Look up all values for a key.

Returns Error(NotFound) if the key does not exist.

pub fn member(
  of table: PBag(k, v),
  key key: k,
) -> Result(Bool, shelf.ShelfError)

Check if a key exists without returning the values.

pub fn open(
  name name: String,
  path path: String,
) -> Result(PBag(k, v), shelf.ShelfError)

Open a persistent bag table with defaults (WriteBack mode).

let assert Ok(table) = bag.open("tags", "data/tags.dets")
pub fn open_config(
  config: shelf.Config,
) -> Result(PBag(k, v), shelf.ShelfError)

Open a persistent bag table with full configuration.

pub fn reload(table: PBag(k, v)) -> Result(Nil, shelf.ShelfError)

Discard unsaved ETS changes and reload from DETS.

pub fn save(table: PBag(k, v)) -> Result(Nil, shelf.ShelfError)

Snapshot the current ETS contents to DETS.

pub fn size(
  of table: PBag(k, v),
) -> Result(Int, shelf.ShelfError)

Return the number of objects stored.

pub fn sync(table: PBag(k, v)) -> Result(Nil, shelf.ShelfError)

Flush the DETS write buffer to the OS.

pub fn to_list(
  from table: PBag(k, v),
) -> Result(List(#(k, v)), shelf.ShelfError)

Return all key-value pairs as a list.

Warning: loads entire table into memory.

pub fn with_table(
  name name: String,
  path path: String,
  fun fun: fn(PBag(k, v)) -> Result(a, shelf.ShelfError),
) -> Result(a, shelf.ShelfError)

Use a table within a callback, ensuring it is closed afterward.

use table <- bag.with_table("tags", "data/tags.dets")
bag.insert(table, "color", "red")
Search Document