slate/duplicate_bag

Types

An open DETS duplicate bag table with typed keys and values.

pub opaque type DuplicateBag(k, v)

Values

pub fn close(
  table: DuplicateBag(k, v),
) -> Result(Nil, slate.DetsError)

Close the table, flushing all pending writes to disk.

pub fn delete_all(
  from table: DuplicateBag(k, v),
) -> Result(Nil, slate.DetsError)

Delete all objects in the table (keeps the table open).

pub fn delete_key(
  from table: DuplicateBag(k, v),
  key key: k,
) -> Result(Nil, slate.DetsError)

Delete all values for the given key.

pub fn delete_object(
  from table: DuplicateBag(k, v),
  key key: k,
  value value: v,
) -> Result(Nil, slate.DetsError)

Delete all occurrences of a specific key-value pair from the table.

In a duplicate bag, this removes every copy of the exact pair. Other values (or different duplicates) for the same key are preserved.

import gleam/dynamic/decode
let assert Ok(table) = duplicate_bag.open("events.dets",
  key_decoder: decode.string, value_decoder: decode.string)
let assert Ok(Nil) = duplicate_bag.insert(table, "click", "btn_a")
let assert Ok(Nil) = duplicate_bag.insert(table, "click", "btn_a")
let assert Ok(Nil) = duplicate_bag.insert(table, "click", "btn_b")
let assert Ok(Nil) = duplicate_bag.delete_object(table, "click", "btn_a")
// Only "btn_b" remains
pub fn fold(
  over table: DuplicateBag(k, v),
  from initial: acc,
  with fun: fn(acc, k, v) -> acc,
) -> Result(acc, slate.DetsError)

Fold over all entries. Order is unspecified.

Returns Error(DecodeErrors(_)) if any entry doesn’t match the expected types. The fold stops at the first decode error.

pub fn info(
  table: DuplicateBag(k, v),
) -> Result(slate.TableInfo, slate.DetsError)

Get information about an open table.

pub fn insert(
  into table: DuplicateBag(k, v),
  key key: k,
  value value: v,
) -> Result(Nil, slate.DetsError)

Insert a key-value pair. Duplicates are stored separately.

pub fn insert_list(
  into table: DuplicateBag(k, v),
  entries entries: List(#(k, v)),
) -> Result(Nil, slate.DetsError)

Insert multiple key-value pairs.

pub fn lookup(
  from table: DuplicateBag(k, v),
  key key: k,
) -> Result(List(v), slate.DetsError)

Look up all values for a key. Returns an empty list if key not found.

Returns Error(DecodeErrors(_)) if any stored value doesn’t match the expected type.

pub fn member(
  of table: DuplicateBag(k, v),
  key key: k,
) -> Result(Bool, slate.DetsError)

Check if a key exists without returning the values.

pub fn open(
  path: String,
  key_decoder key_decoder: decode.Decoder(k),
  value_decoder value_decoder: decode.Decoder(v),
) -> Result(DuplicateBag(k, v), slate.DetsError)

Open or create a DETS duplicate bag table at the given file path.

Decoders are used to validate data read from disk, ensuring type safety even when opening files created by other code or previous runs.

import gleam/dynamic/decode
let assert Ok(table) = duplicate_bag.open("data/events.dets",
  key_decoder: decode.string, value_decoder: decode.string)
pub fn open_with(
  path: String,
  repair: slate.RepairPolicy,
  key_decoder key_decoder: decode.Decoder(k),
  value_decoder value_decoder: decode.Decoder(v),
) -> Result(DuplicateBag(k, v), slate.DetsError)

Open or create a DETS duplicate bag table with repair options.

pub fn open_with_access(
  path: String,
  repair: slate.RepairPolicy,
  access: slate.AccessMode,
  key_decoder key_decoder: decode.Decoder(k),
  value_decoder value_decoder: decode.Decoder(v),
) -> Result(DuplicateBag(k, v), slate.DetsError)

Open a DETS duplicate bag table with repair and access mode options.

Use ReadOnly to open a table for reading only. Write operations on a read-only table will return Error(AccessDenied).

import gleam/dynamic/decode
let assert Ok(table) = duplicate_bag.open_with_access(path, AutoRepair, ReadOnly,
  key_decoder: decode.string, value_decoder: decode.string)
let assert Ok(vals) = duplicate_bag.lookup(table, key: "key")
// duplicate_bag.insert(table, "key", "val") would return Error(AccessDenied)
pub fn size(
  of table: DuplicateBag(k, v),
) -> Result(Int, slate.DetsError)

Return the number of objects stored.

pub fn sync(
  table: DuplicateBag(k, v),
) -> Result(Nil, slate.DetsError)

Flush pending writes to disk without closing the table.

pub fn to_list(
  from table: DuplicateBag(k, v),
) -> Result(List(#(k, v)), slate.DetsError)

Return all key-value pairs as a list.

Warning: loads entire table into memory. Returns Error(DecodeErrors(_)) if any entry doesn’t match the expected types.

pub fn with_table(
  path: String,
  key_decoder key_decoder: decode.Decoder(k),
  value_decoder value_decoder: decode.Decoder(v),
  fun fun: fn(DuplicateBag(k, v)) -> Result(a, slate.DetsError),
) -> Result(a, slate.DetsError)

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

This is the recommended way to use DETS tables for short-lived operations.

import gleam/dynamic/decode
use table <- duplicate_bag.with_table("data/events.dets",
  key_decoder: decode.string, value_decoder: decode.string)
duplicate_bag.insert(table, "click", "button_a")
Search Document