package snkv
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
On This Page
OCaml bindings for SNKV key-value store
Install
dune-project
Dependency
Authors
Maintainers
Sources
0.1.1.tar.gz
md5=975c7022f2c5422f8cd4658fa17294da
sha512=73fa70eb9bb5c9f09f2d5a7f01dfc3eba53e85624c61541a82a4e48461d18e8a9ba57d8cc293408a36cdc57b2bcd82b9a28ecd4eba297a95c9d8f8406fa882d5
doc/README.html
SNKV OCaml Bindings
OCaml FFI bindings and high-level API for SNKV, a persistent, ACID-compliant key-value store built on SQLite's B-tree engine.
Features
- Full SNKV API coverage: All C library functions available through OCaml bindings
- Two-level API: Low-level direct bindings and high-level OCaml-style API with algebraic types
- Column families: Logical namespaces for data organization
- TTL support: Time-to-live with automatic expiration
- Encryption: Password-based authenticated encryption (XChaCha20-Poly1305)
- Transactions: ACID-compliant transactions with read/write modes
- Iterators: Forward, reverse, and prefix-based iteration
- Statistics: Runtime performance and usage metrics
Requirements
- OCaml 5.0+ (tested with 5.4.1)
- Dune 3.21+
- C compiler (GCC or Clang)
- SQLite development headers (included in SNKV amalgamation)
Installation
From OPAM (recommended)
opam install snkvFrom Source
git clone https://codeberg.org/tomaszb/snkv-ocaml.git
cd snkv-ocaml
dune build
dune installFor Development
git clone https://codeberg.org/tomaszb/snkv-ocaml.git
cd snkv-ocaml
opam pin add snkv .Quick Start
Using the High-Level API (Recommended)
open Snkv.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 dbUsing the Low-Level API
open 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 dbDocumentation
Comprehensive API documentation is available:
- Low-Level API - Direct C FFI bindings
- High-Level API - OCaml-style API with algebraic types and error handling
- Examples - Complete example program
You can also generate API documentation locally:
dune build @doc
# Open _build/default/_doc/_html/index.htmlExamples
The project includes a complete example demonstrating all features:
dune exec snkvThis runs the example program in bin/main.ml which covers:
- Basic key-value operations
- Column family management
- Iterator usage with higher-order functions
- TTL operations with expiration
- Transaction handling
- Statistics collection
- Encryption API (create, re-encrypt, remove)
Project Structure
snkv-ocaml/
├── lib/
│ ├── snkv.ml[i] # Low-level C FFI bindings
│ ├── kv.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 configurationTesting
Run the test suite:
dune runtestOr run specific tests:
dune exec test/test_snkv.exeError Handling
The 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 malformed- etc.
Encryption Support
SNKV 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;Performance
The bindings have minimal overhead:
- Direct C function calls through FFI
- Zero-copy for key/value data where possible
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request on Codeberg.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Support
- Issues: Codeberg Issues
- Source: Codeberg Repository
Acknowledgments
- SNKV - The underlying C key-value store
- SQLite - The embedded database engine
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
On This Page