lmdb

LMDB embedded key-value store bindings for Kit

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
c/kit_lmdb.cC FFI wrapper
c/kit_lmdb.hC header for FFI wrapper
examples/basic.kitBasic usage example
kit.tomlPackage manifest with metadata and dependencies
src/lmdb.kitKit LMDB - Lightning Memory-Mapped Database bindings
tests/lmdb.test.kitTests for lmdb

Dependencies

No Kit package dependencies.

Installation

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

System Requirements

PlatformCommand
macOSbrew install lmdb
Ubuntusudo apt install liblmdb-dev
Fedorasudo dnf install lmdb-devel

Usage

import Kit.LMDB as Lmdb

main = fn =>
  println ("LMDB version: " ++ Lmdb.version())

  match Lmdb.open "/tmp/my-database"
    | Ok db ->
      # Store values
      Lmdb.put db "name" "Kit"
      Lmdb.put db "version" "0.1.0"

      # Read values
      match Lmdb.get db "name"
        | Some val -> println ("name: " ++ val)
        | None -> println "name: not found"

      # Check key existence
      if Lmdb.has-key? db "name" then
        println "'name' exists"

      # Count entries
      println ("Entries: " ++ (show (Lmdb.count db)))

      # Update a value
      Lmdb.put db "version" "0.2.0"

      # Delete a key
      Lmdb.delete db "version"

      Lmdb.close db
    | Err e -> println ("Error: " ++ e)

main

Development

Running Examples

Run examples with the interpreter:

kit run examples/basic.kit

Compile examples to a native binary:

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

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/lmdb/, making it available for import as Kit.LMDB in other projects.

License

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

LMDB is released under the OpenLDAP Public License.

Exported Functions & Types

open

Open an LMDB database environment.

Creates the directory if it doesn't exist. Uses a default map size of 10 MB (sufficient for most use cases; increase for larger databases).

Parameters:

Returns:

String -> Result Ptr String

match open "/tmp/mydb"
  | Ok db ->
    # ... use database ...
    close db
  | Err e -> println "Error: ${e}"

open-with-size

Open an LMDB database with a custom map size.

Parameters:

Returns:

String -> Int -> Result Ptr String

# Open with 1 GB map size
match open-with-size "/tmp/largedb" 1073741824
  | Ok db ->
    # ... use database ...
    close db
  | Err e -> println "Error: ${e}"

close

Close the database and release resources.

Parameters:

Ptr -> Unit

close db

put

Store a key-value pair in the database.

If the key already exists, its value is overwritten.

Parameters:

Returns:

Ptr -> String -> String -> Result Unit String

match put db "user:1" "Alice"
  | Ok _ -> println "Stored!"
  | Err e -> println "Error: ${e}"

get

Get a value by key from the database.

Parameters:

Returns:

Ptr -> String -> Option String

match get db "user:1"
  | Some val -> println ("Found: " ++ val)
  | None -> println "Not found"

delete

Delete a key from the database.

Parameters:

Returns:

Ptr -> String -> Result Unit String

match delete db "user:1"
  | Ok _ -> println "Deleted!"
  | Err e -> println "Error: ${e}"

has-key?

Check if a key exists in the database.

Parameters:

Returns:

Ptr -> String -> Bool

if has-key? db "user:1" then
  println "Key exists"

list-keys

List all keys in the database.

Parameters:

Returns:

Ptr -> [String]

keys = list-keys db
List.map (fn(k) => println k) keys

count

Count the number of entries in the database.

Parameters:

Returns:

Ptr -> Int

println ("Entries: " ++ (show (count db)))

version

Get the LMDB library version as a string.

Returns:

String

println ("LMDB: " ++ version())