boogie

A suite of WAL-based data stores

Pure Nim score 15/100 · tests present · no docs generated

Summary

Latest Version 0.2.2
License LGPL-3.0-or-later
CI Status Failing
Downloads 0
Last Indexed 2026-11-21 14:04

Authors

  • George Lemon

Installation

nimble install boogie
choosenim install boogie
git clone https://github.com/openpeeps/boogie

OS Compatibility

Platform Linux macOS Windows FreeBSD OpenBSD NetBSD Android iOS WASM Embedded
boogie ✓ ✓ ✓ - - - - - - -

Dependencies

Package Version Optional
nim >= 2.0.0 No
sorta >= 0.2.0 No
openparser >= 0.3.3 No
dynasm >= 0.1.0 No
db_connector >= 0.1.0 No

Source

Repository https://github.com/openpeeps/boogie
Homepage https://github.com/openpeeps/boogie
Registry Source nimble_official

README


A suite of embedded data stores in Nim with
write-ahead log (WAL) support for durability and crash recovery

nimble install boogie

API reference
Github Actions Github Actions

😍 Key Features

  • BTrees storage and Hash Tables for fast lookups
  • Write-ahead log (WAL) for durability and crash recovery, with batched group commits, CRC32 record framing, and O(1) LSN recovery
  • Simple API for inserting, updating, deleting, and querying records
  • Configurable options for performance tuning, such as batch sizes and flush intervals
  • In-memory or On-disk storage modes
  • Primitive data types (string, int, float, bool, json, null)

What's included?

  1. Key/Value Store - A simple Key-Value store implementation with WAL support, plus an optional lazy-read mode that keeps values on disk
  2. RDBMS Store - A relational WAL-based database with support for schemas, primary keys, typed columns and foreign keys with RESTRICT delete actions
  3. Vector Store - Vector store implementation with WAL support and nearest-neighbor search (cosine / dot / L2), including named partitions that bound a search to a locality scope
  4. Columnar Store - Columnar storage engine for analytics workloads with WAL support and in-memory column caching
  5. Graph Store - A simple graph database with support for nodes, relationships, and basic graph queries (e.g., neighbors, BFS) with WAL support
  6. Document Store - A flexible document store on top of the WAL, storing JSON (or BSON) documents
  7. SQL Driver - A db_connector-compatible driver (boogie/db_boogie) that compiles SQL statements to SQL bytecode and evaluates them against the RDBMS store with a built-in VM

[!NOTE] Boogie is an experimental project mostly made with the chatbot for fun and learning. It is still in early stages, so expect data loss and breaking changes. Use at your own risk.

This can be used as a simple embedded database for your Nim applications. If you want, you can use openpeeps/e2ee to encrypt the data before inserting it into Boogie database.

Examples

Every store supports an in-memory mode and a disk-backed mode with WAL, group commits, and periodic checkpoints. Benchmark numbers come from the bundled test suites (nim r -d:release tests/test_<module>.nim) on the author's development machine.

Key/Value Store

import boogie/stores/kv
import std/options

let kvStore = newInMemoryKvStore()  # or newKvStore("mydb", ksmDisk, enableWal = true)
kvStore.put("greeting", "hello")
kvStore.put("user:1", "alice")
if kvStore.hasKey("greeting"):
  echo kvStore.get("greeting").get()   # "hello"
discard kvStore.delete("user:1")       # true
for k, v in kvStore.pairsUnordered:    # unordered iteration
  echo k, " -> ", v

For very large datasets, newKvStore("mydb", ksmDisk, enableWal = true, lazyReads = true) keeps only a key → offset index in memory and reads values back from the WAL on demand. Values are never resident in RAM (0 bytes retained for 50k×256B in the benchmark), at the cost of a disk seek+read per get instead of a memory lookup.

Operation ops/s
put (memory) ~3.13 M
get (memory) ~4.75 M
delete (memory) ~4.08 M
put (disk + WAL) ~1.39 M
get (disk + WAL) ~7.52 M
delete (disk + WAL) ~2.18 M
get (disk, lazy offset read) ~0.06 M

RDBMS Store

import boogie/stores/rdbms
import std/options

let store = newStore("blogdata", smDisk, enableWal = true, checkpointEveryOps = 10)

let users = newTable(
  name = "users",
  primaryKey = "id",
  columns = [
    newColumn("id", dtInt, false),
    newColumn("name", dtText, false),
    newColumn("email", dtText, true)
  ],
  primaryKeyMode = pkmSerial
)
store.createTableIfNotExist(users)

# Auto-increment primary key
let userPk = store.insertRow("users", row({
  "name": newTextValue("Alice"),
  "email": newTextValue("alice@example.com")
}))

# Explicit primary key
store.insertRow("users", "42", row({"name": newTextValue("Bob")}))

# Point lookup + indexed equality search
echo store.getRow("users", userPk).isSome
let usersT = store.getTable("users").get()
usersT.createIndex("name")
for (pk, r) in usersT.where("name", newTextValue("Alice")):
  echo pk, " -> ", r

discard store.deleteRow("users", userPk)
store.checkpoint()
Operation ops/s
insert ~0.27 M
lookup (by pk) ~0.89 M
ordered scan ~0.12 M

Vector Store

import boogie/stores/vectorstore
import std/options

let vs = newInMemoryVectorStore()  # or newVectorStore("vecdb", smDisk, enableWal = true)
vs.createCollection(newCollection("embeddings", 3))
vs.insert("embeddings", "doc-1", @[0.1'f32, 0.2, 0.3], "tenant-7")
vs.insert("embeddings", "doc-2", @[0.9'f32, 0.8, 0.7], "tenant-7")

echo vs.get("embeddings", "doc-1").isSome
let q = @[0.11'f32, 0.19, 0.31]
# scope the search to a partition to bound the scanned candidate set
for (pk, score) in vs.nearest("embeddings", q, k = 2, dmCosine, "tenant-7"):
  echo pk, " (", score, ")"

Vectors can be grouped into named partitions (a locality scope, like a ring in KoutenDB). A partition-scoped nearest scores only that partition's rows: with 20k vectors across 100 partitions it scans 200 instead of 20,000 (99% reduction) and runs ~50x faster in the benchmark. collection.partitionSize("tenant-7") reports the bounded candidate set size.

Operation ops/s
insert ~3.22 M
get ~6.68 M
delete ~4.95 M
nearest (k=10, dim=32, 20k vectors) ~2.17 K queries/s
nearest, partition-scoped (1/100 candidate set) ~116 K queries/s

Columnar Store

import boogie/stores/columnar
import std/json

var s = openColumnarStore("analytics")

s.createTable(TableSchema(
  name: "events",
  primaryKey: "id",
  rowCount: 0,
  columns: @[
    ColumnSchema(name: "id", kind: ctInt64, nullable: false, codec: ccNone),
    ColumnSchema(name: "user", kind: ctString, nullable: false, codec: ccNone),
    ColumnSchema(name: "amount", kind: ctFloat64, nullable: false, codec: ccNone)
  ]
))

s.insertBatch("events", @[
  %*{"id": 1, "user": "alice", "amount": 10.5},
  %*{"id": 2, "user": "bob", "amount": 25.0}
])

# Projection + filter
let rows = s.scan("events", @["id", "user"], filters = @[
  Filter(column: "amount", op: foGt, value: newJFloat(10.0))
])

# Aggregation
let ag = s.aggregate("events", @[
  AggregateSpec(column: "", kind: akCount, alias: "cnt"),
  AggregateSpec(column: "amount", kind: akAvg, alias: "avg_amount")
])
echo ag["cnt"].getInt()          # 2

Columns are parsed from disk once and cached in memory, so repeated scans/filters over the same columns are much faster than the first (cold) scan.

Operation ops/s
insert (batch) ~0.36 M
scan (cold, first load) ~1.10 M
scan (warm, cached) ~2.68 M
filter (cached) ~3.75 M

Graph Store

import boogie/stores/graphstore
import std/json

var gs = openGraphStore("graphdb")

var tx = beginTx(gs)
let alice = createNode(tx, @["Person"], %*{"name": "Alice"})
let bob   = createNode(tx, @["Person"], %*{"name": "Bob"})
discard createRelationship(tx, alice, bob, "KNOWS", %*{"since": 2024})
commit(tx)

echo gs.neighbors(alice)          # @[bob]
for r in gs.outgoing(alice, "KNOWS"):
  echo r.toId
for n in gs.findNodesByLabel("Person"):
  echo n.id
echo gs.traverseBfs(alice, maxDepth = 2).len

closeGraphStore(gs)
Operation ops/s
commit (nodes + rels) ~0.11 M
getNode ~6 M
neighbors ~4 M
findNodesByLabel (5k nodes) ~330 µs

Document Store

import boogie/stores/docstore
import std/json, std/options

var store = openDocumentStore("doctest", name = "documents", defaultEncoding = deJson)
store.insert("k1", %*{"name": "Alice", "age": 30})
store.upsert("k1", %*{"name": "Alice", "age": 31})

if store.hasKey("k1"):
  echo store.get("k1").get()["age"]   # 31
discard store.delete("k1")
store.checkpoint()
Operation ops/s
insert ~0.92 M
get ~6.90 M
lookup ~15.7 M
delete ~1.55 M

SQL Driver (db_connector compatible)

boogie/db_boogie mirrors the db_connector/db_sqlite API, so applications can switch to Boogie by changing a single import. Statements are parsed with openparser/sql, lowered to SQL bytecode (WHERE predicates run as real VM loops using NULL-aware comparison opcodes), and evaluated against the RDBMS store, with WAL durability and crash recovery for free.

import boogie/db_boogie

let db = open("mydb", "", "", "app")  # or open(":memory:", "", "", "")
db.exec(sql"CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  age INT DEFAULT 0)")

db.exec(sql"INSERT INTO users (name, age) VALUES (?, ?)", "alice", "30")
echo db.insertID(sql"INSERT INTO users (name) VALUES (?)", "bob")

for row in db.fastRows(sql"SELECT id, name FROM users WHERE age > ? ORDER BY name", "18"):
  echo row

# prepared statements with typed bindings
let ps = db.prepare("UPDATE users SET age = age + 1 WHERE id = ?")
ps.bindParam(1, 1'i64)
db.exec(ps)

echo db.getValue(sql"SELECT COUNT(*) FROM users")
db.close()

Supported SQL, by example:

-- tables: typed columns, PRIMARY KEY, NOT NULL, DEFAULT, inline REFERENCES
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INT DEFAULT 0);
CREATE TABLE posts (id INTEGER PRIMARY KEY, author INT REFERENCES users (id), body TEXT);
DROP TABLE IF EXISTS posts;

-- secondary indexes (single-column, persisted; UNIQUE is enforced)
CREATE UNIQUE INDEX idx_user_name ON users (name);
DROP INDEX idx_user_name;

-- schema evolution
ALTER TABLE users ADD COLUMN email TEXT NOT NULL DEFAULT '';
ALTER TABLE users RENAME COLUMN email TO mail;
ALTER TABLE users RENAME TO accounts;

-- inserts: multi-row, serial PKs, placeholders
INSERT INTO users (name, age) VALUES ('alice', 30), ('bob', 25);
INSERT INTO users (name) VALUES (?);

-- filtering, ordering, paging
SELECT id, name FROM users WHERE age BETWEEN 18 AND 65 ORDER BY name LIMIT 10 OFFSET 5;
SELECT DISTINCT age FROM users WHERE name LIKE 'a%' AND age IS NOT NULL;

-- aggregates, grouping, having
SELECT COUNT(*) FROM users;
SELECT age, COUNT(*), AVG(age) FROM users GROUP BY age HAVING COUNT(*) > 1;

-- joins (INNER/CROSS/LEFT/RIGHT/FULL, aliases, ON/USING, outer joins keep
-- unmatched rows with NULLs); aggregates and GROUP BY work over joins too
SELECT u.name, p.body FROM users u JOIN posts p ON p.author = u.id;
SELECT u.name, p.body FROM users u LEFT JOIN posts p ON p.author = u.id;
SELECT a.id, b.id FROM a FULL JOIN b USING (id);

-- set operations (trailing ORDER BY/LIMIT applies to the whole union)
SELECT name FROM users UNION ALL SELECT name FROM admins ORDER BY name LIMIT 5;
SELECT name FROM users UNION SELECT name FROM admins;

-- updates and deletes
UPDATE users SET age = age + 1 WHERE id = ?;
DELETE FROM users WHERE age < 18;

Expressions support comparisons, AND/OR/NOT, arithmetic, IS [NOT] NULL, LIKE/ILIKE (%/_ wildcards), BETWEEN, single-value IN (v), and ?/$n/:name placeholders.

Not supported yet: subqueries (incl. IN (SELECT ...)), multi-value IN (a, b) (cannot parse upstream), DROP COLUMN (dropped by the upstream parser), CASE, CHECK constraints, and INSERT INTO ... SELECT.

[!NOTE] Statements are parsed once per connection and cached; exec() binds its arguments as typed values so repeated calls hit the plan cache. Single-statement INSERT/SELECT/UPDATE/DELETE run through native fast paths (point lookups by primary key, indexed equality via the store's equality index, direct row access) and only complex shapes fall back to the VM loop. Numeric comparisons follow SQLite column-affinity rules (text operands coerce when they parse) and NULL propagates per SQL semantics.

SQL benchmarks (tests/test_sqlengine_bench.nim, -d:release, Apple Silicon; re-run on your machine — single-threaded, page-cache warm):

Operation (in-memory) Throughput
insert ~136 K ops/s
point query by PK (prepared rebind) ~300 K q/s
point query (indexed equality) ~187 K q/s
point query (unindexed scan) ~2.6 K q/s
full scan (SELECT *) ~730 K rows/s
range scan (ORDER BY + LIMIT) ~13 K rows/s
ORDER BY (full sort) ~700 K rows/s
update ~48 K ops/s
delete ~11 K ops/s
aggregates (COUNT/SUM/AVG/MIN/MAX over 5 K rows) ~0.02 s
DISTINCT ~18 K unique/s
Operation (disk + WAL) Throughput
insert ~155 K ops/s
point query ~165 q/s
full scan ~660 K rows/s
update ~45 K ops/s
delete ~11 K ops/s
aggregates (over 5 K rows) ~0.02 s

[!TIP] Run the full test + benchmark suites with nimble test -d:release (the -d:release flag is required for accurate benchmarks).

❤ Contributions & Support

🎩 License

MIT license. Made by Humans from OpenPeeps.
Copyright OpenPeeps & Contributors — All rights reserved.