slate/set

Types

An open DETS set table with typed keys and values.

pub opaque type Set(k, v)

Values

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

Close the table, flushing all pending writes to disk.

The table handle must not be used after closing.

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

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

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

Delete the entry with the given key.

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

Delete a specific key-value pair from the table.

Unlike delete_key, this only removes the entry if both the key and value match. For set tables, this acts as a conditional delete: the entry is removed only when the stored value equals the given value.

pub fn fold(
  over table: Set(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: Set(k, v),
) -> Result(slate.TableInfo, slate.DetsError)

Get information about an open table.

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

Insert a key-value pair. Overwrites if key exists.

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

Insert multiple key-value pairs.

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

Insert only if the key does not already exist.

Returns Error(KeyAlreadyPresent) if the key exists.

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

Look up the value for a key.

Returns Error(NotFound) if the key does not exist. Returns Error(DecodeErrors(_)) if the stored value doesn’t match the expected type.

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

Check if a key exists without returning the value.

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

Open or create a DETS set 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) = set.open("data/cache.dets",
  key_decoder: decode.string, value_decoder: decode.int)
pub fn open_with(
  path: String,
  repair: slate.RepairPolicy,
  key_decoder key_decoder: decode.Decoder(k),
  value_decoder value_decoder: decode.Decoder(v),
) -> Result(Set(k, v), slate.DetsError)

Open or create a DETS set 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(Set(k, v), slate.DetsError)

Open a DETS set 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) = set.open_with_access(path, AutoRepair, ReadOnly,
  key_decoder: decode.string, value_decoder: decode.string)
let assert Ok(val) = set.lookup(table, key: "key")
// set.insert(table, "key", "val") would return Error(AccessDenied)
pub fn size(of table: Set(k, v)) -> Result(Int, slate.DetsError)

Return the number of objects stored.

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

Flush pending writes to disk without closing the table.

pub fn to_list(
  from table: Set(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 update_counter(
  in table: Set(k, Int),
  key key: k,
  increment amount: Int,
) -> Result(Int, slate.DetsError)

Atomically increment a counter value by the given amount.

The value associated with the key must be an integer. Returns the new value after incrementing. The increment can be negative.

Returns an error if the key doesn’t exist or the value is not an integer.

import gleam/dynamic/decode
let assert Ok(table) = set.open("counters.dets",
  key_decoder: decode.string, value_decoder: decode.int)
let assert Ok(Nil) = set.insert(table, "hits", 0)
let assert Ok(1) = set.update_counter(table, "hits", 1)
let assert Ok(3) = set.update_counter(table, "hits", 2)
pub fn with_table(
  path: String,
  key_decoder key_decoder: decode.Decoder(k),
  value_decoder value_decoder: decode.Decoder(v),
  fun fun: fn(Set(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 <- set.with_table("data/cache.dets",
  key_decoder: decode.string, value_decoder: decode.string)
set.insert(table, "key", "value")
Search Document