quiverdb 0.6.1 → 0.9.2

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.
Files changed (88) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +95 -17
  3. package/libs/linux-x86_64/libquiver.so +0 -0
  4. package/libs/linux-x86_64/libquiver.so.0 +0 -0
  5. package/libs/linux-x86_64/libquiver_c.so +0 -0
  6. package/libs/windows-x86_64/libquiver.dll +0 -0
  7. package/libs/windows-x86_64/libquiver_c.dll +0 -0
  8. package/mod.ts +25 -0
  9. package/package.json +17 -30
  10. package/src/composites.ts +89 -0
  11. package/src/create.ts +152 -0
  12. package/src/csv.ts +118 -0
  13. package/src/database.ts +171 -0
  14. package/src/errors.ts +21 -0
  15. package/src/ffi-helpers.ts +157 -0
  16. package/src/index.ts +17 -0
  17. package/src/introspection.ts +33 -0
  18. package/src/loader.ts +285 -0
  19. package/src/lua-runner.ts +48 -0
  20. package/src/metadata.ts +218 -0
  21. package/src/query.ts +160 -0
  22. package/src/read.ts +572 -0
  23. package/src/time-series.ts +291 -0
  24. package/src/transaction.ts +27 -0
  25. package/{dist/types.d.ts → src/types.ts} +8 -1
  26. package/dist/composites.d.ts +0 -9
  27. package/dist/composites.d.ts.map +0 -1
  28. package/dist/composites.js +0 -69
  29. package/dist/composites.js.map +0 -1
  30. package/dist/create.d.ts +0 -9
  31. package/dist/create.d.ts.map +0 -1
  32. package/dist/create.js +0 -104
  33. package/dist/create.js.map +0 -1
  34. package/dist/csv.d.ts +0 -11
  35. package/dist/csv.d.ts.map +0 -1
  36. package/dist/csv.js +0 -67
  37. package/dist/csv.js.map +0 -1
  38. package/dist/database.d.ts +0 -13
  39. package/dist/database.d.ts.map +0 -1
  40. package/dist/database.js +0 -42
  41. package/dist/database.js.map +0 -1
  42. package/dist/errors.d.ts +0 -5
  43. package/dist/errors.d.ts.map +0 -1
  44. package/dist/errors.js +0 -18
  45. package/dist/errors.js.map +0 -1
  46. package/dist/ffi-helpers.d.ts +0 -57
  47. package/dist/ffi-helpers.d.ts.map +0 -1
  48. package/dist/ffi-helpers.js +0 -136
  49. package/dist/ffi-helpers.js.map +0 -1
  50. package/dist/index.d.ts +0 -17
  51. package/dist/index.d.ts.map +0 -1
  52. package/dist/index.js +0 -13
  53. package/dist/index.js.map +0 -1
  54. package/dist/introspection.d.ts +0 -10
  55. package/dist/introspection.d.ts.map +0 -1
  56. package/dist/introspection.js +0 -27
  57. package/dist/introspection.js.map +0 -1
  58. package/dist/loader.d.ts +0 -7
  59. package/dist/loader.d.ts.map +0 -1
  60. package/dist/loader.js +0 -165
  61. package/dist/loader.js.map +0 -1
  62. package/dist/lua-runner.d.ts +0 -11
  63. package/dist/lua-runner.d.ts.map +0 -1
  64. package/dist/lua-runner.js +0 -43
  65. package/dist/lua-runner.js.map +0 -1
  66. package/dist/metadata.d.ts +0 -28
  67. package/dist/metadata.d.ts.map +0 -1
  68. package/dist/metadata.js +0 -100
  69. package/dist/metadata.js.map +0 -1
  70. package/dist/query.d.ts +0 -9
  71. package/dist/query.d.ts.map +0 -1
  72. package/dist/query.js +0 -90
  73. package/dist/query.js.map +0 -1
  74. package/dist/read.d.ts +0 -25
  75. package/dist/read.d.ts.map +0 -1
  76. package/dist/read.js +0 -220
  77. package/dist/read.js.map +0 -1
  78. package/dist/time-series.d.ts +0 -12
  79. package/dist/time-series.d.ts.map +0 -1
  80. package/dist/time-series.js +0 -155
  81. package/dist/time-series.js.map +0 -1
  82. package/dist/transaction.d.ts +0 -10
  83. package/dist/transaction.d.ts.map +0 -1
  84. package/dist/transaction.js +0 -22
  85. package/dist/transaction.js.map +0 -1
  86. package/dist/types.d.ts.map +0 -1
  87. package/dist/types.js +0 -2
  88. package/dist/types.js.map +0 -1
package/src/read.ts ADDED
@@ -0,0 +1,572 @@
1
+ import { ptr } from "bun:ffi";
2
+ import { Database } from "./database.ts";
3
+ import { check } from "./errors.ts";
4
+ import {
5
+ allocPtrOut,
6
+ allocUint64Out,
7
+ decodeFloat64Array,
8
+ decodeInt64Array,
9
+ decodePtrArray,
10
+ decodeStringArray,
11
+ decodeStringFromBuf,
12
+ decodeUint64Array,
13
+ readPtrOut,
14
+ readUint64Out,
15
+ toCString,
16
+ } from "./ffi-helpers.ts";
17
+ import { getSymbols, type NativePointer } from "./loader.ts";
18
+
19
+ // --- Scalar array reads ---
20
+
21
+ Database.prototype.readScalarIntegers = function (
22
+ this: Database,
23
+ collection: string,
24
+ attribute: string,
25
+ ): number[] {
26
+ const lib = getSymbols();
27
+ const collBuf = toCString(collection);
28
+ const attrBuf = toCString(attribute);
29
+ const outValues = allocPtrOut();
30
+ const outCount = allocUint64Out();
31
+ check(
32
+ lib.quiver_database_read_scalar_integers(
33
+ this._handle,
34
+ collBuf.buf,
35
+ attrBuf.buf,
36
+ outValues.ptr,
37
+ outCount.ptr,
38
+ ),
39
+ );
40
+ const count = readUint64Out(outCount);
41
+ if (count === 0) return [];
42
+ const arrPtr = readPtrOut(outValues);
43
+ const result = decodeInt64Array(arrPtr, count);
44
+ lib.quiver_database_free_integer_array(arrPtr);
45
+ return result;
46
+ };
47
+
48
+ Database.prototype.readScalarFloats = function (
49
+ this: Database,
50
+ collection: string,
51
+ attribute: string,
52
+ ): number[] {
53
+ const lib = getSymbols();
54
+ const collBuf = toCString(collection);
55
+ const attrBuf = toCString(attribute);
56
+ const outValues = allocPtrOut();
57
+ const outCount = allocUint64Out();
58
+ check(
59
+ lib.quiver_database_read_scalar_floats(
60
+ this._handle,
61
+ collBuf.buf,
62
+ attrBuf.buf,
63
+ outValues.ptr,
64
+ outCount.ptr,
65
+ ),
66
+ );
67
+ const count = readUint64Out(outCount);
68
+ if (count === 0) return [];
69
+ const arrPtr = readPtrOut(outValues);
70
+ const result = decodeFloat64Array(arrPtr, count);
71
+ lib.quiver_database_free_float_array(arrPtr);
72
+ return result;
73
+ };
74
+
75
+ Database.prototype.readScalarStrings = function (
76
+ this: Database,
77
+ collection: string,
78
+ attribute: string,
79
+ ): string[] {
80
+ const lib = getSymbols();
81
+ const collBuf = toCString(collection);
82
+ const attrBuf = toCString(attribute);
83
+ const outValues = allocPtrOut();
84
+ const outCount = allocUint64Out();
85
+ check(
86
+ lib.quiver_database_read_scalar_strings(
87
+ this._handle,
88
+ collBuf.buf,
89
+ attrBuf.buf,
90
+ outValues.ptr,
91
+ outCount.ptr,
92
+ ),
93
+ );
94
+ const count = readUint64Out(outCount);
95
+ if (count === 0) return [];
96
+ const arrPtr = readPtrOut(outValues);
97
+ const result = decodeStringArray(arrPtr, count);
98
+ lib.quiver_database_free_string_array(arrPtr, BigInt(count));
99
+ return result;
100
+ };
101
+
102
+ // --- Scalar by-ID reads ---
103
+
104
+ Database.prototype.readScalarIntegerById = function (
105
+ this: Database,
106
+ collection: string,
107
+ attribute: string,
108
+ id: number,
109
+ ): number | null {
110
+ const lib = getSymbols();
111
+ const collBuf = toCString(collection);
112
+ const attrBuf = toCString(attribute);
113
+ const outValBuf = new Uint8Array(8);
114
+ const outValPtr = ptr(outValBuf);
115
+ const outHasBuf = new Uint8Array(4);
116
+ const outHasPtr = ptr(outHasBuf);
117
+ check(
118
+ lib.quiver_database_read_scalar_integer_by_id(
119
+ this._handle,
120
+ collBuf.buf,
121
+ attrBuf.buf,
122
+ BigInt(id),
123
+ outValPtr,
124
+ outHasPtr,
125
+ ),
126
+ );
127
+ if (new DataView(outHasBuf.buffer).getInt32(0, true) === 0) return null;
128
+ return Number(new DataView(outValBuf.buffer).getBigInt64(0, true));
129
+ };
130
+
131
+ Database.prototype.readScalarFloatById = function (
132
+ this: Database,
133
+ collection: string,
134
+ attribute: string,
135
+ id: number,
136
+ ): number | null {
137
+ const lib = getSymbols();
138
+ const collBuf = toCString(collection);
139
+ const attrBuf = toCString(attribute);
140
+ const outValBuf = new Uint8Array(8);
141
+ const outValPtr = ptr(outValBuf);
142
+ const outHasBuf = new Uint8Array(4);
143
+ const outHasPtr = ptr(outHasBuf);
144
+ check(
145
+ lib.quiver_database_read_scalar_float_by_id(
146
+ this._handle,
147
+ collBuf.buf,
148
+ attrBuf.buf,
149
+ BigInt(id),
150
+ outValPtr,
151
+ outHasPtr,
152
+ ),
153
+ );
154
+ if (new DataView(outHasBuf.buffer).getInt32(0, true) === 0) return null;
155
+ return new DataView(outValBuf.buffer).getFloat64(0, true);
156
+ };
157
+
158
+ Database.prototype.readScalarStringById = function (
159
+ this: Database,
160
+ collection: string,
161
+ attribute: string,
162
+ id: number,
163
+ ): string | null {
164
+ const lib = getSymbols();
165
+ const collBuf = toCString(collection);
166
+ const attrBuf = toCString(attribute);
167
+ const outValue = allocPtrOut();
168
+ const outHasBuf = new Uint8Array(4);
169
+ const outHasPtr = ptr(outHasBuf);
170
+ check(
171
+ lib.quiver_database_read_scalar_string_by_id(
172
+ this._handle,
173
+ collBuf.buf,
174
+ attrBuf.buf,
175
+ BigInt(id),
176
+ outValue.ptr,
177
+ outHasPtr,
178
+ ),
179
+ );
180
+ if (new DataView(outHasBuf.buffer).getInt32(0, true) === 0) return null;
181
+ const result = decodeStringFromBuf(outValue);
182
+ lib.quiver_database_free_string(readPtrOut(outValue));
183
+ return result;
184
+ };
185
+
186
+ // --- Read element IDs ---
187
+
188
+ Database.prototype.readElementIds = function (this: Database, collection: string): number[] {
189
+ const lib = getSymbols();
190
+ const collBuf = toCString(collection);
191
+ const outIds = allocPtrOut();
192
+ const outCount = allocUint64Out();
193
+ check(lib.quiver_database_read_element_ids(this._handle, collBuf.buf, outIds.ptr, outCount.ptr));
194
+ const count = readUint64Out(outCount);
195
+ if (count === 0) return [];
196
+ const arrPtr = readPtrOut(outIds);
197
+ const result = decodeInt64Array(arrPtr, count);
198
+ lib.quiver_database_free_integer_array(arrPtr);
199
+ return result;
200
+ };
201
+
202
+ // --- Vector bulk reads ---
203
+
204
+ function readBulkIntegers(
205
+ lib: ReturnType<typeof getSymbols>,
206
+ handle: NativePointer,
207
+ fn: string,
208
+ collection: string,
209
+ attribute: string,
210
+ ): number[][] {
211
+ const collBuf = toCString(collection);
212
+ const attrBuf = toCString(attribute);
213
+ const outVectors = allocPtrOut();
214
+ const outSizes = allocPtrOut();
215
+ const outCount = allocUint64Out();
216
+ check(
217
+ (lib as Record<string, Function>)[fn](
218
+ handle,
219
+ collBuf.buf,
220
+ attrBuf.buf,
221
+ outVectors.ptr,
222
+ outSizes.ptr,
223
+ outCount.ptr,
224
+ ),
225
+ );
226
+ const count = readUint64Out(outCount);
227
+ if (count === 0) return [];
228
+ const vectorsPtr = readPtrOut(outVectors);
229
+ const sizesPtr = readPtrOut(outSizes);
230
+ const vectorPtrs = decodePtrArray(vectorsPtr, count);
231
+ const sizes = decodeUint64Array(sizesPtr, count);
232
+ const result: number[][] = new Array(count);
233
+ for (let i = 0; i < count; i++) {
234
+ result[i] = sizes[i] === 0 ? [] : decodeInt64Array(vectorPtrs[i], sizes[i]);
235
+ }
236
+ lib.quiver_database_free_integer_vectors(vectorsPtr, sizesPtr, BigInt(count));
237
+ return result;
238
+ }
239
+
240
+ function readBulkFloats(
241
+ lib: ReturnType<typeof getSymbols>,
242
+ handle: NativePointer,
243
+ fn: string,
244
+ collection: string,
245
+ attribute: string,
246
+ ): number[][] {
247
+ const collBuf = toCString(collection);
248
+ const attrBuf = toCString(attribute);
249
+ const outVectors = allocPtrOut();
250
+ const outSizes = allocPtrOut();
251
+ const outCount = allocUint64Out();
252
+ check(
253
+ (lib as Record<string, Function>)[fn](
254
+ handle,
255
+ collBuf.buf,
256
+ attrBuf.buf,
257
+ outVectors.ptr,
258
+ outSizes.ptr,
259
+ outCount.ptr,
260
+ ),
261
+ );
262
+ const count = readUint64Out(outCount);
263
+ if (count === 0) return [];
264
+ const vectorsPtr = readPtrOut(outVectors);
265
+ const sizesPtr = readPtrOut(outSizes);
266
+ const vectorPtrs = decodePtrArray(vectorsPtr, count);
267
+ const sizes = decodeUint64Array(sizesPtr, count);
268
+ const result: number[][] = new Array(count);
269
+ for (let i = 0; i < count; i++) {
270
+ result[i] = sizes[i] === 0 ? [] : decodeFloat64Array(vectorPtrs[i], sizes[i]);
271
+ }
272
+ lib.quiver_database_free_float_vectors(vectorsPtr, sizesPtr, BigInt(count));
273
+ return result;
274
+ }
275
+
276
+ function readBulkStrings(
277
+ lib: ReturnType<typeof getSymbols>,
278
+ handle: NativePointer,
279
+ fn: string,
280
+ collection: string,
281
+ attribute: string,
282
+ ): string[][] {
283
+ const collBuf = toCString(collection);
284
+ const attrBuf = toCString(attribute);
285
+ const outVectors = allocPtrOut();
286
+ const outSizes = allocPtrOut();
287
+ const outCount = allocUint64Out();
288
+ check(
289
+ (lib as Record<string, Function>)[fn](
290
+ handle,
291
+ collBuf.buf,
292
+ attrBuf.buf,
293
+ outVectors.ptr,
294
+ outSizes.ptr,
295
+ outCount.ptr,
296
+ ),
297
+ );
298
+ const count = readUint64Out(outCount);
299
+ if (count === 0) return [];
300
+ const vectorsPtr = readPtrOut(outVectors);
301
+ const sizesPtr = readPtrOut(outSizes);
302
+ const vectorPtrs = decodePtrArray(vectorsPtr, count);
303
+ const sizes = decodeUint64Array(sizesPtr, count);
304
+ const result: string[][] = new Array(count);
305
+ for (let i = 0; i < count; i++) {
306
+ result[i] = sizes[i] === 0 ? [] : decodeStringArray(vectorPtrs[i], sizes[i]);
307
+ }
308
+ lib.quiver_database_free_string_vectors(vectorsPtr, sizesPtr, BigInt(count));
309
+ return result;
310
+ }
311
+
312
+ Database.prototype.readVectorIntegers = function (
313
+ this: Database,
314
+ collection: string,
315
+ attribute: string,
316
+ ): number[][] {
317
+ return readBulkIntegers(
318
+ getSymbols(),
319
+ this._handle,
320
+ "quiver_database_read_vector_integers",
321
+ collection,
322
+ attribute,
323
+ );
324
+ };
325
+ Database.prototype.readVectorFloats = function (
326
+ this: Database,
327
+ collection: string,
328
+ attribute: string,
329
+ ): number[][] {
330
+ return readBulkFloats(
331
+ getSymbols(),
332
+ this._handle,
333
+ "quiver_database_read_vector_floats",
334
+ collection,
335
+ attribute,
336
+ );
337
+ };
338
+ Database.prototype.readVectorStrings = function (
339
+ this: Database,
340
+ collection: string,
341
+ attribute: string,
342
+ ): string[][] {
343
+ return readBulkStrings(
344
+ getSymbols(),
345
+ this._handle,
346
+ "quiver_database_read_vector_strings",
347
+ collection,
348
+ attribute,
349
+ );
350
+ };
351
+ Database.prototype.readSetIntegers = function (
352
+ this: Database,
353
+ collection: string,
354
+ attribute: string,
355
+ ): number[][] {
356
+ return readBulkIntegers(
357
+ getSymbols(),
358
+ this._handle,
359
+ "quiver_database_read_set_integers",
360
+ collection,
361
+ attribute,
362
+ );
363
+ };
364
+ Database.prototype.readSetFloats = function (
365
+ this: Database,
366
+ collection: string,
367
+ attribute: string,
368
+ ): number[][] {
369
+ return readBulkFloats(
370
+ getSymbols(),
371
+ this._handle,
372
+ "quiver_database_read_set_floats",
373
+ collection,
374
+ attribute,
375
+ );
376
+ };
377
+ Database.prototype.readSetStrings = function (
378
+ this: Database,
379
+ collection: string,
380
+ attribute: string,
381
+ ): string[][] {
382
+ return readBulkStrings(
383
+ getSymbols(),
384
+ this._handle,
385
+ "quiver_database_read_set_strings",
386
+ collection,
387
+ attribute,
388
+ );
389
+ };
390
+
391
+ // --- By-ID reads ---
392
+
393
+ function readByIdIntegers(
394
+ lib: ReturnType<typeof getSymbols>,
395
+ handle: NativePointer,
396
+ fn: string,
397
+ collection: string,
398
+ attribute: string,
399
+ id: number,
400
+ ): number[] {
401
+ const collBuf = toCString(collection);
402
+ const attrBuf = toCString(attribute);
403
+ const outValues = allocPtrOut();
404
+ const outCount = allocUint64Out();
405
+ check(
406
+ (lib as Record<string, Function>)[fn](
407
+ handle,
408
+ collBuf.buf,
409
+ attrBuf.buf,
410
+ BigInt(id),
411
+ outValues.ptr,
412
+ outCount.ptr,
413
+ ),
414
+ );
415
+ const count = readUint64Out(outCount);
416
+ if (count === 0) return [];
417
+ const arrPtr = readPtrOut(outValues);
418
+ const result = decodeInt64Array(arrPtr, count);
419
+ lib.quiver_database_free_integer_array(arrPtr);
420
+ return result;
421
+ }
422
+
423
+ function readByIdFloats(
424
+ lib: ReturnType<typeof getSymbols>,
425
+ handle: NativePointer,
426
+ fn: string,
427
+ collection: string,
428
+ attribute: string,
429
+ id: number,
430
+ ): number[] {
431
+ const collBuf = toCString(collection);
432
+ const attrBuf = toCString(attribute);
433
+ const outValues = allocPtrOut();
434
+ const outCount = allocUint64Out();
435
+ check(
436
+ (lib as Record<string, Function>)[fn](
437
+ handle,
438
+ collBuf.buf,
439
+ attrBuf.buf,
440
+ BigInt(id),
441
+ outValues.ptr,
442
+ outCount.ptr,
443
+ ),
444
+ );
445
+ const count = readUint64Out(outCount);
446
+ if (count === 0) return [];
447
+ const arrPtr = readPtrOut(outValues);
448
+ const result = decodeFloat64Array(arrPtr, count);
449
+ lib.quiver_database_free_float_array(arrPtr);
450
+ return result;
451
+ }
452
+
453
+ function readByIdStrings(
454
+ lib: ReturnType<typeof getSymbols>,
455
+ handle: NativePointer,
456
+ fn: string,
457
+ collection: string,
458
+ attribute: string,
459
+ id: number,
460
+ ): string[] {
461
+ const collBuf = toCString(collection);
462
+ const attrBuf = toCString(attribute);
463
+ const outValues = allocPtrOut();
464
+ const outCount = allocUint64Out();
465
+ check(
466
+ (lib as Record<string, Function>)[fn](
467
+ handle,
468
+ collBuf.buf,
469
+ attrBuf.buf,
470
+ BigInt(id),
471
+ outValues.ptr,
472
+ outCount.ptr,
473
+ ),
474
+ );
475
+ const count = readUint64Out(outCount);
476
+ if (count === 0) return [];
477
+ const arrPtr = readPtrOut(outValues);
478
+ const result = decodeStringArray(arrPtr, count);
479
+ lib.quiver_database_free_string_array(arrPtr, BigInt(count));
480
+ return result;
481
+ }
482
+
483
+ Database.prototype.readVectorIntegersById = function (
484
+ this: Database,
485
+ collection: string,
486
+ attribute: string,
487
+ id: number,
488
+ ): number[] {
489
+ return readByIdIntegers(
490
+ getSymbols(),
491
+ this._handle,
492
+ "quiver_database_read_vector_integers_by_id",
493
+ collection,
494
+ attribute,
495
+ id,
496
+ );
497
+ };
498
+ Database.prototype.readVectorFloatsById = function (
499
+ this: Database,
500
+ collection: string,
501
+ attribute: string,
502
+ id: number,
503
+ ): number[] {
504
+ return readByIdFloats(
505
+ getSymbols(),
506
+ this._handle,
507
+ "quiver_database_read_vector_floats_by_id",
508
+ collection,
509
+ attribute,
510
+ id,
511
+ );
512
+ };
513
+ Database.prototype.readVectorStringsById = function (
514
+ this: Database,
515
+ collection: string,
516
+ attribute: string,
517
+ id: number,
518
+ ): string[] {
519
+ return readByIdStrings(
520
+ getSymbols(),
521
+ this._handle,
522
+ "quiver_database_read_vector_strings_by_id",
523
+ collection,
524
+ attribute,
525
+ id,
526
+ );
527
+ };
528
+ Database.prototype.readSetIntegersById = function (
529
+ this: Database,
530
+ collection: string,
531
+ attribute: string,
532
+ id: number,
533
+ ): number[] {
534
+ return readByIdIntegers(
535
+ getSymbols(),
536
+ this._handle,
537
+ "quiver_database_read_set_integers_by_id",
538
+ collection,
539
+ attribute,
540
+ id,
541
+ );
542
+ };
543
+ Database.prototype.readSetFloatsById = function (
544
+ this: Database,
545
+ collection: string,
546
+ attribute: string,
547
+ id: number,
548
+ ): number[] {
549
+ return readByIdFloats(
550
+ getSymbols(),
551
+ this._handle,
552
+ "quiver_database_read_set_floats_by_id",
553
+ collection,
554
+ attribute,
555
+ id,
556
+ );
557
+ };
558
+ Database.prototype.readSetStringsById = function (
559
+ this: Database,
560
+ collection: string,
561
+ attribute: string,
562
+ id: number,
563
+ ): string[] {
564
+ return readByIdStrings(
565
+ getSymbols(),
566
+ this._handle,
567
+ "quiver_database_read_set_strings_by_id",
568
+ collection,
569
+ attribute,
570
+ id,
571
+ );
572
+ };