kts

Kit Term Storage - fast, concurrent in-memory tables inspired by Erlang's ETS

Files

FileDescription
.editorconfigEditor formatting configuration
.gitignoreGit ignore rules for build artifacts and dependencies
.tool-versionsasdf tool versions (Zig, Kit)
LICENSEMIT license file
README.mdThis file
examples/basic.kitBasic usage example
examples/snapshot.kitExample: snapshot
kit.tomlPackage manifest with metadata and dependencies
src/kts.kitKTS error type for typed error handling.
tests/error-types.test.kitTests for error-types
tests/query-operations.test.kitTests for query-operations
tests/set-operations.test.kitTests for set-operations
tests/snapshot.test.kitTests for snapshot
tests/table-lifecycle.test.kitTests for table-lifecycle
zig/kit_ffi.zigZig FFI module for kit ffi
zig/kts.zigZig FFI module for kts

Dependencies

No Kit package dependencies.

Installation

kit add gitlab.com/kit-lang/packages/kit-kts.git

Usage

import Kit.KTS as KTS

main = fn(-env: Env) =>
  # Create a named set table
  users = KTS.new "users" :set[:named]

  # Insert records
  -ins = KTS.insert users {id: 1, name: "Alice", role: "admin"}
  KTS.insert-batch users [
    {id: 2, name: "Bob", role: "user"}, 
    {id: 3, name: "Carol", role: "admin"}
  ]

  # Lookup by key
  match KTS.lookup users 1
    | Some user -> println "Found: ${to-string user}"
    | None -> println "Not found"

  # Pattern matching query
  admins = KTS.pattern-match users {role: "admin"}
  println "Admins: ${to-string (length admins)}"

  # Save table to disk
  match KTS.save users "/tmp/users.json"
    | Ok _ -> println "Saved!"
    | Err e -> println "Save failed: ${KTSError.message e}"

  # Delete and restore from snapshot
  -del = KTS.delete-table users
  match KTS.load "/tmp/users.json"
    | Ok restored ->
      println "Restored ${to-string (KTS.size restored)} records"
      -d = KTS.delete-table restored
      no-op
    | Err e -> println "Load failed: ${KTSError.message e}"

main

API Overview

Table Management

FunctionSignatureDescription
KTS.newString -> Symbol -> List Symbol -> PtrCreate a table (:set, :ordered-set, :bag, :duplicate-bag)
KTS.delete-tablePtr -> BoolDelete a table and free resources
KTS.whereisString -> Option PtrLook up a named table by name
KTS.infoPtr -> RecordGet table metadata (name, type, size)
KTS.renamePtr -> String -> BoolRename a table

Insert Operations

FunctionSignatureDescription
KTS.insertPtr -> a -> BoolInsert or overwrite a record
KTS.insert-newPtr -> a -> BoolInsert only if key doesn't exist
KTS.insert-batchPtr -> List a -> BoolBulk insert multiple records

Lookup Operations

FunctionSignatureDescription
KTS.lookupPtr -> a -> Option bLook up a record by key
KTS.has-key?Ptr -> a -> BoolCheck if a key exists

Delete Operations

FunctionSignatureDescription
KTS.deletePtr -> a -> BoolDelete a record by key
KTS.delete-recordPtr -> a -> BoolDelete a specific record (for bags)
KTS.delete-allPtr -> BoolDelete all records from a table

Query Operations

FunctionSignatureDescription
KTS.selectPtr -> (a -> Bool) -> List aSelect records matching a predicate
KTS.select-limitPtr -> (a -> Bool) -> Int -> List aSelect with a result limit
KTS.foldPtr -> b -> (a -> b -> b) -> bFold over all records
KTS.pattern-matchPtr -> a -> List bMatch records by pattern (:_ wildcard)
KTS.to-listPtr -> List aGet all records as a list
KTS.keysPtr -> List aGet all keys as a list

Utility Operations

FunctionSignatureDescription
KTS.sizePtr -> IntGet the number of records
KTS.is-empty?Ptr -> BoolCheck if the table is empty
KTS.memoryPtr -> IntGet approximate memory usage in bytes

Snapshot Operations

FunctionSignatureDescription
KTS.savePtr -> String -> Result Unit KTSErrorSave table to a JSON file
KTS.loadString -> Result Ptr KTSErrorLoad table from a JSON snapshot

Development

Running Examples

Run examples with the interpreter:

kit run examples/basic.kit
kit run examples/snapshot.kit

Compile examples to a native binary:

kit build examples/basic.kit && ./basic
kit build examples/snapshot.kit && ./snapshot

Running Tests

Run the test suite:

kit test

Run the test suite with coverage:

kit test --coverage

Running kit dev

Run the standard development workflow (format, check, test):

kit dev

This will:

  1. Format and check source files in src/
  2. Run tests in tests/ with coverage

Generating Documentation

Generate API documentation from doc comments:

kit doc

Note: Kit sources with doc comments (##) will generate HTML documents in docs/*.html

Cleaning Build Artifacts

Remove generated files, caches, and build artifacts:

kit task clean

Note: Defined in kit.toml.

Local Installation

To install this package locally for development:

kit install

This installs the package to ~/.kit/packages/@kit/kts/, making it available for import as Kit.KTS in other projects.

License

This package is released under the MIT License - see LICENSE for details.

Exported Functions & Types

KTSError

KTS error type for typed error handling.

Variants

TableCreateError {message}
TableNotFoundError {message}
InvalidKeyError {message}
TableClosedError {message}

TableType

Table type variants for different storage patterns. - Set: Unique keys, O(1) lookup (hash map) - OrderedSet: Unique keys, sorted, O(log n) lookup (balanced tree) - Bag: Duplicate keys allowed, unique values per key - DuplicateBag: Duplicate keys and values allowed

Variants

Set
OrderedSet
Bag
DuplicateBag

TableOption

Table options for configuring access and behavior. - :named - Table can be looked up by name via KTS.whereis - :public - Any process can read/write (default) - :protected - Any process can read, only owner can write - :private - Only owner can read/write

Variants

Named
Public
Protected
Private