Page
Library
Module
Module type
Parameter
Class
Class type
Source
OCaml FFI bindings and high-level API for SNKV, a persistent, ACID-compliant key-value store built on SQLite's B-tree engine.
opam install snkvgit clone https://codeberg.org/tomaszb/snkv-ocaml.git
cd snkv-ocaml
dune build
dune installgit clone https://codeberg.org/tomaszb/snkv-ocaml.git
cd snkv-ocaml
opam pin add snkv .open Kv
(* Open an in-memory database *)
let db = open_exn () in
(* Basic operations *)
let () = put db ~key:"hello" ~value:"world" |> Result.get_ok in
let value = get db ~key:"hello" |> Result.get_ok in
print_endline value; (* "world" *)
(* TTL operations *)
let now = TTL.now_ms () in
let () = TTL.put db ~key:"temp" ~value:"data" ~expire_ms:(Int64.add now 5000L)
|> Result.get_ok in
(* Column families *)
let cf = CF.create db "mycf" |> Result.get_ok in
let () = CF.put cf ~key:"foo" ~value:"bar" |> Result.get_ok in
(* Transactions *)
let () = Txn.with_transaction db Txn.Write (fun () ->
put db ~key:"tx1" ~value:"val1"
) |> Result.get_ok in
close dbopen Snkv
(* Direct C bindings *)
let db = open_ () in
put db ~key:"hello" ~value:"world";
let value = get db ~key:"hello" in
print_endline value;
close dbComprehensive API documentation is available:
You can also generate API documentation locally:
dune build @doc
# Open _build/default/_doc/_html/index.htmlThe project includes a complete example demonstrating all features:
dune exec snkvThis runs the example program in bin/main.ml which covers:
snkv-ocaml/
├── lib/
│ ├── snkv.ml[i] # Low-level C FFI bindings
│ ├── snkv_api.ml[i] # High-level OCaml API
│ └── snkv_stubs.c # C implementation
├── bin/
│ └── main.ml # Example program
├── test/
│ └── test_snkv.ml # Test suite
├── doc/ # Documentation
└── dune-project # Build configurationRun the test suite:
dune runtestOr run specific tests:
dune exec test/test_snkv.exeThe high-level API uses OCaml's result type:
match Kv.put db ~key:"test" ~value:"data" with
| Ok () -> print_endline "Success"
| Error e -> Printf.printf "Error: %s\n" (Kv.error_to_string e)Error codes are mapped to algebraic types:
Ok - SuccessError - Generic errorBusy - Database locked by another connectionNotFound - Key or column family not foundAuthFailed - Wrong password or corrupted encrypted storeCorrupt - Database file is malformedSNKV supports password-based authenticated encryption:
(* Create encrypted store *)
let db = Encryption.open_exn ~filename:"secure.db" ~password:"secret" in
(* Check encryption status *)
let encrypted = Encryption.is_encrypted db in
(* Re-encrypt with new password *)
Encryption.reencrypt db ~password:"newsecret" |> Result.get_ok;
(* Remove encryption *)
Encryption.remove_encryption db |> Result.get_ok;The bindings have minimal overhead:
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request on Codeberg.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)