Skip to content

Commit 22aa544

Browse files
committed
switch to pkg/sorta
Signed-off-by: George Lemon <georgelemon@protonmail.com>
1 parent dc6393b commit 22aa544

4 files changed

Lines changed: 12 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</p>
1414

1515
## 😍 Key Features
16-
- Red-black tree ordered storage of rows and Hash Tables for fast lookups
16+
- BTrees storage and Hash Tables for fast lookups
1717
- Write-ahead log (WAL) for durability and crash recovery
1818
- Simple API for inserting, updating, deleting, and querying records
1919
- Configurable options for performance tuning, such as batch sizes and flush intervals
@@ -23,9 +23,6 @@
2323
>[!NOTE]
2424
> 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.
2525
26-
>[!NOTE]
27-
> Fun fact, this package is using [pkg/rbtree](https://github.com/Nycto/RBTreeNim), a 11-year-old Nim package that implements a red-black tree. It is a great example of how the Nim ecosystem has some hidden gems that can be used in new projects. 📦
28-
2926
This can be used as a simple embedded database for your Nim applications. If you want, you can use [openpeeps/e2ee](https://github.com/openpeeps/e2ee) to encrypt the data before inserting it into Boogie database.
3027

3128
## Examples

boogie.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ srcDir = "src"
1010
# Dependencies
1111

1212
requires "nim >= 2.2.0"
13-
requires "rbtree"
13+
requires "sorta"
1414
requires "flatty"

src/boogie.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Made by Humans from OpenPeeps
55
# https://github.com/openpeeps/boogie
66

7-
import pkg/rbtree
7+
import pkg/sorta
88
import ./boogie/[store, wal]
99

10-
export rbtree, store, wal
10+
export sorta, store, wal

src/boogie/store.nim

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import std/[tables, options, strformat,
88
json, strutils, os, sets]
99

10-
import pkg/[flatty, rbtree]
10+
import pkg/[flatty, sorta]
1111
import ./wal
1212

1313
## This module implements a simple WAL-based embedded database for Nim.
@@ -66,7 +66,7 @@ type
6666
pk: string
6767
cols: RowData
6868

69-
RowIndex* = RedBlackTree[RowRecord, string]
69+
RowIndex* = SortedTable[RowRecord, string]
7070
## An ordered index of rows in a table, keyed by primary key value. This allows for efficient
7171
## ordered iteration and range queries.
7272

@@ -142,7 +142,7 @@ proc cmp(a, b: RowRecord): int = cmp(a.pk, b.pk)
142142
proc extract(r: RowRecord): string = r.pk
143143

144144
proc `==`*(a, b: RowRecord): bool =
145-
## Equality comp for RowRecord. Used by the RBTree to determine
145+
## Equality comp for RowRecord. Used by the SortedTable to determine
146146
## if two records are the same. We consider two records equal if
147147
## their primary keys are equal, since the primary key is the unique
148148
## identifier for a row.
@@ -268,7 +268,7 @@ proc newTable*(name: string, primaryKey: string, columns: openArray[ColumnDef],
268268
pkSequence: 0'u64,
269269
columns: colsSeq,
270270
columnsByName: colsByName,
271-
rows: newRBTree[RowRecord, string](),
271+
rows: initSortedTable[RowRecord, string](),
272272
)
273273
else:
274274
DbTable(
@@ -277,7 +277,7 @@ proc newTable*(name: string, primaryKey: string, columns: openArray[ColumnDef],
277277
pkType: pkmManual,
278278
columns: colsSeq,
279279
columnsByName: colsByName,
280-
rows: newRBTree[RowRecord, string](),
280+
rows: initSortedTable[RowRecord, string](),
281281
)
282282

283283
proc newColumn*(name: string, kind: DataType, nullable: bool): ColumnDef =
@@ -527,9 +527,9 @@ proc ensureOrderIndex(t: DbTable) =
527527
# be called before any operation that requires ordered access to rows.
528528
if not t.orderIndexDirty:
529529
return
530-
t.rows = newRBTree[RowRecord, string]()
530+
t.rows = initSortedTable[RowRecord, string]()
531531
for _, rec in t.rowsByPk.pairs:
532-
t.rows.insert(rec)
532+
t.rows[rec] = rec.pk
533533
t.orderIndexDirty = false
534534

535535
proc insertRowNoWal(t: DbTable, pk: string, data: RowData): string =
@@ -736,7 +736,7 @@ iterator allRows*(t: DbTable): (string, RowData) =
736736
## Iterate over all rows in the table in primary key order. This will be fast
737737
## if the order index is clean, but may be slower if the index needs to be rebuilt.
738738
t.ensureOrderIndex()
739-
for rec in t.rows:
739+
for rec in t.rows.keys:
740740
yield (rec.pk, rec.cols)
741741

742742
iterator allRowsByPk*(t: DbTable): (string, RowData) =

0 commit comments

Comments
 (0)