rosedb 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +138 -9
- package/assets/benchmark.svg +122 -0
- package/index.ts +168 -52
- package/librosedb_node.so +0 -0
- package/node.mjs +518 -0
- package/package.json +26 -9
package/README.md
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
# RoséDB
|
|
2
2
|
|
|
3
|
-
High-performance embedded Key-Value & Document database engine written in **Rust** with native **Bun
|
|
3
|
+
High-performance embedded Key-Value & Document database engine written in **Rust** with native bindings for **Bun**, **Node.js**, **Go**, and **Python**.
|
|
4
|
+
|
|
5
|
+
- **Source Code (GitHub)**: [https://github.com/Blckrose08/rosedb](https://github.com/Blckrose08/rosedb)
|
|
6
|
+
- **Package (npm)**: [https://www.npmjs.com/package/rosedb](https://www.npmjs.com/package/rosedb)
|
|
4
7
|
|
|
5
8
|
## Fitur Utama
|
|
6
9
|
|
|
@@ -10,7 +13,11 @@ High-performance embedded Key-Value & Document database engine written in **Rust
|
|
|
10
13
|
- **Sharded LRU Cache (`rosedb-cache`)**: 32-shard bounded LRU cache (`max_memory`, `max_entries`) untuk skalabilitas multi-reader.
|
|
11
14
|
- **MVCC & Snapshot Isolation (`rosedb-core`)**: Transaksi ACID, `db.snapshot()` untuk pembacaan konsisten tanpa memblokir writer, serta automatic garbage collection versi lama saat snapshot ditutup.
|
|
12
15
|
- **Keamanan (`rosedb-security`)**: Verifikasi integritas **CRC32C**, enkripsi opsional **AES-256-GCM** at rest dengan nonce acak per record, key derivation **Argon2id**, validasi batas key/value, proteksi path traversal, dan file permission ketat (`0o700` / `0o600`).
|
|
13
|
-
- **
|
|
16
|
+
- **Multi-Language Bindings**:
|
|
17
|
+
- **Bun**: `bun:ffi` (`bindings/bun/index.ts`)
|
|
18
|
+
- **Node.js (>= 18)**: `koffi` (`bindings/bun/node.mjs`)
|
|
19
|
+
- **Go**: `cgo` (`bindings/go`)
|
|
20
|
+
- **Python (>= 3.9)**: `ctypes` zero-dependency (`bindings/python`)
|
|
14
21
|
|
|
15
22
|
---
|
|
16
23
|
|
|
@@ -28,7 +35,9 @@ npm install rosedb
|
|
|
28
35
|
|
|
29
36
|
---
|
|
30
37
|
|
|
31
|
-
##
|
|
38
|
+
## Contoh Kode Setiap Bahasa
|
|
39
|
+
|
|
40
|
+
### 1. Bun / TypeScript
|
|
32
41
|
|
|
33
42
|
```ts
|
|
34
43
|
import { Database } from "rosedb";
|
|
@@ -65,9 +74,41 @@ console.log(db.integrityCheck());
|
|
|
65
74
|
db.close();
|
|
66
75
|
```
|
|
67
76
|
|
|
68
|
-
|
|
77
|
+
### 2. Node.js (ESM, `>= 18.0.0`)
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
import { Database } from "rosedb";
|
|
81
|
+
|
|
82
|
+
const db = new Database("./rose-data", {
|
|
83
|
+
sync_on_commit: true,
|
|
84
|
+
encryption_password: "optional-secret-password",
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
db.put("username", "Blckrose");
|
|
88
|
+
console.log(db.get("username"));
|
|
89
|
+
|
|
90
|
+
await db.putAsync("role", "developer");
|
|
91
|
+
console.log(await db.getAsync("role"));
|
|
92
|
+
|
|
93
|
+
db.batch([
|
|
94
|
+
{ op: "put", key: "item:1", value: "alpha" },
|
|
95
|
+
{ op: "put", key: "item:2", value: "beta" },
|
|
96
|
+
]);
|
|
69
97
|
|
|
70
|
-
|
|
98
|
+
console.log(db.mget(["item:1", "item:2", "missing"]));
|
|
99
|
+
console.log(db.scan("item:1", "item:2"));
|
|
100
|
+
|
|
101
|
+
const snap = db.snapshot();
|
|
102
|
+
db.put("username", "Updated");
|
|
103
|
+
console.log("Current:", db.get("username"));
|
|
104
|
+
console.log("Snapshot:", snap.get("username"));
|
|
105
|
+
snap.close();
|
|
106
|
+
|
|
107
|
+
db.checkpoint();
|
|
108
|
+
db.close();
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### 3. Rust
|
|
71
112
|
|
|
72
113
|
```rust
|
|
73
114
|
use rosedb_core::Database;
|
|
@@ -87,21 +128,109 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
87
128
|
assert_eq!(db.get("username")?.as_deref(), Some(b"Updated".as_slice()));
|
|
88
129
|
snap.close();
|
|
89
130
|
|
|
131
|
+
db.checkpoint()?;
|
|
90
132
|
db.close()?;
|
|
91
133
|
Ok(())
|
|
92
134
|
}
|
|
93
135
|
```
|
|
94
136
|
|
|
137
|
+
### 4. Go (`cgo`)
|
|
138
|
+
|
|
139
|
+
```go
|
|
140
|
+
package main
|
|
141
|
+
|
|
142
|
+
import (
|
|
143
|
+
"fmt"
|
|
144
|
+
"log"
|
|
145
|
+
"rosedb"
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
func main() {
|
|
149
|
+
pwd := "optional-secret-password"
|
|
150
|
+
db, err := rosedb.Open("./rose-data", &rosedb.Options{
|
|
151
|
+
EncryptionPassword: &pwd,
|
|
152
|
+
})
|
|
153
|
+
if err != nil {
|
|
154
|
+
log.Fatal(err)
|
|
155
|
+
}
|
|
156
|
+
defer db.Close()
|
|
157
|
+
|
|
158
|
+
if err := db.PutString("username", "Blckrose"); err != nil {
|
|
159
|
+
log.Fatal(err)
|
|
160
|
+
}
|
|
161
|
+
val, ok, err := db.GetString("username")
|
|
162
|
+
fmt.Println(val, ok, err)
|
|
163
|
+
|
|
164
|
+
_ = db.Batch([]rosedb.BatchOp{
|
|
165
|
+
{Op: "put", Key: "user:1", Value: "Alice"},
|
|
166
|
+
{Op: "put", Key: "user:2", Value: "Bob"},
|
|
167
|
+
})
|
|
168
|
+
entries, _ := db.Scan([]byte("user:1"), []byte("user:2"), 0)
|
|
169
|
+
fmt.Println("Scan:", entries)
|
|
170
|
+
|
|
171
|
+
snap, _ := db.Snapshot()
|
|
172
|
+
_ = db.PutString("username", "Updated")
|
|
173
|
+
snapVal, _, _ := snap.Get([]byte("username"))
|
|
174
|
+
fmt.Println("Snapshot:", string(snapVal))
|
|
175
|
+
snap.Close()
|
|
176
|
+
|
|
177
|
+
_ = db.Checkpoint()
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### 5. Python (`ctypes`, `>= 3.9`)
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
from rosedb import Database
|
|
185
|
+
|
|
186
|
+
with Database("./rose-data", {"encryption_password": "optional-secret-password"}) as db:
|
|
187
|
+
db.put("username", "Blckrose")
|
|
188
|
+
print(db.get("username"))
|
|
189
|
+
|
|
190
|
+
db.batch([
|
|
191
|
+
{"op": "put", "key": "item:1", "value": "alpha"},
|
|
192
|
+
{"op": "put", "key": "item:2", "value": "beta"},
|
|
193
|
+
])
|
|
194
|
+
print(db.mget(["item:1", "item:2", "missing"]))
|
|
195
|
+
print(db.scan("item:1", "item:2"))
|
|
196
|
+
|
|
197
|
+
with db.snapshot() as snap:
|
|
198
|
+
db.put("username", "Updated")
|
|
199
|
+
print("Current:", db.get("username"))
|
|
200
|
+
print("Snapshot:", snap.get("username"))
|
|
201
|
+
|
|
202
|
+
db.checkpoint()
|
|
203
|
+
print(db.stats())
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
---
|
|
207
|
+
|
|
208
|
+
## Benchmark Lintas Bahasa
|
|
209
|
+
|
|
210
|
+

|
|
211
|
+
|
|
212
|
+
Pengujian dijalankan pada hardware yang sama (`Intel Xeon E5-2695 v4`, value `128 byte`, mode release):
|
|
213
|
+
|
|
214
|
+
| Operasi | Rust Native | Go (`cgo`) | Bun (`bun:ffi`) | Node.js (`koffi`) | Python (`ctypes`) |
|
|
215
|
+
| --- | --- | --- | --- | --- | --- |
|
|
216
|
+
| **Single `put` (`sync=true`)** | **182.031 ops/s** | 172.124 ops/s | 112.179 ops/s | 110.889 ops/s | 100.607 ops/s |
|
|
217
|
+
| **Single `put` (`sync=false`)** | 438.671 ops/s | **453.520 ops/s** | 307.767 ops/s | 275.484 ops/s | 163.529 ops/s |
|
|
218
|
+
| **Batch 1.000 (`sync=false`)** | **510.010 ops/s** | 289.120 ops/s | 306.689 ops/s | 377.038 ops/s | 238.101 ops/s |
|
|
219
|
+
| **Random `get`** | **1.527.514 ops/s** | 846.064 ops/s | 510.442 ops/s | 596.199 ops/s | 97.142 ops/s |
|
|
220
|
+
| **Range `scan` (1.000 rows)** | 2.070.149 rows/s | **3.740.054 rows/s** | 1.377.148 rows/s | 1.201.920 rows/s | 965.965 rows/s |
|
|
221
|
+
|
|
95
222
|
---
|
|
96
223
|
|
|
97
|
-
## Build dari Source &
|
|
224
|
+
## Build dari Source & Pengujian
|
|
98
225
|
|
|
99
226
|
```bash
|
|
227
|
+
git clone https://github.com/Blckrose08/rosedb.git
|
|
228
|
+
cd rosedb
|
|
100
229
|
cargo build --release
|
|
101
230
|
cargo test --workspace
|
|
102
|
-
cd bindings/bun
|
|
103
|
-
|
|
104
|
-
|
|
231
|
+
cd bindings/bun && bun test && node --test node.test.mjs
|
|
232
|
+
cd ../go && go test -v ./...
|
|
233
|
+
cd ../python && python3 -m unittest discover -s tests -v
|
|
105
234
|
```
|
|
106
235
|
|
|
107
236
|
---
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="920" height="520" viewBox="0 0 920 520" fill="none">
|
|
2
|
+
<rect width="920" height="520" rx="16" fill="#0F172A"/>
|
|
3
|
+
<text x="36" y="44" fill="#F8FAFC" font-family="system-ui, -apple-system, Segoe UI, sans-serif" font-size="22" font-weight="700">RoséDB Multi-Language Benchmark (128B Value, Release)</text>
|
|
4
|
+
<text x="36" y="68" fill="#94A3B8" font-family="system-ui, -apple-system, Segoe UI, sans-serif" font-size="13">Intel Xeon E5-2695 v4 • Throughput in ops/sec or rows/sec (higher is better)</text>
|
|
5
|
+
|
|
6
|
+
<g font-family="system-ui, -apple-system, Segoe UI, sans-serif" font-size="12" font-weight="600">
|
|
7
|
+
<rect x="36" y="88" width="12" height="12" rx="3" fill="#F97316"/>
|
|
8
|
+
<text x="54" y="98" fill="#E2E8F0">Rust Native</text>
|
|
9
|
+
<rect x="150" y="88" width="12" height="12" rx="3" fill="#06B6D4"/>
|
|
10
|
+
<text x="168" y="98" fill="#E2E8F0">Go (cgo)</text>
|
|
11
|
+
<rect x="245" y="88" width="12" height="12" rx="3" fill="#F43F5E"/>
|
|
12
|
+
<text x="263" y="98" fill="#E2E8F0">Bun (bun:ffi)</text>
|
|
13
|
+
<rect x="365" y="88" width="12" height="12" rx="3" fill="#22C55E"/>
|
|
14
|
+
<text x="383" y="98" fill="#E2E8F0">Node.js (koffi)</text>
|
|
15
|
+
<rect x="495" y="88" width="12" height="12" rx="3" fill="#EAB308"/>
|
|
16
|
+
<text x="513" y="98" fill="#E2E8F0">Python (ctypes)</text>
|
|
17
|
+
</g>
|
|
18
|
+
|
|
19
|
+
<g transform="translate(36, 125)">
|
|
20
|
+
<rect width="410" height="175" rx="12" fill="#1E293B" stroke="#334155"/>
|
|
21
|
+
<text x="18" y="28" fill="#F8FAFC" font-family="system-ui, sans-serif" font-size="14" font-weight="700">Single Put (sync=false, ops/s)</text>
|
|
22
|
+
<g font-family="system-ui, sans-serif" font-size="11" fill="#CBD5E1">
|
|
23
|
+
<text x="18" y="58">Rust</text>
|
|
24
|
+
<rect x="82" y="47" width="232" height="14" rx="4" fill="#F97316"/>
|
|
25
|
+
<text x="322" y="58" fill="#F8FAFC" font-weight="600">438,671</text>
|
|
26
|
+
|
|
27
|
+
<text x="18" y="83">Go</text>
|
|
28
|
+
<rect x="82" y="72" width="240" height="14" rx="4" fill="#06B6D4"/>
|
|
29
|
+
<text x="330" y="83" fill="#F8FAFC" font-weight="600">453,520</text>
|
|
30
|
+
|
|
31
|
+
<text x="18" y="108">Bun</text>
|
|
32
|
+
<rect x="82" y="97" width="163" height="14" rx="4" fill="#F43F5E"/>
|
|
33
|
+
<text x="253" y="108" fill="#F8FAFC" font-weight="600">307,767</text>
|
|
34
|
+
|
|
35
|
+
<text x="18" y="133">Node.js</text>
|
|
36
|
+
<rect x="82" y="122" width="146" height="14" rx="4" fill="#22C55E"/>
|
|
37
|
+
<text x="236" y="133" fill="#F8FAFC" font-weight="600">275,484</text>
|
|
38
|
+
|
|
39
|
+
<text x="18" y="158">Python</text>
|
|
40
|
+
<rect x="82" y="147" width="87" height="14" rx="4" fill="#EAB308"/>
|
|
41
|
+
<text x="177" y="158" fill="#F8FAFC" font-weight="600">163,529</text>
|
|
42
|
+
</g>
|
|
43
|
+
</g>
|
|
44
|
+
|
|
45
|
+
<g transform="translate(474, 125)">
|
|
46
|
+
<rect width="410" height="175" rx="12" fill="#1E293B" stroke="#334155"/>
|
|
47
|
+
<text x="18" y="28" fill="#F8FAFC" font-family="system-ui, sans-serif" font-size="14" font-weight="700">Batch 1,000 (sync=false, ops/s)</text>
|
|
48
|
+
<g font-family="system-ui, sans-serif" font-size="11" fill="#CBD5E1">
|
|
49
|
+
<text x="18" y="58">Rust</text>
|
|
50
|
+
<rect x="82" y="47" width="240" height="14" rx="4" fill="#F97316"/>
|
|
51
|
+
<text x="330" y="58" fill="#F8FAFC" font-weight="600">510,010</text>
|
|
52
|
+
|
|
53
|
+
<text x="18" y="83">Node.js</text>
|
|
54
|
+
<rect x="82" y="72" width="177" height="14" rx="4" fill="#22C55E"/>
|
|
55
|
+
<text x="267" y="83" fill="#F8FAFC" font-weight="600">377,038</text>
|
|
56
|
+
|
|
57
|
+
<text x="18" y="108">Bun</text>
|
|
58
|
+
<rect x="82" y="97" width="144" height="14" rx="4" fill="#F43F5E"/>
|
|
59
|
+
<text x="234" y="108" fill="#F8FAFC" font-weight="600">306,689</text>
|
|
60
|
+
|
|
61
|
+
<text x="18" y="133">Go</text>
|
|
62
|
+
<rect x="82" y="122" width="136" height="14" rx="4" fill="#06B6D4"/>
|
|
63
|
+
<text x="226" y="133" fill="#F8FAFC" font-weight="600">289,120</text>
|
|
64
|
+
|
|
65
|
+
<text x="18" y="158">Python</text>
|
|
66
|
+
<rect x="82" y="147" width="112" height="14" rx="4" fill="#EAB308"/>
|
|
67
|
+
<text x="202" y="158" fill="#F8FAFC" font-weight="600">238,101</text>
|
|
68
|
+
</g>
|
|
69
|
+
</g>
|
|
70
|
+
|
|
71
|
+
<g transform="translate(36, 320)">
|
|
72
|
+
<rect width="410" height="175" rx="12" fill="#1E293B" stroke="#334155"/>
|
|
73
|
+
<text x="18" y="28" fill="#F8FAFC" font-family="system-ui, sans-serif" font-size="14" font-weight="700">Random Get (cache hit, ops/s)</text>
|
|
74
|
+
<g font-family="system-ui, sans-serif" font-size="11" fill="#CBD5E1">
|
|
75
|
+
<text x="18" y="58">Rust</text>
|
|
76
|
+
<rect x="82" y="47" width="240" height="14" rx="4" fill="#F97316"/>
|
|
77
|
+
<text x="330" y="58" fill="#F8FAFC" font-weight="600">1,527,514</text>
|
|
78
|
+
|
|
79
|
+
<text x="18" y="83">Go</text>
|
|
80
|
+
<rect x="82" y="72" width="133" height="14" rx="4" fill="#06B6D4"/>
|
|
81
|
+
<text x="223" y="83" fill="#F8FAFC" font-weight="600">846,064</text>
|
|
82
|
+
|
|
83
|
+
<text x="18" y="108">Node.js</text>
|
|
84
|
+
<rect x="82" y="97" width="94" height="14" rx="4" fill="#22C55E"/>
|
|
85
|
+
<text x="184" y="108" fill="#F8FAFC" font-weight="600">596,199</text>
|
|
86
|
+
|
|
87
|
+
<text x="18" y="133">Bun</text>
|
|
88
|
+
<rect x="82" y="122" width="80" height="14" rx="4" fill="#F43F5E"/>
|
|
89
|
+
<text x="170" y="133" fill="#F8FAFC" font-weight="600">510,442</text>
|
|
90
|
+
|
|
91
|
+
<text x="18" y="158">Python</text>
|
|
92
|
+
<rect x="82" y="147" width="16" height="14" rx="4" fill="#EAB308"/>
|
|
93
|
+
<text x="106" y="158" fill="#F8FAFC" font-weight="600">97,142</text>
|
|
94
|
+
</g>
|
|
95
|
+
</g>
|
|
96
|
+
|
|
97
|
+
<g transform="translate(474, 320)">
|
|
98
|
+
<rect width="410" height="175" rx="12" fill="#1E293B" stroke="#334155"/>
|
|
99
|
+
<text x="18" y="28" fill="#F8FAFC" font-family="system-ui, sans-serif" font-size="14" font-weight="700">Range Scan (1,000 rows, rows/s)</text>
|
|
100
|
+
<g font-family="system-ui, sans-serif" font-size="11" fill="#CBD5E1">
|
|
101
|
+
<text x="18" y="58">Go</text>
|
|
102
|
+
<rect x="82" y="47" width="240" height="14" rx="4" fill="#06B6D4"/>
|
|
103
|
+
<text x="330" y="58" fill="#F8FAFC" font-weight="600">3,740,054</text>
|
|
104
|
+
|
|
105
|
+
<text x="18" y="83">Rust</text>
|
|
106
|
+
<rect x="82" y="72" width="133" height="14" rx="4" fill="#F97316"/>
|
|
107
|
+
<text x="223" y="83" fill="#F8FAFC" font-weight="600">2,070,149</text>
|
|
108
|
+
|
|
109
|
+
<text x="18" y="108">Bun</text>
|
|
110
|
+
<rect x="82" y="97" width="88" height="14" rx="4" fill="#F43F5E"/>
|
|
111
|
+
<text x="178" y="108" fill="#F8FAFC" font-weight="600">1,377,148</text>
|
|
112
|
+
|
|
113
|
+
<text x="18" y="133">Node.js</text>
|
|
114
|
+
<rect x="82" y="122" width="77" height="14" rx="4" fill="#22C55E"/>
|
|
115
|
+
<text x="167" y="133" fill="#F8FAFC" font-weight="600">1,201,920</text>
|
|
116
|
+
|
|
117
|
+
<text x="18" y="158">Python</text>
|
|
118
|
+
<rect x="82" y="147" width="62" height="14" rx="4" fill="#EAB308"/>
|
|
119
|
+
<text x="152" y="158" fill="#F8FAFC" font-weight="600">965,965</text>
|
|
120
|
+
</g>
|
|
121
|
+
</g>
|
|
122
|
+
</svg>
|
package/index.ts
CHANGED
|
@@ -89,6 +89,22 @@ const lib = dlopen(resolveLibraryPath(), {
|
|
|
89
89
|
],
|
|
90
90
|
returns: FFIType.ptr,
|
|
91
91
|
},
|
|
92
|
+
rosedb_scan_into: {
|
|
93
|
+
args: [
|
|
94
|
+
FFIType.ptr,
|
|
95
|
+
FFIType.ptr,
|
|
96
|
+
FFIType.u64,
|
|
97
|
+
FFIType.ptr,
|
|
98
|
+
FFIType.u64,
|
|
99
|
+
FFIType.i64,
|
|
100
|
+
FFIType.ptr,
|
|
101
|
+
FFIType.u64,
|
|
102
|
+
FFIType.ptr,
|
|
103
|
+
FFIType.ptr,
|
|
104
|
+
FFIType.ptr,
|
|
105
|
+
],
|
|
106
|
+
returns: FFIType.i32,
|
|
107
|
+
},
|
|
92
108
|
rosedb_batch_json: {
|
|
93
109
|
args: [FFIType.ptr, FFIType.ptr, FFIType.u64, FFIType.ptr],
|
|
94
110
|
returns: FFIType.i32,
|
|
@@ -309,10 +325,22 @@ export interface IntegrityReport {
|
|
|
309
325
|
export class Database {
|
|
310
326
|
private handle: number | null;
|
|
311
327
|
private keyBuf = new Uint8Array(64 * 1024);
|
|
328
|
+
private endBuf = new Uint8Array(64 * 1024);
|
|
329
|
+
private valBuf = new Uint8Array(64 * 1024);
|
|
312
330
|
private outBuf = new Uint8Array(64 * 1024);
|
|
331
|
+
private scanBuf = Buffer.allocUnsafe(1024 * 1024);
|
|
332
|
+
private scanView = new DataView(this.scanBuf.buffer, this.scanBuf.byteOffset, this.scanBuf.byteLength);
|
|
313
333
|
private outLenBuf = new BigUint64Array(1);
|
|
314
334
|
private overflowPtrBuf = new BigUint64Array(1);
|
|
315
335
|
private errBuf = new BigUint64Array(1);
|
|
336
|
+
private keyBufPtr = ptr(this.keyBuf);
|
|
337
|
+
private endBufPtr = ptr(this.endBuf);
|
|
338
|
+
private valBufPtr = ptr(this.valBuf);
|
|
339
|
+
private outBufPtr = ptr(this.outBuf);
|
|
340
|
+
private scanBufPtr = ptr(this.scanBuf);
|
|
341
|
+
private outLenPtr = ptr(this.outLenBuf);
|
|
342
|
+
private overflowPtrPtr = ptr(this.overflowPtrBuf);
|
|
343
|
+
private errBufPtr = ptr(this.errBuf);
|
|
316
344
|
|
|
317
345
|
constructor(path: string, options?: DatabaseOptions) {
|
|
318
346
|
const pathBytes = encoder.encode(path);
|
|
@@ -349,20 +377,39 @@ export class Database {
|
|
|
349
377
|
ttlMs?: number
|
|
350
378
|
): void {
|
|
351
379
|
const h = this.ensureHandle();
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
380
|
+
let kPtr: any;
|
|
381
|
+
let kLen: number;
|
|
382
|
+
if (typeof key === "string" && key.length <= 20000) {
|
|
383
|
+
kLen = encoder.encodeInto(key, this.keyBuf).written;
|
|
384
|
+
kPtr = this.keyBufPtr;
|
|
385
|
+
} else {
|
|
386
|
+
const k = toBytes(key);
|
|
387
|
+
kPtr = ptr(k);
|
|
388
|
+
kLen = k.length;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
let vPtr: any;
|
|
392
|
+
let vLen: number;
|
|
393
|
+
if (typeof value === "string" && value.length <= 20000) {
|
|
394
|
+
vLen = encoder.encodeInto(value, this.valBuf).written;
|
|
395
|
+
vPtr = vLen > 0 ? this.valBufPtr : null;
|
|
396
|
+
} else {
|
|
397
|
+
const v = toBytes(value);
|
|
398
|
+
vLen = v.length;
|
|
399
|
+
vPtr = vLen > 0 ? ptr(v) : null;
|
|
400
|
+
}
|
|
401
|
+
|
|
355
402
|
const rc = lib.symbols.rosedb_put(
|
|
356
403
|
h,
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
404
|
+
kPtr,
|
|
405
|
+
kLen,
|
|
406
|
+
vPtr,
|
|
407
|
+
vLen,
|
|
361
408
|
ttlMs ? BigInt(ttlMs) : -1n,
|
|
362
|
-
|
|
409
|
+
this.errBufPtr
|
|
363
410
|
);
|
|
364
|
-
|
|
365
|
-
|
|
411
|
+
if (rc !== 0) {
|
|
412
|
+
const err = takeError(this.errBuf);
|
|
366
413
|
throw new Error(err || "put failed");
|
|
367
414
|
}
|
|
368
415
|
}
|
|
@@ -372,28 +419,26 @@ export class Database {
|
|
|
372
419
|
let kPtr: any;
|
|
373
420
|
let kLen: number;
|
|
374
421
|
if (typeof key === "string" && key.length <= 20000) {
|
|
375
|
-
|
|
376
|
-
kPtr =
|
|
377
|
-
kLen = written;
|
|
422
|
+
kLen = encoder.encodeInto(key, this.keyBuf).written;
|
|
423
|
+
kPtr = this.keyBufPtr;
|
|
378
424
|
} else {
|
|
379
425
|
const k = toBytes(key);
|
|
380
426
|
kPtr = ptr(k);
|
|
381
427
|
kLen = k.length;
|
|
382
428
|
}
|
|
383
429
|
|
|
384
|
-
this.errBuf[0] = 0n;
|
|
385
430
|
const rc = lib.symbols.rosedb_get_into(
|
|
386
431
|
h,
|
|
387
432
|
kPtr,
|
|
388
433
|
kLen,
|
|
389
|
-
|
|
434
|
+
this.outBufPtr,
|
|
390
435
|
this.outBuf.length,
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
436
|
+
this.outLenPtr,
|
|
437
|
+
this.overflowPtrPtr,
|
|
438
|
+
this.errBufPtr
|
|
394
439
|
);
|
|
395
|
-
|
|
396
|
-
|
|
440
|
+
if (rc < 0) {
|
|
441
|
+
const err = takeError(this.errBuf);
|
|
397
442
|
throw new Error(err || "get failed");
|
|
398
443
|
}
|
|
399
444
|
if (rc === 0) {
|
|
@@ -461,22 +506,38 @@ export class Database {
|
|
|
461
506
|
|
|
462
507
|
delete(key: string | Uint8Array): void {
|
|
463
508
|
const h = this.ensureHandle();
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
509
|
+
let kPtr: any;
|
|
510
|
+
let kLen: number;
|
|
511
|
+
if (typeof key === "string" && key.length <= 20000) {
|
|
512
|
+
kLen = encoder.encodeInto(key, this.keyBuf).written;
|
|
513
|
+
kPtr = this.keyBufPtr;
|
|
514
|
+
} else {
|
|
515
|
+
const k = toBytes(key);
|
|
516
|
+
kPtr = ptr(k);
|
|
517
|
+
kLen = k.length;
|
|
518
|
+
}
|
|
519
|
+
const rc = lib.symbols.rosedb_delete(h, kPtr, kLen, this.errBufPtr);
|
|
520
|
+
if (rc !== 0) {
|
|
521
|
+
const err = takeError(this.errBuf);
|
|
469
522
|
throw new Error(err || "delete failed");
|
|
470
523
|
}
|
|
471
524
|
}
|
|
472
525
|
|
|
473
526
|
exists(key: string | Uint8Array): boolean {
|
|
474
527
|
const h = this.ensureHandle();
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
528
|
+
let kPtr: any;
|
|
529
|
+
let kLen: number;
|
|
530
|
+
if (typeof key === "string" && key.length <= 20000) {
|
|
531
|
+
kLen = encoder.encodeInto(key, this.keyBuf).written;
|
|
532
|
+
kPtr = this.keyBufPtr;
|
|
533
|
+
} else {
|
|
534
|
+
const k = toBytes(key);
|
|
535
|
+
kPtr = ptr(k);
|
|
536
|
+
kLen = k.length;
|
|
537
|
+
}
|
|
538
|
+
const rc = lib.symbols.rosedb_exists(h, kPtr, kLen, this.errBufPtr);
|
|
539
|
+
if (rc < 0) {
|
|
540
|
+
const err = takeError(this.errBuf);
|
|
480
541
|
throw new Error(err || "exists failed");
|
|
481
542
|
}
|
|
482
543
|
return rc === 1;
|
|
@@ -484,34 +545,89 @@ export class Database {
|
|
|
484
545
|
|
|
485
546
|
scan(start?: string, end?: string, limit?: number): ScanEntry[] {
|
|
486
547
|
const h = this.ensureHandle();
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
548
|
+
let sPtr: any = null;
|
|
549
|
+
let sLen = 0;
|
|
550
|
+
if (start && start.length > 0) {
|
|
551
|
+
if (start.length <= 20000) {
|
|
552
|
+
sLen = encoder.encodeInto(start, this.keyBuf).written;
|
|
553
|
+
sPtr = ptr(this.keyBuf);
|
|
554
|
+
} else {
|
|
555
|
+
const b = encoder.encode(start);
|
|
556
|
+
sLen = b.length;
|
|
557
|
+
sPtr = ptr(b);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
491
560
|
|
|
492
|
-
|
|
561
|
+
let ePtr: any = null;
|
|
562
|
+
let eLen = 0;
|
|
563
|
+
if (end && end.length > 0) {
|
|
564
|
+
if (end.length <= 20000) {
|
|
565
|
+
eLen = encoder.encodeInto(end, this.endBuf).written;
|
|
566
|
+
ePtr = ptr(this.endBuf);
|
|
567
|
+
} else {
|
|
568
|
+
const b = encoder.encode(end);
|
|
569
|
+
eLen = b.length;
|
|
570
|
+
ePtr = ptr(b);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
this.errBuf[0] = 0n;
|
|
575
|
+
const rc = lib.symbols.rosedb_scan_into(
|
|
493
576
|
h,
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
577
|
+
sPtr,
|
|
578
|
+
sLen,
|
|
579
|
+
ePtr,
|
|
580
|
+
eLen,
|
|
498
581
|
limit ? BigInt(limit) : -1n,
|
|
499
|
-
ptr(
|
|
500
|
-
|
|
582
|
+
ptr(this.scanBuf),
|
|
583
|
+
this.scanBuf.length,
|
|
584
|
+
ptr(this.outLenBuf),
|
|
585
|
+
ptr(this.overflowPtrBuf),
|
|
586
|
+
ptr(this.errBuf)
|
|
501
587
|
);
|
|
502
|
-
const err = takeError(errBuf);
|
|
503
|
-
if (err) {
|
|
504
|
-
throw new Error(err);
|
|
588
|
+
const err = takeError(this.errBuf);
|
|
589
|
+
if (rc < 0 || err) {
|
|
590
|
+
throw new Error(err || "scan failed");
|
|
505
591
|
}
|
|
506
|
-
|
|
507
|
-
|
|
592
|
+
|
|
593
|
+
if (rc === 1) {
|
|
594
|
+
const count = this.scanView.getUint32(0, true);
|
|
595
|
+
const out = new Array<ScanEntry>(count);
|
|
596
|
+
let off = 4;
|
|
597
|
+
const buf = this.scanBuf;
|
|
598
|
+
const view = this.scanView;
|
|
599
|
+
for (let i = 0; i < count; i++) {
|
|
600
|
+
const kLen = view.getUint32(off, true);
|
|
601
|
+
const vLen = view.getUint32(off + 4, true);
|
|
602
|
+
off += 8;
|
|
603
|
+
const key = buf.toString("utf8", off, off + kLen);
|
|
604
|
+
off += kLen;
|
|
605
|
+
const value = buf.toString("utf8", off, off + vLen);
|
|
606
|
+
off += vLen;
|
|
607
|
+
out[i] = { key, value };
|
|
608
|
+
}
|
|
609
|
+
return out;
|
|
508
610
|
}
|
|
509
|
-
|
|
510
|
-
const
|
|
511
|
-
|
|
512
|
-
);
|
|
513
|
-
lib.symbols.rosedb_free_bytes(
|
|
514
|
-
|
|
611
|
+
|
|
612
|
+
const len = Number(this.outLenBuf[0]);
|
|
613
|
+
const ovPtr = Number(this.overflowPtrBuf[0]);
|
|
614
|
+
const copy = Buffer.from(toArrayBuffer(ovPtr as any, 0, len).slice(0));
|
|
615
|
+
lib.symbols.rosedb_free_bytes(ovPtr as any, len);
|
|
616
|
+
const view = new DataView(copy.buffer, copy.byteOffset, copy.byteLength);
|
|
617
|
+
const count = view.getUint32(0, true);
|
|
618
|
+
const out = new Array<ScanEntry>(count);
|
|
619
|
+
let off = 4;
|
|
620
|
+
for (let i = 0; i < count; i++) {
|
|
621
|
+
const kLen = view.getUint32(off, true);
|
|
622
|
+
const vLen = view.getUint32(off + 4, true);
|
|
623
|
+
off += 8;
|
|
624
|
+
const key = copy.toString("utf8", off, off + kLen);
|
|
625
|
+
off += kLen;
|
|
626
|
+
const value = copy.toString("utf8", off, off + vLen);
|
|
627
|
+
off += vLen;
|
|
628
|
+
out[i] = { key, value };
|
|
629
|
+
}
|
|
630
|
+
return out;
|
|
515
631
|
}
|
|
516
632
|
|
|
517
633
|
batch(ops: BatchOperation[]): void {
|
package/librosedb_node.so
CHANGED
|
Binary file
|
package/node.mjs
ADDED
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
import koffi from "koffi";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
function resolveLibraryPath() {
|
|
7
|
+
const envPath = process.env.ROSEDB_LIB_PATH;
|
|
8
|
+
if (envPath && existsSync(envPath)) {
|
|
9
|
+
return envPath;
|
|
10
|
+
}
|
|
11
|
+
const baseDir = dirname(fileURLToPath(import.meta.url));
|
|
12
|
+
const candidates = [
|
|
13
|
+
join(baseDir, "librosedb_node.so"),
|
|
14
|
+
join(baseDir, "librosedb_node.dylib"),
|
|
15
|
+
join(baseDir, "../../target/release/librosedb_node.so"),
|
|
16
|
+
join(baseDir, "../../target/debug/librosedb_node.so"),
|
|
17
|
+
join(baseDir, "../../target/release/librosedb_node.dylib"),
|
|
18
|
+
join(baseDir, "../../target/debug/librosedb_node.dylib"),
|
|
19
|
+
];
|
|
20
|
+
for (const candidate of candidates) {
|
|
21
|
+
if (existsSync(candidate)) {
|
|
22
|
+
return candidate;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
throw new Error(
|
|
26
|
+
"RoséDB native library (librosedb_node.so) not found. Run `cargo build --release` first."
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const native = koffi.load(resolveLibraryPath());
|
|
31
|
+
|
|
32
|
+
const fnOpen = native.func("void *rosedb_open(const uint8_t *path_ptr, size_t path_len, const uint8_t *opts_ptr, size_t opts_len, _Out_ void **err_out)");
|
|
33
|
+
const fnClose = native.func("int32_t rosedb_close(void *handle, _Out_ void **err_out)");
|
|
34
|
+
const fnPut = native.func("int32_t rosedb_put(void *handle, const uint8_t *key_ptr, size_t key_len, const uint8_t *val_ptr, size_t val_len, int64_t ttl_ms, void *err_out)");
|
|
35
|
+
const fnGet = native.func("void *rosedb_get(void *handle, const uint8_t *key_ptr, size_t key_len, _Out_ size_t *out_len, _Out_ void **err_out)");
|
|
36
|
+
const fnGetInto = native.func("int32_t rosedb_get_into(void *handle, const uint8_t *key_ptr, size_t key_len, uint8_t *out_buf_ptr, size_t out_buf_cap, uint64_t *out_len, uint64_t *overflow_ptr_out, void *err_out)");
|
|
37
|
+
const fnMgetJson = native.func("void *rosedb_mget_json(void *handle, const uint8_t *keys_ptr, size_t keys_len, _Out_ size_t *out_len, _Out_ void **err_out)");
|
|
38
|
+
const fnDelete = native.func("int32_t rosedb_delete(void *handle, const uint8_t *key_ptr, size_t key_len, void *err_out)");
|
|
39
|
+
const fnExists = native.func("int32_t rosedb_exists(void *handle, const uint8_t *key_ptr, size_t key_len, void *err_out)");
|
|
40
|
+
const fnScanInto = native.func("int32_t rosedb_scan_into(void *handle, const uint8_t *start_ptr, size_t start_len, const uint8_t *end_ptr, size_t end_len, int64_t limit, uint8_t *out_buf_ptr, size_t out_buf_cap, uint64_t *out_len, uint64_t *overflow_ptr_out, void *err_out)");
|
|
41
|
+
const fnBatchBin = native.func("int32_t rosedb_batch_bin(void *handle, const uint8_t *buf_ptr, size_t buf_len, void *err_out)");
|
|
42
|
+
const fnCheckpoint = native.func("int32_t rosedb_checkpoint(void *handle, _Out_ void **err_out)");
|
|
43
|
+
const fnBackup = native.func("int32_t rosedb_backup(void *handle, const uint8_t *dest_ptr, size_t dest_len, _Out_ void **err_out)");
|
|
44
|
+
const fnStatsJson = native.func("void *rosedb_stats_json(void *handle, _Out_ size_t *out_len, _Out_ void **err_out)");
|
|
45
|
+
const fnIntegrityCheckJson = native.func("void *rosedb_integrity_check_json(void *handle, _Out_ size_t *out_len, _Out_ void **err_out)");
|
|
46
|
+
const fnSnapshotBegin = native.func("void *rosedb_snapshot_begin(void *handle, _Out_ void **err_out)");
|
|
47
|
+
const fnSnapshotVersion = native.func("uint64_t rosedb_snapshot_version(void *snap)");
|
|
48
|
+
const fnSnapshotGet = native.func("void *rosedb_snapshot_get(void *snap, const uint8_t *key_ptr, size_t key_len, _Out_ size_t *out_len, _Out_ void **err_out)");
|
|
49
|
+
const fnSnapshotScanJson = native.func("void *rosedb_snapshot_scan_json(void *snap, const uint8_t *start_ptr, size_t start_len, const uint8_t *end_ptr, size_t end_len, int64_t limit, _Out_ size_t *out_len, _Out_ void **err_out)");
|
|
50
|
+
const fnSnapshotClose = native.func("void rosedb_snapshot_close(void *snap)");
|
|
51
|
+
const fnFreeBytes = native.func("void rosedb_free_bytes(void *ptr, size_t len)");
|
|
52
|
+
const fnFreeCstr = native.func("void rosedb_free_cstr(void *ptr)");
|
|
53
|
+
|
|
54
|
+
const encoder = new TextEncoder();
|
|
55
|
+
const decoder = new TextDecoder();
|
|
56
|
+
|
|
57
|
+
function toBytes(input) {
|
|
58
|
+
return typeof input === "string" ? encoder.encode(input) : input;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function takeError(errBox) {
|
|
62
|
+
const errPtr = errBox[0];
|
|
63
|
+
if (!errPtr) return null;
|
|
64
|
+
const msg = koffi.decode(errPtr, "char", -1);
|
|
65
|
+
fnFreeCstr(errPtr);
|
|
66
|
+
errBox[0] = null;
|
|
67
|
+
return msg;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function takePtrError(errPtrBuf) {
|
|
71
|
+
const rawAddr = errPtrBuf[0];
|
|
72
|
+
if (!rawAddr) return null;
|
|
73
|
+
const msg = koffi.decode(rawAddr, "char", -1);
|
|
74
|
+
fnFreeCstr(rawAddr);
|
|
75
|
+
errPtrBuf[0] = 0n;
|
|
76
|
+
return msg;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function readAndFreeBytes(resPtr, len) {
|
|
80
|
+
const raw = koffi.decode(resPtr, "uint8_t", len);
|
|
81
|
+
const copy = new Uint8Array(raw);
|
|
82
|
+
fnFreeBytes(resPtr, len);
|
|
83
|
+
return copy;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export class Snapshot {
|
|
87
|
+
constructor(snapHandle) {
|
|
88
|
+
this.snapHandle = snapHandle;
|
|
89
|
+
this.outLenBuf = [0];
|
|
90
|
+
this.errBox = [null];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
ensureOpen() {
|
|
94
|
+
if (!this.snapHandle) {
|
|
95
|
+
throw new Error("Snapshot is closed");
|
|
96
|
+
}
|
|
97
|
+
return this.snapHandle;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
version() {
|
|
101
|
+
const h = this.ensureOpen();
|
|
102
|
+
return Number(fnSnapshotVersion(h));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
get(key) {
|
|
106
|
+
const h = this.ensureOpen();
|
|
107
|
+
const k = toBytes(key);
|
|
108
|
+
this.errBox[0] = null;
|
|
109
|
+
const resPtr = fnSnapshotGet(h, k, k.length, this.outLenBuf, this.errBox);
|
|
110
|
+
const err = takeError(this.errBox);
|
|
111
|
+
if (err) throw new Error(err);
|
|
112
|
+
if (!resPtr) return null;
|
|
113
|
+
const bytes = readAndFreeBytes(resPtr, Number(this.outLenBuf[0]));
|
|
114
|
+
return decoder.decode(bytes);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
scan(start, end, limit) {
|
|
118
|
+
const h = this.ensureOpen();
|
|
119
|
+
const sBytes = start ? encoder.encode(start) : null;
|
|
120
|
+
const eBytes = end ? encoder.encode(end) : null;
|
|
121
|
+
this.errBox[0] = null;
|
|
122
|
+
const resPtr = fnSnapshotScanJson(
|
|
123
|
+
h,
|
|
124
|
+
sBytes,
|
|
125
|
+
sBytes ? sBytes.length : 0,
|
|
126
|
+
eBytes,
|
|
127
|
+
eBytes ? eBytes.length : 0,
|
|
128
|
+
limit ? BigInt(limit) : -1n,
|
|
129
|
+
this.outLenBuf,
|
|
130
|
+
this.errBox
|
|
131
|
+
);
|
|
132
|
+
const err = takeError(this.errBox);
|
|
133
|
+
if (err) throw new Error(err);
|
|
134
|
+
if (!resPtr) return [];
|
|
135
|
+
const bytes = readAndFreeBytes(resPtr, Number(this.outLenBuf[0]));
|
|
136
|
+
return JSON.parse(decoder.decode(bytes));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
close() {
|
|
140
|
+
if (!this.snapHandle) return;
|
|
141
|
+
fnSnapshotClose(this.snapHandle);
|
|
142
|
+
this.snapHandle = null;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export class Database {
|
|
147
|
+
constructor(path, options) {
|
|
148
|
+
const pathBytes = encoder.encode(path);
|
|
149
|
+
const optsBytes = options ? encoder.encode(JSON.stringify(options)) : null;
|
|
150
|
+
const errBox = [null];
|
|
151
|
+
const h = fnOpen(
|
|
152
|
+
pathBytes,
|
|
153
|
+
pathBytes.length,
|
|
154
|
+
optsBytes,
|
|
155
|
+
optsBytes ? optsBytes.length : 0,
|
|
156
|
+
errBox
|
|
157
|
+
);
|
|
158
|
+
const err = takeError(errBox);
|
|
159
|
+
if (err || !h) {
|
|
160
|
+
throw new Error(err || "Failed to open RoséDB database");
|
|
161
|
+
}
|
|
162
|
+
this.handle = h;
|
|
163
|
+
this.keyBuf = Buffer.allocUnsafe(64 * 1024);
|
|
164
|
+
this.endBuf = Buffer.allocUnsafe(64 * 1024);
|
|
165
|
+
this.valBuf = Buffer.allocUnsafe(64 * 1024);
|
|
166
|
+
this.outBuf = Buffer.allocUnsafe(64 * 1024);
|
|
167
|
+
this.scanBuf = Buffer.allocUnsafe(1024 * 1024);
|
|
168
|
+
this.batchBuf = Buffer.allocUnsafe(1024 * 1024);
|
|
169
|
+
this.outLenBuf = new BigUint64Array(1);
|
|
170
|
+
this.overflowPtrBuf = new BigUint64Array(1);
|
|
171
|
+
this.errPtrBuf = new BigUint64Array(1);
|
|
172
|
+
this.errBox = [null];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
ensureHandle() {
|
|
176
|
+
if (!this.handle) {
|
|
177
|
+
throw new Error("RoséDB database is closed");
|
|
178
|
+
}
|
|
179
|
+
return this.handle;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
put(key, value, ttlMs) {
|
|
183
|
+
const h = this.ensureHandle();
|
|
184
|
+
let kPtr;
|
|
185
|
+
let kLen;
|
|
186
|
+
if (typeof key === "string" && key.length <= 20000) {
|
|
187
|
+
kLen = this.keyBuf.write(key, 0, "utf8");
|
|
188
|
+
kPtr = this.keyBuf;
|
|
189
|
+
} else {
|
|
190
|
+
const k = toBytes(key);
|
|
191
|
+
kPtr = k;
|
|
192
|
+
kLen = k.length;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
let vPtr;
|
|
196
|
+
let vLen;
|
|
197
|
+
if (typeof value === "string" && value.length <= 20000) {
|
|
198
|
+
vLen = this.valBuf.write(value, 0, "utf8");
|
|
199
|
+
vPtr = vLen > 0 ? this.valBuf : null;
|
|
200
|
+
} else {
|
|
201
|
+
const v = toBytes(value);
|
|
202
|
+
vLen = v.length;
|
|
203
|
+
vPtr = vLen > 0 ? v : null;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const rc = fnPut(
|
|
207
|
+
h,
|
|
208
|
+
kPtr,
|
|
209
|
+
kLen,
|
|
210
|
+
vPtr,
|
|
211
|
+
vLen,
|
|
212
|
+
ttlMs ? ttlMs : -1,
|
|
213
|
+
this.errPtrBuf
|
|
214
|
+
);
|
|
215
|
+
if (rc !== 0) {
|
|
216
|
+
const err = takePtrError(this.errPtrBuf);
|
|
217
|
+
throw new Error(err || "put failed");
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
get(key) {
|
|
222
|
+
const h = this.ensureHandle();
|
|
223
|
+
let kPtr;
|
|
224
|
+
let kLen;
|
|
225
|
+
if (typeof key === "string" && key.length <= 20000) {
|
|
226
|
+
kLen = this.keyBuf.write(key, 0, "utf8");
|
|
227
|
+
kPtr = this.keyBuf;
|
|
228
|
+
} else {
|
|
229
|
+
const k = toBytes(key);
|
|
230
|
+
kPtr = k;
|
|
231
|
+
kLen = k.length;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const rc = fnGetInto(
|
|
235
|
+
h,
|
|
236
|
+
kPtr,
|
|
237
|
+
kLen,
|
|
238
|
+
this.outBuf,
|
|
239
|
+
this.outBuf.length,
|
|
240
|
+
this.outLenBuf,
|
|
241
|
+
this.overflowPtrBuf,
|
|
242
|
+
this.errPtrBuf
|
|
243
|
+
);
|
|
244
|
+
if (rc < 0) {
|
|
245
|
+
const err = takePtrError(this.errPtrBuf);
|
|
246
|
+
throw new Error(err || "get failed");
|
|
247
|
+
}
|
|
248
|
+
if (rc === 0) return null;
|
|
249
|
+
const len = Number(this.outLenBuf[0]);
|
|
250
|
+
if (rc === 1) {
|
|
251
|
+
return this.outBuf.toString("utf8", 0, len);
|
|
252
|
+
}
|
|
253
|
+
const ovPtr = this.overflowPtrBuf[0];
|
|
254
|
+
this.overflowPtrBuf[0] = 0n;
|
|
255
|
+
const bytes = readAndFreeBytes(ovPtr, len);
|
|
256
|
+
return decoder.decode(bytes);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
mget(keys) {
|
|
260
|
+
const h = this.ensureHandle();
|
|
261
|
+
const payload = encoder.encode(JSON.stringify(keys));
|
|
262
|
+
this.errBox[0] = null;
|
|
263
|
+
const resPtr = fnMgetJson(h, payload, payload.length, this.outLenBuf, this.errBox);
|
|
264
|
+
const err = takeError(this.errBox);
|
|
265
|
+
if (err || !resPtr) throw new Error(err || "mget failed");
|
|
266
|
+
const bytes = readAndFreeBytes(resPtr, Number(this.outLenBuf[0]));
|
|
267
|
+
return JSON.parse(decoder.decode(bytes));
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
getBuffer(key) {
|
|
271
|
+
const h = this.ensureHandle();
|
|
272
|
+
const k = toBytes(key);
|
|
273
|
+
this.errBox[0] = null;
|
|
274
|
+
const resPtr = fnGet(h, k, k.length, this.outLenBuf, this.errBox);
|
|
275
|
+
const err = takeError(this.errBox);
|
|
276
|
+
if (err) throw new Error(err);
|
|
277
|
+
if (!resPtr) return null;
|
|
278
|
+
return readAndFreeBytes(resPtr, Number(this.outLenBuf[0]));
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
delete(key) {
|
|
282
|
+
const h = this.ensureHandle();
|
|
283
|
+
let kPtr;
|
|
284
|
+
let kLen;
|
|
285
|
+
if (typeof key === "string" && key.length <= 20000) {
|
|
286
|
+
kLen = this.keyBuf.write(key, 0, "utf8");
|
|
287
|
+
kPtr = this.keyBuf;
|
|
288
|
+
} else {
|
|
289
|
+
const k = toBytes(key);
|
|
290
|
+
kPtr = k;
|
|
291
|
+
kLen = k.length;
|
|
292
|
+
}
|
|
293
|
+
const rc = fnDelete(h, kPtr, kLen, this.errPtrBuf);
|
|
294
|
+
if (rc !== 0) {
|
|
295
|
+
const err = takePtrError(this.errPtrBuf);
|
|
296
|
+
throw new Error(err || "delete failed");
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
exists(key) {
|
|
301
|
+
const h = this.ensureHandle();
|
|
302
|
+
let kPtr;
|
|
303
|
+
let kLen;
|
|
304
|
+
if (typeof key === "string" && key.length <= 20000) {
|
|
305
|
+
kLen = this.keyBuf.write(key, 0, "utf8");
|
|
306
|
+
kPtr = this.keyBuf;
|
|
307
|
+
} else {
|
|
308
|
+
const k = toBytes(key);
|
|
309
|
+
kPtr = k;
|
|
310
|
+
kLen = k.length;
|
|
311
|
+
}
|
|
312
|
+
const rc = fnExists(h, kPtr, kLen, this.errPtrBuf);
|
|
313
|
+
if (rc < 0) {
|
|
314
|
+
const err = takePtrError(this.errPtrBuf);
|
|
315
|
+
throw new Error(err || "exists failed");
|
|
316
|
+
}
|
|
317
|
+
return rc === 1;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
scan(start, end, limit) {
|
|
321
|
+
const h = this.ensureHandle();
|
|
322
|
+
let sPtr = null;
|
|
323
|
+
let sLen = 0;
|
|
324
|
+
if (start && start.length > 0) {
|
|
325
|
+
if (start.length <= 20000) {
|
|
326
|
+
sLen = this.keyBuf.write(start, 0, "utf8");
|
|
327
|
+
sPtr = this.keyBuf;
|
|
328
|
+
} else {
|
|
329
|
+
const b = encoder.encode(start);
|
|
330
|
+
sLen = b.length;
|
|
331
|
+
sPtr = b;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
let ePtr = null;
|
|
336
|
+
let eLen = 0;
|
|
337
|
+
if (end && end.length > 0) {
|
|
338
|
+
if (end.length <= 20000) {
|
|
339
|
+
eLen = this.endBuf.write(end, 0, "utf8");
|
|
340
|
+
ePtr = this.endBuf;
|
|
341
|
+
} else {
|
|
342
|
+
const b = encoder.encode(end);
|
|
343
|
+
eLen = b.length;
|
|
344
|
+
ePtr = b;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const rc = fnScanInto(
|
|
349
|
+
h,
|
|
350
|
+
sPtr,
|
|
351
|
+
sLen,
|
|
352
|
+
ePtr,
|
|
353
|
+
eLen,
|
|
354
|
+
limit ? limit : -1,
|
|
355
|
+
this.scanBuf,
|
|
356
|
+
this.scanBuf.length,
|
|
357
|
+
this.outLenBuf,
|
|
358
|
+
this.overflowPtrBuf,
|
|
359
|
+
this.errPtrBuf
|
|
360
|
+
);
|
|
361
|
+
if (rc < 0) {
|
|
362
|
+
const err = takePtrError(this.errPtrBuf);
|
|
363
|
+
throw new Error(err || "scan failed");
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const buf =
|
|
367
|
+
rc === 1
|
|
368
|
+
? this.scanBuf
|
|
369
|
+
: Buffer.from(
|
|
370
|
+
readAndFreeBytes(
|
|
371
|
+
(() => {
|
|
372
|
+
const p = this.overflowPtrBuf[0];
|
|
373
|
+
this.overflowPtrBuf[0] = 0n;
|
|
374
|
+
return p;
|
|
375
|
+
})(),
|
|
376
|
+
Number(this.outLenBuf[0])
|
|
377
|
+
)
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
const count = buf.readUInt32LE(0);
|
|
381
|
+
const out = new Array(count);
|
|
382
|
+
let off = 4;
|
|
383
|
+
for (let i = 0; i < count; i++) {
|
|
384
|
+
const kLen = buf.readUInt32LE(off);
|
|
385
|
+
const vLen = buf.readUInt32LE(off + 4);
|
|
386
|
+
off += 8;
|
|
387
|
+
const key = buf.toString("utf8", off, off + kLen);
|
|
388
|
+
off += kLen;
|
|
389
|
+
const value = buf.toString("utf8", off, off + vLen);
|
|
390
|
+
off += vLen;
|
|
391
|
+
out[i] = { key, value };
|
|
392
|
+
}
|
|
393
|
+
return out;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
batch(ops) {
|
|
397
|
+
const h = this.ensureHandle();
|
|
398
|
+
const count = ops.length;
|
|
399
|
+
let buf = this.batchBuf;
|
|
400
|
+
let off = 4;
|
|
401
|
+
buf.writeUInt32LE(count, 0);
|
|
402
|
+
|
|
403
|
+
for (let i = 0; i < count; i++) {
|
|
404
|
+
const item = ops[i];
|
|
405
|
+
const opCode = item.op === "put" ? 1 : 2;
|
|
406
|
+
const keyStr = item.key;
|
|
407
|
+
const valStr = item.value || "";
|
|
408
|
+
const est = 9 + keyStr.length * 3 + valStr.length * 3;
|
|
409
|
+
if (off + est > buf.length) {
|
|
410
|
+
const grown = Buffer.allocUnsafe(Math.max(buf.length * 2, off + est + 4096));
|
|
411
|
+
buf.copy(grown, 0, 0, off);
|
|
412
|
+
buf = grown;
|
|
413
|
+
this.batchBuf = grown;
|
|
414
|
+
}
|
|
415
|
+
buf[off] = opCode;
|
|
416
|
+
const kLen = buf.write(keyStr, off + 9, "utf8");
|
|
417
|
+
const vLen = opCode === 1 && valStr.length > 0 ? buf.write(valStr, off + 9 + kLen, "utf8") : 0;
|
|
418
|
+
buf.writeUInt32LE(kLen, off + 1);
|
|
419
|
+
buf.writeUInt32LE(vLen, off + 5);
|
|
420
|
+
off += 9 + kLen + vLen;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const rc = fnBatchBin(h, buf, off, this.errPtrBuf);
|
|
424
|
+
if (rc !== 0) {
|
|
425
|
+
const err = takePtrError(this.errPtrBuf);
|
|
426
|
+
throw new Error(err || "batch failed");
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
checkpoint() {
|
|
431
|
+
const h = this.ensureHandle();
|
|
432
|
+
this.errBox[0] = null;
|
|
433
|
+
const rc = fnCheckpoint(h, this.errBox);
|
|
434
|
+
if (rc !== 0) {
|
|
435
|
+
const err = takeError(this.errBox);
|
|
436
|
+
throw new Error(err || "checkpoint failed");
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
backup(destDir) {
|
|
441
|
+
const h = this.ensureHandle();
|
|
442
|
+
const destBytes = encoder.encode(destDir);
|
|
443
|
+
this.errBox[0] = null;
|
|
444
|
+
const rc = fnBackup(h, destBytes, destBytes.length, this.errBox);
|
|
445
|
+
if (rc !== 0) {
|
|
446
|
+
const err = takeError(this.errBox);
|
|
447
|
+
throw new Error(err || "backup failed");
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
stats() {
|
|
452
|
+
const h = this.ensureHandle();
|
|
453
|
+
this.errBox[0] = null;
|
|
454
|
+
const resPtr = fnStatsJson(h, this.outLenBuf, this.errBox);
|
|
455
|
+
const err = takeError(this.errBox);
|
|
456
|
+
if (err || !resPtr) throw new Error(err || "stats failed");
|
|
457
|
+
const bytes = readAndFreeBytes(resPtr, Number(this.outLenBuf[0]));
|
|
458
|
+
return JSON.parse(decoder.decode(bytes));
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
integrityCheck() {
|
|
462
|
+
const h = this.ensureHandle();
|
|
463
|
+
this.errBox[0] = null;
|
|
464
|
+
const resPtr = fnIntegrityCheckJson(h, this.outLenBuf, this.errBox);
|
|
465
|
+
const err = takeError(this.errBox);
|
|
466
|
+
if (err || !resPtr) throw new Error(err || "integrityCheck failed");
|
|
467
|
+
const bytes = readAndFreeBytes(resPtr, Number(this.outLenBuf[0]));
|
|
468
|
+
return JSON.parse(decoder.decode(bytes));
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
snapshot() {
|
|
472
|
+
const h = this.ensureHandle();
|
|
473
|
+
this.errBox[0] = null;
|
|
474
|
+
const snapPtr = fnSnapshotBegin(h, this.errBox);
|
|
475
|
+
const err = takeError(this.errBox);
|
|
476
|
+
if (err || !snapPtr) throw new Error(err || "Failed to begin snapshot");
|
|
477
|
+
return new Snapshot(snapPtr);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
close() {
|
|
481
|
+
if (!this.handle) return;
|
|
482
|
+
this.errBox[0] = null;
|
|
483
|
+
const rc = fnClose(this.handle, this.errBox);
|
|
484
|
+
this.handle = null;
|
|
485
|
+
if (rc !== 0) {
|
|
486
|
+
const err = takeError(this.errBox);
|
|
487
|
+
throw new Error(err || "close failed");
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
async putAsync(key, value, ttlMs) {
|
|
492
|
+
return Promise.resolve().then(() => this.put(key, value, ttlMs));
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
async getAsync(key) {
|
|
496
|
+
return Promise.resolve().then(() => this.get(key));
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
async deleteAsync(key) {
|
|
500
|
+
return Promise.resolve().then(() => this.delete(key));
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
async scanAsync(start, end, limit) {
|
|
504
|
+
return Promise.resolve().then(() => this.scan(start, end, limit));
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
async batchAsync(ops) {
|
|
508
|
+
return Promise.resolve().then(() => this.batch(ops));
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
async checkpointAsync() {
|
|
512
|
+
return Promise.resolve().then(() => this.checkpoint());
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
async backupAsync(destDir) {
|
|
516
|
+
return Promise.resolve().then(() => this.backup(destDir));
|
|
517
|
+
}
|
|
518
|
+
}
|
package/package.json
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rosedb",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "High-performance embedded Key-Value & Document database engine written in Rust with Bun FFI bindings (ACID, WAL, B+Tree, MVCC Snapshot Isolation, AES-256-GCM).",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "High-performance embedded Key-Value & Document database engine written in Rust with Bun FFI & Node.js bindings (ACID, WAL, B+Tree, MVCC Snapshot Isolation, AES-256-GCM).",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "./
|
|
6
|
+
"main": "./node.mjs",
|
|
7
7
|
"types": "./index.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
-
"
|
|
11
|
-
"types": "./index.ts"
|
|
10
|
+
"bun": "./index.ts",
|
|
11
|
+
"types": "./index.ts",
|
|
12
|
+
"import": "./node.mjs",
|
|
13
|
+
"default": "./node.mjs"
|
|
12
14
|
}
|
|
13
15
|
},
|
|
14
16
|
"files": [
|
|
15
17
|
"index.ts",
|
|
18
|
+
"node.mjs",
|
|
16
19
|
"librosedb_node.so",
|
|
17
20
|
"README.md",
|
|
18
|
-
"LICENSE"
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"assets/benchmark.svg"
|
|
19
23
|
],
|
|
20
24
|
"keywords": [
|
|
21
25
|
"database",
|
|
@@ -23,6 +27,7 @@
|
|
|
23
27
|
"kv",
|
|
24
28
|
"rust",
|
|
25
29
|
"bun",
|
|
30
|
+
"nodejs",
|
|
26
31
|
"ffi",
|
|
27
32
|
"wal",
|
|
28
33
|
"mvcc",
|
|
@@ -31,14 +36,26 @@
|
|
|
31
36
|
],
|
|
32
37
|
"author": "Blckrose",
|
|
33
38
|
"license": "MIT",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/Blckrose08/rosedb.git"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/Blckrose08/rosedb#readme",
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/Blckrose08/rosedb/issues"
|
|
46
|
+
},
|
|
34
47
|
"publishConfig": {
|
|
35
48
|
"access": "public"
|
|
36
49
|
},
|
|
37
50
|
"engines": {
|
|
38
|
-
"bun": ">=1.1.0"
|
|
51
|
+
"bun": ">=1.1.0",
|
|
52
|
+
"node": ">=18.0.0"
|
|
39
53
|
},
|
|
40
54
|
"scripts": {
|
|
41
|
-
"test": "bun test",
|
|
42
|
-
"prepack": "cp ../../target/release/librosedb_node.so ./librosedb_node.so && cp ../../README.md ./README.md && cp ../../LICENSE ./LICENSE"
|
|
55
|
+
"test": "bun test && node --test node.test.mjs",
|
|
56
|
+
"prepack": "cp ../../target/release/librosedb_node.so ./librosedb_node.so && cp ../../README.md ./README.md && cp ../../LICENSE ./LICENSE && mkdir -p ./assets && cp ../../assets/benchmark.svg ./assets/benchmark.svg"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"koffi": "^3.3.8"
|
|
43
60
|
}
|
|
44
61
|
}
|