Skip to content
/ lamb Public

An Erlang Term Storage library for the Gleam language.

Notifications You must be signed in to change notification settings

chouzar/lamb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

539e429 · Dec 12, 2024

History

36 Commits
Sep 20, 2024
Dec 12, 2024
Dec 9, 2024
Sep 20, 2024
Dec 12, 2024
Dec 12, 2024
Dec 9, 2024

Repository files navigation

lamb

Package Version Hex Docs

A gleam library for operating and querying ETS tables.

import lamb.{Set, Private}
import lamb/query
import lamb/query/term as t

type User {
  User(name: String, age: Int, bio: String)
}

pub fn main() {
  // Create a table and insert 4 records.
  let assert Ok(table) = lamb.create("users", Private, Set, False)

  lamb.insert(table, 1, User("Raúl", age: 35, bio: "While at friends gatherings, plays yugioh."))
  lamb.insert(table, 2, User("César", age: 33, bio: "While outdoors, likes bird watching."))
  lamb.insert(table, 3, User("Carlos", age: 30, bio: "Always craving for coffee."))
  lamb.insert(table, 4, User("Adrián", age: 26, bio: "Simply exists."))

  // Retrieve all User records.
  let _records = lamb.search(table, query.new())

  // Retrieve all User ids.
  let to_index = fn(index, _record) { index }

  let query =
    query.new()
    |> query.index(t.v(0))
    |> query.map(to_index)

  let _ids = lamb.all(table, query)

  // Retrieve all User records in batches of 2.
  let assert Records([_, _], step) = lamb.batch(table, by: 2, where: q.new())
  let assert Records([_, _], step) = lamb.continue(step)
  let assert End([]) = lamb.continue(step)
}

The API does rely on matchspecs to query stored data, in erlang matchspecs are composed by a Tuple of arity 3 called a MatchFunction:

  • A Head that contains the shape of the data we want to match to, as well as variable declarations.
  • A list of Condition expressions that can help filter data through predicates.
  • A Body that declares the shape and variables we'd like to output from the MatchFunction.

None of the current operations enforce types, querying so far is a "dynamic" operation.

Development

Run tests:

gleam test

Run benchmarks:

gleam run --module benchmark

Installation

gleam add lamb