@hakodb/client 0.6.1 → 0.6.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/src/native.ts CHANGED
@@ -1,1580 +1,1605 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
-
3
- type Handle = any;
4
-
5
- /**
6
- * matches HK_OnSnapshotCallback in hako.h
7
- */
8
- export type WatchCallback = (collection: string, path: string, kind: number) => void;
9
-
10
- export interface NativeBindings {
11
- engineOpen(path: string): Handle;
12
- engineIsIndexesReady(engine: Handle): boolean;
13
- engineOpenWithConfig(path: string, config: Handle): Handle;
14
- engineFree(engine: Handle): void;
15
- engineBackup(engine: Handle, path: string): number; // Added
16
- engineCompact(engine: Handle): number;
17
- engineGetStats(engine: Handle): string | null;
18
- engineGetAuditLog(engine: Handle): string | null;
19
- engineSnapshotIndices(engine: Handle): number;
20
- engineListIndexes(engine: Handle, collection: string | null): string | null;
21
-
22
- // Configuration Builder
23
- configNew(): Handle;
24
- configFree(config: Handle): void;
25
- configSetDurability(config: Handle, mode: number): void;
26
- configSetEncryptionKey(config: Handle, key: string | null): void;
27
- configSetEncryptedCollections(config: Handle, collectionsJson: string | null): number;
28
- configSetAuditLog(config: Handle, enabled: boolean, path: string | null): void;
29
- configSetQueryWorkers(config: Handle, count: number): void;
30
- configSetMemoryLimits(config: Handle, mmap: number, maxInlined: number): void;
31
- configSetStorageTuning(
32
- config: Handle,
33
- pageSize: number,
34
- threshold: number,
35
- groupCommit: number
36
- ): void;
37
- configSetBlobThreshold(config: Handle, thresholdBytes: number): void;
38
- configSetCompression(config: Handle, enabled: boolean, level: number): void;
39
- configSetBackgroundMaintenance(config: Handle, enabled: boolean): void;
40
-
41
- // Real-time Watch
42
- engineWatch(engine: Handle, collection: string, callback: WatchCallback): Handle;
43
- watchFree(watch: Handle): void;
44
-
45
- // Document Builder
46
- docNew(): Handle;
47
- docFree(doc: Handle): void;
48
- docInsertStr(doc: Handle, key: string, value: string): number;
49
- docInsertInt(doc: Handle, key: string, value: number | bigint): number;
50
- docInsertFloat(doc: Handle, key: string, value: number): number;
51
- docInsertBool(doc: Handle, key: string, value: boolean): number;
52
- docInsertNull(doc: Handle, key: string): number;
53
- docInsertBin(doc: Handle, key: string, bytes: Uint8Array): number;
54
- docInsertTimestamp(doc: Handle, key: string, micros: bigint): number; // Added
55
- docInsertServerTimestamp(doc: Handle, key: string): number; // Added
56
- docInsertReference(doc: Handle, key: string, targetCollection: string, targetId: string): number;
57
- docToJson(doc: Handle): string | null;
58
-
59
- // Engine CRUD
60
- engineInsert(engine: Handle, collection: string, docId: string, doc: Handle): number;
61
- engineGet(engine: Handle, collection: string, docId: string): Handle;
62
- engineDelete(engine: Handle, collection: string, docId: string): number;
63
- engineDeleteLocal(engine: Handle, collection: string, docId: string): number;
64
- engineDeleteEx(engine: Handle, collection: string, docId: string, force: boolean): number;
65
- engineRestore(engine: Handle, collection: string, docId: string): number;
66
- engineRestoreAvailable(engine: Handle, collection: string | null): string | null;
67
- engineDeleteQueue(engine: Handle, collection: string | null): string | null;
68
- engineSweepNow(engine: Handle): number;
69
- engineSetCollectionLocal(engine: Handle, collection: string, local: number): number;
70
- engineReplicateKey(engine: Handle, collection: string, docId: string): number;
71
- engineReplicateCollection(engine: Handle, collection: string): number;
72
- engineVacuumCollection(engine: Handle, collection: string): number;
73
- engineRelocateDocs(engine: Handle, src: string, dst: string, idsJson: string): string | null;
74
- engineLoadCollection(engine: Handle, collection: string): number;
75
- engineUnloadCollection(engine: Handle, collection: string): number;
76
- engineUnloadedCollections(engine: Handle): string | null;
77
- enginePatch(engine: Handle, collection: string, docId: string, updates: Handle): number;
78
- engineInsertSubDoc(engine: Handle, col: string, id: string, subCol: string, subId: string, doc: Handle): number;
79
- engineGetByRef(engine: Handle, doc: Handle, fieldKey: string): Handle;
80
- engineCreateIndex(engine: Handle, collection: string, fieldsJson: string): number;
81
-
82
- // Atomic Batch
83
- batchNew(): Handle;
84
- batchFree(batch: Handle): void;
85
- batchSet(batch: Handle, collection: string, docId: string, doc: Handle): number;
86
- batchDelete(batch: Handle, collection: string, docId: string): number;
87
- batchDeleteEx(batch: Handle, collection: string, docId: string, force: boolean): number;
88
- batchCommit(engine: Handle, batch: Handle): number;
89
-
90
- // Serializable Transactions
91
- transactionBegin(engine: Handle): Handle;
92
- transactionGet(engine: Handle, tx: Handle, collection: string, docId: string): Handle;
93
- transactionSet(tx: Handle, collection: string, docId: string, doc: Handle): number;
94
- transactionCommit(engine: Handle, tx: Handle): number;
95
- transactionFree(tx: Handle): void;
96
-
97
- // Query API
98
- queryNew(collection: string): Handle;
99
- queryFree(query: Handle): void;
100
- queryWhereEqStr(query: Handle, field: string, value: string): number;
101
- queryWhereEqBool(query: Handle, field: string, value: boolean): number;
102
- queryWhereEqInt(query: Handle, field: string, value: number | bigint): number;
103
- queryWhereNeStr(query: Handle, field: string, value: string): number;
104
- queryWhereNeInt(query: Handle, field: string, value: number | bigint): number;
105
- queryWhereGtStr(query: Handle, field: string, value: string): number;
106
- queryWhereGtInt(query: Handle, field: string, value: number | bigint): number;
107
- queryWhereGteStr(query: Handle, field: string, value: string): number;
108
- queryWhereGteInt(query: Handle, field: string, value: number | bigint): number;
109
- queryWhereLtStr(query: Handle, field: string, value: string): number;
110
- queryWhereLtInt(query: Handle, field: string, value: number | bigint): number;
111
- queryWhereLteStr(query: Handle, field: string, value: string): number;
112
- queryWhereLteInt(query: Handle, field: string, value: number | bigint): number;
113
- queryOrderBy(query: Handle, field: string, ascending: boolean): number;
114
- queryLimit(query: Handle, limit: number): number;
115
- queryOffset(query: Handle, offset: number): number;
116
- querySelectField(query: Handle, field: string): number;
117
- queryExecute(engine: Handle, query: Handle): string | null;
118
- queryDelete(engine: Handle, query: Handle): number;
119
- queryDeleteLocal(engine: Handle, query: Handle): number;
120
- queryPatch(engine: Handle, query: Handle, patchDoc: Handle): number;
121
- queryExecuteToHandles(engine: Handle, query: Handle): Handle;
122
- resultSetCount(results: Handle): number;
123
- resultSetGetDoc(results: Handle, index: number): Handle;
124
- resultSetFree(results: Handle): void;
125
- resultSetToJson(results: Handle): string | null;
126
- // Raw result sets (v0.8.3): pinned bytes, not decoded docs. Rows are
127
- // resolved selectively via rawDocToDoc (decode only what you touch);
128
- // page with queryStartAfterRaw (no decode at all). rawDocBytes/Id need
129
- // backend memory reads — use resolve-then-read instead (see client.ts).
130
- queryExecuteRaw(engine: Handle, query: Handle): Handle;
131
- rawResultCount(results: Handle): number;
132
- rawResultGet(results: Handle, index: number): Handle;
133
- rawResultFree(results: Handle): void;
134
- rawDocToDoc(engine: Handle, rawDoc: Handle, collection: string): Handle;
135
- queryStartAfterRaw(query: Handle, anchorRawDoc: Handle): number;
136
- // Borrowed views (v0.8.11): lazy typed pulls, no owned construction.
137
- // Numeric/bool getters cross by value (no memory reads); strings and
138
- // walks stay on resolve/raw paths (documented in client.ts).
139
- viewGet(engine: Handle, collection: string, docId: string): Handle;
140
- viewFree(view: Handle): void;
141
- viewFieldCount(view: Handle): number;
142
- viewHasField(view: Handle, key: string): boolean;
143
- viewGetInt(view: Handle, key: string): number | bigint | null;
144
- viewGetFloat(view: Handle, key: string): number | null;
145
- viewGetBool(view: Handle, key: string): boolean | null;
146
- viewToDoc(view: Handle, docId: string): Handle;
147
- queryDeferBlobs(query: Handle, defer: boolean): number;
148
- docResolveBlobs(engine: Handle, collection: string, doc: Handle): number;
149
- engineInsertTake(engine: Handle, collection: string, docId: string, doc: Handle): number;
150
- configSetWalReserveBytes(c: Handle, bytes: number | bigint): void;
151
-
152
- // Full-Text Search Queries (Added)
153
- queryWhereMatch(query: Handle, field: string, value: string): number;
154
- queryWhereMatchPrefix(query: Handle, field: string, value: string): number;
155
- queryWhereContains(query: Handle, field: string, value: string): number;
156
- queryWhereStartsWith(query: Handle, field: string, value: string): number;
157
- queryWhereOrStr(query: Handle, field: string, value: string): number;
158
- queryWhereOrInt(query: Handle, field: string, value: number | bigint): number;
159
-
160
- // Aggregation API
161
- queryAggregateCount(query: Handle): number;
162
- queryAggregateSum(query: Handle, field: string): number;
163
- queryAggregateAvg(query: Handle, field: string): number;
164
- queryAggregateMin(query: Handle, field: string): number;
165
- queryAggregateMax(query: Handle, field: string): number;
166
- queryExecuteAggregation(engine: Handle, query: Handle): string | null;
167
-
168
- engineListCollections(engine: Handle): string | null;
169
-
170
- // Net Sync
171
- netSyncerNew(engine: Handle, name: string, roomKey: string): Handle;
172
- netSyncerStart(syncer: Handle, port: number): number;
173
- netSyncerSetDiscovery(syncer: Handle, mode: number): number;
174
- netSyncerStatus(syncer: Handle): string | null;
175
- netSyncerFree(syncer: Handle): void;
176
-
177
- // Cloud Sync (bi-directional WebSocket replication)
178
- cloudSyncNew(engine: Handle, mode: number, clientId: string | null, roomName: string | null, roomKey: string | null, authToken: string | null): Handle;
179
- cloudSyncServerNew(engine: Handle, serverId: string | null, authToken: string | null, roomName: string | null, roomKey: string | null): Handle;
180
- cloudSyncClientNew(engine: Handle, clientId: string | null, roomName: string | null, roomKey: string | null, authToken: string | null): Handle;
181
- cloudSyncStart(cloudSync: Handle, address: string): number;
182
- cloudSyncStatus(cloudSync: Handle): string | null;
183
- cloudSyncStop(cloudSync: Handle): void;
184
- cloudSyncFree(cloudSync: Handle): void;
185
-
186
- // v0.5.9
187
- createFtsIndex(engine: Handle, collection: string, field: string): number;
188
- createSimpleIndex(engine: Handle, collection: string, field: string): number;
189
-
190
- // Vector KNN (core >=0.13.3): one-shot search returns owned JSON
191
- // [{id, score, doc}] (null = hard error, e.g. dim mismatch — read
192
- // lastError); unindexed field returns "[]".
193
- createVectorIndex(engine: Handle, collection: string, field: string, dim: number, metric: number): number;
194
- engineFindNear(engine: Handle, collection: string, field: string, vec: Float32Array, k: number): string | null;
195
-
196
- // Graph traversal (core >=0.14.0): one-shot BFS returns owned JSON
197
- // [{collection, id, depth, doc}] (null = hard error, e.g. incoming
198
- // into untracked — read lastError); missing anchor returns "[]".
199
- // direction 0/1/2 = outgoing/incoming/both; edge null = any ref field.
200
- createTraverseIndex(engine: Handle, collection: string): number;
201
- engineFindPaths(engine: Handle, collection: string, id: string, direction: number, edge: string | null, maxDepth: number, k: number): string | null;
202
- // Phase B (core >=0.15.0): edge registration + weighted Dijkstra
203
- // (same hit shape, cost-ordered; cost rides each hit).
204
- createEdgeIndex(engine: Handle, collection: string): number;
205
- engineShortestPaths(engine: Handle, collection: string, id: string, direction: number, edge: string | null, via: string | null, weight: string | null, maxDepth: number, k: number): string | null;
206
-
207
- // v0.19.6 parity: index drops, full graph/traverse API, JSON writes.
208
- dropSimpleIndex(engine: Handle, collection: string, field: string): number;
209
- dropVectorIndex(engine: Handle, collection: string, field: string): number;
210
- dropCompositeIndex(engine: Handle, collection: string, id: number): number;
211
- dropEdgeIndex(engine: Handle, collection: string): number;
212
- dropFtsIndex(engine: Handle, collection: string, field: string): number;
213
- dropTraverseIndex(engine: Handle, collection: string): number;
214
- createVectorIndexV2(engine: Handle, collection: string, field: string, dim: number, metric: number, m: number, efConstruction: number, efSearch: number): number;
215
- findPathsFull(engine: Handle, collection: string, id: string, direction: number, edge: string | null, via: string | null, maxDepth: number, k: number, includePath: boolean, includeVia: boolean, nodeField: string | null, nodeJSON: string | null, typeField: string | null, typeJSON: string | null, matchField: string | null, matchJSON: string | null): string | null;
216
- simplePathsFull(engine: Handle, collection: string, id: string, direction: number, edge: string | null, via: string | null, weight: string | null, maxDepth: number, k: number, includePath: boolean, includeVia: boolean, nodeField: string | null, nodeJSON: string | null, typeField: string | null, typeJSON: string | null, matchField: string | null, matchJSON: string | null): string | null;
217
- exportGraph(engine: Handle, collection: string, direction: number, edge: string | null, via: string | null, nodeField: string | null, nodeJSON: string | null): string | null;
218
- graphAnalysis(engine: Handle, collection: string, op: string, direction: number, edge: string | null, via: string | null, nodeField: string | null, nodeJSON: string | null): string | null;
219
- insertJSON(engine: Handle, collection: string, id: string, json: string): number;
220
- batchSetJSON(batch: Handle, collection: string, id: string, json: string): number;
221
-
222
- // Array API
223
- arrayNew(): Handle;
224
- arrayFree(array: Handle): void;
225
- arrayAppendStr(array: Handle, value: string): number;
226
- arrayAppendInt(array: Handle, value: number | bigint): number;
227
- arrayAppendDoc(array: Handle, doc: Handle): number;
228
-
229
- // Nested structures
230
- docInsertDoc(parent: Handle, key: string, child: Handle): number;
231
- docInsertArray(parent: Handle, key: string, array: Handle): number;
232
-
233
- // Query extensions
234
- queryWhereIn(query: Handle, field: string, array: Handle): number;
235
- queryWhereNotIn(query: Handle, field: string, array: Handle): number;
236
- queryWhereArrayContainsAny(query: Handle, field: string, array: Handle): number;
237
- queryWhereArrayContainsStr(query: Handle, field: string, value: string): number;
238
- queryWhereArrayContainsInt(query: Handle, field: string, value: number | bigint): number;
239
- queryStartAfter(query: Handle, anchorDoc: Handle): number;
240
- queryStartAt(query: Handle, anchorDoc: Handle): number;
241
- queryEndAt(query: Handle, anchorDoc: Handle): number;
242
- queryEndBefore(query: Handle, anchorDoc: Handle): number;
243
-
244
- // Unix-only hk_socket_sync_* is deliberately absent: Windows builds omit
245
- // the symbol, so binding it unconditionally would break koffi on Windows.
246
-
247
- // F8: per-collection schemas over the C ABI.
248
- engineSetSchema(engine: Handle, collection: string, schemaJson: string): number;
249
- engineGetSchema(engine: Handle, collection: string): string | null;
250
- engineClearSchema(engine: Handle, collection: string): number;
251
- engineListSchemas(engine: Handle): string | null;
252
-
253
- // F5: versioned reads, transaction patch/get_many, retry classification.
254
- engineGetVersioned(engine: Handle, collection: string, docId: string): { doc: Handle; version: bigint };
255
- transactionGetMany(engine: Handle, tx: Handle, collection: string, idsJson: string): string | null;
256
- transactionPatch(engine: Handle, tx: Handle, collection: string, docId: string, updatesJson: string): number;
257
- errorLastKind(): number;
258
- errorIsRetryable(): boolean;
259
-
260
- // F7/F3: explain + array/graph predicates.
261
- queryExplain(engine: Handle, query: Handle): string | null;
262
- queryWhereArrayAllEq(query: Handle, field: string, valueJson: string): number;
263
- queryWhereElemMatch(query: Handle, field: string, filtersJson: string): number;
264
- queryWhereNear(query: Handle, field: string, vec: Float32Array): number;
265
- queryWhereTraverse(query: Handle, startCol: string, startId: string, direction: number, edge: string | null, maxDepth: number): number;
266
-
267
- // F4: aggregation pipeline (raw handle; typed builder is separate work).
268
- pipelineNew(collection: string): Handle;
269
- pipelineFree(pipeline: Handle): void;
270
- pipelineMatch(pipeline: Handle, query: Handle): number;
271
- pipelineProject(pipeline: Handle, fieldsJson: string): number;
272
- pipelineSort(pipeline: Handle, field: string, ascending: boolean): number;
273
- pipelineLimit(pipeline: Handle, limit: number): number;
274
- pipelineOffset(pipeline: Handle, offset: number): number;
275
- pipelineUnwind(pipeline: Handle, field: string): number;
276
- pipelineGroup(pipeline: Handle, key: string, accumsJson: string): number;
277
- pipelineExecute(engine: Handle, pipeline: Handle): string | null;
278
- pipelineExecuteToHandles(engine: Handle, pipeline: Handle): Handle;
279
-
280
- // Config additions (0.12-0.16 core knobs).
281
- configSetGroupCommitIntervalMs(config: Handle, ms: number | bigint): void;
282
- configSetPageCacheCapacity(config: Handle, capacity: number): void;
283
- configSetSyncExcluded(config: Handle, collectionsJson: string): number;
284
- configSetLazyCollections(config: Handle, collectionsJson: string): number;
285
- configSetReplicationCollections(config: Handle, collectionsJson: string): number;
286
-
287
- // Quiescence / diagnostics / logging.
288
- engineAwaitQuiescent(engine: Handle, timeoutMs: number | bigint): boolean;
289
- engineQuiescenceStatus(engine: Handle): string | null;
290
- debugWriteStats(): string | null;
291
- logEnableStderr(): void;
292
- logDisableStderr(): void;
293
-
294
- // Lazy cursor walks: callbacks copy immediately; returning false stops.
295
- cursorWalk(engine: Handle, query: Handle, callback: (id: string, bytes: Uint8Array) => boolean): number;
296
- cursorWalkView(engine: Handle, query: Handle, callback: (id: string, view: Handle) => boolean): number;
297
-
298
- // Borrowed raw/view reads: copied before return (lifetime is the result set/view).
299
- rawDocBytes(rawDoc: Handle): Uint8Array | null;
300
- rawDocId(rawDoc: Handle): string | null;
301
- viewGetStr(view: Handle, key: string): string | null;
302
- viewGetBytes(view: Handle, key: string): Uint8Array | null;
303
-
304
- // P2 (core 0.18): JSON read paths + dotted-path view pulls. JSON strings
305
- // are owned (free happens in the binding); view pulls copy before return.
306
- engineGetJson(engine: Handle, collection: string, docId: string): string | null;
307
- queryExecuteJson(engine: Handle, query: Handle): string | null;
308
- viewToJson(view: Handle): string | null;
309
- cursorWalkJson(engine: Handle, query: Handle, callback: (id: string, json: Uint8Array) => boolean): number;
310
- viewGetPathInt(view: Handle, path: string): number | bigint | null;
311
- viewGetPathFloat(view: Handle, path: string): number | null;
312
- viewGetPathBool(view: Handle, path: string): boolean | null;
313
- viewGetPathStr(view: Handle, path: string): string | null;
314
- viewGetPathBytes(view: Handle, path: string): Uint8Array | null;
315
-
316
- lastError(): string;
317
- }
318
-
319
- function isBunRuntime(): boolean {
320
- return typeof (globalThis as any).Bun !== 'undefined';
321
- }
322
-
323
- function defaultLibraryPath(): string {
324
- const libName = process.platform === 'win32' ? 'hakodb.dll' :
325
- process.platform === 'darwin' ? 'libhakodb.dylib' : 'libhakodb.so';
326
- // Explicit path wins (loadNativeBindings(path)); otherwise the binary
327
- // shipped under ./native/ (populated by sync-native.* from a core
328
- // checkout or release asset). HAKODB_LIB_PATH overrides both.
329
- const override = typeof process !== 'undefined' ? process.env.HAKODB_LIB_PATH : undefined;
330
- if (override) return override;
331
- return `./native/${libName}`;
332
- }
333
-
334
- function resolveLibraryPath(explicitPath?: string): string {
335
- return explicitPath ?? defaultLibraryPath();
336
- }
337
-
338
- async function createBunBindings(libPath: string): Promise<NativeBindings> {
339
- const ffi = await import('bun:ffi');
340
- const { dlopen, FFIType, CString, JSCallback, toArrayBuffer } = ffi as any;
341
-
342
- const symbols = dlopen(libPath, {
343
- hk_engine_open: { args: [FFIType.cstring], returns: FFIType.ptr },
344
- hk_engine_is_indexes_ready: { args: [FFIType.ptr], returns: FFIType.bool },
345
- hk_engine_open_with_config: { args: [FFIType.cstring, FFIType.ptr], returns: FFIType.ptr },
346
- hk_engine_free: { args: [FFIType.ptr], returns: FFIType.void },
347
- hk_engine_backup: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
348
- hk_engine_compact: { args: [FFIType.ptr], returns: FFIType.i32 },
349
- hk_engine_get_stats: { args: [FFIType.ptr], returns: FFIType.ptr },
350
- hk_engine_get_audit_log: { args: [FFIType.ptr], returns: FFIType.ptr },
351
- hk_engine_snapshot_indices: { args: [FFIType.ptr], returns: FFIType.i32 },
352
- hk_engine_list_indexes: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.ptr },
353
-
354
- hk_config_new: { args: [], returns: FFIType.ptr },
355
- hk_config_free: { args: [FFIType.ptr], returns: FFIType.void },
356
- hk_config_set_durability: { args: [FFIType.ptr, FFIType.i32], returns: FFIType.void },
357
- hk_config_set_encryption_key: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.void },
358
- hk_config_set_encrypted_collections: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
359
- hk_config_set_audit_log: { args: [FFIType.ptr, FFIType.bool, FFIType.cstring], returns: FFIType.void },
360
- hk_config_set_query_workers: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.void },
361
- hk_config_set_memory_limits: { args: [FFIType.ptr, FFIType.usize, FFIType.usize], returns: FFIType.void },
362
- hk_config_set_storage_tuning: { args: [FFIType.ptr, FFIType.usize, FFIType.usize, FFIType.usize], returns: FFIType.void },
363
- hk_config_set_blob_threshold: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.void },
364
- hk_config_set_compression: { args: [FFIType.ptr, FFIType.bool, FFIType.i32], returns: FFIType.void },
365
- hk_config_set_background_maintenance: { args: [FFIType.ptr, FFIType.bool], returns: FFIType.void },
366
-
367
- hk_engine_watch: { args: [FFIType.ptr, FFIType.cstring, FFIType.function, FFIType.ptr], returns: FFIType.ptr },
368
- hk_watch_free: { args: [FFIType.ptr], returns: FFIType.void },
369
-
370
- hk_doc_new: { args: [], returns: FFIType.ptr },
371
- hk_doc_free: { args: [FFIType.ptr], returns: FFIType.void },
372
- hk_doc_insert_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
373
- hk_doc_insert_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
374
- hk_doc_insert_float: { args: [FFIType.ptr, FFIType.cstring, FFIType.f64], returns: FFIType.i32 },
375
- hk_doc_insert_bool: { args: [FFIType.ptr, FFIType.cstring, FFIType.bool], returns: FFIType.i32 },
376
- hk_doc_insert_null: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
377
- hk_doc_insert_bin: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr, FFIType.usize], returns: FFIType.i32 },
378
- hk_doc_insert_timestamp: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
379
- hk_doc_insert_server_timestamp: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
380
- hk_doc_to_json: { args: [FFIType.ptr], returns: FFIType.ptr },
381
-
382
- hk_engine_insert: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
383
- hk_engine_get: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
384
- hk_engine_delete: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
385
- hk_engine_delete_local: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
386
- hk_engine_delete_ext: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.i32], returns: FFIType.i32 },
387
- hk_engine_restore: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
388
- hk_engine_restore_available: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.ptr },
389
- hk_engine_delete_queue: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.ptr },
390
- hk_engine_sweep_now: { args: [FFIType.ptr], returns: FFIType.i32 },
391
- hk_engine_set_collection_local: { args: [FFIType.ptr, FFIType.cstring, FFIType.i32], returns: FFIType.i32 },
392
- hk_engine_replicate_key: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
393
- hk_engine_replicate_collection: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
394
- hk_engine_vacuum_collection: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
395
- hk_engine_relocate_docs: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
396
- hk_engine_load_collection: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
397
- hk_engine_unload_collection: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
398
- hk_engine_unloaded_collections: { args: [FFIType.ptr], returns: FFIType.ptr },
399
- hk_engine_patch: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
400
- hk_engine_insert_subdoc: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
401
- hk_engine_get_by_ref: { args: [FFIType.ptr, FFIType.ptr, FFIType.cstring], returns: FFIType.ptr },
402
- hk_engine_create_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.u32 },
403
- hk_engine_create_vector_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.u32], returns: FFIType.i32 },
404
- hk_engine_find_near: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.ptr, FFIType.usize, FFIType.usize], returns: FFIType.ptr },
405
- hk_engine_create_traverse_index: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
406
- hk_engine_find_paths: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.cstring, FFIType.usize, FFIType.usize], returns: FFIType.ptr },
407
- hk_engine_create_edge_index: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
408
- hk_engine_shortest_paths: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.usize, FFIType.usize], returns: FFIType.ptr },
409
-
410
- hk_batch_new: { args: [], returns: FFIType.ptr },
411
-
412
- // v0.19.6 parity
413
- hk_engine_drop_simple_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
414
- hk_engine_drop_vector_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
415
- hk_engine_drop_composite_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.u32], returns: FFIType.i32 },
416
- hk_engine_drop_edge_index: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
417
- hk_engine_drop_fts_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
418
- hk_engine_drop_traverse_index: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
419
- hk_engine_create_vector_index_v2: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.u32, FFIType.usize, FFIType.usize, FFIType.usize], returns: FFIType.i32 },
420
- hk_engine_find_paths_v4: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.cstring, FFIType.cstring, FFIType.usize, FFIType.usize, FFIType.u32, FFIType.u32, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
421
- hk_engine_find_simple_paths_v3: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.usize, FFIType.usize, FFIType.u32, FFIType.u32, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
422
- hk_engine_export_graph: { args: [FFIType.ptr, FFIType.cstring, FFIType.u32, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
423
- hk_engine_graph_analysis: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
424
- hk_engine_insert_json: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
425
- hk_batch_set_json: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
426
- hk_batch_free: { args: [FFIType.ptr], returns: FFIType.void },
427
- hk_batch_set: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
428
- hk_batch_delete: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
429
- hk_batch_delete_ext: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.i32], returns: FFIType.i32 },
430
- hk_batch_commit: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
431
-
432
- hk_transaction_begin: { args: [FFIType.ptr], returns: FFIType.ptr },
433
- hk_transaction_get: { args: [FFIType.ptr, FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
434
- hk_transaction_set: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
435
- hk_transaction_commit: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
436
- hk_transaction_free: { args: [FFIType.ptr], returns: FFIType.void },
437
-
438
- hk_query_new: { args: [FFIType.cstring], returns: FFIType.ptr },
439
- hk_query_free: { args: [FFIType.ptr], returns: FFIType.void },
440
- hk_query_where_eq_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
441
- hk_query_where_eq_bool: { args: [FFIType.ptr, FFIType.cstring, FFIType.bool], returns: FFIType.i32 },
442
- hk_query_where_eq_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
443
- hk_query_where_ne_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
444
- hk_query_where_ne_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
445
- hk_query_where_gt_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
446
- hk_query_where_gt_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
447
- hk_query_where_gte_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
448
- hk_query_where_gte_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
449
- hk_query_where_lt_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
450
- hk_query_where_lt_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
451
- hk_query_where_lte_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
452
- hk_query_where_lte_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
453
- hk_query_where_match: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
454
- hk_query_where_match_prefix: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
455
- hk_query_where_contains: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
456
- hk_query_where_starts_with: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
457
- hk_query_order_by: { args: [FFIType.ptr, FFIType.cstring, FFIType.bool], returns: FFIType.i32 },
458
- hk_query_limit: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.i32 },
459
- hk_query_offset: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.i32 },
460
- hk_query_select_field: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
461
- hk_query_execute: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
462
- hk_query_delete: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
463
- hk_query_delete_local: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
464
- hk_query_patch: { args: [FFIType.ptr, FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
465
- hk_query_execute_to_handles: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
466
- hk_result_set_count: { args: [FFIType.ptr], returns: FFIType.usize },
467
- hk_result_set_get_doc: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.ptr },
468
- hk_result_set_free: { args: [FFIType.ptr], returns: FFIType.void },
469
- hk_result_set_to_json: { args: [FFIType.ptr], returns: FFIType.ptr },
470
- // Raw result sets (v0.8.3). All crossings are handles/strings/ints —
471
- // no (ptr,len) memory reads needed (anchor + resolve by handle).
472
- hk_query_execute_raw: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
473
- hk_rawresult_count: { args: [FFIType.ptr], returns: FFIType.usize },
474
- hk_rawresult_get: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.ptr },
475
- hk_rawresult_free: { args: [FFIType.ptr], returns: FFIType.void },
476
- hk_query_start_after_raw: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
477
- hk_rawdoc_to_doc: { args: [FFIType.ptr, FFIType.ptr, FFIType.cstring], returns: FFIType.ptr },
478
- hk_query_defer_blobs: { args: [FFIType.ptr, FFIType.i32], returns: FFIType.i32 },
479
- hk_doc_resolve_blobs: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
480
- hk_engine_insert_take: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
481
- hk_config_set_wal_reserve_bytes: { args: [FFIType.ptr, FFIType.u64], returns: FFIType.void },
482
-
483
- hk_query_aggregate_count: { args: [FFIType.ptr], returns: FFIType.i32 },
484
- hk_query_aggregate_sum: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
485
- hk_query_aggregate_avg: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
486
- hk_query_aggregate_min: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
487
- hk_query_aggregate_max: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
488
- hk_query_execute_aggregation: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
489
- hk_query_where_or_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
490
- hk_query_where_or_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
491
-
492
- // v0.5.9
493
- // Indexing
494
- hk_engine_create_fts_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
495
- hk_engine_create_simple_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
496
-
497
- // Array API
498
- hk_array_new: { args: [], returns: FFIType.ptr },
499
- hk_array_free: { args: [FFIType.ptr], returns: FFIType.void },
500
- hk_array_append_str: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
501
- hk_array_append_int: { args: [FFIType.ptr, FFIType.i64], returns: FFIType.i32 },
502
- hk_array_append_doc: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
503
-
504
- // Nested structures
505
- hk_doc_insert_doc: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
506
- hk_doc_insert_array: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
507
-
508
- // Query extensions
509
- hk_query_where_in: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
510
- hk_query_where_not_in: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
511
- hk_query_where_array_contains_any: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
512
- hk_query_where_array_contains: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
513
- hk_query_start_after: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
514
- hk_query_start_at: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
515
- hk_query_end_at: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
516
- hk_query_end_before: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
517
-
518
- hk_doc_insert_reference: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
519
-
520
- hk_cloud_sync_new: { args: [FFIType.ptr, FFIType.i32, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
521
- hk_cloud_sync_server_new: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
522
- hk_cloud_sync_client_new: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
523
- hk_cloud_sync_start: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
524
- hk_cloud_sync_status: { args: [FFIType.ptr], returns: FFIType.ptr },
525
- hk_cloud_sync_stop: { args: [FFIType.ptr], returns: FFIType.void },
526
- hk_cloud_sync_free: { args: [FFIType.ptr], returns: FFIType.void },
527
-
528
- // 0.16.5 parity: F8 schemas, F5 versioned/tx, F7 explain, F3 predicates,
529
- // F4 pipelines, config knobs, quiescence, lazy walks and borrowed reads.
530
- hk_config_set_group_commit_interval_ms: { args: [FFIType.ptr, FFIType.u64], returns: FFIType.void },
531
- hk_config_set_page_cache_capacity: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.void },
532
- hk_config_set_sync_excluded: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
533
- hk_config_set_lazy_collections: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
534
- hk_config_set_replication_collections: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
535
- hk_engine_await_quiescent: { args: [FFIType.ptr, FFIType.u64], returns: FFIType.bool },
536
- hk_engine_quiescence_status: { args: [FFIType.ptr], returns: FFIType.ptr },
537
- hk_engine_get_versioned: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.ptr], returns: FFIType.ptr },
538
- hk_engine_set_schema: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
539
- hk_engine_get_schema: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.ptr },
540
- hk_engine_clear_schema: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
541
- hk_engine_list_schemas: { args: [FFIType.ptr], returns: FFIType.ptr },
542
- hk_query_explain: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
543
- hk_query_where_array_all_eq: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
544
- hk_query_where_elem_match: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
545
- hk_query_where_near: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr, FFIType.usize], returns: FFIType.i32 },
546
- hk_query_where_traverse: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.cstring, FFIType.usize], returns: FFIType.i32 },
547
- hk_pipeline_new: { args: [FFIType.cstring], returns: FFIType.ptr },
548
- hk_pipeline_free: { args: [FFIType.ptr], returns: FFIType.void },
549
- hk_pipeline_match: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
550
- hk_pipeline_project: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
551
- hk_pipeline_sort: { args: [FFIType.ptr, FFIType.cstring, FFIType.bool], returns: FFIType.i32 },
552
- hk_pipeline_limit: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.i32 },
553
- hk_pipeline_offset: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.i32 },
554
- hk_pipeline_unwind: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
555
- hk_pipeline_group: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
556
- hk_pipeline_execute: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
557
- hk_pipeline_execute_to_handles: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
558
- hk_transaction_get_many: { args: [FFIType.ptr, FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
559
- hk_transaction_patch: { args: [FFIType.ptr, FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
560
- hk_error_last_kind: { args: [], returns: FFIType.i32 },
561
- hk_error_is_retryable: { args: [], returns: FFIType.bool },
562
- hk_debug_write_stats: { args: [], returns: FFIType.ptr },
563
- hk_log_enable_stderr: { args: [], returns: FFIType.void },
564
- hk_log_disable_stderr: { args: [], returns: FFIType.void },
565
- hk_cursor_walk: { args: [FFIType.ptr, FFIType.ptr, FFIType.function, FFIType.ptr], returns: FFIType.i64 },
566
- hk_cursor_walk_view: { args: [FFIType.ptr, FFIType.ptr, FFIType.function, FFIType.ptr], returns: FFIType.i64 },
567
- hk_rawdoc_bytes: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
568
- hk_rawdoc_id: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
569
- hk_view_get_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.ptr },
570
- hk_view_get_bytes: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.ptr },
571
- hk_engine_get_json: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
572
- hk_query_execute_json: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
573
- hk_cursor_walk_json: { args: [FFIType.ptr, FFIType.ptr, FFIType.function, FFIType.ptr], returns: FFIType.i64 },
574
- hk_view_to_json: { args: [FFIType.ptr], returns: FFIType.ptr },
575
- hk_view_get_path_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.bool },
576
- hk_view_get_path_float: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.bool },
577
- hk_view_get_path_bool: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
578
- hk_view_get_path_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.ptr },
579
- hk_view_get_path_bytes: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.ptr },
580
-
581
- // ================= ERRORS =================
582
- hk_last_error: { args: [], returns: FFIType.ptr },
583
- hk_string_free: { args: [FFIType.ptr], returns: FFIType.void }
584
-
585
- }).symbols;
586
-
587
- const toC = (s: string | null) => s ? Buffer.from(s + '\0') : null;
588
- // ponytail: typed-array pointer for FFI out-params (i64/double reads).
589
- // Guarded: only view getters use it, and they throw a clear error on
590
- // ancient runtimes instead of a cryptic TypeError.
591
- const bunPtr = (ffi as any).ptr;
592
- const ptrOf = (arr: BigInt64Array | Float64Array): any => {
593
- if (!bunPtr) throw new Error('bun:ffi ptr() unavailable — upgrade Bun');
594
- return bunPtr(arr);
595
- };
596
- const ptrToStringAndFree = (ptr: any): string | null => {
597
- if (!ptr) return null;
598
- const text = new CString(ptr).toString();
599
- symbols.hk_string_free(ptr);
600
- return text;
601
- };
602
-
603
- return {
604
- engineOpen: (path) => symbols.hk_engine_open(toC(path)),
605
- engineIsIndexesReady: (engine) => symbols.hk_engine_is_indexes_ready(engine),
606
- engineOpenWithConfig: (path, config) => symbols.hk_engine_open_with_config(toC(path), config),
607
- engineFree: (engine) => symbols.hk_engine_free(engine),
608
- engineBackup: (e, p) => symbols.hk_engine_backup(e, toC(p)),
609
- engineCompact: (e) => symbols.hk_engine_compact(e),
610
- engineGetStats: (e) => ptrToStringAndFree(symbols.hk_engine_get_stats(e)),
611
- engineGetAuditLog: (e) => ptrToStringAndFree(symbols.hk_engine_get_audit_log(e)),
612
- engineSnapshotIndices: (e) => symbols.hk_engine_snapshot_indices(e),
613
- engineListIndexes: (e, c) => ptrToStringAndFree(symbols.hk_engine_list_indexes(e, toC(c))),
614
-
615
- configNew: () => symbols.hk_config_new(),
616
- configFree: (c) => symbols.hk_config_free(c),
617
- configSetDurability: (c, m) => symbols.hk_config_set_durability(c, m),
618
- configSetEncryptionKey: (c, k) => symbols.hk_config_set_encryption_key(c, toC(k)),
619
- configSetEncryptedCollections: (c, json) => symbols.hk_config_set_encrypted_collections(c, toC(json)),
620
- configSetAuditLog: (c, e, p) => symbols.hk_config_set_audit_log(c, e, toC(p)),
621
- configSetQueryWorkers: (c, count) => symbols.hk_config_set_query_workers(c, count),
622
- configSetMemoryLimits: (c, m, mi) => symbols.hk_config_set_memory_limits(c, m, mi),
623
- configSetStorageTuning: (c, ps, th, gc) => symbols.hk_config_set_storage_tuning(c, ps, th, gc),
624
- configSetBlobThreshold: (c, t) => symbols.hk_config_set_blob_threshold(c, t),
625
- configSetCompression: (c, e, l) => symbols.hk_config_set_compression(c, e, l),
626
- configSetBackgroundMaintenance: (c, e) => symbols.hk_config_set_background_maintenance(c, e),
627
-
628
- engineWatch: (engine, collection, callback) => {
629
- const cb = new JSCallback((c: any, p: any, kind: number) => {
630
- callback(new CString(c).toString(), new CString(p).toString(), kind);
631
- }, { args: [FFIType.ptr, FFIType.ptr, FFIType.i32], returns: FFIType.void });
632
- return symbols.hk_engine_watch(engine, toC(collection), cb, null);
633
- },
634
- watchFree: (watch) => symbols.hk_watch_free(watch),
635
-
636
- docNew: () => symbols.hk_doc_new(),
637
- docFree: (doc) => symbols.hk_doc_free(doc),
638
- docInsertStr: (doc, key, value) => symbols.hk_doc_insert_str(doc, toC(key), toC(value)),
639
- docInsertInt: (doc, key, value) => symbols.hk_doc_insert_int(doc, toC(key), BigInt(value)),
640
- docInsertFloat: (doc, key, value) => symbols.hk_doc_insert_float(doc, toC(key), value),
641
- docInsertBool: (doc, key, value) => symbols.hk_doc_insert_bool(doc, toC(key), value),
642
- docInsertNull: (doc, key) => symbols.hk_doc_insert_null(doc, toC(key)),
643
- docInsertBin: (doc, key, bytes) => symbols.hk_doc_insert_bin(doc, toC(key), bytes, bytes.byteLength),
644
- docInsertTimestamp: (doc, key, micros) => symbols.hk_doc_insert_timestamp(doc, toC(key), micros),
645
- docInsertServerTimestamp: (doc, key) => symbols.hk_doc_insert_server_timestamp(doc, toC(key)),
646
- docInsertReference: (doc, key, tc, tid) => symbols.hk_doc_insert_reference(doc, toC(key), toC(tc), toC(tid)),
647
- docToJson: (doc) => ptrToStringAndFree(symbols.hk_doc_to_json(doc)),
648
-
649
- engineInsert: (engine, collection, docId, doc) => symbols.hk_engine_insert(engine, toC(collection), toC(docId), doc),
650
- engineGet: (engine, collection, docId) => symbols.hk_engine_get(engine, toC(collection), toC(docId)),
651
- engineDelete: (engine, collection, docId) => symbols.hk_engine_delete(engine, toC(collection), toC(docId)),
652
- engineDeleteLocal: (engine, collection, docId) => symbols.hk_engine_delete_local(engine, toC(collection), toC(docId)),
653
- engineDeleteEx: (engine, collection, docId, force) => symbols.hk_engine_delete_ext(engine, toC(collection), toC(docId), force ? 1 : 0),
654
- engineRestore: (engine, collection, docId) => symbols.hk_engine_restore(engine, toC(collection), toC(docId)),
655
- engineRestoreAvailable: (engine, collection) => ptrToStringAndFree(symbols.hk_engine_restore_available(engine, (collection ? toC(collection) : null) as unknown as string)),
656
- engineDeleteQueue: (engine, collection) => ptrToStringAndFree(symbols.hk_engine_delete_queue(engine, (collection ? toC(collection) : null) as unknown as string)),
657
- engineSweepNow: (engine) => symbols.hk_engine_sweep_now(engine),
658
- engineSetCollectionLocal: (engine, collection, local) => symbols.hk_engine_set_collection_local(engine, toC(collection), local),
659
- engineReplicateKey: (engine, collection, docId) => symbols.hk_engine_replicate_key(engine, toC(collection), toC(docId)),
660
- engineReplicateCollection: (engine, collection) => symbols.hk_engine_replicate_collection(engine, toC(collection)),
661
- engineVacuumCollection: (engine, collection) => symbols.hk_engine_vacuum_collection(engine, toC(collection)),
662
- engineRelocateDocs: (engine, src, dst, idsJson) => ptrToStringAndFree(symbols.hk_engine_relocate_docs(engine, toC(src), toC(dst), toC(idsJson))),
663
- engineLoadCollection: (engine, collection) => symbols.hk_engine_load_collection(engine, toC(collection)),
664
- engineUnloadCollection: (engine, collection) => symbols.hk_engine_unload_collection(engine, toC(collection)),
665
- engineUnloadedCollections: (engine) => ptrToStringAndFree(symbols.hk_engine_unloaded_collections(engine)),
666
- enginePatch: (engine, collection, docId, updates) => symbols.hk_engine_patch(engine, toC(collection), toC(docId), updates),
667
- engineInsertSubDoc: (engine, col, id, subCol, subId, doc) => symbols.hk_engine_insert_subdoc(engine, toC(col), toC(id), toC(subCol), toC(subId), doc),
668
- engineGetByRef: (engine, doc, fieldKey) => symbols.hk_engine_get_by_ref(engine, doc, toC(fieldKey)),
669
- engineCreateIndex: (engine, collection, fieldsJson) => symbols.hk_engine_create_index(engine, toC(collection), toC(fieldsJson)),
670
-
671
- batchNew: () => symbols.hk_batch_new(),
672
- batchFree: (batch) => symbols.hk_batch_free(batch),
673
- batchSet: (batch, collection, docId, doc) => symbols.hk_batch_set(batch, toC(collection), toC(docId), doc),
674
- batchDelete: (batch, collection, docId) => symbols.hk_batch_delete(batch, toC(collection), toC(docId)),
675
- batchDeleteEx: (batch, collection, docId, force) => symbols.hk_batch_delete_ext(batch, toC(collection), toC(docId), force ? 1 : 0),
676
- batchCommit: (engine, batch) => symbols.hk_batch_commit(engine, batch),
677
-
678
- transactionBegin: (engine) => symbols.hk_transaction_begin(engine),
679
- transactionGet: (engine, tx, collection, docId) => symbols.hk_transaction_get(engine, tx, toC(collection), toC(docId)),
680
- transactionSet: (tx, collection, docId, doc) => symbols.hk_transaction_set(tx, toC(collection), toC(docId), doc),
681
- transactionCommit: (engine, tx) => symbols.hk_transaction_commit(engine, tx),
682
- transactionFree: (tx) => symbols.hk_transaction_free(tx),
683
-
684
- queryNew: (collection) => symbols.hk_query_new(toC(collection)),
685
- queryFree: (query) => symbols.hk_query_free(query),
686
- queryWhereEqStr: (query, field, value) => symbols.hk_query_where_eq_str(query, toC(field), toC(value)),
687
- queryWhereEqBool: (query, field, value) => symbols.hk_query_where_eq_bool(query, toC(field), value),
688
- queryWhereEqInt: (query, field, value) => symbols.hk_query_where_eq_int(query, toC(field), BigInt(value)),
689
- queryWhereNeStr: (query, field, value) => symbols.hk_query_where_ne_str(query, toC(field), toC(value)),
690
- queryWhereNeInt: (query, field, value) => symbols.hk_query_where_ne_int(query, toC(field), BigInt(value)),
691
- queryWhereGtStr: (query, field, value) => symbols.hk_query_where_gt_str(query, toC(field), toC(value)),
692
- queryWhereGtInt: (query, field, value) => symbols.hk_query_where_gt_int(query, toC(field), BigInt(value)),
693
- queryWhereGteStr: (query, field, value) => symbols.hk_query_where_gte_str(query, toC(field), toC(value)),
694
- queryWhereGteInt: (query, field, value) => symbols.hk_query_where_gte_int(query, toC(field), BigInt(value)),
695
- queryWhereLtStr: (query, field, value) => symbols.hk_query_where_lt_str(query, toC(field), toC(value)),
696
- queryWhereLtInt: (query, field, value) => symbols.hk_query_where_lt_int(query, toC(field), BigInt(value)),
697
- queryWhereLteStr: (query, field, value) => symbols.hk_query_where_lte_str(query, toC(field), toC(value)),
698
- queryWhereLteInt: (query, field, value) => symbols.hk_query_where_lte_int(query, toC(field), BigInt(value)),
699
- queryWhereMatch: (query, field, value) => symbols.hk_query_where_match(query, toC(field), toC(value)),
700
- queryWhereMatchPrefix: (query, field, value) => symbols.hk_query_where_match_prefix(query, toC(field), toC(value)),
701
- queryWhereContains: (query, field, value) => symbols.hk_query_where_contains(query, toC(field), toC(value)),
702
- queryWhereStartsWith: (query, field, value) => symbols.hk_query_where_starts_with(query, toC(field), toC(value)),
703
- queryWhereOrStr: (query, field, value) => symbols.hk_query_where_or_str(query, toC(field), toC(value)),
704
- queryWhereOrInt: (query, field, value) => symbols.hk_query_where_or_int(query, toC(field), BigInt(value)),
705
- queryOrderBy: (query, field, asc) => symbols.hk_query_order_by(query, toC(field), asc),
706
- queryLimit: (query, limit) => symbols.hk_query_limit(query, limit),
707
- queryOffset: (query, offset) => symbols.hk_query_offset(query, offset),
708
- querySelectField: (query, field) => symbols.hk_query_select_field(query, toC(field)),
709
- queryExecute: (engine, query) => ptrToStringAndFree(symbols.hk_query_execute(engine, query)),
710
- queryDelete: (engine, query) => symbols.hk_query_delete(engine, query),
711
- queryDeleteLocal: (engine, query) => symbols.hk_query_delete_local(engine, query),
712
- queryPatch: (engine, query, patchDoc) => symbols.hk_query_patch(engine, query, patchDoc),
713
- queryExecuteToHandles: (engine, query) => symbols.hk_query_execute_to_handles(engine, query),
714
- resultSetCount: (results) => symbols.hk_result_set_count(results),
715
- resultSetGetDoc: (results, index) => symbols.hk_result_set_get_doc(results, index),
716
- resultSetFree: (results) => symbols.hk_result_set_free(results),
717
- resultSetToJson: (results) => ptrToStringAndFree(symbols.hk_result_set_to_json(results)),
718
- queryExecuteRaw: (engine, query) => symbols.hk_query_execute_raw(engine, query),
719
- rawResultCount: (results) => symbols.hk_rawresult_count(results),
720
- rawResultGet: (results, index) => symbols.hk_rawresult_get(results, index),
721
- rawResultFree: (results) => symbols.hk_rawresult_free(results),
722
- queryStartAfterRaw: (query, anchorRawDoc) => symbols.hk_query_start_after_raw(query, anchorRawDoc),
723
- rawDocToDoc: (engine, rawDoc, collection) => symbols.hk_rawdoc_to_doc(engine, rawDoc, toC(collection)),
724
- viewGet: (engine, collection, docId) => symbols.hk_view_get(engine, toC(collection), toC(docId)),
725
- viewFree: (view) => symbols.hk_view_free(view),
726
- viewFieldCount: (view) => symbols.hk_view_field_count(view),
727
- viewHasField: (view, key) => symbols.hk_view_has_field(view, toC(key)),
728
- viewGetInt: (view, key) => {
729
- const out = new BigInt64Array(1);
730
- return symbols.hk_view_get_int(view, toC(key), ptrOf(out)) ? out[0] : null;
731
- },
732
- viewGetFloat: (view, key) => {
733
- const out = new Float64Array(1);
734
- return symbols.hk_view_get_float(view, toC(key), ptrOf(out)) ? out[0] : null;
735
- },
736
- viewGetBool: (view, key) => {
737
- const r = symbols.hk_view_get_bool(view, toC(key));
738
- return r < 0 ? null : r !== 0;
739
- },
740
- viewToDoc: (view, docId) => symbols.hk_view_to_doc(view, toC(docId)),
741
- queryDeferBlobs: (query, defer) => symbols.hk_query_defer_blobs(query, defer ? 1 : 0),
742
- docResolveBlobs: (engine, collection, doc) => symbols.hk_doc_resolve_blobs(engine, toC(collection), doc),
743
- engineInsertTake: (engine, collection, docId, doc) => symbols.hk_engine_insert_take(engine, toC(collection), toC(docId), doc),
744
- configSetWalReserveBytes: (c, bytes) => symbols.hk_config_set_wal_reserve_bytes(c, bytes),
745
-
746
- queryAggregateCount: (q) => symbols.hk_query_aggregate_count(q),
747
- queryAggregateSum: (q, f) => symbols.hk_query_aggregate_sum(q, toC(f)),
748
- queryAggregateAvg: (q, f) => symbols.hk_query_aggregate_avg(q, toC(f)),
749
- queryAggregateMin: (q, f) => symbols.hk_query_aggregate_min(q, toC(f)),
750
- queryAggregateMax: (q, f) => symbols.hk_query_aggregate_max(q, toC(f)),
751
- queryExecuteAggregation: (e, q) => ptrToStringAndFree(symbols.hk_query_execute_aggregation(e, q)),
752
- engineListCollections: (engine) => ptrToStringAndFree(symbols.hk_engine_list_collections(engine)),
753
- netSyncerNew: (engine, name, roomKey) => symbols.hk_net_syncer_new(engine, toC(name), toC(roomKey)),
754
- netSyncerStart: (syncer, port) => symbols.hk_net_syncer_start(syncer, port),
755
- netSyncerSetDiscovery: (syncer, mode) => symbols.hk_net_syncer_set_discovery(syncer, mode),
756
- netSyncerStatus: (syncer) => ptrToStringAndFree(symbols.hk_net_syncer_status(syncer)),
757
- netSyncerFree: (syncer) => symbols.hk_net_syncer_free(syncer),
758
-
759
- cloudSyncNew: (engine, mode, clientId, roomName, roomKey, authToken) => symbols.hk_cloud_sync_new(engine, mode, toC(clientId), toC(roomName), toC(roomKey), toC(authToken)),
760
- cloudSyncServerNew: (engine, serverId, authToken, roomName, roomKey) => symbols.hk_cloud_sync_server_new(engine, toC(serverId), toC(authToken), toC(roomName), toC(roomKey)),
761
- cloudSyncClientNew: (engine, clientId, roomName, roomKey, authToken) => symbols.hk_cloud_sync_client_new(engine, toC(clientId), toC(roomName), toC(roomKey), toC(authToken)),
762
- cloudSyncStart: (cs, address) => symbols.hk_cloud_sync_start(cs, toC(address)),
763
- cloudSyncStatus: (cs) => ptrToStringAndFree(symbols.hk_cloud_sync_status(cs)),
764
- cloudSyncStop: (cs) => symbols.hk_cloud_sync_stop(cs),
765
- cloudSyncFree: (cs) => symbols.hk_cloud_sync_free(cs),
766
-
767
- // ================= v0.5.9 =================
768
-
769
- // Indexing
770
- createFtsIndex: (engine, collection, field) => symbols.hk_engine_create_fts_index(engine, toC(collection), toC(field)),
771
- createSimpleIndex: (engine, collection, field) => symbols.hk_engine_create_simple_index(engine, toC(collection), toC(field)),
772
- createVectorIndex: (engine, collection, field, dim, metric) => symbols.hk_engine_create_vector_index(engine, toC(collection), toC(field), dim, metric),
773
- // Float32Array crosses as its LE-f32 bytes (bun:ffi takes the typed
774
- // array as ptr, like docInsertBin takes bytes).
775
- engineFindNear: (engine, collection, field, vec, k) => ptrToStringAndFree(symbols.hk_engine_find_near(engine, toC(collection), toC(field), vec, vec.length, k)),
776
- createTraverseIndex: (engine, collection) => symbols.hk_engine_create_traverse_index(engine, toC(collection)),
777
- // Null edge crosses as NULL (any ref field); bun:ffi takes null for ptr/cstring.
778
- engineFindPaths: (engine, collection, id, direction, edge, maxDepth, k) => ptrToStringAndFree(symbols.hk_engine_find_paths(engine, toC(collection), toC(id), direction, edge === null ? null : toC(edge), maxDepth, k)),
779
- createEdgeIndex: (engine, collection) => symbols.hk_engine_create_edge_index(engine, toC(collection)),
780
- engineShortestPaths: (engine, collection, id, direction, edge, via, weight, maxDepth, k) => ptrToStringAndFree(symbols.hk_engine_shortest_paths(engine, toC(collection), toC(id), direction, edge === null ? null : toC(edge), via === null ? null : toC(via), weight === null ? null : toC(weight), maxDepth, k)),
781
-
782
- // v0.19.6 parity
783
- dropSimpleIndex: (engine, collection, field) => symbols.hk_engine_drop_simple_index(engine, toC(collection), toC(field)),
784
- dropVectorIndex: (engine, collection, field) => symbols.hk_engine_drop_vector_index(engine, toC(collection), toC(field)),
785
- dropCompositeIndex: (engine, collection, id) => symbols.hk_engine_drop_composite_index(engine, toC(collection), id),
786
- dropEdgeIndex: (engine, collection) => symbols.hk_engine_drop_edge_index(engine, toC(collection)),
787
- dropFtsIndex: (engine, collection, field) => symbols.hk_engine_drop_fts_index(engine, toC(collection), toC(field)),
788
- dropTraverseIndex: (engine, collection) => symbols.hk_engine_drop_traverse_index(engine, toC(collection)),
789
- createVectorIndexV2: (engine, collection, field, dim, metric, m, efConstruction, efSearch) => symbols.hk_engine_create_vector_index_v2(engine, toC(collection), toC(field), dim, metric, m, efConstruction, efSearch),
790
- findPathsFull: (engine, collection, id, direction, edge, via, maxDepth, k, includePath, includeVia, nodeField, nodeJSON, typeField, typeJSON, matchField, matchJSON) => ptrToStringAndFree(symbols.hk_engine_find_paths_v4(engine, toC(collection), toC(id), direction, edge === null ? null : toC(edge), via === null ? null : toC(via), maxDepth, k, includePath ? 1 : 0, includeVia ? 1 : 0, nodeField === null ? null : toC(nodeField), nodeJSON === null ? null : toC(nodeJSON), typeField === null ? null : toC(typeField), typeJSON === null ? null : toC(typeJSON), matchField === null ? null : toC(matchField), matchJSON === null ? null : toC(matchJSON))),
791
- simplePathsFull: (engine, collection, id, direction, edge, via, weight, maxDepth, k, includePath, includeVia, nodeField, nodeJSON, typeField, typeJSON, matchField, matchJSON) => ptrToStringAndFree(symbols.hk_engine_find_simple_paths_v3(engine, toC(collection), toC(id), direction, edge === null ? null : toC(edge), via === null ? null : toC(via), weight === null ? null : toC(weight), maxDepth, k, includePath ? 1 : 0, includeVia ? 1 : 0, nodeField === null ? null : toC(nodeField), nodeJSON === null ? null : toC(nodeJSON), typeField === null ? null : toC(typeField), typeJSON === null ? null : toC(typeJSON), matchField === null ? null : toC(matchField), matchJSON === null ? null : toC(matchJSON))),
792
- exportGraph: (engine, collection, direction, edge, via, nodeField, nodeJSON) => ptrToStringAndFree(symbols.hk_engine_export_graph(engine, toC(collection), direction, edge === null ? null : toC(edge), via === null ? null : toC(via), nodeField === null ? null : toC(nodeField), nodeJSON === null ? null : toC(nodeJSON))),
793
- graphAnalysis: (engine, collection, op, direction, edge, via, nodeField, nodeJSON) => ptrToStringAndFree(symbols.hk_engine_graph_analysis(engine, toC(collection), toC(op), direction, edge === null ? null : toC(edge), via === null ? null : toC(via), nodeField === null ? null : toC(nodeField), nodeJSON === null ? null : toC(nodeJSON))),
794
- insertJSON: (engine, collection, id, json) => symbols.hk_engine_insert_json(engine, toC(collection), toC(id), toC(json)),
795
- batchSetJSON: (batch, collection, id, json) => symbols.hk_batch_set_json(batch, toC(collection), toC(id), toC(json)),
796
-
797
- // Array API
798
- arrayNew: () => symbols.hk_array_new(),
799
- arrayFree: (arr) => symbols.hk_array_free(arr),
800
- arrayAppendStr: (arr, value) => symbols.hk_array_append_str(arr, toC(value)),
801
- arrayAppendInt: (arr, value) => symbols.hk_array_append_int(arr, BigInt(value)),
802
- arrayAppendDoc: (arr, doc) => symbols.hk_array_append_doc(arr, doc),
803
-
804
- // Nested
805
- docInsertDoc: (parent, key, child) => symbols.hk_doc_insert_doc(parent, toC(key), child),
806
- docInsertArray: (parent, key, arr) => symbols.hk_doc_insert_array(parent, toC(key), arr),
807
-
808
- // Query extensions
809
- queryWhereIn: (query, field, arr) => symbols.hk_query_where_in(query, toC(field), arr),
810
- queryWhereNotIn: (query, field, arr) => symbols.hk_query_where_not_in(query, toC(field), arr),
811
- queryWhereArrayContainsAny: (query, field, arr) => symbols.hk_query_where_array_contains_any(query, toC(field), arr),
812
- queryWhereArrayContainsStr: (query, field, value) => symbols.hk_query_where_array_contains(query, toC(field), toC(String(value))),
813
- queryWhereArrayContainsInt: (query, field, value) => symbols.hk_query_where_array_contains(query, toC(field), toC(String(value))),
814
- queryStartAfter: (query, anchorDoc) => symbols.hk_query_start_after(query, anchorDoc),
815
- queryStartAt: (query, anchorDoc) => symbols.hk_query_start_at(query, anchorDoc),
816
- queryEndAt: (query, anchorDoc) => symbols.hk_query_end_at(query, anchorDoc),
817
- queryEndBefore: (query, anchorDoc) => symbols.hk_query_end_before(query, anchorDoc),
818
-
819
- // 0.16.5 parity.
820
- engineSetSchema: (e, c, json) => symbols.hk_engine_set_schema(e, toC(c), toC(json)),
821
- engineGetSchema: (e, c) => ptrToStringAndFree(symbols.hk_engine_get_schema(e, toC(c))),
822
- engineClearSchema: (e, c) => symbols.hk_engine_clear_schema(e, toC(c)),
823
- engineListSchemas: (e) => ptrToStringAndFree(symbols.hk_engine_list_schemas(e)),
824
- engineGetVersioned: (e, c, id) => {
825
- const out = new BigInt64Array(1);
826
- const doc = symbols.hk_engine_get_versioned(e, toC(c), toC(id), ptrOf(out));
827
- return { doc, version: out[0] };
828
- },
829
- engineAwaitQuiescent: (e, ms) => symbols.hk_engine_await_quiescent(e, BigInt(ms)),
830
- engineQuiescenceStatus: (e) => ptrToStringAndFree(symbols.hk_engine_quiescence_status(e)),
831
- transactionGetMany: (e, tx, c, idsJson) => ptrToStringAndFree(symbols.hk_transaction_get_many(e, tx, toC(c), toC(idsJson))),
832
- transactionPatch: (e, tx, c, id, updatesJson) => symbols.hk_transaction_patch(e, tx, toC(c), toC(id), toC(updatesJson)),
833
- errorLastKind: () => symbols.hk_error_last_kind(),
834
- errorIsRetryable: () => symbols.hk_error_is_retryable(),
835
- debugWriteStats: () => ptrToStringAndFree(symbols.hk_debug_write_stats()),
836
- logEnableStderr: () => symbols.hk_log_enable_stderr(),
837
- logDisableStderr: () => symbols.hk_log_disable_stderr(),
838
- queryExplain: (e, q) => ptrToStringAndFree(symbols.hk_query_explain(e, q)),
839
- queryWhereArrayAllEq: (q, f, json) => symbols.hk_query_where_array_all_eq(q, toC(f), toC(json)),
840
- queryWhereElemMatch: (q, f, json) => symbols.hk_query_where_elem_match(q, toC(f), toC(json)),
841
- queryWhereNear: (q, f, vec) => symbols.hk_query_where_near(q, toC(f), vec, vec.length),
842
- queryWhereTraverse: (q, sc, sid, dir, edge, depth) => symbols.hk_query_where_traverse(q, toC(sc), toC(sid), dir, edge === null ? null : toC(edge), depth),
843
- pipelineNew: (c) => symbols.hk_pipeline_new(toC(c)),
844
- pipelineFree: (p) => symbols.hk_pipeline_free(p),
845
- pipelineMatch: (p, q) => symbols.hk_pipeline_match(p, q),
846
- pipelineProject: (p, json) => symbols.hk_pipeline_project(p, toC(json)),
847
- pipelineSort: (p, f, asc) => symbols.hk_pipeline_sort(p, toC(f), asc),
848
- pipelineLimit: (p, n) => symbols.hk_pipeline_limit(p, n),
849
- pipelineOffset: (p, n) => symbols.hk_pipeline_offset(p, n),
850
- pipelineUnwind: (p, f) => symbols.hk_pipeline_unwind(p, toC(f)),
851
- pipelineGroup: (p, k, json) => symbols.hk_pipeline_group(p, toC(k), toC(json)),
852
- pipelineExecute: (e, p) => ptrToStringAndFree(symbols.hk_pipeline_execute(e, p)),
853
- pipelineExecuteToHandles: (e, p) => symbols.hk_pipeline_execute_to_handles(e, p),
854
- configSetGroupCommitIntervalMs: (c, ms) => symbols.hk_config_set_group_commit_interval_ms(c, BigInt(ms)),
855
- configSetPageCacheCapacity: (c, n) => symbols.hk_config_set_page_cache_capacity(c, n),
856
- configSetSyncExcluded: (c, json) => symbols.hk_config_set_sync_excluded(c, toC(json)),
857
- configSetLazyCollections: (c, json) => symbols.hk_config_set_lazy_collections(c, toC(json)),
858
- configSetReplicationCollections: (c, json) => symbols.hk_config_set_replication_collections(c, toC(json)),
859
- cursorWalk: (e, q, callback) => {
860
- const cb = new JSCallback((idPtr: any, idLen: number, bytesPtr: any, bytesLen: number) => {
861
- const id = new TextDecoder().decode(new Uint8Array(toArrayBuffer(idPtr, 0, Number(idLen))).slice());
862
- const bytes = new Uint8Array(toArrayBuffer(bytesPtr, 0, Number(bytesLen))).slice();
863
- return callback(id, bytes);
864
- }, { args: [FFIType.ptr, FFIType.usize, FFIType.ptr, FFIType.usize, FFIType.ptr], returns: FFIType.bool });
865
- return Number(symbols.hk_cursor_walk(e, q, cb, null));
866
- },
867
- cursorWalkView: (e, q, callback) => {
868
- const cb = new JSCallback((idPtr: any, idLen: number, view: any) => {
869
- const id = new TextDecoder().decode(new Uint8Array(toArrayBuffer(idPtr, 0, Number(idLen))).slice());
870
- return callback(id, view);
871
- }, { args: [FFIType.ptr, FFIType.usize, FFIType.ptr, FFIType.ptr], returns: FFIType.bool });
872
- return Number(symbols.hk_cursor_walk_view(e, q, cb, null));
873
- },
874
- rawDocBytes: (raw) => {
875
- const len = new BigInt64Array(1);
876
- const ptr = symbols.hk_rawdoc_bytes(raw, ptrOf(len));
877
- return ptr ? new Uint8Array(toArrayBuffer(ptr, 0, Number(len[0]))).slice() : null;
878
- },
879
- rawDocId: (raw) => {
880
- const len = new BigInt64Array(1);
881
- const ptr = symbols.hk_rawdoc_id(raw, ptrOf(len));
882
- return ptr ? new TextDecoder().decode(new Uint8Array(toArrayBuffer(ptr, 0, Number(len[0]))).slice()) : null;
883
- },
884
- viewGetStr: (view, key) => {
885
- const len = new BigInt64Array(1);
886
- const ptr = symbols.hk_view_get_str(view, toC(key), ptrOf(len));
887
- return ptr ? new TextDecoder().decode(new Uint8Array(toArrayBuffer(ptr, 0, Number(len[0]))).slice()) : null;
888
- },
889
- viewGetBytes: (view, key) => {
890
- const len = new BigInt64Array(1);
891
- const ptr = symbols.hk_view_get_bytes(view, toC(key), ptrOf(len));
892
- return ptr ? new Uint8Array(toArrayBuffer(ptr, 0, Number(len[0]))).slice() : null;
893
- },
894
-
895
- // P2 (core 0.18): JSON read paths + dotted-path pulls.
896
- engineGetJson: (e, c, id) => ptrToStringAndFree(symbols.hk_engine_get_json(e, toC(c), toC(id))),
897
- queryExecuteJson: (e, q) => ptrToStringAndFree(symbols.hk_query_execute_json(e, q)),
898
- viewToJson: (view) => ptrToStringAndFree(symbols.hk_view_to_json(view)),
899
- cursorWalkJson: (e, q, callback) => {
900
- const cb = new JSCallback((idPtr: any, idLen: number, jsonPtr: any, jsonLen: number) => {
901
- const id = new TextDecoder().decode(new Uint8Array(toArrayBuffer(idPtr, 0, Number(idLen))).slice());
902
- const json = new Uint8Array(toArrayBuffer(jsonPtr, 0, Number(jsonLen))).slice();
903
- return callback(id, json);
904
- }, { args: [FFIType.ptr, FFIType.usize, FFIType.ptr, FFIType.usize, FFIType.ptr], returns: FFIType.bool });
905
- return Number(symbols.hk_cursor_walk_json(e, q, cb, null));
906
- },
907
- viewGetPathInt: (view, path) => {
908
- const out = new BigInt64Array(1);
909
- return symbols.hk_view_get_path_int(view, toC(path), ptrOf(out)) ? out[0] : null;
910
- },
911
- viewGetPathFloat: (view, path) => {
912
- const out = new Float64Array(1);
913
- return symbols.hk_view_get_path_float(view, toC(path), ptrOf(out)) ? out[0] : null;
914
- },
915
- viewGetPathBool: (view, path) => {
916
- const r = symbols.hk_view_get_path_bool(view, toC(path));
917
- return r < 0 ? null : r !== 0;
918
- },
919
- viewGetPathStr: (view, path) => {
920
- const len = new BigInt64Array(1);
921
- const ptr = symbols.hk_view_get_path_str(view, toC(path), ptrOf(len));
922
- return ptr ? new TextDecoder().decode(new Uint8Array(toArrayBuffer(ptr, 0, Number(len[0]))).slice()) : null;
923
- },
924
- viewGetPathBytes: (view, path) => {
925
- const len = new BigInt64Array(1);
926
- const ptr = symbols.hk_view_get_path_bytes(view, toC(path), ptrOf(len));
927
- return ptr ? new Uint8Array(toArrayBuffer(ptr, 0, Number(len[0]))).slice() : null;
928
- },
929
-
930
- lastError: () => {
931
- const ptr = symbols.hk_last_error();
932
- return ptr ? new CString(ptr).toString() : 'unknown ffi error';
933
- }
934
- };
935
- }
936
-
937
- async function createNodeBindings(libPath: string): Promise<NativeBindings> {
938
- const koffiModule = await import('koffi');
939
- const koffi: any = (koffiModule as any).default ?? koffiModule;
940
- const lib = koffi.load(libPath);
941
-
942
- // ponytail: koffi needs opaque struct declarations up front — without
943
- // these EVERY struct-typed decl throws at bindings creation, i.e. the
944
- // node backend was dead on arrival (bun resolves pointers structurally
945
- // and never noticed). Declared once here for all present and future fns.
946
- for (const t of ['HK_Array', 'HK_Batch', 'HK_CloudSync', 'HK_Config', 'HK_Doc', 'HK_Engine', 'HK_NetSyncer', 'HK_Pipeline', 'HK_Query', 'HK_RawDoc', 'HK_RawResultSet', 'HK_ResultSet', 'HK_Transaction', 'HK_ViewDoc', 'HK_Watch']) koffi.opaque(t);
947
-
948
- // ponytail: owned C strings (Rust-allocated) must come back through a
949
- // DISPOSABLE type wired to hk_string_free — never bare char* (koffi
950
- // auto-decodes to a JS string and the native buffer leaks) and never
951
- // default koffi.free (wrong allocator: Rust memory freed by C free is
952
- // UB). Declared before the table; the closure runs post-init.
953
- const flStringFreeRaw = lib.func('void hk_string_free(void* value)');
954
- const HeapStr = koffi.disposable('HeapStr', 'str', (ptr: any) => { flStringFreeRaw(ptr); });
955
-
956
- // Canonical callback signature (matches HK_OnSnapshotCallback in
957
- // hako.h). Kept as documentation; register() below takes the
958
- // typedef NAME as a string, not this proto object.
959
- const OnSnapshotCB = koffi.proto('void HK_OnSnapshotCallback(const char *collection, const char *path, int32_t kind, void *user_data)');
960
- // ids are length-delimited (not NUL-terminated): take them as raw bytes.
961
- const WalkCB = koffi.proto('bool HkWalkCallback(const uint8_t *id, size_t id_len, const uint8_t *bytes, size_t bytes_len, void *userdata)');
962
- const ViewWalkCB = koffi.proto('bool HkViewWalkCallback(const uint8_t *id, size_t id_len, const HK_ViewDoc *view, void *userdata)');
963
-
964
- const fn = {
965
- hk_engine_open: lib.func('HK_Engine* hk_engine_open(const char* path)'),
966
- hk_engine_is_indexes_ready: lib.func('bool hk_engine_is_indexes_ready(HK_Engine* engine)'),
967
- hk_engine_open_with_config: lib.func('HK_Engine* hk_engine_open_with_config(const char* path, HK_Config* config)'),
968
- hk_engine_free: lib.func('void hk_engine_free(HK_Engine* engine)'),
969
- hk_engine_backup: lib.func('int hk_engine_backup(HK_Engine* engine, const char* path)'),
970
- hk_engine_compact: lib.func('int hk_engine_compact(HK_Engine* engine)'),
971
- // ponytail: owned-string returns are HeapStr (auto-freed via
972
- // hk_string_free by the disposable — never bare char*, which leaks,
973
- // and never default koffi.free (wrong allocator for Rust memory).
974
- // hk_last_error stays char* (TLS, never freed).
975
- hk_engine_get_stats: lib.func('HeapStr hk_engine_get_stats(HK_Engine* engine)'),
976
- hk_engine_get_audit_log: lib.func('HeapStr hk_engine_get_audit_log(HK_Engine* engine)'),
977
- hk_engine_snapshot_indices: lib.func('int hk_engine_snapshot_indices(HK_Engine* engine)'),
978
- hk_engine_list_indexes: lib.func('HeapStr hk_engine_list_indexes(HK_Engine* engine, const char* collection)'),
979
-
980
- hk_config_new: lib.func('HK_Config* hk_config_new()'),
981
- hk_config_free: lib.func('void hk_config_free(HK_Config* config)'),
982
- hk_config_set_durability: lib.func('void hk_config_set_durability(HK_Config* config, int32_t mode)'),
983
- hk_config_set_encryption_key: lib.func('void hk_config_set_encryption_key(HK_Config* config, const char* key)'),
984
- hk_config_set_encrypted_collections: lib.func('int hk_config_set_encrypted_collections(HK_Config* config, const char* collections_json)'),
985
- hk_config_set_audit_log: lib.func('void hk_config_set_audit_log(HK_Config* config, bool enabled, const char* path)'),
986
- hk_config_set_query_workers: lib.func('void hk_config_set_query_workers(HK_Config* config, size_t count)'),
987
- hk_config_set_memory_limits: lib.func('void hk_config_set_memory_limits(HK_Config* config, size_t mmap_size, size_t max_inlined_bytes)'),
988
- hk_config_set_storage_tuning: lib.func('void hk_config_set_storage_tuning(HK_Config* config, size_t page_size, size_t compaction_threshold, size_t group_commit_max_ops)'),
989
- hk_config_set_blob_threshold: lib.func('void hk_config_set_blob_threshold(HK_Config* config, size_t threshold_bytes)'),
990
- hk_config_set_compression: lib.func('void hk_config_set_compression(HK_Config* config, bool enabled, int32_t level)'),
991
- hk_config_set_background_maintenance: lib.func('void hk_config_set_background_maintenance(HK_Config* config, bool enabled)'),
992
-
993
- hk_engine_watch: lib.func('HK_Watch* hk_engine_watch(HK_Engine* engine, const char* collection, HK_OnSnapshotCallback* callback, void* user_data)'),
994
- hk_watch_free: lib.func('void hk_watch_free(HK_Watch* watch)'),
995
-
996
- hk_doc_new: lib.func('HK_Doc* hk_doc_new()'),
997
- hk_doc_free: lib.func('void hk_doc_free(HK_Doc* doc)'),
998
- hk_doc_insert_str: lib.func('int hk_doc_insert_str(HK_Doc* doc, const char* key, const char* value)'),
999
- hk_doc_insert_int: lib.func('int hk_doc_insert_int(HK_Doc* doc, const char* key, int64_t value)'),
1000
- hk_doc_insert_float: lib.func('int hk_doc_insert_float(HK_Doc* doc, const char* key, double value)'),
1001
- hk_doc_insert_bool: lib.func('int hk_doc_insert_bool(HK_Doc* doc, const char* key, bool value)'),
1002
- hk_doc_insert_null: lib.func('int hk_doc_insert_null(HK_Doc* doc, const char* key)'),
1003
- hk_doc_insert_bin: lib.func('int hk_doc_insert_bin(HK_Doc* doc, const char* key, const uint8_t* data, size_t len)'),
1004
- hk_doc_insert_timestamp: lib.func('int hk_doc_insert_timestamp(HK_Doc* doc, const char* key, int64_t micros)'),
1005
- hk_doc_insert_server_timestamp: lib.func('int hk_doc_insert_server_timestamp(HK_Doc* doc, const char* key)'),
1006
- hk_doc_to_json: lib.func('HeapStr hk_doc_to_json(const HK_Doc* doc)'),
1007
-
1008
- hk_engine_insert: lib.func('int hk_engine_insert(HK_Engine* engine, const char* collection, const char* doc_id, const HK_Doc* doc)'),
1009
- hk_engine_get: lib.func('HK_Doc* hk_engine_get(HK_Engine* engine, const char* collection, const char* doc_id)'),
1010
- hk_engine_delete: lib.func('int hk_engine_delete(HK_Engine* engine, const char* collection, const char* doc_id)'),
1011
- hk_engine_delete_local: lib.func('int hk_engine_delete_local(HK_Engine* engine, const char* collection, const char* doc_id)'),
1012
- hk_engine_delete_ext: lib.func('int hk_engine_delete_ext(HK_Engine* engine, const char* collection, const char* doc_id, int32_t mode)'),
1013
- hk_engine_restore: lib.func('int hk_engine_restore(HK_Engine* engine, const char* collection, const char* doc_id)'),
1014
- hk_engine_restore_available: lib.func('HeapStr hk_engine_restore_available(HK_Engine* engine, const char* collection)'),
1015
- hk_engine_delete_queue: lib.func('HeapStr hk_engine_delete_queue(HK_Engine* engine, const char* collection)'),
1016
- hk_engine_sweep_now: lib.func('int hk_engine_sweep_now(HK_Engine* engine)'),
1017
- hk_engine_set_collection_local: lib.func('int hk_engine_set_collection_local(HK_Engine* engine, const char* collection, int local)'),
1018
- hk_engine_replicate_key: lib.func('int hk_engine_replicate_key(HK_Engine* engine, const char* collection, const char* doc_id)'),
1019
- hk_engine_replicate_collection: lib.func('int hk_engine_replicate_collection(HK_Engine* engine, const char* collection)'),
1020
- hk_engine_vacuum_collection: lib.func('int hk_engine_vacuum_collection(HK_Engine* engine, const char* collection)'),
1021
- hk_engine_relocate_docs: lib.func('HeapStr hk_engine_relocate_docs(HK_Engine* engine, const char* src, const char* dst, const char* ids_json)'),
1022
- hk_engine_load_collection: lib.func('int hk_engine_load_collection(HK_Engine* engine, const char* collection)'),
1023
- hk_engine_unload_collection: lib.func('int hk_engine_unload_collection(HK_Engine* engine, const char* collection)'),
1024
- hk_engine_unloaded_collections: lib.func('HeapStr hk_engine_unloaded_collections(HK_Engine* engine)'),
1025
- hk_engine_patch: lib.func('int hk_engine_patch(HK_Engine* engine, const char* collection, const char* doc_id, const HK_Doc* updates)'),
1026
- hk_engine_insert_subdoc: lib.func('int hk_engine_insert_subdoc(HK_Engine* engine, const char* col, const char* id, const char* sub_col, const char* sub_id, const HK_Doc* doc)'),
1027
- hk_engine_get_by_ref: lib.func('HK_Doc* hk_engine_get_by_ref(HK_Engine* engine, const HK_Doc* doc, const char* field_key)'),
1028
- hk_engine_create_index: lib.func('uint32_t hk_engine_create_index(HK_Engine* engine, const char* collection, const char* fields_json)'),
1029
- hk_engine_create_vector_index: lib.func('int hk_engine_create_vector_index(HK_Engine* engine, const char* collection, const char* field, uint32_t dim, uint32_t metric)'),
1030
- hk_engine_find_near: lib.func('HeapStr hk_engine_find_near(HK_Engine* engine, const char* collection, const char* field, const float* vec, size_t len, size_t k)'),
1031
- hk_engine_create_traverse_index: lib.func('int hk_engine_create_traverse_index(HK_Engine* engine, const char* collection)'),
1032
- hk_engine_find_paths: lib.func('HeapStr hk_engine_find_paths(HK_Engine* engine, const char* collection, const char* doc_id, uint32_t direction, const char* edge, size_t max_depth, size_t k)'),
1033
- hk_engine_create_edge_index: lib.func('int hk_engine_create_edge_index(HK_Engine* engine, const char* collection)'),
1034
- hk_engine_shortest_paths: lib.func('HeapStr hk_engine_shortest_paths(HK_Engine* engine, const char* collection, const char* doc_id, uint32_t direction, const char* edge, const char* via, const char* weight, size_t max_depth, size_t k)'),
1035
-
1036
- // v0.19.6 parity
1037
- hk_engine_drop_simple_index: lib.func('int hk_engine_drop_simple_index(HK_Engine* engine, const char* collection, const char* field)'),
1038
- hk_engine_drop_vector_index: lib.func('int hk_engine_drop_vector_index(HK_Engine* engine, const char* collection, const char* field)'),
1039
- hk_engine_drop_composite_index: lib.func('int hk_engine_drop_composite_index(HK_Engine* engine, const char* collection, uint32_t id)'),
1040
- hk_engine_drop_edge_index: lib.func('int hk_engine_drop_edge_index(HK_Engine* engine, const char* collection)'),
1041
- hk_engine_drop_fts_index: lib.func('int hk_engine_drop_fts_index(HK_Engine* engine, const char* collection, const char* field)'),
1042
- hk_engine_drop_traverse_index: lib.func('int hk_engine_drop_traverse_index(HK_Engine* engine, const char* collection)'),
1043
- hk_engine_create_vector_index_v2: lib.func('int hk_engine_create_vector_index_v2(HK_Engine* engine, const char* collection, const char* field, uint32_t dim, uint32_t metric, size_t m, size_t ef_construction, size_t ef_search)'),
1044
- hk_engine_find_paths_v4: lib.func('HeapStr hk_engine_find_paths_v4(HK_Engine* engine, const char* collection, const char* doc_id, uint32_t direction, const char* edge, const char* via, size_t max_depth, size_t k, uint32_t include_path, uint32_t include_via, const char* filter_field, const char* filter_json, const char* type_field, const char* type_json, const char* match_field, const char* match_json)'),
1045
- hk_engine_find_simple_paths_v3: lib.func('HeapStr hk_engine_find_simple_paths_v3(HK_Engine* engine, const char* collection, const char* doc_id, uint32_t direction, const char* edge, const char* via, const char* weight, size_t max_depth, size_t k, uint32_t include_path, uint32_t include_via, const char* filter_field, const char* filter_json, const char* type_field, const char* type_json, const char* match_field, const char* match_json)'),
1046
- hk_engine_export_graph: lib.func('HeapStr hk_engine_export_graph(HK_Engine* engine, const char* collection, uint32_t direction, const char* edge, const char* via, const char* filter_field, const char* filter_json)'),
1047
- hk_engine_graph_analysis: lib.func('HeapStr hk_engine_graph_analysis(HK_Engine* engine, const char* collection, const char* op, uint32_t direction, const char* edge, const char* via, const char* filter_field, const char* filter_json)'),
1048
- hk_engine_insert_json: lib.func('int hk_engine_insert_json(HK_Engine* engine, const char* collection, const char* doc_id, const char* json)'),
1049
- hk_batch_set_json: lib.func('int hk_batch_set_json(HK_Batch* batch, const char* collection, const char* doc_id, const char* json)'),
1050
-
1051
- hk_batch_new: lib.func('HK_Batch* hk_batch_new()'),
1052
- hk_batch_free: lib.func('void hk_batch_free(HK_Batch* batch)'),
1053
- hk_batch_set: lib.func('int hk_batch_set(HK_Batch* batch, const char* collection, const char* doc_id, const HK_Doc* doc)'),
1054
- hk_batch_delete: lib.func('int hk_batch_delete(HK_Batch* batch, const char* collection, const char* doc_id)'),
1055
- hk_batch_delete_ext: lib.func('int hk_batch_delete_ext(HK_Batch* batch, const char* collection, const char* doc_id, int32_t mode)'),
1056
- hk_batch_commit: lib.func('int hk_batch_commit(HK_Engine* engine, HK_Batch* batch)'),
1057
-
1058
- hk_transaction_begin: lib.func('HK_Transaction* hk_transaction_begin(HK_Engine* engine)'),
1059
- hk_transaction_get: lib.func('HK_Doc* hk_transaction_get(HK_Engine* engine, HK_Transaction* tx, const char* collection, const char* doc_id)'),
1060
- hk_transaction_set: lib.func('int hk_transaction_set(HK_Transaction* tx, const char* collection, const char* doc_id, const HK_Doc* doc)'),
1061
- hk_transaction_commit: lib.func('int hk_transaction_commit(HK_Engine* engine, HK_Transaction* tx)'),
1062
- hk_transaction_free: lib.func('void hk_transaction_free(HK_Transaction* tx)'),
1063
-
1064
- hk_query_new: lib.func('HK_Query* hk_query_new(const char* collection)'),
1065
- hk_query_free: lib.func('void hk_query_free(HK_Query* query)'),
1066
- hk_query_where_eq_str: lib.func('int hk_query_where_eq_str(HK_Query* query, const char* field, const char* value)'),
1067
- hk_query_where_eq_bool: lib.func('int hk_query_where_eq_bool(HK_Query* query, const char* field, bool value)'),
1068
- hk_query_where_eq_int: lib.func('int hk_query_where_eq_int(HK_Query* query, const char* field, int64_t value)'),
1069
- hk_query_where_ne_str: lib.func('int hk_query_where_ne_str(HK_Query* query, const char* field, const char* value)'),
1070
- hk_query_where_ne_int: lib.func('int hk_query_where_ne_int(HK_Query* query, const char* field, int64_t value)'),
1071
- hk_query_where_gt_str: lib.func('int hk_query_where_gt_str(HK_Query* query, const char* field, const char* value)'),
1072
- hk_query_where_gt_int: lib.func('int hk_query_where_gt_int(HK_Query* query, const char* field, int64_t value)'),
1073
- hk_query_where_gte_str: lib.func('int hk_query_where_gte_str(HK_Query* query, const char* field, const char* value)'),
1074
- hk_query_where_gte_int: lib.func('int hk_query_where_gte_int(HK_Query* query, const char* field, int64_t value)'),
1075
- hk_query_where_lt_str: lib.func('int hk_query_where_lt_str(HK_Query* query, const char* field, const char* value)'),
1076
- hk_query_where_lt_int: lib.func('int hk_query_where_lt_int(HK_Query* query, const char* field, int64_t value)'),
1077
- hk_query_where_lte_str: lib.func('int hk_query_where_lte_str(HK_Query* query, const char* field, const char* value)'),
1078
- hk_query_where_lte_int: lib.func('int hk_query_where_lte_int(HK_Query* query, const char* field, int64_t value)'),
1079
- hk_query_where_match: lib.func('int hk_query_where_match(HK_Query* query, const char* field, const char* value)'),
1080
- hk_query_where_match_prefix: lib.func('int hk_query_where_match_prefix(HK_Query* query, const char* field, const char* value)'),
1081
- hk_query_where_contains: lib.func('int hk_query_where_contains(HK_Query* query, const char* field, const char* value)'),
1082
- hk_query_where_starts_with: lib.func('int hk_query_where_starts_with(HK_Query* query, const char* field, const char* value)'),
1083
- hk_query_order_by: lib.func('int hk_query_order_by(HK_Query* query, const char* field, bool ascending)'),
1084
- hk_query_limit: lib.func('int hk_query_limit(HK_Query* query, size_t limit)'),
1085
- hk_query_offset: lib.func('int hk_query_offset(HK_Query* query, size_t offset)'),
1086
- hk_query_select_field: lib.func('int hk_query_select_field(HK_Query* query, const char* field)'),
1087
- hk_query_execute: lib.func('HeapStr hk_query_execute(HK_Engine* engine, const HK_Query* query)'),
1088
- hk_query_delete: lib.func('int hk_query_delete(HK_Engine* engine, HK_Query* query)'),
1089
- hk_query_delete_local: lib.func('int hk_query_delete_local(HK_Engine* engine, HK_Query* query)'),
1090
- hk_query_patch: lib.func('int hk_query_patch(HK_Engine* engine, HK_Query* query, const HK_Doc* patch_doc)'),
1091
- hk_query_execute_to_handles: lib.func('HK_ResultSet* hk_query_execute_to_handles(HK_Engine* engine, const HK_Query* query)'),
1092
- hk_result_set_count: lib.func('size_t hk_result_set_count(HK_ResultSet* results)'),
1093
- hk_result_set_get_doc: lib.func('HK_Doc* hk_result_set_get_doc(HK_ResultSet* results, size_t index)'),
1094
- hk_result_set_free: lib.func('void hk_result_set_free(HK_ResultSet* results)'),
1095
- hk_result_set_to_json: lib.func('HeapStr hk_result_set_to_json(HK_ResultSet* results)'),
1096
- hk_query_execute_raw: lib.func('HK_RawResultSet* hk_query_execute_raw(HK_Engine* engine, const HK_Query* query)'),
1097
- hk_rawresult_count: lib.func('size_t hk_rawresult_count(HK_RawResultSet* results)'),
1098
- hk_rawresult_get: lib.func('HK_RawDoc* hk_rawresult_get(HK_RawResultSet* results, size_t index)'),
1099
- hk_rawresult_free: lib.func('void hk_rawresult_free(HK_RawResultSet* results)'),
1100
- hk_query_start_after_raw: lib.func('int hk_query_start_after_raw(HK_Query* query, const HK_RawDoc* anchor_doc)'),
1101
- hk_rawdoc_to_doc: lib.func('HK_Doc* hk_rawdoc_to_doc(HK_Engine* engine, const HK_RawDoc* raw_doc, const char* collection)'),
1102
- hk_view_get: lib.func('HK_ViewDoc* hk_view_get(HK_Engine* engine, const char* collection, const char* doc_id)'),
1103
- hk_view_free: lib.func('void hk_view_free(HK_ViewDoc* view)'),
1104
- hk_view_field_count: lib.func('size_t hk_view_field_count(const HK_ViewDoc* view)'),
1105
- hk_view_has_field: lib.func('bool hk_view_has_field(const HK_ViewDoc* view, const char* key)'),
1106
- hk_view_get_int: lib.func('bool hk_view_get_int(const HK_ViewDoc* view, const char* key, _Out_ int64_t *out)'),
1107
- hk_view_get_float: lib.func('bool hk_view_get_float(const HK_ViewDoc* view, const char* key, _Out_ double *out)'),
1108
- hk_view_get_bool: lib.func('int hk_view_get_bool(const HK_ViewDoc* view, const char* key)'),
1109
- hk_view_to_doc: lib.func('HK_Doc* hk_view_to_doc(const HK_ViewDoc* view, const char* doc_id)'),
1110
- hk_query_defer_blobs: lib.func('int hk_query_defer_blobs(HK_Query* query, int defer)'),
1111
- hk_doc_resolve_blobs: lib.func('int hk_doc_resolve_blobs(HK_Engine* engine, const char* collection, HK_Doc* doc)'),
1112
- hk_engine_insert_take: lib.func('int hk_engine_insert_take(HK_Engine* engine, const char* collection, const char* doc_id, HK_Doc* doc)'),
1113
- hk_config_set_wal_reserve_bytes: lib.func('void hk_config_set_wal_reserve_bytes(HK_Config* config, uint64_t bytes)'),
1114
-
1115
- hk_query_aggregate_count: lib.func('int hk_query_aggregate_count(HK_Query* query)'),
1116
- hk_query_aggregate_sum: lib.func('int hk_query_aggregate_sum(HK_Query* query, const char* field)'),
1117
- hk_query_aggregate_avg: lib.func('int hk_query_aggregate_avg(HK_Query* query, const char* field)'),
1118
- hk_query_aggregate_min: lib.func('int hk_query_aggregate_min(HK_Query* query, const char* field)'),
1119
- hk_query_aggregate_max: lib.func('int hk_query_aggregate_max(HK_Query* query, const char* field)'),
1120
- hk_query_execute_aggregation: lib.func('HeapStr hk_query_execute_aggregation(HK_Engine* engine, const HK_Query* query)'),
1121
- hk_query_where_or_str: lib.func('int hk_query_where_or_str(HK_Query* query, const char* field, const char* value)'),
1122
- hk_query_where_or_int: lib.func('int hk_query_where_or_int(HK_Query* query, const char* field, int64_t value)'),
1123
-
1124
- hk_engine_list_collections: lib.func('HeapStr hk_engine_list_collections(HK_Engine* engine)'),
1125
- hk_net_syncer_new: lib.func('HK_NetSyncer* hk_net_syncer_new(HK_Engine* engine, const char* name, const char* room_key)'),
1126
- hk_net_syncer_start: lib.func('int hk_net_syncer_start(HK_NetSyncer* syncer, uint16_t port)'),
1127
- hk_net_syncer_set_discovery: lib.func('int hk_net_syncer_set_discovery(HK_NetSyncer* syncer, int mode)'),
1128
- hk_net_syncer_status: lib.func('HeapStr hk_net_syncer_status(HK_NetSyncer* syncer)'),
1129
- hk_net_syncer_free: lib.func('void hk_net_syncer_free(HK_NetSyncer* syncer)'),
1130
-
1131
- // v0.5.9
1132
- // Indexing
1133
- hk_engine_create_fts_index: lib.func('int hk_engine_create_fts_index(HK_Engine* engine, const char* collection, const char* field)'),
1134
- hk_engine_create_simple_index: lib.func('int hk_engine_create_simple_index(HK_Engine* engine, const char* collection, const char* field)'),
1135
-
1136
- // Array API
1137
- hk_array_new: lib.func('HK_Array* hk_array_new()'),
1138
- hk_array_free: lib.func('void hk_array_free(HK_Array* arr)'),
1139
- hk_array_append_str: lib.func('int hk_array_append_str(HK_Array* arr, const char* value)'),
1140
- hk_array_append_int: lib.func('int hk_array_append_int(HK_Array* arr, int64_t value)'),
1141
- hk_array_append_doc: lib.func('int hk_array_append_doc(HK_Array* arr, const HK_Doc* doc)'),
1142
-
1143
- // Nested
1144
- hk_doc_insert_doc: lib.func('int hk_doc_insert_doc(HK_Doc* parent, const char* key, HK_Doc* child)'),
1145
- hk_doc_insert_array: lib.func('int hk_doc_insert_array(HK_Doc* parent, const char* key, HK_Array* arr)'),
1146
-
1147
- // Query extensions
1148
- hk_query_where_in: lib.func('int hk_query_where_in(HK_Query* query, const char* field, HK_Array* arr)'),
1149
- hk_query_where_not_in: lib.func('int hk_query_where_not_in(HK_Query* query, const char* field, HK_Array* arr)'),
1150
- hk_query_where_array_contains_any: lib.func('int hk_query_where_array_contains_any(HK_Query* query, const char* field, HK_Array* arr)'),
1151
- hk_query_where_array_contains: lib.func('int hk_query_where_array_contains(HK_Query* query, const char* field, const char* value)'),
1152
- hk_query_start_after: lib.func('int hk_query_start_after(HK_Query* query, HK_Doc* anchor)'),
1153
- hk_query_start_at: lib.func('int hk_query_start_at(HK_Query* query, const HK_Doc* anchor)'),
1154
- hk_query_end_at: lib.func('int hk_query_end_at(HK_Query* query, const HK_Doc* anchor)'),
1155
- hk_query_end_before: lib.func('int hk_query_end_before(HK_Query* query, const HK_Doc* anchor)'),
1156
-
1157
- hk_doc_insert_reference: lib.func('int hk_doc_insert_reference(HK_Doc* doc, const char* key, const char* target_collection, const char* target_id)'),
1158
-
1159
- hk_cloud_sync_new: lib.func('HK_CloudSync* hk_cloud_sync_new(HK_Engine* engine, int32_t mode, const char* client_id, const char* room_name, const char* room_key, const char* auth_token)'),
1160
- hk_cloud_sync_server_new: lib.func('HK_CloudSync* hk_cloud_sync_server_new(HK_Engine* engine, const char* server_id, const char* auth_token, const char* room_name, const char* room_key)'),
1161
- hk_cloud_sync_client_new: lib.func('HK_CloudSync* hk_cloud_sync_client_new(HK_Engine* engine, const char* client_id, const char* room_name, const char* room_key, const char* auth_token)'),
1162
- hk_cloud_sync_start: lib.func('int hk_cloud_sync_start(HK_CloudSync* cloud_sync, const char* address)'),
1163
- hk_cloud_sync_status: lib.func('HeapStr hk_cloud_sync_status(HK_CloudSync* cloud_sync)'),
1164
- hk_cloud_sync_stop: lib.func('void hk_cloud_sync_stop(HK_CloudSync* cloud_sync)'),
1165
- hk_cloud_sync_free: lib.func('void hk_cloud_sync_free(HK_CloudSync* cloud_sync)'),
1166
-
1167
- // 0.16.5 parity: F8 schemas, F5 versioned/tx, F7 explain, F3 predicates,
1168
- // F4 pipelines, config knobs, quiescence, lazy walks and borrowed reads.
1169
- hk_config_set_group_commit_interval_ms: lib.func('void hk_config_set_group_commit_interval_ms(HK_Config* config, uint64_t ms)'),
1170
- hk_config_set_page_cache_capacity: lib.func('void hk_config_set_page_cache_capacity(HK_Config* config, size_t capacity)'),
1171
- hk_config_set_sync_excluded: lib.func('int hk_config_set_sync_excluded(HK_Config* config, const char* collections_json)'),
1172
- hk_config_set_lazy_collections: lib.func('int hk_config_set_lazy_collections(HK_Config* config, const char* collections_json)'),
1173
- hk_config_set_replication_collections: lib.func('int hk_config_set_replication_collections(HK_Config* config, const char* collections_json)'),
1174
- hk_engine_await_quiescent: lib.func('bool hk_engine_await_quiescent(HK_Engine* engine, uint64_t timeout_ms)'),
1175
- hk_engine_quiescence_status: lib.func('HeapStr hk_engine_quiescence_status(HK_Engine* engine)'),
1176
- hk_engine_get_versioned: lib.func('HK_Doc* hk_engine_get_versioned(HK_Engine* engine, const char* collection, const char* doc_id, _Out_ uint64_t *out_version)'),
1177
- hk_engine_set_schema: lib.func('int hk_engine_set_schema(HK_Engine* engine, const char* collection, const char* schema_json)'),
1178
- hk_engine_get_schema: lib.func('HeapStr hk_engine_get_schema(HK_Engine* engine, const char* collection)'),
1179
- hk_engine_clear_schema: lib.func('int hk_engine_clear_schema(HK_Engine* engine, const char* collection)'),
1180
- hk_engine_list_schemas: lib.func('HeapStr hk_engine_list_schemas(HK_Engine* engine)'),
1181
- hk_query_explain: lib.func('HeapStr hk_query_explain(HK_Engine* engine, const HK_Query* query)'),
1182
- hk_query_where_array_all_eq: lib.func('int hk_query_where_array_all_eq(HK_Query* query, const char* field, const char* value_json)'),
1183
- hk_query_where_elem_match: lib.func('int hk_query_where_elem_match(HK_Query* query, const char* field, const char* filters_json)'),
1184
- hk_query_where_near: lib.func('int hk_query_where_near(HK_Query* query, const char* field, const float* vec, size_t len)'),
1185
- hk_query_where_traverse: lib.func('int hk_query_where_traverse(HK_Query* query, const char* start_col, const char* start_id, uint32_t direction, const char* edge_field, size_t max_depth)'),
1186
- hk_pipeline_new: lib.func('HK_Pipeline* hk_pipeline_new(const char* collection)'),
1187
- hk_pipeline_free: lib.func('void hk_pipeline_free(HK_Pipeline* pipeline)'),
1188
- hk_pipeline_match: lib.func('int hk_pipeline_match(HK_Pipeline* pipeline, const HK_Query* query)'),
1189
- hk_pipeline_project: lib.func('int hk_pipeline_project(HK_Pipeline* pipeline, const char* fields_json)'),
1190
- hk_pipeline_sort: lib.func('int hk_pipeline_sort(HK_Pipeline* pipeline, const char* field, bool ascending)'),
1191
- hk_pipeline_limit: lib.func('int hk_pipeline_limit(HK_Pipeline* pipeline, size_t limit)'),
1192
- hk_pipeline_offset: lib.func('int hk_pipeline_offset(HK_Pipeline* pipeline, size_t offset)'),
1193
- hk_pipeline_unwind: lib.func('int hk_pipeline_unwind(HK_Pipeline* pipeline, const char* field)'),
1194
- hk_pipeline_group: lib.func('int hk_pipeline_group(HK_Pipeline* pipeline, const char* key, const char* accums_json)'),
1195
- hk_pipeline_execute: lib.func('HeapStr hk_pipeline_execute(HK_Engine* engine, const HK_Pipeline* pipeline)'),
1196
- hk_pipeline_execute_to_handles: lib.func('HK_ResultSet* hk_pipeline_execute_to_handles(HK_Engine* engine, const HK_Pipeline* pipeline)'),
1197
- hk_transaction_get_many: lib.func('HeapStr hk_transaction_get_many(HK_Engine* engine, HK_Transaction* tx, const char* collection, const char* ids_json)'),
1198
- hk_transaction_patch: lib.func('int hk_transaction_patch(HK_Engine* engine, HK_Transaction* tx, const char* collection, const char* doc_id, const char* updates_json)'),
1199
- hk_error_last_kind: lib.func('int hk_error_last_kind()'),
1200
- hk_error_is_retryable: lib.func('bool hk_error_is_retryable()'),
1201
- hk_debug_write_stats: lib.func('HeapStr hk_debug_write_stats()'),
1202
- hk_log_enable_stderr: lib.func('void hk_log_enable_stderr()'),
1203
- hk_log_disable_stderr: lib.func('void hk_log_disable_stderr()'),
1204
- hk_cursor_walk: lib.func('int64_t hk_cursor_walk(HK_Engine* engine, const HK_Query* query, HkWalkCallback* callback, void* userdata)'),
1205
- hk_cursor_walk_view: lib.func('int64_t hk_cursor_walk_view(HK_Engine* engine, const HK_Query* query, HkViewWalkCallback* callback, void* userdata)'),
1206
- hk_rawdoc_bytes: lib.func('const uint8_t* hk_rawdoc_bytes(const HK_RawDoc* doc, _Out_ uintptr_t *len_out)'),
1207
- // Return as uint8_t* so koffi hands back a pointer; the id is
1208
- // length-delimited, not NUL-terminated.
1209
- hk_rawdoc_id: lib.func('const uint8_t* hk_rawdoc_id(const HK_RawDoc* doc, _Out_ uintptr_t *len_out)'),
1210
- hk_view_get_str: lib.func('const uint8_t* hk_view_get_str(const HK_ViewDoc* view, const char* key, _Out_ uintptr_t *len_out)'),
1211
- hk_view_get_bytes: lib.func('const uint8_t* hk_view_get_bytes(const HK_ViewDoc* view, const char* key, _Out_ uintptr_t *len_out)'),
1212
- hk_engine_get_json: lib.func('HeapStr hk_engine_get_json(HK_Engine* engine, const char* collection, const char* doc_id)'),
1213
- hk_query_execute_json: lib.func('HeapStr hk_query_execute_json(HK_Engine* engine, const HK_Query* query)'),
1214
- hk_cursor_walk_json: lib.func('int64_t hk_cursor_walk_json(HK_Engine* engine, const HK_Query* query, HkWalkCallback* callback, void* userdata)'),
1215
- hk_view_to_json: lib.func('HeapStr hk_view_to_json(const HK_ViewDoc* view)'),
1216
- hk_view_get_path_int: lib.func('bool hk_view_get_path_int(const HK_ViewDoc* view, const char* path, _Out_ int64_t *out)'),
1217
- hk_view_get_path_float: lib.func('bool hk_view_get_path_float(const HK_ViewDoc* view, const char* path, _Out_ double *out)'),
1218
- hk_view_get_path_bool: lib.func('int hk_view_get_path_bool(const HK_ViewDoc* view, const char* path)'),
1219
- hk_view_get_path_str: lib.func('const uint8_t* hk_view_get_path_str(const HK_ViewDoc* view, const char* path, _Out_ uintptr_t *len_out)'),
1220
- hk_view_get_path_bytes: lib.func('const uint8_t* hk_view_get_path_bytes(const HK_ViewDoc* view, const char* path, _Out_ uintptr_t *len_out)'),
1221
-
1222
- hk_last_error: lib.func('const char* hk_last_error()'),
1223
- hk_string_free: lib.func('void hk_string_free(void* value)')
1224
- };
1225
-
1226
- // ponytail: HeapStr returns arrive as JS strings, ALREADY freed via
1227
- // hk_string_free by the disposable — no decode, no manual free (both
1228
- // crash or leak: decode on externals segfaults, freeing a copy leaks).
1229
- const ptrToStringAndFree = (s: any): string | null => {
1230
- if (!s) return null;
1231
- return s as string;
1232
- };
1233
-
1234
- return {
1235
- engineOpen: (path) => fn.hk_engine_open(path),
1236
- engineIsIndexesReady: (engine) => fn.hk_engine_is_indexes_ready(engine),
1237
- engineOpenWithConfig: (path, config) => fn.hk_engine_open_with_config(path, config),
1238
- engineFree: (engine) => fn.hk_engine_free(engine),
1239
- engineBackup: (e, p) => fn.hk_engine_backup(e, p),
1240
- engineCompact: (e) => fn.hk_engine_compact(e),
1241
- engineGetStats: (e) => ptrToStringAndFree(fn.hk_engine_get_stats(e)),
1242
- engineGetAuditLog: (e) => ptrToStringAndFree(fn.hk_engine_get_audit_log(e)),
1243
- engineSnapshotIndices: (e) => fn.hk_engine_snapshot_indices(e),
1244
- engineListIndexes: (e, c) => ptrToStringAndFree(fn.hk_engine_list_indexes(e, c ?? null)),
1245
-
1246
- configNew: () => fn.hk_config_new(),
1247
- configFree: (c) => fn.hk_config_free(c),
1248
- configSetDurability: (c, m) => fn.hk_config_set_durability(c, m),
1249
- configSetEncryptionKey: (c, k) => fn.hk_config_set_encryption_key(c, k),
1250
- configSetEncryptedCollections: (c, json) => fn.hk_config_set_encrypted_collections(c, json),
1251
- configSetAuditLog: (c, e, p) => fn.hk_config_set_audit_log(c, e, p),
1252
- configSetQueryWorkers: (c, count) => fn.hk_config_set_query_workers(c, count),
1253
- configSetMemoryLimits: (c, m, mi) => fn.hk_config_set_memory_limits(c, m, mi),
1254
- configSetStorageTuning: (c, ps, th, gc) => fn.hk_config_set_storage_tuning(c, ps, th, gc),
1255
- configSetBlobThreshold: (c, t) => fn.hk_config_set_blob_threshold(c, t),
1256
- configSetCompression: (c, e, l) => fn.hk_config_set_compression(c, e, l),
1257
- configSetBackgroundMaintenance: (c, e) => fn.hk_config_set_background_maintenance(c, e),
1258
-
1259
- engineWatch: (engine, collection, callback) => {
1260
- const wrapper = (c: string, p: string, kind: number, _user: any) => callback(c, p, kind);
1261
- // ponytail: koffi.register takes the typedef NAME as a string here —
1262
- // passing the proto object itself throws "Unexpected ... type".
1263
- return fn.hk_engine_watch(engine, collection, koffi.register(wrapper, 'HK_OnSnapshotCallback *'), null);
1264
- },
1265
- watchFree: (watch) => fn.hk_watch_free(watch),
1266
-
1267
- docNew: () => fn.hk_doc_new(),
1268
- docFree: (doc) => fn.hk_doc_free(doc),
1269
- docInsertStr: (doc, key, value) => fn.hk_doc_insert_str(doc, key, value),
1270
- docInsertInt: (doc, key, value) => fn.hk_doc_insert_int(doc, key, value),
1271
- docInsertFloat: (doc, key, value) => fn.hk_doc_insert_float(doc, key, value),
1272
- docInsertBool: (doc, key, value) => fn.hk_doc_insert_bool(doc, key, value),
1273
- docInsertNull: (doc, key) => fn.hk_doc_insert_null(doc, key),
1274
- docInsertBin: (doc, key, bytes) => fn.hk_doc_insert_bin(doc, key, Buffer.from(bytes), bytes.byteLength),
1275
- docInsertTimestamp: (doc, key, micros) => fn.hk_doc_insert_timestamp(doc, key, micros),
1276
- docInsertServerTimestamp: (doc, key) => fn.hk_doc_insert_server_timestamp(doc, key),
1277
- docInsertReference: (doc, key, tc, tid) => fn.hk_doc_insert_reference(doc, key, tc, tid),
1278
- docToJson: (doc) => ptrToStringAndFree(fn.hk_doc_to_json(doc)),
1279
-
1280
- engineInsert: (engine, collection, docId, doc) => fn.hk_engine_insert(engine, collection, docId, doc),
1281
- engineGet: (engine, collection, docId) => fn.hk_engine_get(engine, collection, docId),
1282
- engineDelete: (engine, collection, docId) => fn.hk_engine_delete(engine, collection, docId),
1283
- engineDeleteLocal: (engine, collection, docId) => fn.hk_engine_delete_local(engine, collection, docId),
1284
- engineDeleteEx: (engine, collection, docId, force) => fn.hk_engine_delete_ext(engine, collection, docId, force ? 1 : 0),
1285
- engineRestore: (engine, collection, docId) => fn.hk_engine_restore(engine, collection, docId),
1286
- engineRestoreAvailable: (engine, collection) => ptrToStringAndFree(fn.hk_engine_restore_available(engine, collection as unknown as string)),
1287
- engineDeleteQueue: (engine, collection) => ptrToStringAndFree(fn.hk_engine_delete_queue(engine, collection as unknown as string)),
1288
- engineSweepNow: (engine) => fn.hk_engine_sweep_now(engine),
1289
- engineSetCollectionLocal: (engine, collection, local) => fn.hk_engine_set_collection_local(engine, collection, local),
1290
- engineReplicateKey: (engine, collection, docId) => fn.hk_engine_replicate_key(engine, collection, docId),
1291
- engineReplicateCollection: (engine, collection) => fn.hk_engine_replicate_collection(engine, collection),
1292
- engineVacuumCollection: (engine, collection) => fn.hk_engine_vacuum_collection(engine, collection),
1293
- engineRelocateDocs: (engine, src, dst, idsJson) => ptrToStringAndFree(fn.hk_engine_relocate_docs(engine, src, dst, idsJson)),
1294
- engineLoadCollection: (engine, collection) => fn.hk_engine_load_collection(engine, collection),
1295
- engineUnloadCollection: (engine, collection) => fn.hk_engine_unload_collection(engine, collection),
1296
- engineUnloadedCollections: (engine) => ptrToStringAndFree(fn.hk_engine_unloaded_collections(engine)),
1297
- enginePatch: (engine, collection, docId, updates) => fn.hk_engine_patch(engine, collection, docId, updates),
1298
- engineInsertSubDoc: (engine, col, id, subCol, subId, doc) => fn.hk_engine_insert_subdoc(engine, col, id, subCol, subId, doc),
1299
- engineGetByRef: (engine, doc, fieldKey) => fn.hk_engine_get_by_ref(engine, doc, fieldKey),
1300
- engineCreateIndex: (engine, collection, fieldsJson) => fn.hk_engine_create_index(engine, collection, fieldsJson),
1301
-
1302
- batchNew: () => fn.hk_batch_new(),
1303
- batchFree: (batch) => fn.hk_batch_free(batch),
1304
- batchSet: (batch, collection, docId, doc) => fn.hk_batch_set(batch, collection, docId, doc),
1305
- batchDelete: (batch, collection, docId) => fn.hk_batch_delete(batch, collection, docId),
1306
- batchDeleteEx: (batch, collection, docId, force) => fn.hk_batch_delete_ext(batch, collection, docId, force ? 1 : 0),
1307
- batchCommit: (engine, batch) => fn.hk_batch_commit(engine, batch),
1308
-
1309
- transactionBegin: (engine) => fn.hk_transaction_begin(engine),
1310
- transactionGet: (engine, tx, collection, docId) => fn.hk_transaction_get(engine, tx, collection, docId),
1311
- transactionSet: (tx, collection, docId, doc) => fn.hk_transaction_set(tx, collection, docId, doc),
1312
- transactionCommit: (engine, tx) => fn.hk_transaction_commit(engine, tx),
1313
- transactionFree: (tx) => fn.hk_transaction_free(tx),
1314
-
1315
- queryNew: (collection) => fn.hk_query_new(collection),
1316
- queryFree: (query) => fn.hk_query_free(query),
1317
- queryWhereEqStr: (query, field, value) => fn.hk_query_where_eq_str(query, field, value),
1318
- queryWhereEqBool: (query, field, value) => fn.hk_query_where_eq_bool(query, field, value),
1319
- queryWhereEqInt: (query, field, value) => fn.hk_query_where_eq_int(query, field, value),
1320
- queryWhereNeStr: (query, field, value) => fn.hk_query_where_ne_str(query, field, value),
1321
- queryWhereNeInt: (query, field, value) => fn.hk_query_where_ne_int(query, field, value),
1322
- queryWhereGtStr: (query, field, value) => fn.hk_query_where_gt_str(query, field, value),
1323
- queryWhereGtInt: (query, field, value) => fn.hk_query_where_gt_int(query, field, value),
1324
- queryWhereGteStr: (query, field, value) => fn.hk_query_where_gte_str(query, field, value),
1325
- queryWhereGteInt: (query, field, value) => fn.hk_query_where_gte_int(query, field, value),
1326
- queryWhereLtStr: (query, field, value) => fn.hk_query_where_lt_str(query, field, value),
1327
- queryWhereLtInt: (query, field, value) => fn.hk_query_where_lt_int(query, field, value),
1328
- queryWhereLteStr: (query, field, value) => fn.hk_query_where_lte_str(query, field, value),
1329
- queryWhereLteInt: (query, field, value) => fn.hk_query_where_lte_int(query, field, value),
1330
- queryWhereMatch: (query, field, value) => fn.hk_query_where_match(query, field, value),
1331
- queryWhereMatchPrefix: (query, field, value) => fn.hk_query_where_match_prefix(query, field, value),
1332
- queryWhereContains: (query, field, value) => fn.hk_query_where_contains(query, field, value),
1333
- queryWhereStartsWith: (query, field, value) => fn.hk_query_where_starts_with(query, field, value),
1334
- queryWhereOrStr: (query, field, value) => fn.hk_query_where_or_str(query, field, value),
1335
- queryWhereOrInt: (query, field, value) => fn.hk_query_where_or_int(query, field, value),
1336
- queryOrderBy: (query, field, asc) => fn.hk_query_order_by(query, field, asc),
1337
- queryLimit: (query, limit) => fn.hk_query_limit(query, limit),
1338
- queryOffset: (query, offset) => fn.hk_query_offset(query, offset),
1339
- querySelectField: (query, field) => fn.hk_query_select_field(query, field),
1340
- queryExecute: (engine, query) => ptrToStringAndFree(fn.hk_query_execute(engine, query)),
1341
- queryDelete: (engine, query) => fn.hk_query_delete(engine, query),
1342
- queryDeleteLocal: (engine, query) => fn.hk_query_delete_local(engine, query),
1343
- queryPatch: (engine, query, patchDoc) => fn.hk_query_patch(engine, query, patchDoc),
1344
- queryExecuteToHandles: (engine, query) => fn.hk_query_execute_to_handles(engine, query),
1345
- resultSetCount: (results) => fn.hk_result_set_count(results),
1346
- resultSetGetDoc: (results, index) => fn.hk_result_set_get_doc(results, index),
1347
- resultSetFree: (results) => fn.hk_result_set_free(results),
1348
- resultSetToJson: (results) => ptrToStringAndFree(fn.hk_result_set_to_json(results)),
1349
- queryExecuteRaw: (engine, query) => fn.hk_query_execute_raw(engine, query),
1350
- rawResultCount: (results) => fn.hk_rawresult_count(results),
1351
- rawResultGet: (results, index) => fn.hk_rawresult_get(results, index),
1352
- rawResultFree: (results) => fn.hk_rawresult_free(results),
1353
- queryStartAfterRaw: (query, anchorRawDoc) => fn.hk_query_start_after_raw(query, anchorRawDoc),
1354
- rawDocToDoc: (engine, rawDoc, collection) => fn.hk_rawdoc_to_doc(engine, rawDoc, collection),
1355
- viewGet: (engine, collection, docId) => fn.hk_view_get(engine, collection, docId),
1356
- viewFree: (view) => fn.hk_view_free(view),
1357
- viewFieldCount: (view) => fn.hk_view_field_count(view),
1358
- viewHasField: (view, key) => fn.hk_view_has_field(view, key),
1359
- viewGetInt: (view, key) => {
1360
- const out: bigint[] = [0n];
1361
- return fn.hk_view_get_int(view, key, out) ? out[0] : null;
1362
- },
1363
- viewGetFloat: (view, key) => {
1364
- const out: number[] = [0];
1365
- return fn.hk_view_get_float(view, key, out) ? out[0] : null;
1366
- },
1367
- viewGetBool: (view, key) => {
1368
- const r: number = fn.hk_view_get_bool(view, key);
1369
- return r < 0 ? null : r !== 0;
1370
- },
1371
- viewToDoc: (view, docId) => fn.hk_view_to_doc(view, docId),
1372
- queryDeferBlobs: (query, defer) => fn.hk_query_defer_blobs(query, defer ? 1 : 0),
1373
- docResolveBlobs: (engine, collection, doc) => fn.hk_doc_resolve_blobs(engine, collection, doc),
1374
- engineInsertTake: (engine, collection, docId, doc) => fn.hk_engine_insert_take(engine, collection, docId, doc),
1375
- configSetWalReserveBytes: (c, bytes) => fn.hk_config_set_wal_reserve_bytes(c, bytes),
1376
-
1377
- queryAggregateCount: (q) => fn.hk_query_aggregate_count(q),
1378
- queryAggregateSum: (q, f) => fn.hk_query_aggregate_sum(q, f),
1379
- queryAggregateAvg: (q, f) => fn.hk_query_aggregate_avg(q, f),
1380
- queryAggregateMin: (q, f) => fn.hk_query_aggregate_min(q, f),
1381
- queryAggregateMax: (q, f) => fn.hk_query_aggregate_max(q, f),
1382
- queryExecuteAggregation: (e, q) => ptrToStringAndFree(fn.hk_query_execute_aggregation(e, q)),
1383
- engineListCollections: (engine) => ptrToStringAndFree(fn.hk_engine_list_collections(engine)),
1384
- netSyncerNew: (engine, name, roomKey) => fn.hk_net_syncer_new(engine, name, roomKey),
1385
- netSyncerStart: (syncer, port) => fn.hk_net_syncer_start(syncer, port),
1386
- netSyncerSetDiscovery: (syncer, mode) => fn.hk_net_syncer_set_discovery(syncer, mode),
1387
- netSyncerStatus: (syncer) => ptrToStringAndFree(fn.hk_net_syncer_status(syncer)),
1388
- netSyncerFree: (syncer) => fn.hk_net_syncer_free(syncer),
1389
-
1390
- cloudSyncNew: (engine, mode, clientId, roomName, roomKey, authToken) => fn.hk_cloud_sync_new(engine, mode, clientId, roomName, roomKey, authToken),
1391
- cloudSyncServerNew: (engine, serverId, authToken, roomName, roomKey) => fn.hk_cloud_sync_server_new(engine, serverId, authToken, roomName, roomKey),
1392
- cloudSyncClientNew: (engine, clientId, roomName, roomKey, authToken) => fn.hk_cloud_sync_client_new(engine, clientId, roomName, roomKey, authToken),
1393
- cloudSyncStart: (cs, address) => fn.hk_cloud_sync_start(cs, address),
1394
- cloudSyncStatus: (cs) => ptrToStringAndFree(fn.hk_cloud_sync_status(cs)),
1395
- cloudSyncStop: (cs) => fn.hk_cloud_sync_stop(cs),
1396
- cloudSyncFree: (cs) => fn.hk_cloud_sync_free(cs),
1397
-
1398
- // ================= v0.5.9 =================
1399
-
1400
- // Indexing
1401
- createFtsIndex: (engine, collection, field) => fn.hk_engine_create_fts_index(engine, collection, field),
1402
- createSimpleIndex: (engine, collection, field) => fn.hk_engine_create_simple_index(engine, collection, field),
1403
- createVectorIndex: (engine, collection, field, dim, metric) => fn.hk_engine_create_vector_index(engine, collection, field, dim, metric),
1404
- // LE-f32 bytes via Buffer over the Float32Array (koffi takes Buffer
1405
- // for float*, same as docInsertBin takes Buffer for uint8_t*).
1406
- engineFindNear: (engine, collection, field, vec, k) => fn.hk_engine_find_near(engine, collection, field, Buffer.from(vec.buffer, vec.byteOffset, vec.byteLength), vec.length, k),
1407
- createTraverseIndex: (engine, collection) => fn.hk_engine_create_traverse_index(engine, collection),
1408
- // Null edge crosses as NULL (koffi takes null for char*).
1409
- engineFindPaths: (engine, collection, id, direction, edge, maxDepth, k) => fn.hk_engine_find_paths(engine, collection, id, direction, edge, maxDepth, k),
1410
- createEdgeIndex: (engine, collection) => fn.hk_engine_create_edge_index(engine, collection),
1411
- engineShortestPaths: (engine, collection, id, direction, edge, via, weight, maxDepth, k) => fn.hk_engine_shortest_paths(engine, collection, id, direction, edge, via, weight, maxDepth, k),
1412
-
1413
- // v0.19.6 parity (koffi takes null for char* directly)
1414
- dropSimpleIndex: (engine, collection, field) => fn.hk_engine_drop_simple_index(engine, collection, field),
1415
- dropVectorIndex: (engine, collection, field) => fn.hk_engine_drop_vector_index(engine, collection, field),
1416
- dropCompositeIndex: (engine, collection, id) => fn.hk_engine_drop_composite_index(engine, collection, id),
1417
- dropEdgeIndex: (engine, collection) => fn.hk_engine_drop_edge_index(engine, collection),
1418
- dropFtsIndex: (engine, collection, field) => fn.hk_engine_drop_fts_index(engine, collection, field),
1419
- dropTraverseIndex: (engine, collection) => fn.hk_engine_drop_traverse_index(engine, collection),
1420
- createVectorIndexV2: (engine, collection, field, dim, metric, m, efConstruction, efSearch) => fn.hk_engine_create_vector_index_v2(engine, collection, field, dim, metric, m, efConstruction, efSearch),
1421
- findPathsFull: (engine, collection, id, direction, edge, via, maxDepth, k, includePath, includeVia, nodeField, nodeJSON, typeField, typeJSON, matchField, matchJSON) => fn.hk_engine_find_paths_v4(engine, collection, id, direction, edge, via, maxDepth, k, includePath ? 1 : 0, includeVia ? 1 : 0, nodeField, nodeJSON, typeField, typeJSON, matchField, matchJSON),
1422
- simplePathsFull: (engine, collection, id, direction, edge, via, weight, maxDepth, k, includePath, includeVia, nodeField, nodeJSON, typeField, typeJSON, matchField, matchJSON) => fn.hk_engine_find_simple_paths_v3(engine, collection, id, direction, edge, via, weight, maxDepth, k, includePath ? 1 : 0, includeVia ? 1 : 0, nodeField, nodeJSON, typeField, typeJSON, matchField, matchJSON),
1423
- exportGraph: (engine, collection, direction, edge, via, nodeField, nodeJSON) => fn.hk_engine_export_graph(engine, collection, direction, edge, via, nodeField, nodeJSON),
1424
- graphAnalysis: (engine, collection, op, direction, edge, via, nodeField, nodeJSON) => fn.hk_engine_graph_analysis(engine, collection, op, direction, edge, via, nodeField, nodeJSON),
1425
- insertJSON: (engine, collection, id, json) => fn.hk_engine_insert_json(engine, collection, id, json),
1426
- batchSetJSON: (batch, collection, id, json) => fn.hk_batch_set_json(batch, collection, id, json),
1427
-
1428
- // Array API
1429
- arrayNew: () => fn.hk_array_new(),
1430
- arrayFree: (arr) => fn.hk_array_free(arr),
1431
- arrayAppendStr: (arr, value) => fn.hk_array_append_str(arr, value),
1432
- arrayAppendInt: (arr, value) => fn.hk_array_append_int(arr, value),
1433
- arrayAppendDoc: (arr, doc) => fn.hk_array_append_doc(arr, doc),
1434
-
1435
- // Nested
1436
- docInsertDoc: (parent, key, child) => fn.hk_doc_insert_doc(parent, key, child),
1437
- docInsertArray: (parent, key, arr) => fn.hk_doc_insert_array(parent, key, arr),
1438
-
1439
- // Query extensions
1440
- queryWhereIn: (query, field, arr) => fn.hk_query_where_in(query, field, arr),
1441
- queryWhereNotIn: (query, field, arr) => fn.hk_query_where_not_in(query, field, arr),
1442
- queryWhereArrayContainsAny: (query, field, arr) => fn.hk_query_where_array_contains_any(query, field, arr),
1443
- queryWhereArrayContainsStr: (query, field, value) => fn.hk_query_where_array_contains(query, field, String(value)),
1444
- queryWhereArrayContainsInt: (query, field, value) => fn.hk_query_where_array_contains(query, field, String(value)),
1445
- queryStartAfter: (query, anchorDoc) => fn.hk_query_start_after(query, anchorDoc),
1446
- queryStartAt: (query, anchorDoc) => fn.hk_query_start_at(query, anchorDoc),
1447
- queryEndAt: (query, anchorDoc) => fn.hk_query_end_at(query, anchorDoc),
1448
- queryEndBefore: (query, anchorDoc) => fn.hk_query_end_before(query, anchorDoc),
1449
-
1450
- // 0.16.5 parity.
1451
- engineSetSchema: (e, c, json) => fn.hk_engine_set_schema(e, c, json),
1452
- engineGetSchema: (e, c) => ptrToStringAndFree(fn.hk_engine_get_schema(e, c)),
1453
- engineClearSchema: (e, c) => fn.hk_engine_clear_schema(e, c),
1454
- engineListSchemas: (e) => ptrToStringAndFree(fn.hk_engine_list_schemas(e)),
1455
- engineGetVersioned: (e, c, id) => {
1456
- const out: bigint[] = [0n];
1457
- const doc = fn.hk_engine_get_versioned(e, c, id, out);
1458
- return { doc, version: out[0] };
1459
- },
1460
- engineAwaitQuiescent: (e, ms) => fn.hk_engine_await_quiescent(e, ms),
1461
- engineQuiescenceStatus: (e) => ptrToStringAndFree(fn.hk_engine_quiescence_status(e)),
1462
- transactionGetMany: (e, tx, c, idsJson) => ptrToStringAndFree(fn.hk_transaction_get_many(e, tx, c, idsJson)),
1463
- transactionPatch: (e, tx, c, id, updatesJson) => fn.hk_transaction_patch(e, tx, c, id, updatesJson),
1464
- errorLastKind: () => fn.hk_error_last_kind(),
1465
- errorIsRetryable: () => fn.hk_error_is_retryable(),
1466
- debugWriteStats: () => ptrToStringAndFree(fn.hk_debug_write_stats()),
1467
- logEnableStderr: () => fn.hk_log_enable_stderr(),
1468
- logDisableStderr: () => fn.hk_log_disable_stderr(),
1469
- queryExplain: (e, q) => ptrToStringAndFree(fn.hk_query_explain(e, q)),
1470
- queryWhereArrayAllEq: (q, f, json) => fn.hk_query_where_array_all_eq(q, f, json),
1471
- queryWhereElemMatch: (q, f, json) => fn.hk_query_where_elem_match(q, f, json),
1472
- queryWhereNear: (q, f, vec) => fn.hk_query_where_near(q, f, Buffer.from(vec.buffer, vec.byteOffset, vec.byteLength), vec.length),
1473
- queryWhereTraverse: (q, sc, sid, dir, edge, depth) => fn.hk_query_where_traverse(q, sc, sid, dir, edge, depth),
1474
- pipelineNew: (c) => fn.hk_pipeline_new(c),
1475
- pipelineFree: (p) => fn.hk_pipeline_free(p),
1476
- pipelineMatch: (p, q) => fn.hk_pipeline_match(p, q),
1477
- pipelineProject: (p, json) => fn.hk_pipeline_project(p, json),
1478
- pipelineSort: (p, f, asc) => fn.hk_pipeline_sort(p, f, asc),
1479
- pipelineLimit: (p, n) => fn.hk_pipeline_limit(p, n),
1480
- pipelineOffset: (p, n) => fn.hk_pipeline_offset(p, n),
1481
- pipelineUnwind: (p, f) => fn.hk_pipeline_unwind(p, f),
1482
- pipelineGroup: (p, k, json) => fn.hk_pipeline_group(p, k, json),
1483
- pipelineExecute: (e, p) => ptrToStringAndFree(fn.hk_pipeline_execute(e, p)),
1484
- pipelineExecuteToHandles: (e, p) => fn.hk_pipeline_execute_to_handles(e, p),
1485
- configSetGroupCommitIntervalMs: (c, ms) => fn.hk_config_set_group_commit_interval_ms(c, ms),
1486
- configSetPageCacheCapacity: (c, n) => fn.hk_config_set_page_cache_capacity(c, n),
1487
- configSetSyncExcluded: (c, json) => fn.hk_config_set_sync_excluded(c, json),
1488
- configSetLazyCollections: (c, json) => fn.hk_config_set_lazy_collections(c, json),
1489
- configSetReplicationCollections: (c, json) => fn.hk_config_set_replication_collections(c, json),
1490
- cursorWalk: (e, q, callback) => {
1491
- const wrapper = (id: any, idLen: number, bytes: any, bytesLen: number) => {
1492
- const rowId = new TextDecoder().decode(Uint8Array.from(koffi.decode(id, 'uint8_t', Number(idLen))));
1493
- const rowBytes = Uint8Array.from(koffi.decode(bytes, 'uint8_t', Number(bytesLen)));
1494
- return callback(rowId, rowBytes);
1495
- };
1496
- return Number(fn.hk_cursor_walk(e, q, koffi.register(wrapper, 'HkWalkCallback *'), null));
1497
- },
1498
- cursorWalkView: (e, q, callback) => {
1499
- const wrapper = (id: any, idLen: number, view: any) => {
1500
- const rowId = new TextDecoder().decode(Uint8Array.from(koffi.decode(id, 'uint8_t', Number(idLen))));
1501
- return callback(rowId, view);
1502
- };
1503
- return Number(fn.hk_cursor_walk_view(e, q, koffi.register(wrapper, 'HkViewWalkCallback *'), null));
1504
- },
1505
- rawDocBytes: (raw) => {
1506
- const len: number[] = [0];
1507
- const ptr = fn.hk_rawdoc_bytes(raw, len);
1508
- return ptr ? Uint8Array.from(koffi.decode(ptr, 'uint8_t', Number(len[0]))) : null;
1509
- },
1510
- rawDocId: (raw) => {
1511
- const len: number[] = [0];
1512
- const ptr = fn.hk_rawdoc_id(raw, len);
1513
- return ptr ? new TextDecoder().decode(Uint8Array.from(koffi.decode(ptr, 'uint8_t', Number(len[0])))) : null;
1514
- },
1515
- viewGetStr: (view, key) => {
1516
- const len: number[] = [0];
1517
- const ptr = fn.hk_view_get_str(view, key, len);
1518
- return ptr ? new TextDecoder().decode(Uint8Array.from(koffi.decode(ptr, 'uint8_t', Number(len[0])))) : null;
1519
- },
1520
- viewGetBytes: (view, key) => {
1521
- const len: number[] = [0];
1522
- const ptr = fn.hk_view_get_bytes(view, key, len);
1523
- return ptr ? Uint8Array.from(koffi.decode(ptr, 'uint8_t', Number(len[0]))) : null;
1524
- },
1525
-
1526
- // P2 (core 0.18): JSON read paths + dotted-path pulls.
1527
- engineGetJson: (e, c, id) => ptrToStringAndFree(fn.hk_engine_get_json(e, c, id)),
1528
- queryExecuteJson: (e, q) => ptrToStringAndFree(fn.hk_query_execute_json(e, q)),
1529
- viewToJson: (view) => ptrToStringAndFree(fn.hk_view_to_json(view)),
1530
- cursorWalkJson: (e, q, callback) => {
1531
- const wrapper = (id: any, idLen: number, json: any, jsonLen: number) => {
1532
- const rowId = new TextDecoder().decode(Uint8Array.from(koffi.decode(id, 'uint8_t', Number(idLen))));
1533
- const rowJson = Uint8Array.from(koffi.decode(json, 'uint8_t', Number(jsonLen)));
1534
- return callback(rowId, rowJson);
1535
- };
1536
- return Number(fn.hk_cursor_walk_json(e, q, koffi.register(wrapper, 'HkWalkCallback *'), null));
1537
- },
1538
- viewGetPathInt: (view, path) => {
1539
- const out: bigint[] = [0n];
1540
- return fn.hk_view_get_path_int(view, path, out) ? out[0] : null;
1541
- },
1542
- viewGetPathFloat: (view, path) => {
1543
- const out: number[] = [0];
1544
- return fn.hk_view_get_path_float(view, path, out) ? out[0] : null;
1545
- },
1546
- viewGetPathBool: (view, path) => {
1547
- const r: number = fn.hk_view_get_path_bool(view, path);
1548
- return r < 0 ? null : r !== 0;
1549
- },
1550
- viewGetPathStr: (view, path) => {
1551
- const len: number[] = [0];
1552
- const ptr = fn.hk_view_get_path_str(view, path, len);
1553
- return ptr ? new TextDecoder().decode(Uint8Array.from(koffi.decode(ptr, 'uint8_t', Number(len[0])))) : null;
1554
- },
1555
- viewGetPathBytes: (view, path) => {
1556
- const len: number[] = [0];
1557
- const ptr = fn.hk_view_get_path_bytes(view, path, len);
1558
- return ptr ? Uint8Array.from(koffi.decode(ptr, 'uint8_t', Number(len[0]))) : null;
1559
- },
1560
-
1561
- lastError: () => (fn.hk_last_error() as string) || 'unknown ffi error'
1562
- };
1563
- }
1564
-
1565
- export async function loadNativeBindings(explicitPath?: string): Promise<NativeBindings> {
1566
- const libPath = resolveLibraryPath(explicitPath);
1567
- // return isBunRuntime() ? createBunBindings(libPath) : createNodeBindings(libPath);
1568
-
1569
- // 1. Check for Bun
1570
- if (typeof (globalThis as any).Bun !== 'undefined') {
1571
- return createBunBindings(libPath);
1572
- }
1573
-
1574
- // 2. Check for Node.js (via process)
1575
- if (typeof process !== 'undefined' && process.versions && process.versions.node) {
1576
- return createNodeBindings(libPath);
1577
- }
1578
-
1579
- throw new Error("HakoDB Native Bindings are only supported in Node.js or Bun environments. For browsers, use the Tauri Gateway.");
1580
- }
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+
3
+ type Handle = any;
4
+
5
+ /**
6
+ * matches HK_OnSnapshotCallback in hako.h
7
+ */
8
+ export type WatchCallback = (collection: string, path: string, kind: number) => void;
9
+
10
+ export interface NativeBindings {
11
+ engineOpen(path: string): Handle;
12
+ engineIsIndexesReady(engine: Handle): boolean;
13
+ engineOpenWithConfig(path: string, config: Handle): Handle;
14
+ engineFree(engine: Handle): void;
15
+ engineBackup(engine: Handle, path: string): number; // Added
16
+ engineCompact(engine: Handle): number;
17
+ engineGetStats(engine: Handle): string | null;
18
+ engineGetAuditLog(engine: Handle): string | null;
19
+ engineSnapshotIndices(engine: Handle): number;
20
+ engineListIndexes(engine: Handle, collection: string | null): string | null;
21
+
22
+ // Configuration Builder
23
+ configNew(): Handle;
24
+ configFree(config: Handle): void;
25
+ configSetDurability(config: Handle, mode: number): void;
26
+ configSetEncryptionKey(config: Handle, key: string | null): void;
27
+ configSetEncryptedCollections(config: Handle, collectionsJson: string | null): number;
28
+ configSetAuditLog(config: Handle, enabled: boolean, path: string | null): void;
29
+ configSetQueryWorkers(config: Handle, count: number): void;
30
+ configSetMemoryLimits(config: Handle, mmap: number, maxInlined: number): void;
31
+ configSetStorageTuning(
32
+ config: Handle,
33
+ pageSize: number,
34
+ threshold: number,
35
+ groupCommit: number
36
+ ): void;
37
+ configSetBlobThreshold(config: Handle, thresholdBytes: number): void;
38
+ configSetCompression(config: Handle, enabled: boolean, level: number): void;
39
+ configSetBackgroundMaintenance(config: Handle, enabled: boolean): void;
40
+
41
+ // Real-time Watch
42
+ engineWatch(engine: Handle, collection: string, callback: WatchCallback): Handle;
43
+ watchFree(watch: Handle): void;
44
+
45
+ // Document Builder
46
+ docNew(): Handle;
47
+ docFree(doc: Handle): void;
48
+ docInsertStr(doc: Handle, key: string, value: string): number;
49
+ docInsertInt(doc: Handle, key: string, value: number | bigint): number;
50
+ docInsertFloat(doc: Handle, key: string, value: number): number;
51
+ docInsertBool(doc: Handle, key: string, value: boolean): number;
52
+ docInsertNull(doc: Handle, key: string): number;
53
+ docInsertBin(doc: Handle, key: string, bytes: Uint8Array): number;
54
+ docInsertTimestamp(doc: Handle, key: string, micros: bigint): number; // Added
55
+ docInsertServerTimestamp(doc: Handle, key: string): number; // Added
56
+ docInsertReference(doc: Handle, key: string, targetCollection: string, targetId: string): number;
57
+ docToJson(doc: Handle): string | null;
58
+
59
+ // Engine CRUD
60
+ engineInsert(engine: Handle, collection: string, docId: string, doc: Handle): number;
61
+ engineGet(engine: Handle, collection: string, docId: string): Handle;
62
+ engineDelete(engine: Handle, collection: string, docId: string): number;
63
+ engineDeleteLocal(engine: Handle, collection: string, docId: string): number;
64
+ engineDeleteEx(engine: Handle, collection: string, docId: string, force: boolean): number;
65
+ engineRestore(engine: Handle, collection: string, docId: string): number;
66
+ engineRestoreAvailable(engine: Handle, collection: string | null): string | null;
67
+ engineDeleteQueue(engine: Handle, collection: string | null): string | null;
68
+ engineSweepNow(engine: Handle): number;
69
+ engineSetCollectionLocal(engine: Handle, collection: string, local: number): number;
70
+ engineReplicateKey(engine: Handle, collection: string, docId: string): number;
71
+ engineReplicateCollection(engine: Handle, collection: string): number;
72
+ engineVacuumCollection(engine: Handle, collection: string): number;
73
+ engineRelocateDocs(engine: Handle, src: string, dst: string, idsJson: string): string | null;
74
+ engineLoadCollection(engine: Handle, collection: string): number;
75
+ engineUnloadCollection(engine: Handle, collection: string): number;
76
+ engineUnloadedCollections(engine: Handle): string | null;
77
+ enginePatch(engine: Handle, collection: string, docId: string, updates: Handle): number;
78
+ engineInsertSubDoc(engine: Handle, col: string, id: string, subCol: string, subId: string, doc: Handle): number;
79
+ engineGetByRef(engine: Handle, doc: Handle, fieldKey: string): Handle;
80
+ engineCreateIndex(engine: Handle, collection: string, fieldsJson: string): number;
81
+
82
+ // Atomic Batch
83
+ batchNew(): Handle;
84
+ batchFree(batch: Handle): void;
85
+ batchSet(batch: Handle, collection: string, docId: string, doc: Handle): number;
86
+ batchDelete(batch: Handle, collection: string, docId: string): number;
87
+ batchDeleteEx(batch: Handle, collection: string, docId: string, force: boolean): number;
88
+ batchCommit(engine: Handle, batch: Handle): number;
89
+
90
+ // Serializable Transactions
91
+ transactionBegin(engine: Handle): Handle;
92
+ transactionGet(engine: Handle, tx: Handle, collection: string, docId: string): Handle;
93
+ transactionSet(tx: Handle, collection: string, docId: string, doc: Handle): number;
94
+ transactionCommit(engine: Handle, tx: Handle): number;
95
+ transactionFree(tx: Handle): void;
96
+
97
+ // Query API
98
+ queryNew(collection: string): Handle;
99
+ queryFree(query: Handle): void;
100
+ queryWhereEqStr(query: Handle, field: string, value: string): number;
101
+ queryWhereEqBool(query: Handle, field: string, value: boolean): number;
102
+ queryWhereEqInt(query: Handle, field: string, value: number | bigint): number;
103
+ queryWhereNeStr(query: Handle, field: string, value: string): number;
104
+ queryWhereNeInt(query: Handle, field: string, value: number | bigint): number;
105
+ queryWhereGtStr(query: Handle, field: string, value: string): number;
106
+ queryWhereGtInt(query: Handle, field: string, value: number | bigint): number;
107
+ queryWhereGteStr(query: Handle, field: string, value: string): number;
108
+ queryWhereGteInt(query: Handle, field: string, value: number | bigint): number;
109
+ queryWhereLtStr(query: Handle, field: string, value: string): number;
110
+ queryWhereLtInt(query: Handle, field: string, value: number | bigint): number;
111
+ queryWhereLteStr(query: Handle, field: string, value: string): number;
112
+ queryWhereLteInt(query: Handle, field: string, value: number | bigint): number;
113
+ queryOrderBy(query: Handle, field: string, ascending: boolean): number;
114
+ queryLimit(query: Handle, limit: number): number;
115
+ queryOffset(query: Handle, offset: number): number;
116
+ querySelectField(query: Handle, field: string): number;
117
+ queryExecute(engine: Handle, query: Handle): string | null;
118
+ queryDelete(engine: Handle, query: Handle): number;
119
+ queryDeleteLocal(engine: Handle, query: Handle): number;
120
+ queryPatch(engine: Handle, query: Handle, patchDoc: Handle): number;
121
+ queryExecuteToHandles(engine: Handle, query: Handle): Handle;
122
+ resultSetCount(results: Handle): number;
123
+ resultSetGetDoc(results: Handle, index: number): Handle;
124
+ resultSetFree(results: Handle): void;
125
+ resultSetToJson(results: Handle): string | null;
126
+ // Raw result sets (v0.8.3): pinned bytes, not decoded docs. Rows are
127
+ // resolved selectively via rawDocToDoc (decode only what you touch);
128
+ // page with queryStartAfterRaw (no decode at all). rawDocBytes/Id need
129
+ // backend memory reads — use resolve-then-read instead (see client.ts).
130
+ queryExecuteRaw(engine: Handle, query: Handle): Handle;
131
+ rawResultCount(results: Handle): number;
132
+ rawResultGet(results: Handle, index: number): Handle;
133
+ rawResultFree(results: Handle): void;
134
+ rawDocToDoc(engine: Handle, rawDoc: Handle, collection: string): Handle;
135
+ queryStartAfterRaw(query: Handle, anchorRawDoc: Handle): number;
136
+ // Borrowed views (v0.8.11): lazy typed pulls, no owned construction.
137
+ // Numeric/bool getters cross by value (no memory reads); strings and
138
+ // walks stay on resolve/raw paths (documented in client.ts).
139
+ viewGet(engine: Handle, collection: string, docId: string): Handle;
140
+ viewFree(view: Handle): void;
141
+ viewFieldCount(view: Handle): number;
142
+ viewHasField(view: Handle, key: string): boolean;
143
+ viewGetInt(view: Handle, key: string): number | bigint | null;
144
+ viewGetFloat(view: Handle, key: string): number | null;
145
+ viewGetBool(view: Handle, key: string): boolean | null;
146
+ viewToDoc(view: Handle, docId: string): Handle;
147
+ queryDeferBlobs(query: Handle, defer: boolean): number;
148
+ docResolveBlobs(engine: Handle, collection: string, doc: Handle): number;
149
+ engineInsertTake(engine: Handle, collection: string, docId: string, doc: Handle): number;
150
+ configSetWalReserveBytes(c: Handle, bytes: number | bigint): void;
151
+
152
+ // Full-Text Search Queries (Added)
153
+ queryWhereMatch(query: Handle, field: string, value: string): number;
154
+ queryWhereMatchPrefix(query: Handle, field: string, value: string): number;
155
+ queryWhereContains(query: Handle, field: string, value: string): number;
156
+ queryWhereStartsWith(query: Handle, field: string, value: string): number;
157
+ queryWhereOrStr(query: Handle, field: string, value: string): number;
158
+ queryWhereOrInt(query: Handle, field: string, value: number | bigint): number;
159
+
160
+ // Aggregation API
161
+ queryAggregateCount(query: Handle): number;
162
+ queryAggregateSum(query: Handle, field: string): number;
163
+ queryAggregateAvg(query: Handle, field: string): number;
164
+ queryAggregateMin(query: Handle, field: string): number;
165
+ queryAggregateMax(query: Handle, field: string): number;
166
+ queryExecuteAggregation(engine: Handle, query: Handle): string | null;
167
+
168
+ engineListCollections(engine: Handle): string | null;
169
+
170
+ // Net Sync
171
+ netSyncerNew(engine: Handle, name: string, roomKey: string): Handle;
172
+ netSyncerStart(syncer: Handle, port: number): number;
173
+ netSyncerSetDiscovery(syncer: Handle, mode: number): number;
174
+ netSyncerStatus(syncer: Handle): string | null;
175
+ netSyncerFree(syncer: Handle): void;
176
+
177
+ // Cloud Sync (bi-directional WebSocket replication)
178
+ cloudSyncNew(engine: Handle, mode: number, clientId: string | null, roomName: string | null, roomKey: string | null, authToken: string | null): Handle;
179
+ cloudSyncServerNew(engine: Handle, serverId: string | null, authToken: string | null, roomName: string | null, roomKey: string | null): Handle;
180
+ cloudSyncClientNew(engine: Handle, clientId: string | null, roomName: string | null, roomKey: string | null, authToken: string | null): Handle;
181
+ cloudSyncStart(cloudSync: Handle, address: string): number;
182
+ cloudSyncStatus(cloudSync: Handle): string | null;
183
+ cloudSyncStop(cloudSync: Handle): void;
184
+ cloudSyncFree(cloudSync: Handle): void;
185
+
186
+ // v0.5.9
187
+ createFtsIndex(engine: Handle, collection: string, field: string): number;
188
+ createSimpleIndex(engine: Handle, collection: string, field: string): number;
189
+
190
+ // Vector KNN (core >=0.13.3): one-shot search returns owned JSON
191
+ // [{id, score, doc}] (null = hard error, e.g. dim mismatch — read
192
+ // lastError); unindexed field returns "[]".
193
+ createVectorIndex(engine: Handle, collection: string, field: string, dim: number, metric: number): number;
194
+ engineFindNear(engine: Handle, collection: string, field: string, vec: Float32Array, k: number): string | null;
195
+
196
+ // Graph traversal (core >=0.14.0): one-shot BFS returns owned JSON
197
+ // [{collection, id, depth, doc}] (null = hard error, e.g. incoming
198
+ // into untracked — read lastError); missing anchor returns "[]".
199
+ // direction 0/1/2 = outgoing/incoming/both; edge null = any ref field.
200
+ createTraverseIndex(engine: Handle, collection: string): number;
201
+ engineFindPaths(engine: Handle, collection: string, id: string, direction: number, edge: string | null, maxDepth: number, k: number): string | null;
202
+ // Phase B (core >=0.15.0): edge registration + weighted Dijkstra
203
+ // (same hit shape, cost-ordered; cost rides each hit).
204
+ createEdgeIndex(engine: Handle, collection: string): number;
205
+ engineShortestPaths(engine: Handle, collection: string, id: string, direction: number, edge: string | null, via: string | null, weight: string | null, maxDepth: number, k: number): string | null;
206
+
207
+ // v0.19.6 parity: index drops, full graph/traverse API, JSON writes.
208
+ dropSimpleIndex(engine: Handle, collection: string, field: string): number;
209
+ dropVectorIndex(engine: Handle, collection: string, field: string): number;
210
+ dropCompositeIndex(engine: Handle, collection: string, id: number): number;
211
+ dropEdgeIndex(engine: Handle, collection: string): number;
212
+ dropFtsIndex(engine: Handle, collection: string, field: string): number;
213
+ dropTraverseIndex(engine: Handle, collection: string): number;
214
+ createVectorIndexV2(engine: Handle, collection: string, field: string, dim: number, metric: number, m: number, efConstruction: number, efSearch: number): number;
215
+ findPathsFull(engine: Handle, collection: string, id: string, direction: number, edge: string | null, via: string | null, maxDepth: number, k: number, includePath: boolean, includeVia: boolean, nodeField: string | null, nodeJSON: string | null, typeField: string | null, typeJSON: string | null, matchField: string | null, matchJSON: string | null): string | null;
216
+ simplePathsFull(engine: Handle, collection: string, id: string, direction: number, edge: string | null, via: string | null, weight: string | null, maxDepth: number, k: number, includePath: boolean, includeVia: boolean, nodeField: string | null, nodeJSON: string | null, typeField: string | null, typeJSON: string | null, matchField: string | null, matchJSON: string | null): string | null;
217
+ exportGraph(engine: Handle, collection: string, direction: number, edge: string | null, via: string | null, nodeField: string | null, nodeJSON: string | null): string | null;
218
+ graphAnalysis(engine: Handle, collection: string, op: string, direction: number, edge: string | null, via: string | null, nodeField: string | null, nodeJSON: string | null): string | null;
219
+ insertJSON(engine: Handle, collection: string, id: string, json: string): number;
220
+ batchSetJSON(batch: Handle, collection: string, id: string, json: string): number;
221
+
222
+ // Array API
223
+ arrayNew(): Handle;
224
+ arrayFree(array: Handle): void;
225
+ arrayAppendStr(array: Handle, value: string): number;
226
+ arrayAppendInt(array: Handle, value: number | bigint): number;
227
+ arrayAppendDoc(array: Handle, doc: Handle): number;
228
+
229
+ // Nested structures
230
+ docInsertDoc(parent: Handle, key: string, child: Handle): number;
231
+ docInsertArray(parent: Handle, key: string, array: Handle): number;
232
+
233
+ // Query extensions
234
+ queryWhereIn(query: Handle, field: string, array: Handle): number;
235
+ queryWhereNotIn(query: Handle, field: string, array: Handle): number;
236
+ queryWhereArrayContainsAny(query: Handle, field: string, array: Handle): number;
237
+ queryWhereArrayContainsStr(query: Handle, field: string, value: string): number;
238
+ queryWhereArrayContainsInt(query: Handle, field: string, value: number | bigint): number;
239
+ queryStartAfter(query: Handle, anchorDoc: Handle): number;
240
+ queryStartAt(query: Handle, anchorDoc: Handle): number;
241
+ queryEndAt(query: Handle, anchorDoc: Handle): number;
242
+ queryEndBefore(query: Handle, anchorDoc: Handle): number;
243
+
244
+ // Unix-only hk_socket_sync_* is deliberately absent: Windows builds omit
245
+ // the symbol, so binding it unconditionally would break koffi on Windows.
246
+
247
+ // F8: per-collection schemas over the C ABI.
248
+ engineSetSchema(engine: Handle, collection: string, schemaJson: string): number;
249
+ engineGetSchema(engine: Handle, collection: string): string | null;
250
+ engineClearSchema(engine: Handle, collection: string): number;
251
+ engineListSchemas(engine: Handle): string | null;
252
+
253
+ // F5: versioned reads, transaction patch/get_many, retry classification.
254
+ engineGetVersioned(engine: Handle, collection: string, docId: string): { doc: Handle; version: bigint };
255
+ transactionGetMany(engine: Handle, tx: Handle, collection: string, idsJson: string): string | null;
256
+ transactionPatch(engine: Handle, tx: Handle, collection: string, docId: string, updatesJson: string): number;
257
+ errorLastKind(): number;
258
+ errorIsRetryable(): boolean;
259
+
260
+ // F7/F3: explain + array/graph predicates.
261
+ queryExplain(engine: Handle, query: Handle): string | null;
262
+ queryWhereArrayAllEq(query: Handle, field: string, valueJson: string): number;
263
+ queryWhereElemMatch(query: Handle, field: string, filtersJson: string): number;
264
+ queryWhereNear(query: Handle, field: string, vec: Float32Array): number;
265
+ queryWhereTraverse(query: Handle, startCol: string, startId: string, direction: number, edge: string | null, maxDepth: number): number;
266
+
267
+ // F4: aggregation pipeline (raw handle; typed builder is separate work).
268
+ pipelineNew(collection: string): Handle;
269
+ pipelineFree(pipeline: Handle): void;
270
+ pipelineMatch(pipeline: Handle, query: Handle): number;
271
+ pipelineProject(pipeline: Handle, fieldsJson: string): number;
272
+ pipelineSort(pipeline: Handle, field: string, ascending: boolean): number;
273
+ pipelineLimit(pipeline: Handle, limit: number): number;
274
+ pipelineOffset(pipeline: Handle, offset: number): number;
275
+ pipelineUnwind(pipeline: Handle, field: string): number;
276
+ pipelineGroup(pipeline: Handle, key: string, accumsJson: string): number;
277
+ pipelineExecute(engine: Handle, pipeline: Handle): string | null;
278
+ pipelineExecuteToHandles(engine: Handle, pipeline: Handle): Handle;
279
+
280
+ // Config additions (0.12-0.16 core knobs).
281
+ configSetGroupCommitIntervalMs(config: Handle, ms: number | bigint): void;
282
+ configSetPageCacheCapacity(config: Handle, capacity: number): void;
283
+ configSetSyncExcluded(config: Handle, collectionsJson: string): number;
284
+ configSetLazyCollections(config: Handle, collectionsJson: string): number;
285
+ configSetReplicationCollections(config: Handle, collectionsJson: string): number;
286
+
287
+ // Quiescence / diagnostics / logging.
288
+ engineAwaitQuiescent(engine: Handle, timeoutMs: number | bigint): boolean;
289
+ engineQuiescenceStatus(engine: Handle): string | null;
290
+ debugWriteStats(): string | null;
291
+ logEnableStderr(): void;
292
+ logDisableStderr(): void;
293
+
294
+ // Lazy cursor walks: callbacks copy immediately; returning false stops.
295
+ cursorWalk(engine: Handle, query: Handle, callback: (id: string, bytes: Uint8Array) => boolean): number;
296
+ cursorWalkView(engine: Handle, query: Handle, callback: (id: string, view: Handle) => boolean): number;
297
+
298
+ // Borrowed raw/view reads: copied before return (lifetime is the result set/view).
299
+ rawDocBytes(rawDoc: Handle): Uint8Array | null;
300
+ rawDocId(rawDoc: Handle): string | null;
301
+ viewGetStr(view: Handle, key: string): string | null;
302
+ viewGetBytes(view: Handle, key: string): Uint8Array | null;
303
+
304
+ // P2 (core 0.18): JSON read paths + dotted-path view pulls. JSON strings
305
+ // are owned (free happens in the binding); view pulls copy before return.
306
+ engineGetJson(engine: Handle, collection: string, docId: string): string | null;
307
+ queryExecuteJson(engine: Handle, query: Handle): string | null;
308
+ viewToJson(view: Handle): string | null;
309
+ cursorWalkJson(engine: Handle, query: Handle, callback: (id: string, json: Uint8Array) => boolean): number;
310
+ viewGetPathInt(view: Handle, path: string): number | bigint | null;
311
+ viewGetPathFloat(view: Handle, path: string): number | null;
312
+ viewGetPathBool(view: Handle, path: string): boolean | null;
313
+ viewGetPathStr(view: Handle, path: string): string | null;
314
+ viewGetPathBytes(view: Handle, path: string): Uint8Array | null;
315
+
316
+ lastError(): string;
317
+ }
318
+
319
+ function isBunRuntime(): boolean {
320
+ return typeof (globalThis as any).Bun !== 'undefined';
321
+ }
322
+
323
+ function defaultLibraryPath(): string {
324
+ const libName = process.platform === 'win32' ? 'hakodb.dll' :
325
+ process.platform === 'darwin' ? 'libhakodb.dylib' : 'libhakodb.so';
326
+ // Explicit path wins (loadNativeBindings(path)); otherwise the binary
327
+ // shipped under ./native/ (populated by sync-native.* from a core
328
+ // checkout or release asset). HAKODB_LIB_PATH overrides both.
329
+ const override = typeof process !== 'undefined' ? process.env.HAKODB_LIB_PATH : undefined;
330
+ if (override) return override;
331
+ return `./native/${libName}`;
332
+ }
333
+
334
+ function resolveLibraryPath(explicitPath?: string): string {
335
+ return explicitPath ?? defaultLibraryPath();
336
+ }
337
+
338
+ async function createBunBindings(libPath: string): Promise<NativeBindings> {
339
+ const ffi = await import('bun:ffi');
340
+ const { dlopen, FFIType, CString, JSCallback, toArrayBuffer } = ffi as any;
341
+
342
+ // Watch trampolines must outlive their native watch: the engine invokes
343
+ // them from its watch thread, and a GC'd JSCallback would leave that
344
+ // thread calling freed memory (issue #21). Closed in watchFree after
345
+ // the native join.
346
+ const watchTrampolines = new Map<unknown, { close(): void }>();
347
+
348
+ const symbols = dlopen(libPath, {
349
+ hk_engine_open: { args: [FFIType.cstring], returns: FFIType.ptr },
350
+ hk_engine_is_indexes_ready: { args: [FFIType.ptr], returns: FFIType.bool },
351
+ hk_engine_open_with_config: { args: [FFIType.cstring, FFIType.ptr], returns: FFIType.ptr },
352
+ hk_engine_free: { args: [FFIType.ptr], returns: FFIType.void },
353
+ hk_engine_backup: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
354
+ hk_engine_compact: { args: [FFIType.ptr], returns: FFIType.i32 },
355
+ hk_engine_get_stats: { args: [FFIType.ptr], returns: FFIType.ptr },
356
+ hk_engine_get_audit_log: { args: [FFIType.ptr], returns: FFIType.ptr },
357
+ hk_engine_snapshot_indices: { args: [FFIType.ptr], returns: FFIType.i32 },
358
+ hk_engine_list_indexes: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.ptr },
359
+
360
+ hk_config_new: { args: [], returns: FFIType.ptr },
361
+ hk_config_free: { args: [FFIType.ptr], returns: FFIType.void },
362
+ hk_config_set_durability: { args: [FFIType.ptr, FFIType.i32], returns: FFIType.void },
363
+ hk_config_set_encryption_key: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.void },
364
+ hk_config_set_encrypted_collections: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
365
+ hk_config_set_audit_log: { args: [FFIType.ptr, FFIType.bool, FFIType.cstring], returns: FFIType.void },
366
+ hk_config_set_query_workers: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.void },
367
+ hk_config_set_memory_limits: { args: [FFIType.ptr, FFIType.usize, FFIType.usize], returns: FFIType.void },
368
+ hk_config_set_storage_tuning: { args: [FFIType.ptr, FFIType.usize, FFIType.usize, FFIType.usize], returns: FFIType.void },
369
+ hk_config_set_blob_threshold: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.void },
370
+ hk_config_set_compression: { args: [FFIType.ptr, FFIType.bool, FFIType.i32], returns: FFIType.void },
371
+ hk_config_set_background_maintenance: { args: [FFIType.ptr, FFIType.bool], returns: FFIType.void },
372
+
373
+ hk_engine_watch: { args: [FFIType.ptr, FFIType.cstring, FFIType.function, FFIType.ptr], returns: FFIType.ptr },
374
+ hk_watch_free: { args: [FFIType.ptr], returns: FFIType.void },
375
+
376
+ hk_doc_new: { args: [], returns: FFIType.ptr },
377
+ hk_doc_free: { args: [FFIType.ptr], returns: FFIType.void },
378
+ hk_doc_insert_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
379
+ hk_doc_insert_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
380
+ hk_doc_insert_float: { args: [FFIType.ptr, FFIType.cstring, FFIType.f64], returns: FFIType.i32 },
381
+ hk_doc_insert_bool: { args: [FFIType.ptr, FFIType.cstring, FFIType.bool], returns: FFIType.i32 },
382
+ hk_doc_insert_null: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
383
+ hk_doc_insert_bin: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr, FFIType.usize], returns: FFIType.i32 },
384
+ hk_doc_insert_timestamp: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
385
+ hk_doc_insert_server_timestamp: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
386
+ hk_doc_to_json: { args: [FFIType.ptr], returns: FFIType.ptr },
387
+
388
+ hk_engine_insert: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
389
+ hk_engine_get: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
390
+ hk_engine_delete: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
391
+ hk_engine_delete_local: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
392
+ hk_engine_delete_ext: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.i32], returns: FFIType.i32 },
393
+ hk_engine_restore: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
394
+ hk_engine_restore_available: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.ptr },
395
+ hk_engine_delete_queue: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.ptr },
396
+ hk_engine_sweep_now: { args: [FFIType.ptr], returns: FFIType.i32 },
397
+ hk_net_syncer_new: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
398
+ hk_net_syncer_start: { args: [FFIType.ptr, FFIType.u16], returns: FFIType.i32 },
399
+ hk_net_syncer_set_discovery: { args: [FFIType.ptr, FFIType.i32], returns: FFIType.i32 },
400
+ hk_net_syncer_status: { args: [FFIType.ptr], returns: FFIType.ptr },
401
+ hk_net_syncer_free: { args: [FFIType.ptr], returns: FFIType.void },
402
+ hk_engine_set_collection_local: { args: [FFIType.ptr, FFIType.cstring, FFIType.i32], returns: FFIType.i32 },
403
+ hk_engine_replicate_key: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
404
+ hk_engine_replicate_collection: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
405
+ hk_engine_vacuum_collection: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
406
+ hk_engine_relocate_docs: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
407
+ hk_engine_load_collection: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
408
+ hk_engine_unload_collection: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
409
+ hk_engine_unloaded_collections: { args: [FFIType.ptr], returns: FFIType.ptr },
410
+ hk_engine_patch: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
411
+ hk_engine_insert_subdoc: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
412
+ hk_engine_get_by_ref: { args: [FFIType.ptr, FFIType.ptr, FFIType.cstring], returns: FFIType.ptr },
413
+ hk_engine_create_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.u32 },
414
+ hk_engine_create_vector_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.u32], returns: FFIType.i32 },
415
+ hk_engine_find_near: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.ptr, FFIType.usize, FFIType.usize], returns: FFIType.ptr },
416
+ hk_engine_create_traverse_index: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
417
+ hk_engine_find_paths: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.cstring, FFIType.usize, FFIType.usize], returns: FFIType.ptr },
418
+ hk_engine_create_edge_index: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
419
+ hk_engine_shortest_paths: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.usize, FFIType.usize], returns: FFIType.ptr },
420
+
421
+ hk_batch_new: { args: [], returns: FFIType.ptr },
422
+
423
+ // v0.19.6 parity
424
+ hk_engine_drop_simple_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
425
+ hk_engine_drop_vector_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
426
+ hk_engine_drop_composite_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.u32], returns: FFIType.i32 },
427
+ hk_engine_drop_edge_index: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
428
+ hk_engine_drop_fts_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
429
+ hk_engine_drop_traverse_index: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
430
+ hk_engine_create_vector_index_v2: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.u32, FFIType.usize, FFIType.usize, FFIType.usize], returns: FFIType.i32 },
431
+ hk_engine_find_paths_v4: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.cstring, FFIType.cstring, FFIType.usize, FFIType.usize, FFIType.u32, FFIType.u32, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
432
+ hk_engine_find_simple_paths_v3: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.usize, FFIType.usize, FFIType.u32, FFIType.u32, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
433
+ hk_engine_export_graph: { args: [FFIType.ptr, FFIType.cstring, FFIType.u32, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
434
+ hk_engine_graph_analysis: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
435
+ hk_engine_insert_json: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
436
+ hk_batch_set_json: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
437
+ hk_batch_free: { args: [FFIType.ptr], returns: FFIType.void },
438
+ hk_batch_set: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
439
+ hk_batch_delete: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
440
+ hk_batch_delete_ext: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.i32], returns: FFIType.i32 },
441
+ hk_batch_commit: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
442
+
443
+ hk_transaction_begin: { args: [FFIType.ptr], returns: FFIType.ptr },
444
+ hk_transaction_get: { args: [FFIType.ptr, FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
445
+ hk_transaction_set: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
446
+ hk_transaction_commit: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
447
+ hk_transaction_free: { args: [FFIType.ptr], returns: FFIType.void },
448
+
449
+ hk_query_new: { args: [FFIType.cstring], returns: FFIType.ptr },
450
+ hk_query_free: { args: [FFIType.ptr], returns: FFIType.void },
451
+ hk_query_where_eq_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
452
+ hk_query_where_eq_bool: { args: [FFIType.ptr, FFIType.cstring, FFIType.bool], returns: FFIType.i32 },
453
+ hk_query_where_eq_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
454
+ hk_query_where_ne_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
455
+ hk_query_where_ne_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
456
+ hk_query_where_gt_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
457
+ hk_query_where_gt_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
458
+ hk_query_where_gte_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
459
+ hk_query_where_gte_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
460
+ hk_query_where_lt_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
461
+ hk_query_where_lt_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
462
+ hk_query_where_lte_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
463
+ hk_query_where_lte_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
464
+ hk_query_where_match: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
465
+ hk_query_where_match_prefix: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
466
+ hk_query_where_contains: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
467
+ hk_query_where_starts_with: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
468
+ hk_query_order_by: { args: [FFIType.ptr, FFIType.cstring, FFIType.bool], returns: FFIType.i32 },
469
+ hk_query_limit: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.i32 },
470
+ hk_query_offset: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.i32 },
471
+ hk_query_select_field: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
472
+ hk_query_execute: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
473
+ hk_query_delete: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
474
+ hk_query_delete_local: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
475
+ hk_query_patch: { args: [FFIType.ptr, FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
476
+ hk_query_execute_to_handles: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
477
+ hk_result_set_count: { args: [FFIType.ptr], returns: FFIType.usize },
478
+ hk_result_set_get_doc: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.ptr },
479
+ hk_result_set_free: { args: [FFIType.ptr], returns: FFIType.void },
480
+ hk_result_set_to_json: { args: [FFIType.ptr], returns: FFIType.ptr },
481
+ // Raw result sets (v0.8.3). All crossings are handles/strings/ints —
482
+ // no (ptr,len) memory reads needed (anchor + resolve by handle).
483
+ hk_query_execute_raw: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
484
+ hk_rawresult_count: { args: [FFIType.ptr], returns: FFIType.usize },
485
+ hk_rawresult_get: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.ptr },
486
+ hk_rawresult_free: { args: [FFIType.ptr], returns: FFIType.void },
487
+ hk_query_start_after_raw: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
488
+ hk_rawdoc_to_doc: { args: [FFIType.ptr, FFIType.ptr, FFIType.cstring], returns: FFIType.ptr },
489
+ hk_query_defer_blobs: { args: [FFIType.ptr, FFIType.i32], returns: FFIType.i32 },
490
+ hk_doc_resolve_blobs: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
491
+ hk_engine_insert_take: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
492
+ hk_config_set_wal_reserve_bytes: { args: [FFIType.ptr, FFIType.u64], returns: FFIType.void },
493
+
494
+ hk_query_aggregate_count: { args: [FFIType.ptr], returns: FFIType.i32 },
495
+ hk_query_aggregate_sum: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
496
+ hk_query_aggregate_avg: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
497
+ hk_query_aggregate_min: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
498
+ hk_query_aggregate_max: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
499
+ hk_query_execute_aggregation: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
500
+ hk_query_where_or_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
501
+ hk_query_where_or_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.i64], returns: FFIType.i32 },
502
+
503
+ // v0.5.9
504
+ // Indexing
505
+ hk_engine_create_fts_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
506
+ hk_engine_create_simple_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
507
+
508
+ // Array API
509
+ hk_array_new: { args: [], returns: FFIType.ptr },
510
+ hk_array_free: { args: [FFIType.ptr], returns: FFIType.void },
511
+ hk_array_append_str: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
512
+ hk_array_append_int: { args: [FFIType.ptr, FFIType.i64], returns: FFIType.i32 },
513
+ hk_array_append_doc: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
514
+
515
+ // Nested structures
516
+ hk_doc_insert_doc: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
517
+ hk_doc_insert_array: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
518
+
519
+ // Query extensions
520
+ hk_query_where_in: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
521
+ hk_query_where_not_in: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
522
+ hk_query_where_array_contains_any: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
523
+ hk_query_where_array_contains: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
524
+ hk_query_start_after: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
525
+ hk_query_start_at: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
526
+ hk_query_end_at: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
527
+ hk_query_end_before: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
528
+
529
+ hk_doc_insert_reference: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
530
+
531
+ hk_cloud_sync_new: { args: [FFIType.ptr, FFIType.i32, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
532
+ hk_cloud_sync_server_new: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
533
+ hk_cloud_sync_client_new: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
534
+ hk_cloud_sync_start: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
535
+ hk_cloud_sync_status: { args: [FFIType.ptr], returns: FFIType.ptr },
536
+ hk_cloud_sync_stop: { args: [FFIType.ptr], returns: FFIType.void },
537
+ hk_cloud_sync_free: { args: [FFIType.ptr], returns: FFIType.void },
538
+
539
+ // 0.16.5 parity: F8 schemas, F5 versioned/tx, F7 explain, F3 predicates,
540
+ // F4 pipelines, config knobs, quiescence, lazy walks and borrowed reads.
541
+ hk_config_set_group_commit_interval_ms: { args: [FFIType.ptr, FFIType.u64], returns: FFIType.void },
542
+ hk_config_set_page_cache_capacity: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.void },
543
+ hk_config_set_sync_excluded: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
544
+ hk_config_set_lazy_collections: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
545
+ hk_config_set_replication_collections: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
546
+ hk_engine_await_quiescent: { args: [FFIType.ptr, FFIType.u64], returns: FFIType.bool },
547
+ hk_engine_quiescence_status: { args: [FFIType.ptr], returns: FFIType.ptr },
548
+ hk_engine_get_versioned: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.ptr], returns: FFIType.ptr },
549
+ hk_engine_set_schema: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
550
+ hk_engine_get_schema: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.ptr },
551
+ hk_engine_clear_schema: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
552
+ hk_engine_list_schemas: { args: [FFIType.ptr], returns: FFIType.ptr },
553
+ hk_query_explain: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
554
+ hk_query_where_array_all_eq: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
555
+ hk_query_where_elem_match: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
556
+ hk_query_where_near: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr, FFIType.usize], returns: FFIType.i32 },
557
+ hk_query_where_traverse: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.cstring, FFIType.usize], returns: FFIType.i32 },
558
+ hk_pipeline_new: { args: [FFIType.cstring], returns: FFIType.ptr },
559
+ hk_pipeline_free: { args: [FFIType.ptr], returns: FFIType.void },
560
+ hk_pipeline_match: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
561
+ hk_pipeline_project: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
562
+ hk_pipeline_sort: { args: [FFIType.ptr, FFIType.cstring, FFIType.bool], returns: FFIType.i32 },
563
+ hk_pipeline_limit: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.i32 },
564
+ hk_pipeline_offset: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.i32 },
565
+ hk_pipeline_unwind: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
566
+ hk_pipeline_group: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
567
+ hk_pipeline_execute: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
568
+ hk_pipeline_execute_to_handles: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
569
+ hk_transaction_get_many: { args: [FFIType.ptr, FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
570
+ hk_transaction_patch: { args: [FFIType.ptr, FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
571
+ hk_error_last_kind: { args: [], returns: FFIType.i32 },
572
+ hk_error_is_retryable: { args: [], returns: FFIType.bool },
573
+ hk_debug_write_stats: { args: [], returns: FFIType.ptr },
574
+ hk_log_enable_stderr: { args: [], returns: FFIType.void },
575
+ hk_log_disable_stderr: { args: [], returns: FFIType.void },
576
+ hk_cursor_walk: { args: [FFIType.ptr, FFIType.ptr, FFIType.function, FFIType.ptr], returns: FFIType.i64 },
577
+ hk_cursor_walk_view: { args: [FFIType.ptr, FFIType.ptr, FFIType.function, FFIType.ptr], returns: FFIType.i64 },
578
+ hk_rawdoc_bytes: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
579
+ hk_rawdoc_id: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
580
+ hk_view_get_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.ptr },
581
+ hk_view_get_bytes: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.ptr },
582
+ hk_engine_get_json: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
583
+ hk_query_execute_json: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
584
+ hk_cursor_walk_json: { args: [FFIType.ptr, FFIType.ptr, FFIType.function, FFIType.ptr], returns: FFIType.i64 },
585
+ hk_view_to_json: { args: [FFIType.ptr], returns: FFIType.ptr },
586
+ hk_view_get_path_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.bool },
587
+ hk_view_get_path_float: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.bool },
588
+ hk_view_get_path_bool: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
589
+ hk_view_get_path_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.ptr },
590
+ hk_view_get_path_bytes: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.ptr },
591
+
592
+ // ================= ERRORS =================
593
+ hk_last_error: { args: [], returns: FFIType.ptr },
594
+ hk_string_free: { args: [FFIType.ptr], returns: FFIType.void }
595
+
596
+ }).symbols;
597
+
598
+ const toC = (s: string | null) => s ? Buffer.from(s + '\0') : null;
599
+ // ponytail: typed-array pointer for FFI out-params (i64/double reads).
600
+ // Guarded: only view getters use it, and they throw a clear error on
601
+ // ancient runtimes instead of a cryptic TypeError.
602
+ const bunPtr = (ffi as any).ptr;
603
+ const ptrOf = (arr: BigInt64Array | Float64Array): any => {
604
+ if (!bunPtr) throw new Error('bun:ffi ptr() unavailable — upgrade Bun');
605
+ return bunPtr(arr);
606
+ };
607
+ const ptrToStringAndFree = (ptr: any): string | null => {
608
+ if (!ptr) return null;
609
+ const text = new CString(ptr).toString();
610
+ symbols.hk_string_free(ptr);
611
+ return text;
612
+ };
613
+
614
+ return {
615
+ engineOpen: (path) => symbols.hk_engine_open(toC(path)),
616
+ engineIsIndexesReady: (engine) => symbols.hk_engine_is_indexes_ready(engine),
617
+ engineOpenWithConfig: (path, config) => symbols.hk_engine_open_with_config(toC(path), config),
618
+ engineFree: (engine) => symbols.hk_engine_free(engine),
619
+ engineBackup: (e, p) => symbols.hk_engine_backup(e, toC(p)),
620
+ engineCompact: (e) => symbols.hk_engine_compact(e),
621
+ engineGetStats: (e) => ptrToStringAndFree(symbols.hk_engine_get_stats(e)),
622
+ engineGetAuditLog: (e) => ptrToStringAndFree(symbols.hk_engine_get_audit_log(e)),
623
+ engineSnapshotIndices: (e) => symbols.hk_engine_snapshot_indices(e),
624
+ engineListIndexes: (e, c) => ptrToStringAndFree(symbols.hk_engine_list_indexes(e, toC(c))),
625
+
626
+ configNew: () => symbols.hk_config_new(),
627
+ configFree: (c) => symbols.hk_config_free(c),
628
+ configSetDurability: (c, m) => symbols.hk_config_set_durability(c, m),
629
+ configSetEncryptionKey: (c, k) => symbols.hk_config_set_encryption_key(c, toC(k)),
630
+ configSetEncryptedCollections: (c, json) => symbols.hk_config_set_encrypted_collections(c, toC(json)),
631
+ configSetAuditLog: (c, e, p) => symbols.hk_config_set_audit_log(c, e, toC(p)),
632
+ configSetQueryWorkers: (c, count) => symbols.hk_config_set_query_workers(c, count),
633
+ configSetMemoryLimits: (c, m, mi) => symbols.hk_config_set_memory_limits(c, m, mi),
634
+ configSetStorageTuning: (c, ps, th, gc) => symbols.hk_config_set_storage_tuning(c, ps, th, gc),
635
+ configSetBlobThreshold: (c, t) => symbols.hk_config_set_blob_threshold(c, t),
636
+ configSetCompression: (c, e, l) => symbols.hk_config_set_compression(c, e, l),
637
+ configSetBackgroundMaintenance: (c, e) => symbols.hk_config_set_background_maintenance(c, e),
638
+
639
+ engineWatch: (engine, collection, callback) => {
640
+ // The engine calls the snapshot callback from its native watch
641
+ // thread — bun requires a threadsafe trampoline for that (marshaled
642
+ // onto the JS thread; issue #21).
643
+ const cb = new JSCallback((c: any, p: any, kind: number) => {
644
+ callback(new CString(c).toString(), new CString(p).toString(), kind);
645
+ }, { args: [FFIType.ptr, FFIType.ptr, FFIType.i32], returns: FFIType.void, threadsafe: true });
646
+ const watch = symbols.hk_engine_watch(engine, toC(collection), cb, null);
647
+ watchTrampolines.set(watch, cb);
648
+ return watch;
649
+ },
650
+ watchFree: (watch) => {
651
+ // hk_watch_free joins the watch thread — no invocation can follow,
652
+ // so the trampoline is dead and can be closed + dropped.
653
+ symbols.hk_watch_free(watch);
654
+ const cb = watchTrampolines.get(watch);
655
+ if (cb) {
656
+ cb.close();
657
+ watchTrampolines.delete(watch);
658
+ }
659
+ },
660
+
661
+ docNew: () => symbols.hk_doc_new(),
662
+ docFree: (doc) => symbols.hk_doc_free(doc),
663
+ docInsertStr: (doc, key, value) => symbols.hk_doc_insert_str(doc, toC(key), toC(value)),
664
+ docInsertInt: (doc, key, value) => symbols.hk_doc_insert_int(doc, toC(key), BigInt(value)),
665
+ docInsertFloat: (doc, key, value) => symbols.hk_doc_insert_float(doc, toC(key), value),
666
+ docInsertBool: (doc, key, value) => symbols.hk_doc_insert_bool(doc, toC(key), value),
667
+ docInsertNull: (doc, key) => symbols.hk_doc_insert_null(doc, toC(key)),
668
+ docInsertBin: (doc, key, bytes) => symbols.hk_doc_insert_bin(doc, toC(key), bytes, bytes.byteLength),
669
+ docInsertTimestamp: (doc, key, micros) => symbols.hk_doc_insert_timestamp(doc, toC(key), micros),
670
+ docInsertServerTimestamp: (doc, key) => symbols.hk_doc_insert_server_timestamp(doc, toC(key)),
671
+ docInsertReference: (doc, key, tc, tid) => symbols.hk_doc_insert_reference(doc, toC(key), toC(tc), toC(tid)),
672
+ docToJson: (doc) => ptrToStringAndFree(symbols.hk_doc_to_json(doc)),
673
+
674
+ engineInsert: (engine, collection, docId, doc) => symbols.hk_engine_insert(engine, toC(collection), toC(docId), doc),
675
+ engineGet: (engine, collection, docId) => symbols.hk_engine_get(engine, toC(collection), toC(docId)),
676
+ engineDelete: (engine, collection, docId) => symbols.hk_engine_delete(engine, toC(collection), toC(docId)),
677
+ engineDeleteLocal: (engine, collection, docId) => symbols.hk_engine_delete_local(engine, toC(collection), toC(docId)),
678
+ engineDeleteEx: (engine, collection, docId, force) => symbols.hk_engine_delete_ext(engine, toC(collection), toC(docId), force ? 1 : 0),
679
+ engineRestore: (engine, collection, docId) => symbols.hk_engine_restore(engine, toC(collection), toC(docId)),
680
+ engineRestoreAvailable: (engine, collection) => ptrToStringAndFree(symbols.hk_engine_restore_available(engine, (collection ? toC(collection) : null) as unknown as string)),
681
+ engineDeleteQueue: (engine, collection) => ptrToStringAndFree(symbols.hk_engine_delete_queue(engine, (collection ? toC(collection) : null) as unknown as string)),
682
+ engineSweepNow: (engine) => symbols.hk_engine_sweep_now(engine),
683
+ engineSetCollectionLocal: (engine, collection, local) => symbols.hk_engine_set_collection_local(engine, toC(collection), local),
684
+ engineReplicateKey: (engine, collection, docId) => symbols.hk_engine_replicate_key(engine, toC(collection), toC(docId)),
685
+ engineReplicateCollection: (engine, collection) => symbols.hk_engine_replicate_collection(engine, toC(collection)),
686
+ engineVacuumCollection: (engine, collection) => symbols.hk_engine_vacuum_collection(engine, toC(collection)),
687
+ engineRelocateDocs: (engine, src, dst, idsJson) => ptrToStringAndFree(symbols.hk_engine_relocate_docs(engine, toC(src), toC(dst), toC(idsJson))),
688
+ engineLoadCollection: (engine, collection) => symbols.hk_engine_load_collection(engine, toC(collection)),
689
+ engineUnloadCollection: (engine, collection) => symbols.hk_engine_unload_collection(engine, toC(collection)),
690
+ engineUnloadedCollections: (engine) => ptrToStringAndFree(symbols.hk_engine_unloaded_collections(engine)),
691
+ enginePatch: (engine, collection, docId, updates) => symbols.hk_engine_patch(engine, toC(collection), toC(docId), updates),
692
+ engineInsertSubDoc: (engine, col, id, subCol, subId, doc) => symbols.hk_engine_insert_subdoc(engine, toC(col), toC(id), toC(subCol), toC(subId), doc),
693
+ engineGetByRef: (engine, doc, fieldKey) => symbols.hk_engine_get_by_ref(engine, doc, toC(fieldKey)),
694
+ engineCreateIndex: (engine, collection, fieldsJson) => symbols.hk_engine_create_index(engine, toC(collection), toC(fieldsJson)),
695
+
696
+ batchNew: () => symbols.hk_batch_new(),
697
+ batchFree: (batch) => symbols.hk_batch_free(batch),
698
+ batchSet: (batch, collection, docId, doc) => symbols.hk_batch_set(batch, toC(collection), toC(docId), doc),
699
+ batchDelete: (batch, collection, docId) => symbols.hk_batch_delete(batch, toC(collection), toC(docId)),
700
+ batchDeleteEx: (batch, collection, docId, force) => symbols.hk_batch_delete_ext(batch, toC(collection), toC(docId), force ? 1 : 0),
701
+ batchCommit: (engine, batch) => symbols.hk_batch_commit(engine, batch),
702
+
703
+ transactionBegin: (engine) => symbols.hk_transaction_begin(engine),
704
+ transactionGet: (engine, tx, collection, docId) => symbols.hk_transaction_get(engine, tx, toC(collection), toC(docId)),
705
+ transactionSet: (tx, collection, docId, doc) => symbols.hk_transaction_set(tx, toC(collection), toC(docId), doc),
706
+ transactionCommit: (engine, tx) => symbols.hk_transaction_commit(engine, tx),
707
+ transactionFree: (tx) => symbols.hk_transaction_free(tx),
708
+
709
+ queryNew: (collection) => symbols.hk_query_new(toC(collection)),
710
+ queryFree: (query) => symbols.hk_query_free(query),
711
+ queryWhereEqStr: (query, field, value) => symbols.hk_query_where_eq_str(query, toC(field), toC(value)),
712
+ queryWhereEqBool: (query, field, value) => symbols.hk_query_where_eq_bool(query, toC(field), value),
713
+ queryWhereEqInt: (query, field, value) => symbols.hk_query_where_eq_int(query, toC(field), BigInt(value)),
714
+ queryWhereNeStr: (query, field, value) => symbols.hk_query_where_ne_str(query, toC(field), toC(value)),
715
+ queryWhereNeInt: (query, field, value) => symbols.hk_query_where_ne_int(query, toC(field), BigInt(value)),
716
+ queryWhereGtStr: (query, field, value) => symbols.hk_query_where_gt_str(query, toC(field), toC(value)),
717
+ queryWhereGtInt: (query, field, value) => symbols.hk_query_where_gt_int(query, toC(field), BigInt(value)),
718
+ queryWhereGteStr: (query, field, value) => symbols.hk_query_where_gte_str(query, toC(field), toC(value)),
719
+ queryWhereGteInt: (query, field, value) => symbols.hk_query_where_gte_int(query, toC(field), BigInt(value)),
720
+ queryWhereLtStr: (query, field, value) => symbols.hk_query_where_lt_str(query, toC(field), toC(value)),
721
+ queryWhereLtInt: (query, field, value) => symbols.hk_query_where_lt_int(query, toC(field), BigInt(value)),
722
+ queryWhereLteStr: (query, field, value) => symbols.hk_query_where_lte_str(query, toC(field), toC(value)),
723
+ queryWhereLteInt: (query, field, value) => symbols.hk_query_where_lte_int(query, toC(field), BigInt(value)),
724
+ queryWhereMatch: (query, field, value) => symbols.hk_query_where_match(query, toC(field), toC(value)),
725
+ queryWhereMatchPrefix: (query, field, value) => symbols.hk_query_where_match_prefix(query, toC(field), toC(value)),
726
+ queryWhereContains: (query, field, value) => symbols.hk_query_where_contains(query, toC(field), toC(value)),
727
+ queryWhereStartsWith: (query, field, value) => symbols.hk_query_where_starts_with(query, toC(field), toC(value)),
728
+ queryWhereOrStr: (query, field, value) => symbols.hk_query_where_or_str(query, toC(field), toC(value)),
729
+ queryWhereOrInt: (query, field, value) => symbols.hk_query_where_or_int(query, toC(field), BigInt(value)),
730
+ queryOrderBy: (query, field, asc) => symbols.hk_query_order_by(query, toC(field), asc),
731
+ queryLimit: (query, limit) => symbols.hk_query_limit(query, limit),
732
+ queryOffset: (query, offset) => symbols.hk_query_offset(query, offset),
733
+ querySelectField: (query, field) => symbols.hk_query_select_field(query, toC(field)),
734
+ queryExecute: (engine, query) => ptrToStringAndFree(symbols.hk_query_execute(engine, query)),
735
+ queryDelete: (engine, query) => symbols.hk_query_delete(engine, query),
736
+ queryDeleteLocal: (engine, query) => symbols.hk_query_delete_local(engine, query),
737
+ queryPatch: (engine, query, patchDoc) => symbols.hk_query_patch(engine, query, patchDoc),
738
+ queryExecuteToHandles: (engine, query) => symbols.hk_query_execute_to_handles(engine, query),
739
+ resultSetCount: (results) => symbols.hk_result_set_count(results),
740
+ resultSetGetDoc: (results, index) => symbols.hk_result_set_get_doc(results, index),
741
+ resultSetFree: (results) => symbols.hk_result_set_free(results),
742
+ resultSetToJson: (results) => ptrToStringAndFree(symbols.hk_result_set_to_json(results)),
743
+ queryExecuteRaw: (engine, query) => symbols.hk_query_execute_raw(engine, query),
744
+ rawResultCount: (results) => symbols.hk_rawresult_count(results),
745
+ rawResultGet: (results, index) => symbols.hk_rawresult_get(results, index),
746
+ rawResultFree: (results) => symbols.hk_rawresult_free(results),
747
+ queryStartAfterRaw: (query, anchorRawDoc) => symbols.hk_query_start_after_raw(query, anchorRawDoc),
748
+ rawDocToDoc: (engine, rawDoc, collection) => symbols.hk_rawdoc_to_doc(engine, rawDoc, toC(collection)),
749
+ viewGet: (engine, collection, docId) => symbols.hk_view_get(engine, toC(collection), toC(docId)),
750
+ viewFree: (view) => symbols.hk_view_free(view),
751
+ viewFieldCount: (view) => symbols.hk_view_field_count(view),
752
+ viewHasField: (view, key) => symbols.hk_view_has_field(view, toC(key)),
753
+ viewGetInt: (view, key) => {
754
+ const out = new BigInt64Array(1);
755
+ return symbols.hk_view_get_int(view, toC(key), ptrOf(out)) ? out[0] : null;
756
+ },
757
+ viewGetFloat: (view, key) => {
758
+ const out = new Float64Array(1);
759
+ return symbols.hk_view_get_float(view, toC(key), ptrOf(out)) ? out[0] : null;
760
+ },
761
+ viewGetBool: (view, key) => {
762
+ const r = symbols.hk_view_get_bool(view, toC(key));
763
+ return r < 0 ? null : r !== 0;
764
+ },
765
+ viewToDoc: (view, docId) => symbols.hk_view_to_doc(view, toC(docId)),
766
+ queryDeferBlobs: (query, defer) => symbols.hk_query_defer_blobs(query, defer ? 1 : 0),
767
+ docResolveBlobs: (engine, collection, doc) => symbols.hk_doc_resolve_blobs(engine, toC(collection), doc),
768
+ engineInsertTake: (engine, collection, docId, doc) => symbols.hk_engine_insert_take(engine, toC(collection), toC(docId), doc),
769
+ configSetWalReserveBytes: (c, bytes) => symbols.hk_config_set_wal_reserve_bytes(c, bytes),
770
+
771
+ queryAggregateCount: (q) => symbols.hk_query_aggregate_count(q),
772
+ queryAggregateSum: (q, f) => symbols.hk_query_aggregate_sum(q, toC(f)),
773
+ queryAggregateAvg: (q, f) => symbols.hk_query_aggregate_avg(q, toC(f)),
774
+ queryAggregateMin: (q, f) => symbols.hk_query_aggregate_min(q, toC(f)),
775
+ queryAggregateMax: (q, f) => symbols.hk_query_aggregate_max(q, toC(f)),
776
+ queryExecuteAggregation: (e, q) => ptrToStringAndFree(symbols.hk_query_execute_aggregation(e, q)),
777
+ engineListCollections: (engine) => ptrToStringAndFree(symbols.hk_engine_list_collections(engine)),
778
+ netSyncerNew: (engine, name, roomKey) => symbols.hk_net_syncer_new(engine, toC(name), toC(roomKey)),
779
+ netSyncerStart: (syncer, port) => symbols.hk_net_syncer_start(syncer, port),
780
+ netSyncerSetDiscovery: (syncer, mode) => symbols.hk_net_syncer_set_discovery(syncer, mode),
781
+ netSyncerStatus: (syncer) => ptrToStringAndFree(symbols.hk_net_syncer_status(syncer)),
782
+ netSyncerFree: (syncer) => symbols.hk_net_syncer_free(syncer),
783
+
784
+ cloudSyncNew: (engine, mode, clientId, roomName, roomKey, authToken) => symbols.hk_cloud_sync_new(engine, mode, toC(clientId), toC(roomName), toC(roomKey), toC(authToken)),
785
+ cloudSyncServerNew: (engine, serverId, authToken, roomName, roomKey) => symbols.hk_cloud_sync_server_new(engine, toC(serverId), toC(authToken), toC(roomName), toC(roomKey)),
786
+ cloudSyncClientNew: (engine, clientId, roomName, roomKey, authToken) => symbols.hk_cloud_sync_client_new(engine, toC(clientId), toC(roomName), toC(roomKey), toC(authToken)),
787
+ cloudSyncStart: (cs, address) => symbols.hk_cloud_sync_start(cs, toC(address)),
788
+ cloudSyncStatus: (cs) => ptrToStringAndFree(symbols.hk_cloud_sync_status(cs)),
789
+ cloudSyncStop: (cs) => symbols.hk_cloud_sync_stop(cs),
790
+ cloudSyncFree: (cs) => symbols.hk_cloud_sync_free(cs),
791
+
792
+ // ================= v0.5.9 =================
793
+
794
+ // Indexing
795
+ createFtsIndex: (engine, collection, field) => symbols.hk_engine_create_fts_index(engine, toC(collection), toC(field)),
796
+ createSimpleIndex: (engine, collection, field) => symbols.hk_engine_create_simple_index(engine, toC(collection), toC(field)),
797
+ createVectorIndex: (engine, collection, field, dim, metric) => symbols.hk_engine_create_vector_index(engine, toC(collection), toC(field), dim, metric),
798
+ // Float32Array crosses as its LE-f32 bytes (bun:ffi takes the typed
799
+ // array as ptr, like docInsertBin takes bytes).
800
+ engineFindNear: (engine, collection, field, vec, k) => ptrToStringAndFree(symbols.hk_engine_find_near(engine, toC(collection), toC(field), vec, vec.length, k)),
801
+ createTraverseIndex: (engine, collection) => symbols.hk_engine_create_traverse_index(engine, toC(collection)),
802
+ // Null edge crosses as NULL (any ref field); bun:ffi takes null for ptr/cstring.
803
+ engineFindPaths: (engine, collection, id, direction, edge, maxDepth, k) => ptrToStringAndFree(symbols.hk_engine_find_paths(engine, toC(collection), toC(id), direction, edge === null ? null : toC(edge), maxDepth, k)),
804
+ createEdgeIndex: (engine, collection) => symbols.hk_engine_create_edge_index(engine, toC(collection)),
805
+ engineShortestPaths: (engine, collection, id, direction, edge, via, weight, maxDepth, k) => ptrToStringAndFree(symbols.hk_engine_shortest_paths(engine, toC(collection), toC(id), direction, edge === null ? null : toC(edge), via === null ? null : toC(via), weight === null ? null : toC(weight), maxDepth, k)),
806
+
807
+ // v0.19.6 parity
808
+ dropSimpleIndex: (engine, collection, field) => symbols.hk_engine_drop_simple_index(engine, toC(collection), toC(field)),
809
+ dropVectorIndex: (engine, collection, field) => symbols.hk_engine_drop_vector_index(engine, toC(collection), toC(field)),
810
+ dropCompositeIndex: (engine, collection, id) => symbols.hk_engine_drop_composite_index(engine, toC(collection), id),
811
+ dropEdgeIndex: (engine, collection) => symbols.hk_engine_drop_edge_index(engine, toC(collection)),
812
+ dropFtsIndex: (engine, collection, field) => symbols.hk_engine_drop_fts_index(engine, toC(collection), toC(field)),
813
+ dropTraverseIndex: (engine, collection) => symbols.hk_engine_drop_traverse_index(engine, toC(collection)),
814
+ createVectorIndexV2: (engine, collection, field, dim, metric, m, efConstruction, efSearch) => symbols.hk_engine_create_vector_index_v2(engine, toC(collection), toC(field), dim, metric, m, efConstruction, efSearch),
815
+ findPathsFull: (engine, collection, id, direction, edge, via, maxDepth, k, includePath, includeVia, nodeField, nodeJSON, typeField, typeJSON, matchField, matchJSON) => ptrToStringAndFree(symbols.hk_engine_find_paths_v4(engine, toC(collection), toC(id), direction, edge === null ? null : toC(edge), via === null ? null : toC(via), maxDepth, k, includePath ? 1 : 0, includeVia ? 1 : 0, nodeField === null ? null : toC(nodeField), nodeJSON === null ? null : toC(nodeJSON), typeField === null ? null : toC(typeField), typeJSON === null ? null : toC(typeJSON), matchField === null ? null : toC(matchField), matchJSON === null ? null : toC(matchJSON))),
816
+ simplePathsFull: (engine, collection, id, direction, edge, via, weight, maxDepth, k, includePath, includeVia, nodeField, nodeJSON, typeField, typeJSON, matchField, matchJSON) => ptrToStringAndFree(symbols.hk_engine_find_simple_paths_v3(engine, toC(collection), toC(id), direction, edge === null ? null : toC(edge), via === null ? null : toC(via), weight === null ? null : toC(weight), maxDepth, k, includePath ? 1 : 0, includeVia ? 1 : 0, nodeField === null ? null : toC(nodeField), nodeJSON === null ? null : toC(nodeJSON), typeField === null ? null : toC(typeField), typeJSON === null ? null : toC(typeJSON), matchField === null ? null : toC(matchField), matchJSON === null ? null : toC(matchJSON))),
817
+ exportGraph: (engine, collection, direction, edge, via, nodeField, nodeJSON) => ptrToStringAndFree(symbols.hk_engine_export_graph(engine, toC(collection), direction, edge === null ? null : toC(edge), via === null ? null : toC(via), nodeField === null ? null : toC(nodeField), nodeJSON === null ? null : toC(nodeJSON))),
818
+ graphAnalysis: (engine, collection, op, direction, edge, via, nodeField, nodeJSON) => ptrToStringAndFree(symbols.hk_engine_graph_analysis(engine, toC(collection), toC(op), direction, edge === null ? null : toC(edge), via === null ? null : toC(via), nodeField === null ? null : toC(nodeField), nodeJSON === null ? null : toC(nodeJSON))),
819
+ insertJSON: (engine, collection, id, json) => symbols.hk_engine_insert_json(engine, toC(collection), toC(id), toC(json)),
820
+ batchSetJSON: (batch, collection, id, json) => symbols.hk_batch_set_json(batch, toC(collection), toC(id), toC(json)),
821
+
822
+ // Array API
823
+ arrayNew: () => symbols.hk_array_new(),
824
+ arrayFree: (arr) => symbols.hk_array_free(arr),
825
+ arrayAppendStr: (arr, value) => symbols.hk_array_append_str(arr, toC(value)),
826
+ arrayAppendInt: (arr, value) => symbols.hk_array_append_int(arr, BigInt(value)),
827
+ arrayAppendDoc: (arr, doc) => symbols.hk_array_append_doc(arr, doc),
828
+
829
+ // Nested
830
+ docInsertDoc: (parent, key, child) => symbols.hk_doc_insert_doc(parent, toC(key), child),
831
+ docInsertArray: (parent, key, arr) => symbols.hk_doc_insert_array(parent, toC(key), arr),
832
+
833
+ // Query extensions
834
+ queryWhereIn: (query, field, arr) => symbols.hk_query_where_in(query, toC(field), arr),
835
+ queryWhereNotIn: (query, field, arr) => symbols.hk_query_where_not_in(query, toC(field), arr),
836
+ queryWhereArrayContainsAny: (query, field, arr) => symbols.hk_query_where_array_contains_any(query, toC(field), arr),
837
+ queryWhereArrayContainsStr: (query, field, value) => symbols.hk_query_where_array_contains(query, toC(field), toC(String(value))),
838
+ queryWhereArrayContainsInt: (query, field, value) => symbols.hk_query_where_array_contains(query, toC(field), toC(String(value))),
839
+ queryStartAfter: (query, anchorDoc) => symbols.hk_query_start_after(query, anchorDoc),
840
+ queryStartAt: (query, anchorDoc) => symbols.hk_query_start_at(query, anchorDoc),
841
+ queryEndAt: (query, anchorDoc) => symbols.hk_query_end_at(query, anchorDoc),
842
+ queryEndBefore: (query, anchorDoc) => symbols.hk_query_end_before(query, anchorDoc),
843
+
844
+ // 0.16.5 parity.
845
+ engineSetSchema: (e, c, json) => symbols.hk_engine_set_schema(e, toC(c), toC(json)),
846
+ engineGetSchema: (e, c) => ptrToStringAndFree(symbols.hk_engine_get_schema(e, toC(c))),
847
+ engineClearSchema: (e, c) => symbols.hk_engine_clear_schema(e, toC(c)),
848
+ engineListSchemas: (e) => ptrToStringAndFree(symbols.hk_engine_list_schemas(e)),
849
+ engineGetVersioned: (e, c, id) => {
850
+ const out = new BigInt64Array(1);
851
+ const doc = symbols.hk_engine_get_versioned(e, toC(c), toC(id), ptrOf(out));
852
+ return { doc, version: out[0] };
853
+ },
854
+ engineAwaitQuiescent: (e, ms) => symbols.hk_engine_await_quiescent(e, BigInt(ms)),
855
+ engineQuiescenceStatus: (e) => ptrToStringAndFree(symbols.hk_engine_quiescence_status(e)),
856
+ transactionGetMany: (e, tx, c, idsJson) => ptrToStringAndFree(symbols.hk_transaction_get_many(e, tx, toC(c), toC(idsJson))),
857
+ transactionPatch: (e, tx, c, id, updatesJson) => symbols.hk_transaction_patch(e, tx, toC(c), toC(id), toC(updatesJson)),
858
+ errorLastKind: () => symbols.hk_error_last_kind(),
859
+ errorIsRetryable: () => symbols.hk_error_is_retryable(),
860
+ debugWriteStats: () => ptrToStringAndFree(symbols.hk_debug_write_stats()),
861
+ logEnableStderr: () => symbols.hk_log_enable_stderr(),
862
+ logDisableStderr: () => symbols.hk_log_disable_stderr(),
863
+ queryExplain: (e, q) => ptrToStringAndFree(symbols.hk_query_explain(e, q)),
864
+ queryWhereArrayAllEq: (q, f, json) => symbols.hk_query_where_array_all_eq(q, toC(f), toC(json)),
865
+ queryWhereElemMatch: (q, f, json) => symbols.hk_query_where_elem_match(q, toC(f), toC(json)),
866
+ queryWhereNear: (q, f, vec) => symbols.hk_query_where_near(q, toC(f), vec, vec.length),
867
+ queryWhereTraverse: (q, sc, sid, dir, edge, depth) => symbols.hk_query_where_traverse(q, toC(sc), toC(sid), dir, edge === null ? null : toC(edge), depth),
868
+ pipelineNew: (c) => symbols.hk_pipeline_new(toC(c)),
869
+ pipelineFree: (p) => symbols.hk_pipeline_free(p),
870
+ pipelineMatch: (p, q) => symbols.hk_pipeline_match(p, q),
871
+ pipelineProject: (p, json) => symbols.hk_pipeline_project(p, toC(json)),
872
+ pipelineSort: (p, f, asc) => symbols.hk_pipeline_sort(p, toC(f), asc),
873
+ pipelineLimit: (p, n) => symbols.hk_pipeline_limit(p, n),
874
+ pipelineOffset: (p, n) => symbols.hk_pipeline_offset(p, n),
875
+ pipelineUnwind: (p, f) => symbols.hk_pipeline_unwind(p, toC(f)),
876
+ pipelineGroup: (p, k, json) => symbols.hk_pipeline_group(p, toC(k), toC(json)),
877
+ pipelineExecute: (e, p) => ptrToStringAndFree(symbols.hk_pipeline_execute(e, p)),
878
+ pipelineExecuteToHandles: (e, p) => symbols.hk_pipeline_execute_to_handles(e, p),
879
+ configSetGroupCommitIntervalMs: (c, ms) => symbols.hk_config_set_group_commit_interval_ms(c, BigInt(ms)),
880
+ configSetPageCacheCapacity: (c, n) => symbols.hk_config_set_page_cache_capacity(c, n),
881
+ configSetSyncExcluded: (c, json) => symbols.hk_config_set_sync_excluded(c, toC(json)),
882
+ configSetLazyCollections: (c, json) => symbols.hk_config_set_lazy_collections(c, toC(json)),
883
+ configSetReplicationCollections: (c, json) => symbols.hk_config_set_replication_collections(c, toC(json)),
884
+ cursorWalk: (e, q, callback) => {
885
+ const cb = new JSCallback((idPtr: any, idLen: number, bytesPtr: any, bytesLen: number) => {
886
+ const id = new TextDecoder().decode(new Uint8Array(toArrayBuffer(idPtr, 0, Number(idLen))).slice());
887
+ const bytes = new Uint8Array(toArrayBuffer(bytesPtr, 0, Number(bytesLen))).slice();
888
+ return callback(id, bytes);
889
+ }, { args: [FFIType.ptr, FFIType.usize, FFIType.ptr, FFIType.usize, FFIType.ptr], returns: FFIType.bool });
890
+ return Number(symbols.hk_cursor_walk(e, q, cb, null));
891
+ },
892
+ cursorWalkView: (e, q, callback) => {
893
+ const cb = new JSCallback((idPtr: any, idLen: number, view: any) => {
894
+ const id = new TextDecoder().decode(new Uint8Array(toArrayBuffer(idPtr, 0, Number(idLen))).slice());
895
+ return callback(id, view);
896
+ }, { args: [FFIType.ptr, FFIType.usize, FFIType.ptr, FFIType.ptr], returns: FFIType.bool });
897
+ return Number(symbols.hk_cursor_walk_view(e, q, cb, null));
898
+ },
899
+ rawDocBytes: (raw) => {
900
+ const len = new BigInt64Array(1);
901
+ const ptr = symbols.hk_rawdoc_bytes(raw, ptrOf(len));
902
+ return ptr ? new Uint8Array(toArrayBuffer(ptr, 0, Number(len[0]))).slice() : null;
903
+ },
904
+ rawDocId: (raw) => {
905
+ const len = new BigInt64Array(1);
906
+ const ptr = symbols.hk_rawdoc_id(raw, ptrOf(len));
907
+ return ptr ? new TextDecoder().decode(new Uint8Array(toArrayBuffer(ptr, 0, Number(len[0]))).slice()) : null;
908
+ },
909
+ viewGetStr: (view, key) => {
910
+ const len = new BigInt64Array(1);
911
+ const ptr = symbols.hk_view_get_str(view, toC(key), ptrOf(len));
912
+ return ptr ? new TextDecoder().decode(new Uint8Array(toArrayBuffer(ptr, 0, Number(len[0]))).slice()) : null;
913
+ },
914
+ viewGetBytes: (view, key) => {
915
+ const len = new BigInt64Array(1);
916
+ const ptr = symbols.hk_view_get_bytes(view, toC(key), ptrOf(len));
917
+ return ptr ? new Uint8Array(toArrayBuffer(ptr, 0, Number(len[0]))).slice() : null;
918
+ },
919
+
920
+ // P2 (core 0.18): JSON read paths + dotted-path pulls.
921
+ engineGetJson: (e, c, id) => ptrToStringAndFree(symbols.hk_engine_get_json(e, toC(c), toC(id))),
922
+ queryExecuteJson: (e, q) => ptrToStringAndFree(symbols.hk_query_execute_json(e, q)),
923
+ viewToJson: (view) => ptrToStringAndFree(symbols.hk_view_to_json(view)),
924
+ cursorWalkJson: (e, q, callback) => {
925
+ const cb = new JSCallback((idPtr: any, idLen: number, jsonPtr: any, jsonLen: number) => {
926
+ const id = new TextDecoder().decode(new Uint8Array(toArrayBuffer(idPtr, 0, Number(idLen))).slice());
927
+ const json = new Uint8Array(toArrayBuffer(jsonPtr, 0, Number(jsonLen))).slice();
928
+ return callback(id, json);
929
+ }, { args: [FFIType.ptr, FFIType.usize, FFIType.ptr, FFIType.usize, FFIType.ptr], returns: FFIType.bool });
930
+ return Number(symbols.hk_cursor_walk_json(e, q, cb, null));
931
+ },
932
+ viewGetPathInt: (view, path) => {
933
+ const out = new BigInt64Array(1);
934
+ return symbols.hk_view_get_path_int(view, toC(path), ptrOf(out)) ? out[0] : null;
935
+ },
936
+ viewGetPathFloat: (view, path) => {
937
+ const out = new Float64Array(1);
938
+ return symbols.hk_view_get_path_float(view, toC(path), ptrOf(out)) ? out[0] : null;
939
+ },
940
+ viewGetPathBool: (view, path) => {
941
+ const r = symbols.hk_view_get_path_bool(view, toC(path));
942
+ return r < 0 ? null : r !== 0;
943
+ },
944
+ viewGetPathStr: (view, path) => {
945
+ const len = new BigInt64Array(1);
946
+ const ptr = symbols.hk_view_get_path_str(view, toC(path), ptrOf(len));
947
+ return ptr ? new TextDecoder().decode(new Uint8Array(toArrayBuffer(ptr, 0, Number(len[0]))).slice()) : null;
948
+ },
949
+ viewGetPathBytes: (view, path) => {
950
+ const len = new BigInt64Array(1);
951
+ const ptr = symbols.hk_view_get_path_bytes(view, toC(path), ptrOf(len));
952
+ return ptr ? new Uint8Array(toArrayBuffer(ptr, 0, Number(len[0]))).slice() : null;
953
+ },
954
+
955
+ lastError: () => {
956
+ const ptr = symbols.hk_last_error();
957
+ return ptr ? new CString(ptr).toString() : 'unknown ffi error';
958
+ }
959
+ };
960
+ }
961
+
962
+ async function createNodeBindings(libPath: string): Promise<NativeBindings> {
963
+ const koffiModule = await import('koffi');
964
+ const koffi: any = (koffiModule as any).default ?? koffiModule;
965
+ const lib = koffi.load(libPath);
966
+
967
+ // ponytail: koffi needs opaque struct declarations up front — without
968
+ // these EVERY struct-typed decl throws at bindings creation, i.e. the
969
+ // node backend was dead on arrival (bun resolves pointers structurally
970
+ // and never noticed). Declared once here for all present and future fns.
971
+ for (const t of ['HK_Array', 'HK_Batch', 'HK_CloudSync', 'HK_Config', 'HK_Doc', 'HK_Engine', 'HK_NetSyncer', 'HK_Pipeline', 'HK_Query', 'HK_RawDoc', 'HK_RawResultSet', 'HK_ResultSet', 'HK_Transaction', 'HK_ViewDoc', 'HK_Watch']) koffi.opaque(t);
972
+
973
+ // ponytail: owned C strings (Rust-allocated) must come back through a
974
+ // DISPOSABLE type wired to hk_string_free — never bare char* (koffi
975
+ // auto-decodes to a JS string and the native buffer leaks) and never
976
+ // default koffi.free (wrong allocator: Rust memory freed by C free is
977
+ // UB). Declared before the table; the closure runs post-init.
978
+ const flStringFreeRaw = lib.func('void hk_string_free(void* value)');
979
+ const HeapStr = koffi.disposable('HeapStr', 'str', (ptr: any) => { flStringFreeRaw(ptr); });
980
+
981
+ // Canonical callback signature (matches HK_OnSnapshotCallback in
982
+ // hako.h). Kept as documentation; register() below takes the
983
+ // typedef NAME as a string, not this proto object.
984
+ const OnSnapshotCB = koffi.proto('void HK_OnSnapshotCallback(const char *collection, const char *path, int32_t kind, void *user_data)');
985
+ // ids are length-delimited (not NUL-terminated): take them as raw bytes.
986
+ const WalkCB = koffi.proto('bool HkWalkCallback(const uint8_t *id, size_t id_len, const uint8_t *bytes, size_t bytes_len, void *userdata)');
987
+ const ViewWalkCB = koffi.proto('bool HkViewWalkCallback(const uint8_t *id, size_t id_len, const HK_ViewDoc *view, void *userdata)');
988
+
989
+ const fn = {
990
+ hk_engine_open: lib.func('HK_Engine* hk_engine_open(const char* path)'),
991
+ hk_engine_is_indexes_ready: lib.func('bool hk_engine_is_indexes_ready(HK_Engine* engine)'),
992
+ hk_engine_open_with_config: lib.func('HK_Engine* hk_engine_open_with_config(const char* path, HK_Config* config)'),
993
+ hk_engine_free: lib.func('void hk_engine_free(HK_Engine* engine)'),
994
+ hk_engine_backup: lib.func('int hk_engine_backup(HK_Engine* engine, const char* path)'),
995
+ hk_engine_compact: lib.func('int hk_engine_compact(HK_Engine* engine)'),
996
+ // ponytail: owned-string returns are HeapStr (auto-freed via
997
+ // hk_string_free by the disposable — never bare char*, which leaks,
998
+ // and never default koffi.free (wrong allocator for Rust memory).
999
+ // hk_last_error stays char* (TLS, never freed).
1000
+ hk_engine_get_stats: lib.func('HeapStr hk_engine_get_stats(HK_Engine* engine)'),
1001
+ hk_engine_get_audit_log: lib.func('HeapStr hk_engine_get_audit_log(HK_Engine* engine)'),
1002
+ hk_engine_snapshot_indices: lib.func('int hk_engine_snapshot_indices(HK_Engine* engine)'),
1003
+ hk_engine_list_indexes: lib.func('HeapStr hk_engine_list_indexes(HK_Engine* engine, const char* collection)'),
1004
+
1005
+ hk_config_new: lib.func('HK_Config* hk_config_new()'),
1006
+ hk_config_free: lib.func('void hk_config_free(HK_Config* config)'),
1007
+ hk_config_set_durability: lib.func('void hk_config_set_durability(HK_Config* config, int32_t mode)'),
1008
+ hk_config_set_encryption_key: lib.func('void hk_config_set_encryption_key(HK_Config* config, const char* key)'),
1009
+ hk_config_set_encrypted_collections: lib.func('int hk_config_set_encrypted_collections(HK_Config* config, const char* collections_json)'),
1010
+ hk_config_set_audit_log: lib.func('void hk_config_set_audit_log(HK_Config* config, bool enabled, const char* path)'),
1011
+ hk_config_set_query_workers: lib.func('void hk_config_set_query_workers(HK_Config* config, size_t count)'),
1012
+ hk_config_set_memory_limits: lib.func('void hk_config_set_memory_limits(HK_Config* config, size_t mmap_size, size_t max_inlined_bytes)'),
1013
+ hk_config_set_storage_tuning: lib.func('void hk_config_set_storage_tuning(HK_Config* config, size_t page_size, size_t compaction_threshold, size_t group_commit_max_ops)'),
1014
+ hk_config_set_blob_threshold: lib.func('void hk_config_set_blob_threshold(HK_Config* config, size_t threshold_bytes)'),
1015
+ hk_config_set_compression: lib.func('void hk_config_set_compression(HK_Config* config, bool enabled, int32_t level)'),
1016
+ hk_config_set_background_maintenance: lib.func('void hk_config_set_background_maintenance(HK_Config* config, bool enabled)'),
1017
+
1018
+ hk_engine_watch: lib.func('HK_Watch* hk_engine_watch(HK_Engine* engine, const char* collection, HK_OnSnapshotCallback* callback, void* user_data)'),
1019
+ hk_watch_free: lib.func('void hk_watch_free(HK_Watch* watch)'),
1020
+
1021
+ hk_doc_new: lib.func('HK_Doc* hk_doc_new()'),
1022
+ hk_doc_free: lib.func('void hk_doc_free(HK_Doc* doc)'),
1023
+ hk_doc_insert_str: lib.func('int hk_doc_insert_str(HK_Doc* doc, const char* key, const char* value)'),
1024
+ hk_doc_insert_int: lib.func('int hk_doc_insert_int(HK_Doc* doc, const char* key, int64_t value)'),
1025
+ hk_doc_insert_float: lib.func('int hk_doc_insert_float(HK_Doc* doc, const char* key, double value)'),
1026
+ hk_doc_insert_bool: lib.func('int hk_doc_insert_bool(HK_Doc* doc, const char* key, bool value)'),
1027
+ hk_doc_insert_null: lib.func('int hk_doc_insert_null(HK_Doc* doc, const char* key)'),
1028
+ hk_doc_insert_bin: lib.func('int hk_doc_insert_bin(HK_Doc* doc, const char* key, const uint8_t* data, size_t len)'),
1029
+ hk_doc_insert_timestamp: lib.func('int hk_doc_insert_timestamp(HK_Doc* doc, const char* key, int64_t micros)'),
1030
+ hk_doc_insert_server_timestamp: lib.func('int hk_doc_insert_server_timestamp(HK_Doc* doc, const char* key)'),
1031
+ hk_doc_to_json: lib.func('HeapStr hk_doc_to_json(const HK_Doc* doc)'),
1032
+
1033
+ hk_engine_insert: lib.func('int hk_engine_insert(HK_Engine* engine, const char* collection, const char* doc_id, const HK_Doc* doc)'),
1034
+ hk_engine_get: lib.func('HK_Doc* hk_engine_get(HK_Engine* engine, const char* collection, const char* doc_id)'),
1035
+ hk_engine_delete: lib.func('int hk_engine_delete(HK_Engine* engine, const char* collection, const char* doc_id)'),
1036
+ hk_engine_delete_local: lib.func('int hk_engine_delete_local(HK_Engine* engine, const char* collection, const char* doc_id)'),
1037
+ hk_engine_delete_ext: lib.func('int hk_engine_delete_ext(HK_Engine* engine, const char* collection, const char* doc_id, int32_t mode)'),
1038
+ hk_engine_restore: lib.func('int hk_engine_restore(HK_Engine* engine, const char* collection, const char* doc_id)'),
1039
+ hk_engine_restore_available: lib.func('HeapStr hk_engine_restore_available(HK_Engine* engine, const char* collection)'),
1040
+ hk_engine_delete_queue: lib.func('HeapStr hk_engine_delete_queue(HK_Engine* engine, const char* collection)'),
1041
+ hk_engine_sweep_now: lib.func('int hk_engine_sweep_now(HK_Engine* engine)'),
1042
+ hk_engine_set_collection_local: lib.func('int hk_engine_set_collection_local(HK_Engine* engine, const char* collection, int local)'),
1043
+ hk_engine_replicate_key: lib.func('int hk_engine_replicate_key(HK_Engine* engine, const char* collection, const char* doc_id)'),
1044
+ hk_engine_replicate_collection: lib.func('int hk_engine_replicate_collection(HK_Engine* engine, const char* collection)'),
1045
+ hk_engine_vacuum_collection: lib.func('int hk_engine_vacuum_collection(HK_Engine* engine, const char* collection)'),
1046
+ hk_engine_relocate_docs: lib.func('HeapStr hk_engine_relocate_docs(HK_Engine* engine, const char* src, const char* dst, const char* ids_json)'),
1047
+ hk_engine_load_collection: lib.func('int hk_engine_load_collection(HK_Engine* engine, const char* collection)'),
1048
+ hk_engine_unload_collection: lib.func('int hk_engine_unload_collection(HK_Engine* engine, const char* collection)'),
1049
+ hk_engine_unloaded_collections: lib.func('HeapStr hk_engine_unloaded_collections(HK_Engine* engine)'),
1050
+ hk_engine_patch: lib.func('int hk_engine_patch(HK_Engine* engine, const char* collection, const char* doc_id, const HK_Doc* updates)'),
1051
+ hk_engine_insert_subdoc: lib.func('int hk_engine_insert_subdoc(HK_Engine* engine, const char* col, const char* id, const char* sub_col, const char* sub_id, const HK_Doc* doc)'),
1052
+ hk_engine_get_by_ref: lib.func('HK_Doc* hk_engine_get_by_ref(HK_Engine* engine, const HK_Doc* doc, const char* field_key)'),
1053
+ hk_engine_create_index: lib.func('uint32_t hk_engine_create_index(HK_Engine* engine, const char* collection, const char* fields_json)'),
1054
+ hk_engine_create_vector_index: lib.func('int hk_engine_create_vector_index(HK_Engine* engine, const char* collection, const char* field, uint32_t dim, uint32_t metric)'),
1055
+ hk_engine_find_near: lib.func('HeapStr hk_engine_find_near(HK_Engine* engine, const char* collection, const char* field, const float* vec, size_t len, size_t k)'),
1056
+ hk_engine_create_traverse_index: lib.func('int hk_engine_create_traverse_index(HK_Engine* engine, const char* collection)'),
1057
+ hk_engine_find_paths: lib.func('HeapStr hk_engine_find_paths(HK_Engine* engine, const char* collection, const char* doc_id, uint32_t direction, const char* edge, size_t max_depth, size_t k)'),
1058
+ hk_engine_create_edge_index: lib.func('int hk_engine_create_edge_index(HK_Engine* engine, const char* collection)'),
1059
+ hk_engine_shortest_paths: lib.func('HeapStr hk_engine_shortest_paths(HK_Engine* engine, const char* collection, const char* doc_id, uint32_t direction, const char* edge, const char* via, const char* weight, size_t max_depth, size_t k)'),
1060
+
1061
+ // v0.19.6 parity
1062
+ hk_engine_drop_simple_index: lib.func('int hk_engine_drop_simple_index(HK_Engine* engine, const char* collection, const char* field)'),
1063
+ hk_engine_drop_vector_index: lib.func('int hk_engine_drop_vector_index(HK_Engine* engine, const char* collection, const char* field)'),
1064
+ hk_engine_drop_composite_index: lib.func('int hk_engine_drop_composite_index(HK_Engine* engine, const char* collection, uint32_t id)'),
1065
+ hk_engine_drop_edge_index: lib.func('int hk_engine_drop_edge_index(HK_Engine* engine, const char* collection)'),
1066
+ hk_engine_drop_fts_index: lib.func('int hk_engine_drop_fts_index(HK_Engine* engine, const char* collection, const char* field)'),
1067
+ hk_engine_drop_traverse_index: lib.func('int hk_engine_drop_traverse_index(HK_Engine* engine, const char* collection)'),
1068
+ hk_engine_create_vector_index_v2: lib.func('int hk_engine_create_vector_index_v2(HK_Engine* engine, const char* collection, const char* field, uint32_t dim, uint32_t metric, size_t m, size_t ef_construction, size_t ef_search)'),
1069
+ hk_engine_find_paths_v4: lib.func('HeapStr hk_engine_find_paths_v4(HK_Engine* engine, const char* collection, const char* doc_id, uint32_t direction, const char* edge, const char* via, size_t max_depth, size_t k, uint32_t include_path, uint32_t include_via, const char* filter_field, const char* filter_json, const char* type_field, const char* type_json, const char* match_field, const char* match_json)'),
1070
+ hk_engine_find_simple_paths_v3: lib.func('HeapStr hk_engine_find_simple_paths_v3(HK_Engine* engine, const char* collection, const char* doc_id, uint32_t direction, const char* edge, const char* via, const char* weight, size_t max_depth, size_t k, uint32_t include_path, uint32_t include_via, const char* filter_field, const char* filter_json, const char* type_field, const char* type_json, const char* match_field, const char* match_json)'),
1071
+ hk_engine_export_graph: lib.func('HeapStr hk_engine_export_graph(HK_Engine* engine, const char* collection, uint32_t direction, const char* edge, const char* via, const char* filter_field, const char* filter_json)'),
1072
+ hk_engine_graph_analysis: lib.func('HeapStr hk_engine_graph_analysis(HK_Engine* engine, const char* collection, const char* op, uint32_t direction, const char* edge, const char* via, const char* filter_field, const char* filter_json)'),
1073
+ hk_engine_insert_json: lib.func('int hk_engine_insert_json(HK_Engine* engine, const char* collection, const char* doc_id, const char* json)'),
1074
+ hk_batch_set_json: lib.func('int hk_batch_set_json(HK_Batch* batch, const char* collection, const char* doc_id, const char* json)'),
1075
+
1076
+ hk_batch_new: lib.func('HK_Batch* hk_batch_new()'),
1077
+ hk_batch_free: lib.func('void hk_batch_free(HK_Batch* batch)'),
1078
+ hk_batch_set: lib.func('int hk_batch_set(HK_Batch* batch, const char* collection, const char* doc_id, const HK_Doc* doc)'),
1079
+ hk_batch_delete: lib.func('int hk_batch_delete(HK_Batch* batch, const char* collection, const char* doc_id)'),
1080
+ hk_batch_delete_ext: lib.func('int hk_batch_delete_ext(HK_Batch* batch, const char* collection, const char* doc_id, int32_t mode)'),
1081
+ hk_batch_commit: lib.func('int hk_batch_commit(HK_Engine* engine, HK_Batch* batch)'),
1082
+
1083
+ hk_transaction_begin: lib.func('HK_Transaction* hk_transaction_begin(HK_Engine* engine)'),
1084
+ hk_transaction_get: lib.func('HK_Doc* hk_transaction_get(HK_Engine* engine, HK_Transaction* tx, const char* collection, const char* doc_id)'),
1085
+ hk_transaction_set: lib.func('int hk_transaction_set(HK_Transaction* tx, const char* collection, const char* doc_id, const HK_Doc* doc)'),
1086
+ hk_transaction_commit: lib.func('int hk_transaction_commit(HK_Engine* engine, HK_Transaction* tx)'),
1087
+ hk_transaction_free: lib.func('void hk_transaction_free(HK_Transaction* tx)'),
1088
+
1089
+ hk_query_new: lib.func('HK_Query* hk_query_new(const char* collection)'),
1090
+ hk_query_free: lib.func('void hk_query_free(HK_Query* query)'),
1091
+ hk_query_where_eq_str: lib.func('int hk_query_where_eq_str(HK_Query* query, const char* field, const char* value)'),
1092
+ hk_query_where_eq_bool: lib.func('int hk_query_where_eq_bool(HK_Query* query, const char* field, bool value)'),
1093
+ hk_query_where_eq_int: lib.func('int hk_query_where_eq_int(HK_Query* query, const char* field, int64_t value)'),
1094
+ hk_query_where_ne_str: lib.func('int hk_query_where_ne_str(HK_Query* query, const char* field, const char* value)'),
1095
+ hk_query_where_ne_int: lib.func('int hk_query_where_ne_int(HK_Query* query, const char* field, int64_t value)'),
1096
+ hk_query_where_gt_str: lib.func('int hk_query_where_gt_str(HK_Query* query, const char* field, const char* value)'),
1097
+ hk_query_where_gt_int: lib.func('int hk_query_where_gt_int(HK_Query* query, const char* field, int64_t value)'),
1098
+ hk_query_where_gte_str: lib.func('int hk_query_where_gte_str(HK_Query* query, const char* field, const char* value)'),
1099
+ hk_query_where_gte_int: lib.func('int hk_query_where_gte_int(HK_Query* query, const char* field, int64_t value)'),
1100
+ hk_query_where_lt_str: lib.func('int hk_query_where_lt_str(HK_Query* query, const char* field, const char* value)'),
1101
+ hk_query_where_lt_int: lib.func('int hk_query_where_lt_int(HK_Query* query, const char* field, int64_t value)'),
1102
+ hk_query_where_lte_str: lib.func('int hk_query_where_lte_str(HK_Query* query, const char* field, const char* value)'),
1103
+ hk_query_where_lte_int: lib.func('int hk_query_where_lte_int(HK_Query* query, const char* field, int64_t value)'),
1104
+ hk_query_where_match: lib.func('int hk_query_where_match(HK_Query* query, const char* field, const char* value)'),
1105
+ hk_query_where_match_prefix: lib.func('int hk_query_where_match_prefix(HK_Query* query, const char* field, const char* value)'),
1106
+ hk_query_where_contains: lib.func('int hk_query_where_contains(HK_Query* query, const char* field, const char* value)'),
1107
+ hk_query_where_starts_with: lib.func('int hk_query_where_starts_with(HK_Query* query, const char* field, const char* value)'),
1108
+ hk_query_order_by: lib.func('int hk_query_order_by(HK_Query* query, const char* field, bool ascending)'),
1109
+ hk_query_limit: lib.func('int hk_query_limit(HK_Query* query, size_t limit)'),
1110
+ hk_query_offset: lib.func('int hk_query_offset(HK_Query* query, size_t offset)'),
1111
+ hk_query_select_field: lib.func('int hk_query_select_field(HK_Query* query, const char* field)'),
1112
+ hk_query_execute: lib.func('HeapStr hk_query_execute(HK_Engine* engine, const HK_Query* query)'),
1113
+ hk_query_delete: lib.func('int hk_query_delete(HK_Engine* engine, HK_Query* query)'),
1114
+ hk_query_delete_local: lib.func('int hk_query_delete_local(HK_Engine* engine, HK_Query* query)'),
1115
+ hk_query_patch: lib.func('int hk_query_patch(HK_Engine* engine, HK_Query* query, const HK_Doc* patch_doc)'),
1116
+ hk_query_execute_to_handles: lib.func('HK_ResultSet* hk_query_execute_to_handles(HK_Engine* engine, const HK_Query* query)'),
1117
+ hk_result_set_count: lib.func('size_t hk_result_set_count(HK_ResultSet* results)'),
1118
+ hk_result_set_get_doc: lib.func('HK_Doc* hk_result_set_get_doc(HK_ResultSet* results, size_t index)'),
1119
+ hk_result_set_free: lib.func('void hk_result_set_free(HK_ResultSet* results)'),
1120
+ hk_result_set_to_json: lib.func('HeapStr hk_result_set_to_json(HK_ResultSet* results)'),
1121
+ hk_query_execute_raw: lib.func('HK_RawResultSet* hk_query_execute_raw(HK_Engine* engine, const HK_Query* query)'),
1122
+ hk_rawresult_count: lib.func('size_t hk_rawresult_count(HK_RawResultSet* results)'),
1123
+ hk_rawresult_get: lib.func('HK_RawDoc* hk_rawresult_get(HK_RawResultSet* results, size_t index)'),
1124
+ hk_rawresult_free: lib.func('void hk_rawresult_free(HK_RawResultSet* results)'),
1125
+ hk_query_start_after_raw: lib.func('int hk_query_start_after_raw(HK_Query* query, const HK_RawDoc* anchor_doc)'),
1126
+ hk_rawdoc_to_doc: lib.func('HK_Doc* hk_rawdoc_to_doc(HK_Engine* engine, const HK_RawDoc* raw_doc, const char* collection)'),
1127
+ hk_view_get: lib.func('HK_ViewDoc* hk_view_get(HK_Engine* engine, const char* collection, const char* doc_id)'),
1128
+ hk_view_free: lib.func('void hk_view_free(HK_ViewDoc* view)'),
1129
+ hk_view_field_count: lib.func('size_t hk_view_field_count(const HK_ViewDoc* view)'),
1130
+ hk_view_has_field: lib.func('bool hk_view_has_field(const HK_ViewDoc* view, const char* key)'),
1131
+ hk_view_get_int: lib.func('bool hk_view_get_int(const HK_ViewDoc* view, const char* key, _Out_ int64_t *out)'),
1132
+ hk_view_get_float: lib.func('bool hk_view_get_float(const HK_ViewDoc* view, const char* key, _Out_ double *out)'),
1133
+ hk_view_get_bool: lib.func('int hk_view_get_bool(const HK_ViewDoc* view, const char* key)'),
1134
+ hk_view_to_doc: lib.func('HK_Doc* hk_view_to_doc(const HK_ViewDoc* view, const char* doc_id)'),
1135
+ hk_query_defer_blobs: lib.func('int hk_query_defer_blobs(HK_Query* query, int defer)'),
1136
+ hk_doc_resolve_blobs: lib.func('int hk_doc_resolve_blobs(HK_Engine* engine, const char* collection, HK_Doc* doc)'),
1137
+ hk_engine_insert_take: lib.func('int hk_engine_insert_take(HK_Engine* engine, const char* collection, const char* doc_id, HK_Doc* doc)'),
1138
+ hk_config_set_wal_reserve_bytes: lib.func('void hk_config_set_wal_reserve_bytes(HK_Config* config, uint64_t bytes)'),
1139
+
1140
+ hk_query_aggregate_count: lib.func('int hk_query_aggregate_count(HK_Query* query)'),
1141
+ hk_query_aggregate_sum: lib.func('int hk_query_aggregate_sum(HK_Query* query, const char* field)'),
1142
+ hk_query_aggregate_avg: lib.func('int hk_query_aggregate_avg(HK_Query* query, const char* field)'),
1143
+ hk_query_aggregate_min: lib.func('int hk_query_aggregate_min(HK_Query* query, const char* field)'),
1144
+ hk_query_aggregate_max: lib.func('int hk_query_aggregate_max(HK_Query* query, const char* field)'),
1145
+ hk_query_execute_aggregation: lib.func('HeapStr hk_query_execute_aggregation(HK_Engine* engine, const HK_Query* query)'),
1146
+ hk_query_where_or_str: lib.func('int hk_query_where_or_str(HK_Query* query, const char* field, const char* value)'),
1147
+ hk_query_where_or_int: lib.func('int hk_query_where_or_int(HK_Query* query, const char* field, int64_t value)'),
1148
+
1149
+ hk_engine_list_collections: lib.func('HeapStr hk_engine_list_collections(HK_Engine* engine)'),
1150
+ hk_net_syncer_new: lib.func('HK_NetSyncer* hk_net_syncer_new(HK_Engine* engine, const char* name, const char* room_key)'),
1151
+ hk_net_syncer_start: lib.func('int hk_net_syncer_start(HK_NetSyncer* syncer, uint16_t port)'),
1152
+ hk_net_syncer_set_discovery: lib.func('int hk_net_syncer_set_discovery(HK_NetSyncer* syncer, int mode)'),
1153
+ hk_net_syncer_status: lib.func('HeapStr hk_net_syncer_status(HK_NetSyncer* syncer)'),
1154
+ hk_net_syncer_free: lib.func('void hk_net_syncer_free(HK_NetSyncer* syncer)'),
1155
+
1156
+ // v0.5.9
1157
+ // Indexing
1158
+ hk_engine_create_fts_index: lib.func('int hk_engine_create_fts_index(HK_Engine* engine, const char* collection, const char* field)'),
1159
+ hk_engine_create_simple_index: lib.func('int hk_engine_create_simple_index(HK_Engine* engine, const char* collection, const char* field)'),
1160
+
1161
+ // Array API
1162
+ hk_array_new: lib.func('HK_Array* hk_array_new()'),
1163
+ hk_array_free: lib.func('void hk_array_free(HK_Array* arr)'),
1164
+ hk_array_append_str: lib.func('int hk_array_append_str(HK_Array* arr, const char* value)'),
1165
+ hk_array_append_int: lib.func('int hk_array_append_int(HK_Array* arr, int64_t value)'),
1166
+ hk_array_append_doc: lib.func('int hk_array_append_doc(HK_Array* arr, const HK_Doc* doc)'),
1167
+
1168
+ // Nested
1169
+ hk_doc_insert_doc: lib.func('int hk_doc_insert_doc(HK_Doc* parent, const char* key, HK_Doc* child)'),
1170
+ hk_doc_insert_array: lib.func('int hk_doc_insert_array(HK_Doc* parent, const char* key, HK_Array* arr)'),
1171
+
1172
+ // Query extensions
1173
+ hk_query_where_in: lib.func('int hk_query_where_in(HK_Query* query, const char* field, HK_Array* arr)'),
1174
+ hk_query_where_not_in: lib.func('int hk_query_where_not_in(HK_Query* query, const char* field, HK_Array* arr)'),
1175
+ hk_query_where_array_contains_any: lib.func('int hk_query_where_array_contains_any(HK_Query* query, const char* field, HK_Array* arr)'),
1176
+ hk_query_where_array_contains: lib.func('int hk_query_where_array_contains(HK_Query* query, const char* field, const char* value)'),
1177
+ hk_query_start_after: lib.func('int hk_query_start_after(HK_Query* query, HK_Doc* anchor)'),
1178
+ hk_query_start_at: lib.func('int hk_query_start_at(HK_Query* query, const HK_Doc* anchor)'),
1179
+ hk_query_end_at: lib.func('int hk_query_end_at(HK_Query* query, const HK_Doc* anchor)'),
1180
+ hk_query_end_before: lib.func('int hk_query_end_before(HK_Query* query, const HK_Doc* anchor)'),
1181
+
1182
+ hk_doc_insert_reference: lib.func('int hk_doc_insert_reference(HK_Doc* doc, const char* key, const char* target_collection, const char* target_id)'),
1183
+
1184
+ hk_cloud_sync_new: lib.func('HK_CloudSync* hk_cloud_sync_new(HK_Engine* engine, int32_t mode, const char* client_id, const char* room_name, const char* room_key, const char* auth_token)'),
1185
+ hk_cloud_sync_server_new: lib.func('HK_CloudSync* hk_cloud_sync_server_new(HK_Engine* engine, const char* server_id, const char* auth_token, const char* room_name, const char* room_key)'),
1186
+ hk_cloud_sync_client_new: lib.func('HK_CloudSync* hk_cloud_sync_client_new(HK_Engine* engine, const char* client_id, const char* room_name, const char* room_key, const char* auth_token)'),
1187
+ hk_cloud_sync_start: lib.func('int hk_cloud_sync_start(HK_CloudSync* cloud_sync, const char* address)'),
1188
+ hk_cloud_sync_status: lib.func('HeapStr hk_cloud_sync_status(HK_CloudSync* cloud_sync)'),
1189
+ hk_cloud_sync_stop: lib.func('void hk_cloud_sync_stop(HK_CloudSync* cloud_sync)'),
1190
+ hk_cloud_sync_free: lib.func('void hk_cloud_sync_free(HK_CloudSync* cloud_sync)'),
1191
+
1192
+ // 0.16.5 parity: F8 schemas, F5 versioned/tx, F7 explain, F3 predicates,
1193
+ // F4 pipelines, config knobs, quiescence, lazy walks and borrowed reads.
1194
+ hk_config_set_group_commit_interval_ms: lib.func('void hk_config_set_group_commit_interval_ms(HK_Config* config, uint64_t ms)'),
1195
+ hk_config_set_page_cache_capacity: lib.func('void hk_config_set_page_cache_capacity(HK_Config* config, size_t capacity)'),
1196
+ hk_config_set_sync_excluded: lib.func('int hk_config_set_sync_excluded(HK_Config* config, const char* collections_json)'),
1197
+ hk_config_set_lazy_collections: lib.func('int hk_config_set_lazy_collections(HK_Config* config, const char* collections_json)'),
1198
+ hk_config_set_replication_collections: lib.func('int hk_config_set_replication_collections(HK_Config* config, const char* collections_json)'),
1199
+ hk_engine_await_quiescent: lib.func('bool hk_engine_await_quiescent(HK_Engine* engine, uint64_t timeout_ms)'),
1200
+ hk_engine_quiescence_status: lib.func('HeapStr hk_engine_quiescence_status(HK_Engine* engine)'),
1201
+ hk_engine_get_versioned: lib.func('HK_Doc* hk_engine_get_versioned(HK_Engine* engine, const char* collection, const char* doc_id, _Out_ uint64_t *out_version)'),
1202
+ hk_engine_set_schema: lib.func('int hk_engine_set_schema(HK_Engine* engine, const char* collection, const char* schema_json)'),
1203
+ hk_engine_get_schema: lib.func('HeapStr hk_engine_get_schema(HK_Engine* engine, const char* collection)'),
1204
+ hk_engine_clear_schema: lib.func('int hk_engine_clear_schema(HK_Engine* engine, const char* collection)'),
1205
+ hk_engine_list_schemas: lib.func('HeapStr hk_engine_list_schemas(HK_Engine* engine)'),
1206
+ hk_query_explain: lib.func('HeapStr hk_query_explain(HK_Engine* engine, const HK_Query* query)'),
1207
+ hk_query_where_array_all_eq: lib.func('int hk_query_where_array_all_eq(HK_Query* query, const char* field, const char* value_json)'),
1208
+ hk_query_where_elem_match: lib.func('int hk_query_where_elem_match(HK_Query* query, const char* field, const char* filters_json)'),
1209
+ hk_query_where_near: lib.func('int hk_query_where_near(HK_Query* query, const char* field, const float* vec, size_t len)'),
1210
+ hk_query_where_traverse: lib.func('int hk_query_where_traverse(HK_Query* query, const char* start_col, const char* start_id, uint32_t direction, const char* edge_field, size_t max_depth)'),
1211
+ hk_pipeline_new: lib.func('HK_Pipeline* hk_pipeline_new(const char* collection)'),
1212
+ hk_pipeline_free: lib.func('void hk_pipeline_free(HK_Pipeline* pipeline)'),
1213
+ hk_pipeline_match: lib.func('int hk_pipeline_match(HK_Pipeline* pipeline, const HK_Query* query)'),
1214
+ hk_pipeline_project: lib.func('int hk_pipeline_project(HK_Pipeline* pipeline, const char* fields_json)'),
1215
+ hk_pipeline_sort: lib.func('int hk_pipeline_sort(HK_Pipeline* pipeline, const char* field, bool ascending)'),
1216
+ hk_pipeline_limit: lib.func('int hk_pipeline_limit(HK_Pipeline* pipeline, size_t limit)'),
1217
+ hk_pipeline_offset: lib.func('int hk_pipeline_offset(HK_Pipeline* pipeline, size_t offset)'),
1218
+ hk_pipeline_unwind: lib.func('int hk_pipeline_unwind(HK_Pipeline* pipeline, const char* field)'),
1219
+ hk_pipeline_group: lib.func('int hk_pipeline_group(HK_Pipeline* pipeline, const char* key, const char* accums_json)'),
1220
+ hk_pipeline_execute: lib.func('HeapStr hk_pipeline_execute(HK_Engine* engine, const HK_Pipeline* pipeline)'),
1221
+ hk_pipeline_execute_to_handles: lib.func('HK_ResultSet* hk_pipeline_execute_to_handles(HK_Engine* engine, const HK_Pipeline* pipeline)'),
1222
+ hk_transaction_get_many: lib.func('HeapStr hk_transaction_get_many(HK_Engine* engine, HK_Transaction* tx, const char* collection, const char* ids_json)'),
1223
+ hk_transaction_patch: lib.func('int hk_transaction_patch(HK_Engine* engine, HK_Transaction* tx, const char* collection, const char* doc_id, const char* updates_json)'),
1224
+ hk_error_last_kind: lib.func('int hk_error_last_kind()'),
1225
+ hk_error_is_retryable: lib.func('bool hk_error_is_retryable()'),
1226
+ hk_debug_write_stats: lib.func('HeapStr hk_debug_write_stats()'),
1227
+ hk_log_enable_stderr: lib.func('void hk_log_enable_stderr()'),
1228
+ hk_log_disable_stderr: lib.func('void hk_log_disable_stderr()'),
1229
+ hk_cursor_walk: lib.func('int64_t hk_cursor_walk(HK_Engine* engine, const HK_Query* query, HkWalkCallback* callback, void* userdata)'),
1230
+ hk_cursor_walk_view: lib.func('int64_t hk_cursor_walk_view(HK_Engine* engine, const HK_Query* query, HkViewWalkCallback* callback, void* userdata)'),
1231
+ hk_rawdoc_bytes: lib.func('const uint8_t* hk_rawdoc_bytes(const HK_RawDoc* doc, _Out_ uintptr_t *len_out)'),
1232
+ // Return as uint8_t* so koffi hands back a pointer; the id is
1233
+ // length-delimited, not NUL-terminated.
1234
+ hk_rawdoc_id: lib.func('const uint8_t* hk_rawdoc_id(const HK_RawDoc* doc, _Out_ uintptr_t *len_out)'),
1235
+ hk_view_get_str: lib.func('const uint8_t* hk_view_get_str(const HK_ViewDoc* view, const char* key, _Out_ uintptr_t *len_out)'),
1236
+ hk_view_get_bytes: lib.func('const uint8_t* hk_view_get_bytes(const HK_ViewDoc* view, const char* key, _Out_ uintptr_t *len_out)'),
1237
+ hk_engine_get_json: lib.func('HeapStr hk_engine_get_json(HK_Engine* engine, const char* collection, const char* doc_id)'),
1238
+ hk_query_execute_json: lib.func('HeapStr hk_query_execute_json(HK_Engine* engine, const HK_Query* query)'),
1239
+ hk_cursor_walk_json: lib.func('int64_t hk_cursor_walk_json(HK_Engine* engine, const HK_Query* query, HkWalkCallback* callback, void* userdata)'),
1240
+ hk_view_to_json: lib.func('HeapStr hk_view_to_json(const HK_ViewDoc* view)'),
1241
+ hk_view_get_path_int: lib.func('bool hk_view_get_path_int(const HK_ViewDoc* view, const char* path, _Out_ int64_t *out)'),
1242
+ hk_view_get_path_float: lib.func('bool hk_view_get_path_float(const HK_ViewDoc* view, const char* path, _Out_ double *out)'),
1243
+ hk_view_get_path_bool: lib.func('int hk_view_get_path_bool(const HK_ViewDoc* view, const char* path)'),
1244
+ hk_view_get_path_str: lib.func('const uint8_t* hk_view_get_path_str(const HK_ViewDoc* view, const char* path, _Out_ uintptr_t *len_out)'),
1245
+ hk_view_get_path_bytes: lib.func('const uint8_t* hk_view_get_path_bytes(const HK_ViewDoc* view, const char* path, _Out_ uintptr_t *len_out)'),
1246
+
1247
+ hk_last_error: lib.func('const char* hk_last_error()'),
1248
+ hk_string_free: lib.func('void hk_string_free(void* value)')
1249
+ };
1250
+
1251
+ // ponytail: HeapStr returns arrive as JS strings, ALREADY freed via
1252
+ // hk_string_free by the disposable — no decode, no manual free (both
1253
+ // crash or leak: decode on externals segfaults, freeing a copy leaks).
1254
+ const ptrToStringAndFree = (s: any): string | null => {
1255
+ if (!s) return null;
1256
+ return s as string;
1257
+ };
1258
+
1259
+ return {
1260
+ engineOpen: (path) => fn.hk_engine_open(path),
1261
+ engineIsIndexesReady: (engine) => fn.hk_engine_is_indexes_ready(engine),
1262
+ engineOpenWithConfig: (path, config) => fn.hk_engine_open_with_config(path, config),
1263
+ engineFree: (engine) => fn.hk_engine_free(engine),
1264
+ engineBackup: (e, p) => fn.hk_engine_backup(e, p),
1265
+ engineCompact: (e) => fn.hk_engine_compact(e),
1266
+ engineGetStats: (e) => ptrToStringAndFree(fn.hk_engine_get_stats(e)),
1267
+ engineGetAuditLog: (e) => ptrToStringAndFree(fn.hk_engine_get_audit_log(e)),
1268
+ engineSnapshotIndices: (e) => fn.hk_engine_snapshot_indices(e),
1269
+ engineListIndexes: (e, c) => ptrToStringAndFree(fn.hk_engine_list_indexes(e, c ?? null)),
1270
+
1271
+ configNew: () => fn.hk_config_new(),
1272
+ configFree: (c) => fn.hk_config_free(c),
1273
+ configSetDurability: (c, m) => fn.hk_config_set_durability(c, m),
1274
+ configSetEncryptionKey: (c, k) => fn.hk_config_set_encryption_key(c, k),
1275
+ configSetEncryptedCollections: (c, json) => fn.hk_config_set_encrypted_collections(c, json),
1276
+ configSetAuditLog: (c, e, p) => fn.hk_config_set_audit_log(c, e, p),
1277
+ configSetQueryWorkers: (c, count) => fn.hk_config_set_query_workers(c, count),
1278
+ configSetMemoryLimits: (c, m, mi) => fn.hk_config_set_memory_limits(c, m, mi),
1279
+ configSetStorageTuning: (c, ps, th, gc) => fn.hk_config_set_storage_tuning(c, ps, th, gc),
1280
+ configSetBlobThreshold: (c, t) => fn.hk_config_set_blob_threshold(c, t),
1281
+ configSetCompression: (c, e, l) => fn.hk_config_set_compression(c, e, l),
1282
+ configSetBackgroundMaintenance: (c, e) => fn.hk_config_set_background_maintenance(c, e),
1283
+
1284
+ engineWatch: (engine, collection, callback) => {
1285
+ const wrapper = (c: string, p: string, kind: number, _user: any) => callback(c, p, kind);
1286
+ // ponytail: koffi.register takes the typedef NAME as a string here —
1287
+ // passing the proto object itself throws "Unexpected ... type".
1288
+ return fn.hk_engine_watch(engine, collection, koffi.register(wrapper, 'HK_OnSnapshotCallback *'), null);
1289
+ },
1290
+ watchFree: (watch) => fn.hk_watch_free(watch),
1291
+
1292
+ docNew: () => fn.hk_doc_new(),
1293
+ docFree: (doc) => fn.hk_doc_free(doc),
1294
+ docInsertStr: (doc, key, value) => fn.hk_doc_insert_str(doc, key, value),
1295
+ docInsertInt: (doc, key, value) => fn.hk_doc_insert_int(doc, key, value),
1296
+ docInsertFloat: (doc, key, value) => fn.hk_doc_insert_float(doc, key, value),
1297
+ docInsertBool: (doc, key, value) => fn.hk_doc_insert_bool(doc, key, value),
1298
+ docInsertNull: (doc, key) => fn.hk_doc_insert_null(doc, key),
1299
+ docInsertBin: (doc, key, bytes) => fn.hk_doc_insert_bin(doc, key, Buffer.from(bytes), bytes.byteLength),
1300
+ docInsertTimestamp: (doc, key, micros) => fn.hk_doc_insert_timestamp(doc, key, micros),
1301
+ docInsertServerTimestamp: (doc, key) => fn.hk_doc_insert_server_timestamp(doc, key),
1302
+ docInsertReference: (doc, key, tc, tid) => fn.hk_doc_insert_reference(doc, key, tc, tid),
1303
+ docToJson: (doc) => ptrToStringAndFree(fn.hk_doc_to_json(doc)),
1304
+
1305
+ engineInsert: (engine, collection, docId, doc) => fn.hk_engine_insert(engine, collection, docId, doc),
1306
+ engineGet: (engine, collection, docId) => fn.hk_engine_get(engine, collection, docId),
1307
+ engineDelete: (engine, collection, docId) => fn.hk_engine_delete(engine, collection, docId),
1308
+ engineDeleteLocal: (engine, collection, docId) => fn.hk_engine_delete_local(engine, collection, docId),
1309
+ engineDeleteEx: (engine, collection, docId, force) => fn.hk_engine_delete_ext(engine, collection, docId, force ? 1 : 0),
1310
+ engineRestore: (engine, collection, docId) => fn.hk_engine_restore(engine, collection, docId),
1311
+ engineRestoreAvailable: (engine, collection) => ptrToStringAndFree(fn.hk_engine_restore_available(engine, collection as unknown as string)),
1312
+ engineDeleteQueue: (engine, collection) => ptrToStringAndFree(fn.hk_engine_delete_queue(engine, collection as unknown as string)),
1313
+ engineSweepNow: (engine) => fn.hk_engine_sweep_now(engine),
1314
+ engineSetCollectionLocal: (engine, collection, local) => fn.hk_engine_set_collection_local(engine, collection, local),
1315
+ engineReplicateKey: (engine, collection, docId) => fn.hk_engine_replicate_key(engine, collection, docId),
1316
+ engineReplicateCollection: (engine, collection) => fn.hk_engine_replicate_collection(engine, collection),
1317
+ engineVacuumCollection: (engine, collection) => fn.hk_engine_vacuum_collection(engine, collection),
1318
+ engineRelocateDocs: (engine, src, dst, idsJson) => ptrToStringAndFree(fn.hk_engine_relocate_docs(engine, src, dst, idsJson)),
1319
+ engineLoadCollection: (engine, collection) => fn.hk_engine_load_collection(engine, collection),
1320
+ engineUnloadCollection: (engine, collection) => fn.hk_engine_unload_collection(engine, collection),
1321
+ engineUnloadedCollections: (engine) => ptrToStringAndFree(fn.hk_engine_unloaded_collections(engine)),
1322
+ enginePatch: (engine, collection, docId, updates) => fn.hk_engine_patch(engine, collection, docId, updates),
1323
+ engineInsertSubDoc: (engine, col, id, subCol, subId, doc) => fn.hk_engine_insert_subdoc(engine, col, id, subCol, subId, doc),
1324
+ engineGetByRef: (engine, doc, fieldKey) => fn.hk_engine_get_by_ref(engine, doc, fieldKey),
1325
+ engineCreateIndex: (engine, collection, fieldsJson) => fn.hk_engine_create_index(engine, collection, fieldsJson),
1326
+
1327
+ batchNew: () => fn.hk_batch_new(),
1328
+ batchFree: (batch) => fn.hk_batch_free(batch),
1329
+ batchSet: (batch, collection, docId, doc) => fn.hk_batch_set(batch, collection, docId, doc),
1330
+ batchDelete: (batch, collection, docId) => fn.hk_batch_delete(batch, collection, docId),
1331
+ batchDeleteEx: (batch, collection, docId, force) => fn.hk_batch_delete_ext(batch, collection, docId, force ? 1 : 0),
1332
+ batchCommit: (engine, batch) => fn.hk_batch_commit(engine, batch),
1333
+
1334
+ transactionBegin: (engine) => fn.hk_transaction_begin(engine),
1335
+ transactionGet: (engine, tx, collection, docId) => fn.hk_transaction_get(engine, tx, collection, docId),
1336
+ transactionSet: (tx, collection, docId, doc) => fn.hk_transaction_set(tx, collection, docId, doc),
1337
+ transactionCommit: (engine, tx) => fn.hk_transaction_commit(engine, tx),
1338
+ transactionFree: (tx) => fn.hk_transaction_free(tx),
1339
+
1340
+ queryNew: (collection) => fn.hk_query_new(collection),
1341
+ queryFree: (query) => fn.hk_query_free(query),
1342
+ queryWhereEqStr: (query, field, value) => fn.hk_query_where_eq_str(query, field, value),
1343
+ queryWhereEqBool: (query, field, value) => fn.hk_query_where_eq_bool(query, field, value),
1344
+ queryWhereEqInt: (query, field, value) => fn.hk_query_where_eq_int(query, field, value),
1345
+ queryWhereNeStr: (query, field, value) => fn.hk_query_where_ne_str(query, field, value),
1346
+ queryWhereNeInt: (query, field, value) => fn.hk_query_where_ne_int(query, field, value),
1347
+ queryWhereGtStr: (query, field, value) => fn.hk_query_where_gt_str(query, field, value),
1348
+ queryWhereGtInt: (query, field, value) => fn.hk_query_where_gt_int(query, field, value),
1349
+ queryWhereGteStr: (query, field, value) => fn.hk_query_where_gte_str(query, field, value),
1350
+ queryWhereGteInt: (query, field, value) => fn.hk_query_where_gte_int(query, field, value),
1351
+ queryWhereLtStr: (query, field, value) => fn.hk_query_where_lt_str(query, field, value),
1352
+ queryWhereLtInt: (query, field, value) => fn.hk_query_where_lt_int(query, field, value),
1353
+ queryWhereLteStr: (query, field, value) => fn.hk_query_where_lte_str(query, field, value),
1354
+ queryWhereLteInt: (query, field, value) => fn.hk_query_where_lte_int(query, field, value),
1355
+ queryWhereMatch: (query, field, value) => fn.hk_query_where_match(query, field, value),
1356
+ queryWhereMatchPrefix: (query, field, value) => fn.hk_query_where_match_prefix(query, field, value),
1357
+ queryWhereContains: (query, field, value) => fn.hk_query_where_contains(query, field, value),
1358
+ queryWhereStartsWith: (query, field, value) => fn.hk_query_where_starts_with(query, field, value),
1359
+ queryWhereOrStr: (query, field, value) => fn.hk_query_where_or_str(query, field, value),
1360
+ queryWhereOrInt: (query, field, value) => fn.hk_query_where_or_int(query, field, value),
1361
+ queryOrderBy: (query, field, asc) => fn.hk_query_order_by(query, field, asc),
1362
+ queryLimit: (query, limit) => fn.hk_query_limit(query, limit),
1363
+ queryOffset: (query, offset) => fn.hk_query_offset(query, offset),
1364
+ querySelectField: (query, field) => fn.hk_query_select_field(query, field),
1365
+ queryExecute: (engine, query) => ptrToStringAndFree(fn.hk_query_execute(engine, query)),
1366
+ queryDelete: (engine, query) => fn.hk_query_delete(engine, query),
1367
+ queryDeleteLocal: (engine, query) => fn.hk_query_delete_local(engine, query),
1368
+ queryPatch: (engine, query, patchDoc) => fn.hk_query_patch(engine, query, patchDoc),
1369
+ queryExecuteToHandles: (engine, query) => fn.hk_query_execute_to_handles(engine, query),
1370
+ resultSetCount: (results) => fn.hk_result_set_count(results),
1371
+ resultSetGetDoc: (results, index) => fn.hk_result_set_get_doc(results, index),
1372
+ resultSetFree: (results) => fn.hk_result_set_free(results),
1373
+ resultSetToJson: (results) => ptrToStringAndFree(fn.hk_result_set_to_json(results)),
1374
+ queryExecuteRaw: (engine, query) => fn.hk_query_execute_raw(engine, query),
1375
+ rawResultCount: (results) => fn.hk_rawresult_count(results),
1376
+ rawResultGet: (results, index) => fn.hk_rawresult_get(results, index),
1377
+ rawResultFree: (results) => fn.hk_rawresult_free(results),
1378
+ queryStartAfterRaw: (query, anchorRawDoc) => fn.hk_query_start_after_raw(query, anchorRawDoc),
1379
+ rawDocToDoc: (engine, rawDoc, collection) => fn.hk_rawdoc_to_doc(engine, rawDoc, collection),
1380
+ viewGet: (engine, collection, docId) => fn.hk_view_get(engine, collection, docId),
1381
+ viewFree: (view) => fn.hk_view_free(view),
1382
+ viewFieldCount: (view) => fn.hk_view_field_count(view),
1383
+ viewHasField: (view, key) => fn.hk_view_has_field(view, key),
1384
+ viewGetInt: (view, key) => {
1385
+ const out: bigint[] = [0n];
1386
+ return fn.hk_view_get_int(view, key, out) ? out[0] : null;
1387
+ },
1388
+ viewGetFloat: (view, key) => {
1389
+ const out: number[] = [0];
1390
+ return fn.hk_view_get_float(view, key, out) ? out[0] : null;
1391
+ },
1392
+ viewGetBool: (view, key) => {
1393
+ const r: number = fn.hk_view_get_bool(view, key);
1394
+ return r < 0 ? null : r !== 0;
1395
+ },
1396
+ viewToDoc: (view, docId) => fn.hk_view_to_doc(view, docId),
1397
+ queryDeferBlobs: (query, defer) => fn.hk_query_defer_blobs(query, defer ? 1 : 0),
1398
+ docResolveBlobs: (engine, collection, doc) => fn.hk_doc_resolve_blobs(engine, collection, doc),
1399
+ engineInsertTake: (engine, collection, docId, doc) => fn.hk_engine_insert_take(engine, collection, docId, doc),
1400
+ configSetWalReserveBytes: (c, bytes) => fn.hk_config_set_wal_reserve_bytes(c, bytes),
1401
+
1402
+ queryAggregateCount: (q) => fn.hk_query_aggregate_count(q),
1403
+ queryAggregateSum: (q, f) => fn.hk_query_aggregate_sum(q, f),
1404
+ queryAggregateAvg: (q, f) => fn.hk_query_aggregate_avg(q, f),
1405
+ queryAggregateMin: (q, f) => fn.hk_query_aggregate_min(q, f),
1406
+ queryAggregateMax: (q, f) => fn.hk_query_aggregate_max(q, f),
1407
+ queryExecuteAggregation: (e, q) => ptrToStringAndFree(fn.hk_query_execute_aggregation(e, q)),
1408
+ engineListCollections: (engine) => ptrToStringAndFree(fn.hk_engine_list_collections(engine)),
1409
+ netSyncerNew: (engine, name, roomKey) => fn.hk_net_syncer_new(engine, name, roomKey),
1410
+ netSyncerStart: (syncer, port) => fn.hk_net_syncer_start(syncer, port),
1411
+ netSyncerSetDiscovery: (syncer, mode) => fn.hk_net_syncer_set_discovery(syncer, mode),
1412
+ netSyncerStatus: (syncer) => ptrToStringAndFree(fn.hk_net_syncer_status(syncer)),
1413
+ netSyncerFree: (syncer) => fn.hk_net_syncer_free(syncer),
1414
+
1415
+ cloudSyncNew: (engine, mode, clientId, roomName, roomKey, authToken) => fn.hk_cloud_sync_new(engine, mode, clientId, roomName, roomKey, authToken),
1416
+ cloudSyncServerNew: (engine, serverId, authToken, roomName, roomKey) => fn.hk_cloud_sync_server_new(engine, serverId, authToken, roomName, roomKey),
1417
+ cloudSyncClientNew: (engine, clientId, roomName, roomKey, authToken) => fn.hk_cloud_sync_client_new(engine, clientId, roomName, roomKey, authToken),
1418
+ cloudSyncStart: (cs, address) => fn.hk_cloud_sync_start(cs, address),
1419
+ cloudSyncStatus: (cs) => ptrToStringAndFree(fn.hk_cloud_sync_status(cs)),
1420
+ cloudSyncStop: (cs) => fn.hk_cloud_sync_stop(cs),
1421
+ cloudSyncFree: (cs) => fn.hk_cloud_sync_free(cs),
1422
+
1423
+ // ================= v0.5.9 =================
1424
+
1425
+ // Indexing
1426
+ createFtsIndex: (engine, collection, field) => fn.hk_engine_create_fts_index(engine, collection, field),
1427
+ createSimpleIndex: (engine, collection, field) => fn.hk_engine_create_simple_index(engine, collection, field),
1428
+ createVectorIndex: (engine, collection, field, dim, metric) => fn.hk_engine_create_vector_index(engine, collection, field, dim, metric),
1429
+ // LE-f32 bytes via Buffer over the Float32Array (koffi takes Buffer
1430
+ // for float*, same as docInsertBin takes Buffer for uint8_t*).
1431
+ engineFindNear: (engine, collection, field, vec, k) => fn.hk_engine_find_near(engine, collection, field, Buffer.from(vec.buffer, vec.byteOffset, vec.byteLength), vec.length, k),
1432
+ createTraverseIndex: (engine, collection) => fn.hk_engine_create_traverse_index(engine, collection),
1433
+ // Null edge crosses as NULL (koffi takes null for char*).
1434
+ engineFindPaths: (engine, collection, id, direction, edge, maxDepth, k) => fn.hk_engine_find_paths(engine, collection, id, direction, edge, maxDepth, k),
1435
+ createEdgeIndex: (engine, collection) => fn.hk_engine_create_edge_index(engine, collection),
1436
+ engineShortestPaths: (engine, collection, id, direction, edge, via, weight, maxDepth, k) => fn.hk_engine_shortest_paths(engine, collection, id, direction, edge, via, weight, maxDepth, k),
1437
+
1438
+ // v0.19.6 parity (koffi takes null for char* directly)
1439
+ dropSimpleIndex: (engine, collection, field) => fn.hk_engine_drop_simple_index(engine, collection, field),
1440
+ dropVectorIndex: (engine, collection, field) => fn.hk_engine_drop_vector_index(engine, collection, field),
1441
+ dropCompositeIndex: (engine, collection, id) => fn.hk_engine_drop_composite_index(engine, collection, id),
1442
+ dropEdgeIndex: (engine, collection) => fn.hk_engine_drop_edge_index(engine, collection),
1443
+ dropFtsIndex: (engine, collection, field) => fn.hk_engine_drop_fts_index(engine, collection, field),
1444
+ dropTraverseIndex: (engine, collection) => fn.hk_engine_drop_traverse_index(engine, collection),
1445
+ createVectorIndexV2: (engine, collection, field, dim, metric, m, efConstruction, efSearch) => fn.hk_engine_create_vector_index_v2(engine, collection, field, dim, metric, m, efConstruction, efSearch),
1446
+ findPathsFull: (engine, collection, id, direction, edge, via, maxDepth, k, includePath, includeVia, nodeField, nodeJSON, typeField, typeJSON, matchField, matchJSON) => fn.hk_engine_find_paths_v4(engine, collection, id, direction, edge, via, maxDepth, k, includePath ? 1 : 0, includeVia ? 1 : 0, nodeField, nodeJSON, typeField, typeJSON, matchField, matchJSON),
1447
+ simplePathsFull: (engine, collection, id, direction, edge, via, weight, maxDepth, k, includePath, includeVia, nodeField, nodeJSON, typeField, typeJSON, matchField, matchJSON) => fn.hk_engine_find_simple_paths_v3(engine, collection, id, direction, edge, via, weight, maxDepth, k, includePath ? 1 : 0, includeVia ? 1 : 0, nodeField, nodeJSON, typeField, typeJSON, matchField, matchJSON),
1448
+ exportGraph: (engine, collection, direction, edge, via, nodeField, nodeJSON) => fn.hk_engine_export_graph(engine, collection, direction, edge, via, nodeField, nodeJSON),
1449
+ graphAnalysis: (engine, collection, op, direction, edge, via, nodeField, nodeJSON) => fn.hk_engine_graph_analysis(engine, collection, op, direction, edge, via, nodeField, nodeJSON),
1450
+ insertJSON: (engine, collection, id, json) => fn.hk_engine_insert_json(engine, collection, id, json),
1451
+ batchSetJSON: (batch, collection, id, json) => fn.hk_batch_set_json(batch, collection, id, json),
1452
+
1453
+ // Array API
1454
+ arrayNew: () => fn.hk_array_new(),
1455
+ arrayFree: (arr) => fn.hk_array_free(arr),
1456
+ arrayAppendStr: (arr, value) => fn.hk_array_append_str(arr, value),
1457
+ arrayAppendInt: (arr, value) => fn.hk_array_append_int(arr, value),
1458
+ arrayAppendDoc: (arr, doc) => fn.hk_array_append_doc(arr, doc),
1459
+
1460
+ // Nested
1461
+ docInsertDoc: (parent, key, child) => fn.hk_doc_insert_doc(parent, key, child),
1462
+ docInsertArray: (parent, key, arr) => fn.hk_doc_insert_array(parent, key, arr),
1463
+
1464
+ // Query extensions
1465
+ queryWhereIn: (query, field, arr) => fn.hk_query_where_in(query, field, arr),
1466
+ queryWhereNotIn: (query, field, arr) => fn.hk_query_where_not_in(query, field, arr),
1467
+ queryWhereArrayContainsAny: (query, field, arr) => fn.hk_query_where_array_contains_any(query, field, arr),
1468
+ queryWhereArrayContainsStr: (query, field, value) => fn.hk_query_where_array_contains(query, field, String(value)),
1469
+ queryWhereArrayContainsInt: (query, field, value) => fn.hk_query_where_array_contains(query, field, String(value)),
1470
+ queryStartAfter: (query, anchorDoc) => fn.hk_query_start_after(query, anchorDoc),
1471
+ queryStartAt: (query, anchorDoc) => fn.hk_query_start_at(query, anchorDoc),
1472
+ queryEndAt: (query, anchorDoc) => fn.hk_query_end_at(query, anchorDoc),
1473
+ queryEndBefore: (query, anchorDoc) => fn.hk_query_end_before(query, anchorDoc),
1474
+
1475
+ // 0.16.5 parity.
1476
+ engineSetSchema: (e, c, json) => fn.hk_engine_set_schema(e, c, json),
1477
+ engineGetSchema: (e, c) => ptrToStringAndFree(fn.hk_engine_get_schema(e, c)),
1478
+ engineClearSchema: (e, c) => fn.hk_engine_clear_schema(e, c),
1479
+ engineListSchemas: (e) => ptrToStringAndFree(fn.hk_engine_list_schemas(e)),
1480
+ engineGetVersioned: (e, c, id) => {
1481
+ const out: bigint[] = [0n];
1482
+ const doc = fn.hk_engine_get_versioned(e, c, id, out);
1483
+ return { doc, version: out[0] };
1484
+ },
1485
+ engineAwaitQuiescent: (e, ms) => fn.hk_engine_await_quiescent(e, ms),
1486
+ engineQuiescenceStatus: (e) => ptrToStringAndFree(fn.hk_engine_quiescence_status(e)),
1487
+ transactionGetMany: (e, tx, c, idsJson) => ptrToStringAndFree(fn.hk_transaction_get_many(e, tx, c, idsJson)),
1488
+ transactionPatch: (e, tx, c, id, updatesJson) => fn.hk_transaction_patch(e, tx, c, id, updatesJson),
1489
+ errorLastKind: () => fn.hk_error_last_kind(),
1490
+ errorIsRetryable: () => fn.hk_error_is_retryable(),
1491
+ debugWriteStats: () => ptrToStringAndFree(fn.hk_debug_write_stats()),
1492
+ logEnableStderr: () => fn.hk_log_enable_stderr(),
1493
+ logDisableStderr: () => fn.hk_log_disable_stderr(),
1494
+ queryExplain: (e, q) => ptrToStringAndFree(fn.hk_query_explain(e, q)),
1495
+ queryWhereArrayAllEq: (q, f, json) => fn.hk_query_where_array_all_eq(q, f, json),
1496
+ queryWhereElemMatch: (q, f, json) => fn.hk_query_where_elem_match(q, f, json),
1497
+ queryWhereNear: (q, f, vec) => fn.hk_query_where_near(q, f, Buffer.from(vec.buffer, vec.byteOffset, vec.byteLength), vec.length),
1498
+ queryWhereTraverse: (q, sc, sid, dir, edge, depth) => fn.hk_query_where_traverse(q, sc, sid, dir, edge, depth),
1499
+ pipelineNew: (c) => fn.hk_pipeline_new(c),
1500
+ pipelineFree: (p) => fn.hk_pipeline_free(p),
1501
+ pipelineMatch: (p, q) => fn.hk_pipeline_match(p, q),
1502
+ pipelineProject: (p, json) => fn.hk_pipeline_project(p, json),
1503
+ pipelineSort: (p, f, asc) => fn.hk_pipeline_sort(p, f, asc),
1504
+ pipelineLimit: (p, n) => fn.hk_pipeline_limit(p, n),
1505
+ pipelineOffset: (p, n) => fn.hk_pipeline_offset(p, n),
1506
+ pipelineUnwind: (p, f) => fn.hk_pipeline_unwind(p, f),
1507
+ pipelineGroup: (p, k, json) => fn.hk_pipeline_group(p, k, json),
1508
+ pipelineExecute: (e, p) => ptrToStringAndFree(fn.hk_pipeline_execute(e, p)),
1509
+ pipelineExecuteToHandles: (e, p) => fn.hk_pipeline_execute_to_handles(e, p),
1510
+ configSetGroupCommitIntervalMs: (c, ms) => fn.hk_config_set_group_commit_interval_ms(c, ms),
1511
+ configSetPageCacheCapacity: (c, n) => fn.hk_config_set_page_cache_capacity(c, n),
1512
+ configSetSyncExcluded: (c, json) => fn.hk_config_set_sync_excluded(c, json),
1513
+ configSetLazyCollections: (c, json) => fn.hk_config_set_lazy_collections(c, json),
1514
+ configSetReplicationCollections: (c, json) => fn.hk_config_set_replication_collections(c, json),
1515
+ cursorWalk: (e, q, callback) => {
1516
+ const wrapper = (id: any, idLen: number, bytes: any, bytesLen: number) => {
1517
+ const rowId = new TextDecoder().decode(Uint8Array.from(koffi.decode(id, 'uint8_t', Number(idLen))));
1518
+ const rowBytes = Uint8Array.from(koffi.decode(bytes, 'uint8_t', Number(bytesLen)));
1519
+ return callback(rowId, rowBytes);
1520
+ };
1521
+ return Number(fn.hk_cursor_walk(e, q, koffi.register(wrapper, 'HkWalkCallback *'), null));
1522
+ },
1523
+ cursorWalkView: (e, q, callback) => {
1524
+ const wrapper = (id: any, idLen: number, view: any) => {
1525
+ const rowId = new TextDecoder().decode(Uint8Array.from(koffi.decode(id, 'uint8_t', Number(idLen))));
1526
+ return callback(rowId, view);
1527
+ };
1528
+ return Number(fn.hk_cursor_walk_view(e, q, koffi.register(wrapper, 'HkViewWalkCallback *'), null));
1529
+ },
1530
+ rawDocBytes: (raw) => {
1531
+ const len: number[] = [0];
1532
+ const ptr = fn.hk_rawdoc_bytes(raw, len);
1533
+ return ptr ? Uint8Array.from(koffi.decode(ptr, 'uint8_t', Number(len[0]))) : null;
1534
+ },
1535
+ rawDocId: (raw) => {
1536
+ const len: number[] = [0];
1537
+ const ptr = fn.hk_rawdoc_id(raw, len);
1538
+ return ptr ? new TextDecoder().decode(Uint8Array.from(koffi.decode(ptr, 'uint8_t', Number(len[0])))) : null;
1539
+ },
1540
+ viewGetStr: (view, key) => {
1541
+ const len: number[] = [0];
1542
+ const ptr = fn.hk_view_get_str(view, key, len);
1543
+ return ptr ? new TextDecoder().decode(Uint8Array.from(koffi.decode(ptr, 'uint8_t', Number(len[0])))) : null;
1544
+ },
1545
+ viewGetBytes: (view, key) => {
1546
+ const len: number[] = [0];
1547
+ const ptr = fn.hk_view_get_bytes(view, key, len);
1548
+ return ptr ? Uint8Array.from(koffi.decode(ptr, 'uint8_t', Number(len[0]))) : null;
1549
+ },
1550
+
1551
+ // P2 (core 0.18): JSON read paths + dotted-path pulls.
1552
+ engineGetJson: (e, c, id) => ptrToStringAndFree(fn.hk_engine_get_json(e, c, id)),
1553
+ queryExecuteJson: (e, q) => ptrToStringAndFree(fn.hk_query_execute_json(e, q)),
1554
+ viewToJson: (view) => ptrToStringAndFree(fn.hk_view_to_json(view)),
1555
+ cursorWalkJson: (e, q, callback) => {
1556
+ const wrapper = (id: any, idLen: number, json: any, jsonLen: number) => {
1557
+ const rowId = new TextDecoder().decode(Uint8Array.from(koffi.decode(id, 'uint8_t', Number(idLen))));
1558
+ const rowJson = Uint8Array.from(koffi.decode(json, 'uint8_t', Number(jsonLen)));
1559
+ return callback(rowId, rowJson);
1560
+ };
1561
+ return Number(fn.hk_cursor_walk_json(e, q, koffi.register(wrapper, 'HkWalkCallback *'), null));
1562
+ },
1563
+ viewGetPathInt: (view, path) => {
1564
+ const out: bigint[] = [0n];
1565
+ return fn.hk_view_get_path_int(view, path, out) ? out[0] : null;
1566
+ },
1567
+ viewGetPathFloat: (view, path) => {
1568
+ const out: number[] = [0];
1569
+ return fn.hk_view_get_path_float(view, path, out) ? out[0] : null;
1570
+ },
1571
+ viewGetPathBool: (view, path) => {
1572
+ const r: number = fn.hk_view_get_path_bool(view, path);
1573
+ return r < 0 ? null : r !== 0;
1574
+ },
1575
+ viewGetPathStr: (view, path) => {
1576
+ const len: number[] = [0];
1577
+ const ptr = fn.hk_view_get_path_str(view, path, len);
1578
+ return ptr ? new TextDecoder().decode(Uint8Array.from(koffi.decode(ptr, 'uint8_t', Number(len[0])))) : null;
1579
+ },
1580
+ viewGetPathBytes: (view, path) => {
1581
+ const len: number[] = [0];
1582
+ const ptr = fn.hk_view_get_path_bytes(view, path, len);
1583
+ return ptr ? Uint8Array.from(koffi.decode(ptr, 'uint8_t', Number(len[0]))) : null;
1584
+ },
1585
+
1586
+ lastError: () => (fn.hk_last_error() as string) || 'unknown ffi error'
1587
+ };
1588
+ }
1589
+
1590
+ export async function loadNativeBindings(explicitPath?: string): Promise<NativeBindings> {
1591
+ const libPath = resolveLibraryPath(explicitPath);
1592
+ // return isBunRuntime() ? createBunBindings(libPath) : createNodeBindings(libPath);
1593
+
1594
+ // 1. Check for Bun
1595
+ if (typeof (globalThis as any).Bun !== 'undefined') {
1596
+ return createBunBindings(libPath);
1597
+ }
1598
+
1599
+ // 2. Check for Node.js (via process)
1600
+ if (typeof process !== 'undefined' && process.versions && process.versions.node) {
1601
+ return createNodeBindings(libPath);
1602
+ }
1603
+
1604
+ throw new Error("HakoDB Native Bindings are only supported in Node.js or Bun environments. For browsers, use the Tauri Gateway.");
1605
+ }