shelf

Types

Configuration for opening a persistent table.

pub opaque type Config
pub type ShelfError {
  NotFound
  KeyAlreadyPresent
  TableClosed
  NotOwner
  FileError(String)
  NameConflict
  InvalidPath(String)
  FileSizeLimitExceeded
  TypeMismatch(List(decode.DecodeError))
  ErlangError(String)
}

Constructors

  • NotFound

    No value found for the given key

  • KeyAlreadyPresent

    Key already exists (for insert_new)

  • TableClosed

    Table has been closed or doesn’t exist

  • NotOwner

    The calling process is not the table owner.

    ETS tables are protected — only the process that called open() can perform writes and lifecycle operations. Other processes can read freely. Wrap the table in a supervised actor/server if you need cross-process writes.

  • FileError(String)

    A storage file could not be found, created, opened, or removed, or its filesystem identity could not be established. The message includes the native error detail.

  • NameConflict

    Another live shelf table reserves this canonical path or filesystem identity, even in the same process or while saving. Not related to the table name.

  • InvalidPath(String)

    The DETS file path is invalid (escapes base directory, contains null bytes, or is otherwise unsafe)

  • FileSizeLimitExceeded

    DETS file exceeds the 2 GB limit

  • TypeMismatch(List(decode.DecodeError))

    Data loaded from DETS did not match the expected types.

    Returned when opening a table whose DETS file contains entries that fail to decode with the provided key/value decoders. The list of DecodeErrors describes which fields failed and why. A different DETS table kind returns FileError, not TypeMismatch.

  • ErlangError(String)

    Erlang-level error (catch-all)

Controls when writes are persisted to disk.

pub type WriteMode {
  WriteBack
  WriteThrough
}

Constructors

  • WriteBack

    Writes go to ETS only. Call save() to persist.

    Best for high-throughput writes where you control the save schedule. Data written since the last save() is lost on crash.

  • WriteThrough

    Every write goes to both ETS and DETS immediately.

    Slower writes but no data loss between saves. Reads are still fast (always from ETS).

Values

pub fn cleanup_snapshot(
  directory directory: String,
  base_directory base_directory: String,
) -> Result(Nil, ShelfError)

Remove one abandoned .shelf-save-<pid>-<counter> directory.

Before calling, confirm that the directory belongs to an interrupted save and stop all writers that can access its parent directory, including other VMs and hosts. A directory name, PID, or age does not prove that a save has stopped. This function does not detect writers or lock them out.

Resolves directory against base_directory and checks all entries before deleting any. Rejects symbolic links, nested directories, and unknown files. Removes only regular snapshot, DETS sidecar, and recognized file_sorter temporary files, then removes the empty directory. It does not scan siblings or restore an interrupted snapshot.

Requires trusted directories with no concurrent path replacement. A filesystem error can occur after some files have been removed; correct the cause and retry the same directory. The error includes the failing path and operation. The committed database remains unchanged.

shelf.cleanup_snapshot(
  directory: ".shelf-save-86491-18",
  base_directory: "/app/data",
)
pub fn config(
  name name: String,
  path path: String,
  base_directory base_directory: String,
) -> Config

Create a config with defaults (WriteBack mode).

The name is a diagnostic label for the table — it is not used as an ETS table name and does not need to be unique. Multiple tables can share the same name as long as they use different DETS file paths.

The base_directory restricts DETS file paths to prevent directory traversal attacks. The path is resolved relative to base_directory.

let config = shelf.config(name: "users", path: "users.dets",
  base_directory: "/app/data")
pub fn write_mode(
  config config: Config,
  mode mode: WriteMode,
) -> Config

Set the write mode on a config.

let config =
  shelf.config(name: "users", path: "users.dets",
    base_directory: "/app/data")
  |> shelf.write_mode(shelf.WriteThrough)
✨ Search Document