xitdb 0.1.0 → 0.3.0

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 (64) hide show
  1. package/LICENSE +21 -0
  2. package/dist/core-buffered-file.d.ts +41 -0
  3. package/dist/core-file.d.ts +18 -0
  4. package/dist/core-memory.d.ts +36 -0
  5. package/dist/core.d.ts +23 -0
  6. package/dist/database.d.ts +244 -0
  7. package/dist/exceptions.d.ts +51 -0
  8. package/dist/hasher.d.ts +9 -0
  9. package/dist/index.d.ts +26 -0
  10. package/dist/index.js +429 -266
  11. package/dist/read-array-list.d.ts +13 -0
  12. package/dist/read-counted-hash-map.d.ts +7 -0
  13. package/dist/read-counted-hash-set.d.ts +7 -0
  14. package/dist/read-cursor.d.ts +57 -0
  15. package/dist/read-hash-map.d.ts +27 -0
  16. package/dist/read-hash-set.d.ts +18 -0
  17. package/dist/read-linked-array-list.d.ts +13 -0
  18. package/dist/slot-pointer.d.ts +7 -0
  19. package/dist/slot.d.ts +15 -0
  20. package/{src/slotted.ts → dist/slotted.d.ts} +1 -2
  21. package/dist/tag.d.ts +17 -0
  22. package/dist/write-array-list.d.ts +16 -0
  23. package/dist/write-counted-hash-map.d.ts +7 -0
  24. package/dist/write-counted-hash-set.d.ts +7 -0
  25. package/dist/write-cursor.d.ts +36 -0
  26. package/dist/write-hash-map.d.ts +25 -0
  27. package/dist/write-hash-set.d.ts +19 -0
  28. package/dist/write-linked-array-list.d.ts +19 -0
  29. package/dist/writeable-data.d.ts +20 -0
  30. package/package.json +14 -1
  31. package/.claude/settings.local.json +0 -9
  32. package/bun.lock +0 -24
  33. package/bunfig.toml +0 -1
  34. package/example/README.md +0 -46
  35. package/example/dump.ts +0 -201
  36. package/src/core-buffered-file.ts +0 -226
  37. package/src/core-file.ts +0 -137
  38. package/src/core-memory.ts +0 -179
  39. package/src/core.ts +0 -25
  40. package/src/database.ts +0 -2232
  41. package/src/exceptions.ts +0 -31
  42. package/src/hasher.ts +0 -52
  43. package/src/index.ts +0 -110
  44. package/src/read-array-list.ts +0 -45
  45. package/src/read-counted-hash-map.ts +0 -28
  46. package/src/read-counted-hash-set.ts +0 -28
  47. package/src/read-cursor.ts +0 -546
  48. package/src/read-hash-map.ts +0 -117
  49. package/src/read-hash-set.ts +0 -70
  50. package/src/read-linked-array-list.ts +0 -45
  51. package/src/slot-pointer.ts +0 -15
  52. package/src/slot.ts +0 -51
  53. package/src/tag.ts +0 -23
  54. package/src/write-array-list.ts +0 -65
  55. package/src/write-counted-hash-map.ts +0 -31
  56. package/src/write-counted-hash-set.ts +0 -31
  57. package/src/write-cursor.ts +0 -166
  58. package/src/write-hash-map.ts +0 -129
  59. package/src/write-hash-set.ts +0 -86
  60. package/src/write-linked-array-list.ts +0 -80
  61. package/src/writeable-data.ts +0 -67
  62. package/tests/database.test.ts +0 -2519
  63. package/tests/fixtures/test.db +0 -0
  64. package/tsconfig.json +0 -17
package/dist/index.js CHANGED
@@ -1,5 +1,20 @@
1
1
  // @bun
2
+ var __create = Object.create;
3
+ var __getProtoOf = Object.getPrototypeOf;
2
4
  var __defProp = Object.defineProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __toESM = (mod, isNodeMode, target) => {
8
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
9
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
10
+ for (let key of __getOwnPropNames(mod))
11
+ if (!__hasOwnProp.call(to, key))
12
+ __defProp(to, key, {
13
+ get: () => mod[key],
14
+ enumerable: true
15
+ });
16
+ return to;
17
+ };
3
18
  var __export = (target, all) => {
4
19
  for (var name in all)
5
20
  __defProp(target, name, {
@@ -10,6 +25,7 @@ var __export = (target, all) => {
10
25
  });
11
26
  };
12
27
  var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
28
+ var __require = import.meta.require;
13
29
 
14
30
  // src/tag.ts
15
31
  function tagValueOf(n) {
@@ -45,7 +61,7 @@ class Slot {
45
61
  tag;
46
62
  full;
47
63
  constructor(value = 0n, tag = 0 /* NONE */, full = false) {
48
- this.value = value;
64
+ this.value = typeof value === "bigint" ? value : BigInt(value);
49
65
  this.tag = tag;
50
66
  this.full = full;
51
67
  }
@@ -159,7 +175,7 @@ var init_exceptions = __esm(() => {
159
175
  class Uint {
160
176
  value;
161
177
  constructor(value) {
162
- if (value < 0n) {
178
+ if (value < 0) {
163
179
  throw new Error("Uint must not be negative");
164
180
  }
165
181
  this.value = value;
@@ -246,24 +262,24 @@ class Reader {
246
262
  if (this.size < this.relativePosition)
247
263
  throw new EndOfStreamException;
248
264
  await this.parent.db.core.seek(this.startPosition + this.relativePosition);
249
- const readSize = Math.min(buffer.length, Number(this.size - this.relativePosition));
265
+ const readSize = Math.min(buffer.length, this.size - this.relativePosition);
250
266
  if (readSize === 0)
251
267
  return -1;
252
268
  const reader = this.parent.db.core.reader();
253
269
  const tempBuffer = new Uint8Array(readSize);
254
270
  await reader.readFully(tempBuffer);
255
271
  buffer.set(tempBuffer);
256
- this.relativePosition += BigInt(readSize);
272
+ this.relativePosition += readSize;
257
273
  return readSize;
258
274
  }
259
275
  async readFully(buffer) {
260
- if (this.size < this.relativePosition || this.size - this.relativePosition < BigInt(buffer.length)) {
276
+ if (this.size < this.relativePosition || this.size - this.relativePosition < buffer.length) {
261
277
  throw new EndOfStreamException;
262
278
  }
263
279
  await this.parent.db.core.seek(this.startPosition + this.relativePosition);
264
280
  const reader = this.parent.db.core.reader();
265
281
  await reader.readFully(buffer);
266
- this.relativePosition += BigInt(buffer.length);
282
+ this.relativePosition += buffer.length;
267
283
  }
268
284
  async readByte() {
269
285
  const bytes = new Uint8Array(1);
@@ -289,7 +305,7 @@ class Reader {
289
305
  const bytes = new Uint8Array(readSize);
290
306
  await this.readFully(bytes);
291
307
  const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
292
- return view.getBigInt64(0, false);
308
+ return Number(view.getBigInt64(0, false));
293
309
  }
294
310
  seek(position) {
295
311
  if (position > this.size) {
@@ -312,8 +328,8 @@ class IteratorLevel {
312
328
 
313
329
  class CursorIterator {
314
330
  cursor;
315
- size = 0n;
316
- index = 0n;
331
+ size = 0;
332
+ index = 0;
317
333
  stack = [];
318
334
  nextCursorMaybe = null;
319
335
  initialized = false;
@@ -326,45 +342,45 @@ class CursorIterator {
326
342
  this.initialized = true;
327
343
  switch (this.cursor.slotPtr.slot.tag) {
328
344
  case 0 /* NONE */:
329
- this.size = 0n;
330
- this.index = 0n;
345
+ this.size = 0;
346
+ this.index = 0;
331
347
  this.stack = [];
332
348
  break;
333
349
  case 2 /* ARRAY_LIST */: {
334
- const position = this.cursor.slotPtr.slot.value;
350
+ const position = Number(this.cursor.slotPtr.slot.value);
335
351
  await this.cursor.db.core.seek(position);
336
352
  const reader = this.cursor.db.core.reader();
337
353
  const headerBytes = new Uint8Array(ArrayListHeader.LENGTH);
338
354
  await reader.readFully(headerBytes);
339
355
  const header = ArrayListHeader.fromBytes(headerBytes);
340
356
  this.size = await this.cursor.count();
341
- this.index = 0n;
357
+ this.index = 0;
342
358
  this.stack = await this.initStack(this.cursor, header.ptr, INDEX_BLOCK_SIZE);
343
359
  break;
344
360
  }
345
361
  case 3 /* LINKED_ARRAY_LIST */: {
346
- const position = this.cursor.slotPtr.slot.value;
362
+ const position = Number(this.cursor.slotPtr.slot.value);
347
363
  await this.cursor.db.core.seek(position);
348
364
  const reader = this.cursor.db.core.reader();
349
365
  const headerBytes = new Uint8Array(LinkedArrayListHeader.LENGTH);
350
366
  await reader.readFully(headerBytes);
351
367
  const header = LinkedArrayListHeader.fromBytes(headerBytes);
352
368
  this.size = await this.cursor.count();
353
- this.index = 0n;
369
+ this.index = 0;
354
370
  this.stack = await this.initStack(this.cursor, header.ptr, LINKED_ARRAY_LIST_INDEX_BLOCK_SIZE);
355
371
  break;
356
372
  }
357
373
  case 4 /* HASH_MAP */:
358
374
  case 11 /* HASH_SET */:
359
- this.size = 0n;
360
- this.index = 0n;
361
- this.stack = await this.initStack(this.cursor, this.cursor.slotPtr.slot.value, INDEX_BLOCK_SIZE);
375
+ this.size = 0;
376
+ this.index = 0;
377
+ this.stack = await this.initStack(this.cursor, Number(this.cursor.slotPtr.slot.value), INDEX_BLOCK_SIZE);
362
378
  break;
363
379
  case 12 /* COUNTED_HASH_MAP */:
364
380
  case 13 /* COUNTED_HASH_SET */:
365
- this.size = 0n;
366
- this.index = 0n;
367
- this.stack = await this.initStack(this.cursor, this.cursor.slotPtr.slot.value + 8n, INDEX_BLOCK_SIZE);
381
+ this.size = 0;
382
+ this.index = 0;
383
+ this.stack = await this.initStack(this.cursor, Number(this.cursor.slotPtr.slot.value) + 8, INDEX_BLOCK_SIZE);
368
384
  break;
369
385
  default:
370
386
  throw new UnexpectedTagException;
@@ -412,12 +428,12 @@ class CursorIterator {
412
428
  case 2 /* ARRAY_LIST */:
413
429
  if (!await this.hasNext())
414
430
  return null;
415
- this.index += 1n;
431
+ this.index += 1;
416
432
  return this.nextInternal(INDEX_BLOCK_SIZE);
417
433
  case 3 /* LINKED_ARRAY_LIST */:
418
434
  if (!await this.hasNext())
419
435
  return null;
420
- this.index += 1n;
436
+ this.index += 1;
421
437
  return this.nextInternal(LINKED_ARRAY_LIST_INDEX_BLOCK_SIZE);
422
438
  case 4 /* HASH_MAP */:
423
439
  case 11 /* HASH_SET */:
@@ -446,7 +462,7 @@ class CursorIterator {
446
462
  } else {
447
463
  const nextSlot = level.block[level.index];
448
464
  if (nextSlot.tag === 1 /* INDEX */) {
449
- const nextPos = nextSlot.value;
465
+ const nextPos = Number(nextSlot.value);
450
466
  await this.cursor.db.core.seek(nextPos);
451
467
  const reader = this.cursor.db.core.reader();
452
468
  const indexBlockBytes = new Uint8Array(blockSize);
@@ -462,7 +478,7 @@ class CursorIterator {
462
478
  } else {
463
479
  this.stack[this.stack.length - 1].index += 1;
464
480
  if (!nextSlot.empty()) {
465
- const position = level.position + BigInt(level.index * Slot.LENGTH);
481
+ const position = level.position + level.index * Slot.LENGTH;
466
482
  return new ReadCursor(new SlotPointer(position, nextSlot), this.cursor.db);
467
483
  } else {
468
484
  continue;
@@ -522,13 +538,13 @@ var init_read_cursor = __esm(() => {
522
538
  }
523
539
  if (this.slotPtr.slot.value < 0n)
524
540
  throw new ExpectedUnsignedLongException;
525
- return this.slotPtr.slot.value;
541
+ return Number(this.slotPtr.slot.value);
526
542
  }
527
543
  readInt() {
528
544
  if (this.slotPtr.slot.tag !== 9 /* INT */) {
529
545
  throw new UnexpectedTagException;
530
546
  }
531
- return this.slotPtr.slot.value;
547
+ return Number(this.slotPtr.slot.value);
532
548
  }
533
549
  readFloat() {
534
550
  if (this.slotPtr.slot.tag !== 10 /* FLOAT */) {
@@ -539,23 +555,23 @@ var init_read_cursor = __esm(() => {
539
555
  view.setBigInt64(0, this.slotPtr.slot.value, false);
540
556
  return view.getFloat64(0, false);
541
557
  }
542
- async readBytes(maxSizeMaybe) {
558
+ async readBytes(maxSizeMaybe = null) {
543
559
  const bytesObj = await this.readBytesObject(maxSizeMaybe);
544
560
  return bytesObj.value;
545
561
  }
546
- async readBytesObject(maxSizeMaybe) {
562
+ async readBytesObject(maxSizeMaybe = null) {
547
563
  const reader = this.db.core.reader();
548
564
  switch (this.slotPtr.slot.tag) {
549
565
  case 0 /* NONE */:
550
566
  return new Bytes(new Uint8Array(0));
551
567
  case 6 /* BYTES */: {
552
- await this.db.core.seek(this.slotPtr.slot.value);
568
+ await this.db.core.seek(Number(this.slotPtr.slot.value));
553
569
  const valueSize = await reader.readLong();
554
- if (maxSizeMaybe !== undefined && valueSize > maxSizeMaybe) {
570
+ if (maxSizeMaybe !== null && valueSize > maxSizeMaybe) {
555
571
  throw new StreamTooLongException;
556
572
  }
557
- const startPosition = await this.db.core.position();
558
- const value = new Uint8Array(Number(valueSize));
573
+ const startPosition = this.db.core.position();
574
+ const value = new Uint8Array(valueSize);
559
575
  await reader.readFully(value);
560
576
  let formatTag = null;
561
577
  if (this.slotPtr.slot.full) {
@@ -577,7 +593,7 @@ var init_read_cursor = __esm(() => {
577
593
  break;
578
594
  valueSize += 1;
579
595
  }
580
- if (maxSizeMaybe !== undefined && BigInt(valueSize) > maxSizeMaybe) {
596
+ if (maxSizeMaybe !== null && valueSize > maxSizeMaybe) {
581
597
  throw new StreamTooLongException;
582
598
  }
583
599
  let formatTag = null;
@@ -595,23 +611,23 @@ var init_read_cursor = __esm(() => {
595
611
  if (this.slotPtr.slot.tag !== 5 /* KV_PAIR */) {
596
612
  throw new UnexpectedTagException;
597
613
  }
598
- await this.db.core.seek(this.slotPtr.slot.value);
614
+ await this.db.core.seek(Number(this.slotPtr.slot.value));
599
615
  const kvPairBytes = new Uint8Array(KeyValuePair.length(this.db.header.hashSize));
600
616
  await reader.readFully(kvPairBytes);
601
617
  const kvPair = KeyValuePair.fromBytes(kvPairBytes, this.db.header.hashSize);
602
- const hashPos = this.slotPtr.slot.value;
603
- const keySlotPos = hashPos + BigInt(this.db.header.hashSize);
604
- const valueSlotPos = keySlotPos + BigInt(Slot.LENGTH);
618
+ const hashPos = Number(this.slotPtr.slot.value);
619
+ const keySlotPos = hashPos + this.db.header.hashSize;
620
+ const valueSlotPos = keySlotPos + Slot.LENGTH;
605
621
  return new KeyValuePairCursor(new ReadCursor(new SlotPointer(valueSlotPos, kvPair.valueSlot), this.db), new ReadCursor(new SlotPointer(keySlotPos, kvPair.keySlot), this.db), kvPair.hash);
606
622
  }
607
623
  async reader() {
608
624
  const reader = this.db.core.reader();
609
625
  switch (this.slotPtr.slot.tag) {
610
626
  case 6 /* BYTES */: {
611
- await this.db.core.seek(this.slotPtr.slot.value);
627
+ await this.db.core.seek(Number(this.slotPtr.slot.value));
612
628
  const size = await reader.readLong();
613
- const startPosition = await this.db.core.position();
614
- return new Reader(this, size, startPosition, 0n);
629
+ const startPosition = this.db.core.position();
630
+ return new Reader(this, size, startPosition, 0);
615
631
  }
616
632
  case 7 /* SHORT_BYTES */: {
617
633
  const buffer = new ArrayBuffer(8);
@@ -625,8 +641,8 @@ var init_read_cursor = __esm(() => {
625
641
  break;
626
642
  valueSize += 1;
627
643
  }
628
- const startPosition = this.slotPtr.position + 1n;
629
- return new Reader(this, BigInt(valueSize), startPosition, 0n);
644
+ const startPosition = this.slotPtr.position + 1;
645
+ return new Reader(this, valueSize, startPosition, 0);
630
646
  }
631
647
  default:
632
648
  throw new UnexpectedTagException;
@@ -636,23 +652,23 @@ var init_read_cursor = __esm(() => {
636
652
  const reader = this.db.core.reader();
637
653
  switch (this.slotPtr.slot.tag) {
638
654
  case 0 /* NONE */:
639
- return 0n;
655
+ return 0;
640
656
  case 2 /* ARRAY_LIST */: {
641
- await this.db.core.seek(this.slotPtr.slot.value);
657
+ await this.db.core.seek(Number(this.slotPtr.slot.value));
642
658
  const headerBytes = new Uint8Array(ArrayListHeader.LENGTH);
643
659
  await reader.readFully(headerBytes);
644
660
  const header = ArrayListHeader.fromBytes(headerBytes);
645
661
  return header.size;
646
662
  }
647
663
  case 3 /* LINKED_ARRAY_LIST */: {
648
- await this.db.core.seek(this.slotPtr.slot.value);
664
+ await this.db.core.seek(Number(this.slotPtr.slot.value));
649
665
  const headerBytes = new Uint8Array(LinkedArrayListHeader.LENGTH);
650
666
  await reader.readFully(headerBytes);
651
667
  const header = LinkedArrayListHeader.fromBytes(headerBytes);
652
668
  return header.size;
653
669
  }
654
670
  case 6 /* BYTES */: {
655
- await this.db.core.seek(this.slotPtr.slot.value);
671
+ await this.db.core.seek(Number(this.slotPtr.slot.value));
656
672
  return reader.readLong();
657
673
  }
658
674
  case 7 /* SHORT_BYTES */: {
@@ -667,11 +683,11 @@ var init_read_cursor = __esm(() => {
667
683
  break;
668
684
  size += 1;
669
685
  }
670
- return BigInt(size);
686
+ return size;
671
687
  }
672
688
  case 12 /* COUNTED_HASH_MAP */:
673
689
  case 13 /* COUNTED_HASH_SET */: {
674
- await this.db.core.seek(this.slotPtr.slot.value);
690
+ await this.db.core.seek(Number(this.slotPtr.slot.value));
675
691
  return reader.readLong();
676
692
  }
677
693
  default:
@@ -723,7 +739,7 @@ class Writer {
723
739
  await this.parent.db.core.seek(this.startPosition + this.relativePosition);
724
740
  const writer = this.parent.db.core.writer();
725
741
  await writer.write(buffer);
726
- this.relativePosition += BigInt(buffer.length);
742
+ this.relativePosition += buffer.length;
727
743
  if (this.relativePosition > this.size) {
728
744
  this.size = this.relativePosition;
729
745
  }
@@ -738,7 +754,7 @@ class Writer {
738
754
  throw new UnexpectedWriterPositionException;
739
755
  await writer.write(this.formatTag);
740
756
  }
741
- await this.parent.db.core.seek(this.slot.value);
757
+ await this.parent.db.core.seek(Number(this.slot.value));
742
758
  await writer.writeLong(this.size);
743
759
  if (this.parent.slotPtr.position === null)
744
760
  throw new CursorNotWriteableException;
@@ -797,9 +813,9 @@ var init_write_cursor = __esm(() => {
797
813
  const writer = this.db.core.writer();
798
814
  const ptrPos = await this.db.core.length();
799
815
  await this.db.core.seek(ptrPos);
800
- await writer.writeLong(0n);
816
+ await writer.writeLong(0);
801
817
  const startPosition = await this.db.core.length();
802
- return new Writer(this, 0n, new Slot(ptrPos, 6 /* BYTES */), startPosition, 0n);
818
+ return new Writer(this, 0, new Slot(ptrPos, 6 /* BYTES */), startPosition, 0);
803
819
  }
804
820
  async* [Symbol.asyncIterator]() {
805
821
  const iterator = new WriteCursorIterator(this);
@@ -895,14 +911,16 @@ class ArrayListHeader {
895
911
  toBytes() {
896
912
  const buffer = new ArrayBuffer(ArrayListHeader.LENGTH);
897
913
  const view = new DataView(buffer);
898
- view.setBigInt64(0, this.size, false);
899
- view.setBigInt64(8, this.ptr, false);
914
+ view.setBigInt64(0, BigInt(this.size), false);
915
+ view.setBigInt64(8, BigInt(this.ptr), false);
900
916
  return new Uint8Array(buffer);
901
917
  }
902
918
  static fromBytes(bytes) {
903
919
  const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
904
- const size = checkLong(view.getBigInt64(0, false));
905
- const ptr = checkLong(view.getBigInt64(8, false));
920
+ const size = Number(view.getBigInt64(0, false));
921
+ checkLong(size);
922
+ const ptr = Number(view.getBigInt64(8, false));
923
+ checkLong(ptr);
906
924
  return new ArrayListHeader(ptr, size);
907
925
  }
908
926
  withPtr(ptr) {
@@ -923,15 +941,17 @@ class LinkedArrayListHeader {
923
941
  toBytes() {
924
942
  const buffer = new ArrayBuffer(LinkedArrayListHeader.LENGTH);
925
943
  const view = new DataView(buffer);
926
- view.setBigInt64(0, this.size, false);
927
- view.setBigInt64(8, this.ptr, false);
944
+ view.setBigInt64(0, BigInt(this.size), false);
945
+ view.setBigInt64(8, BigInt(this.ptr), false);
928
946
  view.setUint8(16, this.shift & 63);
929
947
  return new Uint8Array(buffer);
930
948
  }
931
949
  static fromBytes(bytes) {
932
950
  const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
933
- const size = checkLong(view.getBigInt64(0, false));
934
- const ptr = checkLong(view.getBigInt64(8, false));
951
+ const size = Number(view.getBigInt64(0, false));
952
+ checkLong(size);
953
+ const ptr = Number(view.getBigInt64(8, false));
954
+ checkLong(ptr);
935
955
  const shift = view.getUint8(16) & 63;
936
956
  return new LinkedArrayListHeader(shift, ptr, size);
937
957
  }
@@ -1024,11 +1044,11 @@ class ArrayListInit {
1024
1044
  if (isTopLevel) {
1025
1045
  const writer = db.core.writer();
1026
1046
  if (db.header.tag === 0 /* NONE */) {
1027
- await db.core.seek(BigInt(Header.LENGTH));
1028
- const arrayListPtr = BigInt(Header.LENGTH + TopLevelArrayListHeader.LENGTH);
1029
- await writer.write(new TopLevelArrayListHeader(0n, new ArrayListHeader(arrayListPtr, 0n)).toBytes());
1047
+ await db.core.seek(Header.LENGTH);
1048
+ const arrayListPtr = Header.LENGTH + TopLevelArrayListHeader.LENGTH;
1049
+ await writer.write(new TopLevelArrayListHeader(0, new ArrayListHeader(arrayListPtr, 0)).toBytes());
1030
1050
  await writer.write(new Uint8Array(INDEX_BLOCK_SIZE));
1031
- await db.core.seek(0n);
1051
+ await db.core.seek(0);
1032
1052
  db.header = db.header.withTag(2 /* ARRAY_LIST */);
1033
1053
  await writer.write(db.header.toBytes());
1034
1054
  }
@@ -1043,8 +1063,8 @@ class ArrayListInit {
1043
1063
  const writer = db.core.writer();
1044
1064
  let arrayListStart = await db.core.length();
1045
1065
  await db.core.seek(arrayListStart);
1046
- const arrayListPtr = arrayListStart + BigInt(ArrayListHeader.LENGTH);
1047
- await writer.write(new ArrayListHeader(arrayListPtr, 0n).toBytes());
1066
+ const arrayListPtr = arrayListStart + ArrayListHeader.LENGTH;
1067
+ await writer.write(new ArrayListHeader(arrayListPtr, 0).toBytes());
1048
1068
  await writer.write(new Uint8Array(INDEX_BLOCK_SIZE));
1049
1069
  const nextSlotPtr = new SlotPointer(position, new Slot(arrayListStart, 2 /* ARRAY_LIST */));
1050
1070
  await db.core.seek(position);
@@ -1054,7 +1074,7 @@ class ArrayListInit {
1054
1074
  case 2 /* ARRAY_LIST */: {
1055
1075
  const reader = db.core.reader();
1056
1076
  const writer = db.core.writer();
1057
- let arrayListStart = slotPtr.slot.value;
1077
+ let arrayListStart = Number(slotPtr.slot.value);
1058
1078
  if (db.txStart !== null) {
1059
1079
  if (arrayListStart < db.txStart) {
1060
1080
  await db.core.seek(arrayListStart);
@@ -1066,7 +1086,7 @@ class ArrayListInit {
1066
1086
  await reader.readFully(arrayListIndexBlock);
1067
1087
  arrayListStart = await db.core.length();
1068
1088
  await db.core.seek(arrayListStart);
1069
- const nextArrayListPtr = arrayListStart + BigInt(ArrayListHeader.LENGTH);
1089
+ const nextArrayListPtr = arrayListStart + ArrayListHeader.LENGTH;
1070
1090
  const newHeader = header.withPtr(nextArrayListPtr);
1071
1091
  await writer.write(newHeader.toBytes());
1072
1092
  await writer.write(arrayListIndexBlock);
@@ -1101,7 +1121,7 @@ class ArrayListGet2 {
1101
1121
  default:
1102
1122
  throw new UnexpectedTagException;
1103
1123
  }
1104
- const nextArrayListStart = slotPtr.slot.value;
1124
+ const nextArrayListStart = Number(slotPtr.slot.value);
1105
1125
  let index = this.index;
1106
1126
  await db.core.seek(nextArrayListStart);
1107
1127
  const reader = db.core.reader();
@@ -1111,9 +1131,9 @@ class ArrayListGet2 {
1111
1131
  if (index >= header.size || index < -header.size) {
1112
1132
  throw new KeyNotFoundException;
1113
1133
  }
1114
- const key = index < 0n ? header.size - bigAbs(index) : index;
1115
- const lastKey = header.size - 1n;
1116
- const shift = lastKey < BigInt(SLOT_COUNT) ? 0 : Math.floor(Math.log(Number(lastKey)) / Math.log(SLOT_COUNT));
1134
+ const key = index < 0 ? header.size - Math.abs(index) : index;
1135
+ const lastKey = header.size - 1;
1136
+ const shift = lastKey < SLOT_COUNT ? 0 : Math.floor(Math.log(lastKey) / Math.log(SLOT_COUNT));
1117
1137
  const finalSlotPtr = await db.readArrayListSlot(header.ptr, key, shift, writeMode, isTopLevel);
1118
1138
  return db.readSlotPointer(writeMode, path, pathI + 1, finalSlotPtr);
1119
1139
  }
@@ -1128,7 +1148,7 @@ class ArrayListAppend {
1128
1148
  if (tag !== 2 /* ARRAY_LIST */)
1129
1149
  throw new UnexpectedTagException;
1130
1150
  const reader = db.core.reader();
1131
- const nextArrayListStart = slotPtr.slot.value;
1151
+ const nextArrayListStart = Number(slotPtr.slot.value);
1132
1152
  await db.core.seek(nextArrayListStart);
1133
1153
  const headerBytes = new Uint8Array(ArrayListHeader.LENGTH);
1134
1154
  await reader.readFully(headerBytes);
@@ -1162,7 +1182,7 @@ class ArrayListSlice {
1162
1182
  if (slotPtr.slot.tag !== 2 /* ARRAY_LIST */)
1163
1183
  throw new UnexpectedTagException;
1164
1184
  const reader = db.core.reader();
1165
- const nextArrayListStart = slotPtr.slot.value;
1185
+ const nextArrayListStart = Number(slotPtr.slot.value);
1166
1186
  await db.core.seek(nextArrayListStart);
1167
1187
  const headerBytes = new Uint8Array(ArrayListHeader.LENGTH);
1168
1188
  await reader.readFully(headerBytes);
@@ -1191,8 +1211,8 @@ class LinkedArrayListInit {
1191
1211
  const writer = db.core.writer();
1192
1212
  const arrayListStart = await db.core.length();
1193
1213
  await db.core.seek(arrayListStart);
1194
- const arrayListPtr = arrayListStart + BigInt(LinkedArrayListHeader.LENGTH);
1195
- await writer.write(new LinkedArrayListHeader(0, arrayListPtr, 0n).toBytes());
1214
+ const arrayListPtr = arrayListStart + LinkedArrayListHeader.LENGTH;
1215
+ await writer.write(new LinkedArrayListHeader(0, arrayListPtr, 0).toBytes());
1196
1216
  await writer.write(new Uint8Array(LINKED_ARRAY_LIST_INDEX_BLOCK_SIZE));
1197
1217
  const nextSlotPtr = new SlotPointer(position, new Slot(arrayListStart, 3 /* LINKED_ARRAY_LIST */));
1198
1218
  await db.core.seek(position);
@@ -1202,7 +1222,7 @@ class LinkedArrayListInit {
1202
1222
  case 3 /* LINKED_ARRAY_LIST */: {
1203
1223
  const reader = db.core.reader();
1204
1224
  const writer = db.core.writer();
1205
- let arrayListStart = slotPtr.slot.value;
1225
+ let arrayListStart = Number(slotPtr.slot.value);
1206
1226
  if (db.txStart !== null) {
1207
1227
  if (arrayListStart < db.txStart) {
1208
1228
  await db.core.seek(arrayListStart);
@@ -1214,7 +1234,7 @@ class LinkedArrayListInit {
1214
1234
  await reader.readFully(arrayListIndexBlock);
1215
1235
  arrayListStart = await db.core.length();
1216
1236
  await db.core.seek(arrayListStart);
1217
- const nextArrayListPtr = arrayListStart + BigInt(LinkedArrayListHeader.LENGTH);
1237
+ const nextArrayListPtr = arrayListStart + LinkedArrayListHeader.LENGTH;
1218
1238
  const newHeader = header.withPtr(nextArrayListPtr);
1219
1239
  await writer.write(newHeader.toBytes());
1220
1240
  await writer.write(arrayListIndexBlock);
@@ -1249,7 +1269,7 @@ class LinkedArrayListGet {
1249
1269
  throw new UnexpectedTagException;
1250
1270
  }
1251
1271
  let index = this.index;
1252
- await db.core.seek(slotPtr.slot.value);
1272
+ await db.core.seek(Number(slotPtr.slot.value));
1253
1273
  const reader = db.core.reader();
1254
1274
  const headerBytes = new Uint8Array(LinkedArrayListHeader.LENGTH);
1255
1275
  await reader.readFully(headerBytes);
@@ -1257,7 +1277,7 @@ class LinkedArrayListGet {
1257
1277
  if (index >= header.size || index < -header.size) {
1258
1278
  throw new KeyNotFoundException;
1259
1279
  }
1260
- const key = index < 0n ? header.size - bigAbs(index) : index;
1280
+ const key = index < 0 ? header.size - Math.abs(index) : index;
1261
1281
  const finalSlotPtr = await db.readLinkedArrayListSlot(header.ptr, key, header.shift, writeMode, isTopLevel);
1262
1282
  return db.readSlotPointer(writeMode, path, pathI + 1, finalSlotPtr.slotPtr);
1263
1283
  }
@@ -1271,7 +1291,7 @@ class LinkedArrayListAppend {
1271
1291
  if (slotPtr.slot.tag !== 3 /* LINKED_ARRAY_LIST */)
1272
1292
  throw new UnexpectedTagException;
1273
1293
  const reader = db.core.reader();
1274
- const nextArrayListStart = slotPtr.slot.value;
1294
+ const nextArrayListStart = Number(slotPtr.slot.value);
1275
1295
  await db.core.seek(nextArrayListStart);
1276
1296
  const headerBytes = new Uint8Array(LinkedArrayListHeader.LENGTH);
1277
1297
  await reader.readFully(headerBytes);
@@ -1299,7 +1319,7 @@ class LinkedArrayListSlice {
1299
1319
  if (slotPtr.slot.tag !== 3 /* LINKED_ARRAY_LIST */)
1300
1320
  throw new UnexpectedTagException;
1301
1321
  const reader = db.core.reader();
1302
- const nextArrayListStart = slotPtr.slot.value;
1322
+ const nextArrayListStart = Number(slotPtr.slot.value);
1303
1323
  await db.core.seek(nextArrayListStart);
1304
1324
  const headerBytes = new Uint8Array(LinkedArrayListHeader.LENGTH);
1305
1325
  await reader.readFully(headerBytes);
@@ -1327,12 +1347,12 @@ class LinkedArrayListConcat {
1327
1347
  if (this.list.tag !== 3 /* LINKED_ARRAY_LIST */)
1328
1348
  throw new UnexpectedTagException;
1329
1349
  const reader = db.core.reader();
1330
- const nextArrayListStart = slotPtr.slot.value;
1350
+ const nextArrayListStart = Number(slotPtr.slot.value);
1331
1351
  await db.core.seek(nextArrayListStart);
1332
1352
  const headerBytesA = new Uint8Array(LinkedArrayListHeader.LENGTH);
1333
1353
  await reader.readFully(headerBytesA);
1334
1354
  const headerA = LinkedArrayListHeader.fromBytes(headerBytesA);
1335
- await db.core.seek(this.list.value);
1355
+ await db.core.seek(Number(this.list.value));
1336
1356
  const headerBytesB = new Uint8Array(LinkedArrayListHeader.LENGTH);
1337
1357
  await reader.readFully(headerBytesB);
1338
1358
  const headerB = LinkedArrayListHeader.fromBytes(headerBytesB);
@@ -1357,7 +1377,7 @@ class LinkedArrayListInsert {
1357
1377
  if (slotPtr.slot.tag !== 3 /* LINKED_ARRAY_LIST */)
1358
1378
  throw new UnexpectedTagException;
1359
1379
  const reader = db.core.reader();
1360
- const nextArrayListStart = slotPtr.slot.value;
1380
+ const nextArrayListStart = Number(slotPtr.slot.value);
1361
1381
  await db.core.seek(nextArrayListStart);
1362
1382
  const headerBytes = new Uint8Array(LinkedArrayListHeader.LENGTH);
1363
1383
  await reader.readFully(headerBytes);
@@ -1366,8 +1386,8 @@ class LinkedArrayListInsert {
1366
1386
  if (index >= origHeader.size || index < -origHeader.size) {
1367
1387
  throw new KeyNotFoundException;
1368
1388
  }
1369
- const key = index < 0n ? origHeader.size - bigAbs(index) : index;
1370
- const headerA = await db.readLinkedArrayListSlice(origHeader, 0n, key);
1389
+ const key = index < 0 ? origHeader.size - Math.abs(index) : index;
1390
+ const headerA = await db.readLinkedArrayListSlice(origHeader, 0, key);
1371
1391
  const headerB = await db.readLinkedArrayListSlice(origHeader, key, origHeader.size - key);
1372
1392
  const appendResult = await db.readLinkedArrayListSlotAppend(headerA, writeMode, isTopLevel);
1373
1393
  const concatHeader = await db.readLinkedArrayListConcat(appendResult.header, headerB);
@@ -1392,7 +1412,7 @@ class LinkedArrayListRemove {
1392
1412
  if (slotPtr.slot.tag !== 3 /* LINKED_ARRAY_LIST */)
1393
1413
  throw new UnexpectedTagException;
1394
1414
  const reader = db.core.reader();
1395
- const nextArrayListStart = slotPtr.slot.value;
1415
+ const nextArrayListStart = Number(slotPtr.slot.value);
1396
1416
  await db.core.seek(nextArrayListStart);
1397
1417
  const headerBytes = new Uint8Array(LinkedArrayListHeader.LENGTH);
1398
1418
  await reader.readFully(headerBytes);
@@ -1401,9 +1421,9 @@ class LinkedArrayListRemove {
1401
1421
  if (index >= origHeader.size || index < -origHeader.size) {
1402
1422
  throw new KeyNotFoundException;
1403
1423
  }
1404
- const key = index < 0n ? origHeader.size - bigAbs(index) : index;
1405
- const headerA = await db.readLinkedArrayListSlice(origHeader, 0n, key);
1406
- const headerB = await db.readLinkedArrayListSlice(origHeader, key + 1n, origHeader.size - (key + 1n));
1424
+ const key = index < 0 ? origHeader.size - Math.abs(index) : index;
1425
+ const headerA = await db.readLinkedArrayListSlice(origHeader, 0, key);
1426
+ const headerB = await db.readLinkedArrayListSlice(origHeader, key + 1, origHeader.size - (key + 1));
1407
1427
  const concatHeader = await db.readLinkedArrayListConcat(headerA, headerB);
1408
1428
  const nextSlotPtr = new SlotPointer(concatHeader.ptr, new Slot(nextArrayListStart, 3 /* LINKED_ARRAY_LIST */));
1409
1429
  const finalSlotPtr = await db.readSlotPointer(writeMode, path, pathI + 1, nextSlotPtr);
@@ -1429,12 +1449,12 @@ class HashMapInit {
1429
1449
  if (isTopLevel) {
1430
1450
  const writer = db.core.writer();
1431
1451
  if (db.header.tag === 0 /* NONE */) {
1432
- await db.core.seek(BigInt(Header.LENGTH));
1452
+ await db.core.seek(Header.LENGTH);
1433
1453
  if (this.counted) {
1434
- await writer.writeLong(0n);
1454
+ await writer.writeLong(0);
1435
1455
  }
1436
1456
  await writer.write(new Uint8Array(INDEX_BLOCK_SIZE));
1437
- await db.core.seek(0n);
1457
+ await db.core.seek(0);
1438
1458
  db.header = db.header.withTag(tag);
1439
1459
  await writer.write(db.header.toBytes());
1440
1460
  }
@@ -1450,7 +1470,7 @@ class HashMapInit {
1450
1470
  const mapStart = await db.core.length();
1451
1471
  await db.core.seek(mapStart);
1452
1472
  if (this.counted) {
1453
- await writer.writeLong(0n);
1473
+ await writer.writeLong(0);
1454
1474
  }
1455
1475
  await writer.write(new Uint8Array(INDEX_BLOCK_SIZE));
1456
1476
  const nextSlotPtr = new SlotPointer(position, new Slot(mapStart, tag));
@@ -1481,7 +1501,7 @@ class HashMapInit {
1481
1501
  }
1482
1502
  const reader = db.core.reader();
1483
1503
  const writer = db.core.writer();
1484
- let mapStart = slotPtr.slot.value;
1504
+ let mapStart = Number(slotPtr.slot.value);
1485
1505
  if (db.txStart !== null) {
1486
1506
  if (mapStart < db.txStart) {
1487
1507
  await db.core.seek(mapStart);
@@ -1533,16 +1553,16 @@ class HashMapGet {
1533
1553
  default:
1534
1554
  throw new UnexpectedTagException;
1535
1555
  }
1536
- const indexPos = counted ? slotPtr.slot.value + 8n : slotPtr.slot.value;
1556
+ const indexPos = counted ? Number(slotPtr.slot.value) + 8 : Number(slotPtr.slot.value);
1537
1557
  const hash = db.checkHash(this.target);
1538
1558
  const res = await db.readMapSlot(indexPos, hash, 0, writeMode, isTopLevel, this.target);
1539
1559
  if (writeMode === 1 /* READ_WRITE */ && counted && res.isEmpty) {
1540
1560
  const reader = db.core.reader();
1541
1561
  const writer = db.core.writer();
1542
- await db.core.seek(slotPtr.slot.value);
1562
+ await db.core.seek(Number(slotPtr.slot.value));
1543
1563
  const mapCount = await reader.readLong();
1544
- await db.core.seek(slotPtr.slot.value);
1545
- await writer.writeLong(mapCount + 1n);
1564
+ await db.core.seek(Number(slotPtr.slot.value));
1565
+ await writer.writeLong(mapCount + 1);
1546
1566
  }
1547
1567
  return db.readSlotPointer(writeMode, path, pathI + 1, res.slotPtr);
1548
1568
  }
@@ -1571,7 +1591,7 @@ class HashMapRemove {
1571
1591
  default:
1572
1592
  throw new UnexpectedTagException;
1573
1593
  }
1574
- const indexPos = counted ? slotPtr.slot.value + 8n : slotPtr.slot.value;
1594
+ const indexPos = counted ? Number(slotPtr.slot.value) + 8 : Number(slotPtr.slot.value);
1575
1595
  const hash = db.checkHashBytes(this.hash);
1576
1596
  let keyFound = true;
1577
1597
  try {
@@ -1586,10 +1606,10 @@ class HashMapRemove {
1586
1606
  if (writeMode === 1 /* READ_WRITE */ && counted && keyFound) {
1587
1607
  const reader = db.core.reader();
1588
1608
  const writer = db.core.writer();
1589
- await db.core.seek(slotPtr.slot.value);
1609
+ await db.core.seek(Number(slotPtr.slot.value));
1590
1610
  const mapCount = await reader.readLong();
1591
- await db.core.seek(slotPtr.slot.value);
1592
- await writer.writeLong(mapCount - 1n);
1611
+ await db.core.seek(Number(slotPtr.slot.value));
1612
+ await writer.writeLong(mapCount - 1);
1593
1613
  }
1594
1614
  if (!keyFound)
1595
1615
  throw new KeyNotFoundException;
@@ -1617,7 +1637,7 @@ class WriteData {
1617
1637
  } else if (data instanceof Slot) {
1618
1638
  slot = data;
1619
1639
  } else if (data instanceof Uint) {
1620
- if (data.value < 0n) {
1640
+ if (data.value < 0) {
1621
1641
  throw new Error("Uint must not be negative");
1622
1642
  }
1623
1643
  slot = new Slot(data.value, 8 /* UINT */);
@@ -1722,14 +1742,11 @@ function arraysEqual(a, b) {
1722
1742
  return true;
1723
1743
  }
1724
1744
  function checkLong(n) {
1725
- if (n < 0n) {
1745
+ if (n < 0) {
1726
1746
  throw new ExpectedUnsignedLongException;
1727
1747
  }
1728
1748
  return n;
1729
1749
  }
1730
- function bigAbs(n) {
1731
- return n < 0n ? -n : n;
1732
- }
1733
1750
  function bigIntShiftRight(value, bits) {
1734
1751
  let result = 0n;
1735
1752
  for (let i = 0;i < value.length; i++) {
@@ -1749,8 +1766,8 @@ class Database3 {
1749
1766
  }
1750
1767
  static async create(core, hasher) {
1751
1768
  const db = new Database3(core, hasher);
1752
- await core.seek(0n);
1753
- if (await core.length() === 0n) {
1769
+ await core.seek(0);
1770
+ if (await core.length() === 0) {
1754
1771
  db.header = new Header(hasher.id, hasher.digestLength, VERSION, 0 /* NONE */, MAGIC_NUMBER);
1755
1772
  await db.header.write(core);
1756
1773
  await core.flush();
@@ -1767,10 +1784,10 @@ class Database3 {
1767
1784
  async rootCursor() {
1768
1785
  const { WriteCursor: WriteCursor2 } = await Promise.resolve().then(() => (init_write_cursor(), exports_write_cursor));
1769
1786
  if (this.header.tag === 0 /* NONE */) {
1770
- await this.core.seek(0n);
1787
+ await this.core.seek(0);
1771
1788
  this.header = await Header.read(this.core);
1772
1789
  }
1773
- return new WriteCursor2(new SlotPointer(null, new Slot(BigInt(Header.LENGTH), this.header.tag)), this);
1790
+ return new WriteCursor2(new SlotPointer(null, new Slot(Header.LENGTH, this.header.tag)), this);
1774
1791
  }
1775
1792
  async freeze() {
1776
1793
  if (this.txStart !== null) {
@@ -1784,12 +1801,12 @@ class Database3 {
1784
1801
  return;
1785
1802
  const rootCursor = await this.rootCursor();
1786
1803
  const listSize = await rootCursor.count();
1787
- if (listSize === 0n)
1804
+ if (listSize === 0)
1788
1805
  return;
1789
- await this.core.seek(BigInt(Header.LENGTH + ArrayListHeader.LENGTH));
1806
+ await this.core.seek(Header.LENGTH + ArrayListHeader.LENGTH);
1790
1807
  const reader = this.core.reader();
1791
1808
  const headerFileSize = await reader.readLong();
1792
- if (headerFileSize === 0n)
1809
+ if (headerFileSize === 0)
1793
1810
  return;
1794
1811
  const fileSize = await this.core.length();
1795
1812
  if (fileSize === headerFileSize)
@@ -1835,12 +1852,12 @@ class Database3 {
1835
1852
  const reader = this.core.reader();
1836
1853
  const writer = this.core.writer();
1837
1854
  const i = Number(bigIntShiftRight(keyHash, keyOffset * BIT_COUNT) & MASK);
1838
- const slotPos = indexPos + BigInt(Slot.LENGTH * i);
1855
+ const slotPos = indexPos + Slot.LENGTH * i;
1839
1856
  await this.core.seek(slotPos);
1840
1857
  const slotBytes = new Uint8Array(Slot.LENGTH);
1841
1858
  await reader.readFully(slotBytes);
1842
1859
  const slot = Slot.fromBytes(slotBytes);
1843
- const ptr = slot.value;
1860
+ const ptr = Number(slot.value);
1844
1861
  switch (slot.tag) {
1845
1862
  case 0 /* NONE */: {
1846
1863
  switch (writeMode) {
@@ -1849,8 +1866,8 @@ class Database3 {
1849
1866
  case 1 /* READ_WRITE */: {
1850
1867
  const hashPos = await this.core.length();
1851
1868
  await this.core.seek(hashPos);
1852
- const keySlotPos = hashPos + BigInt(this.header.hashSize);
1853
- const valueSlotPos = keySlotPos + BigInt(Slot.LENGTH);
1869
+ const keySlotPos = hashPos + this.header.hashSize;
1870
+ const valueSlotPos = keySlotPos + Slot.LENGTH;
1854
1871
  const kvPair = new KeyValuePair(new Slot, new Slot, keyHash);
1855
1872
  await writer.write(kvPair.toBytes());
1856
1873
  const nextSlot = new Slot(hashPos, 5 /* KV_PAIR */);
@@ -1901,8 +1918,8 @@ class Database3 {
1901
1918
  if (ptr < this.txStart) {
1902
1919
  const hashPos = await this.core.length();
1903
1920
  await this.core.seek(hashPos);
1904
- const keySlotPos2 = hashPos + BigInt(this.header.hashSize);
1905
- const valueSlotPos2 = keySlotPos2 + BigInt(Slot.LENGTH);
1921
+ const keySlotPos2 = hashPos + this.header.hashSize;
1922
+ const valueSlotPos2 = keySlotPos2 + Slot.LENGTH;
1906
1923
  await writer.write(kvPair.toBytes());
1907
1924
  const nextSlot = new Slot(hashPos, 5 /* KV_PAIR */);
1908
1925
  await this.core.seek(slotPos);
@@ -1921,8 +1938,8 @@ class Database3 {
1921
1938
  throw new ExpectedTxStartException;
1922
1939
  }
1923
1940
  }
1924
- const keySlotPos = ptr + BigInt(this.header.hashSize);
1925
- const valueSlotPos = keySlotPos + BigInt(Slot.LENGTH);
1941
+ const keySlotPos = ptr + this.header.hashSize;
1942
+ const valueSlotPos = keySlotPos + Slot.LENGTH;
1926
1943
  let nextSlotPtr;
1927
1944
  if (target.kind === "kv_pair") {
1928
1945
  nextSlotPtr = new SlotPointer(slotPos, slot);
@@ -1944,7 +1961,7 @@ class Database3 {
1944
1961
  const nextIndexPos = await this.core.length();
1945
1962
  await this.core.seek(nextIndexPos);
1946
1963
  await writer.write(new Uint8Array(INDEX_BLOCK_SIZE));
1947
- await this.core.seek(nextIndexPos + BigInt(Slot.LENGTH * nextI));
1964
+ await this.core.seek(nextIndexPos + Slot.LENGTH * nextI);
1948
1965
  await writer.write(slot.toBytes());
1949
1966
  const res = await this.readMapSlot(nextIndexPos, keyHash, keyOffset + 1, writeMode, isTopLevel, target);
1950
1967
  await this.core.seek(slotPos);
@@ -1975,17 +1992,17 @@ class Database3 {
1975
1992
  slotBlock[i2] = Slot.fromBytes(slotBytes);
1976
1993
  }
1977
1994
  const i = Number(bigIntShiftRight(keyHash, keyOffset * BIT_COUNT) & MASK);
1978
- const slotPos = indexPos + BigInt(Slot.LENGTH * i);
1995
+ const slotPos = indexPos + Slot.LENGTH * i;
1979
1996
  const slot = slotBlock[i];
1980
1997
  let nextSlot;
1981
1998
  switch (slot.tag) {
1982
1999
  case 0 /* NONE */:
1983
2000
  throw new KeyNotFoundException;
1984
2001
  case 1 /* INDEX */:
1985
- nextSlot = await this.removeMapSlot(slot.value, keyHash, keyOffset + 1, isTopLevel);
2002
+ nextSlot = await this.removeMapSlot(Number(slot.value), keyHash, keyOffset + 1, isTopLevel);
1986
2003
  break;
1987
2004
  case 5 /* KV_PAIR */: {
1988
- await this.core.seek(slot.value);
2005
+ await this.core.seek(Number(slot.value));
1989
2006
  const kvPairBytes = new Uint8Array(KeyValuePair.length(this.header.hashSize));
1990
2007
  await reader.readFully(kvPairBytes);
1991
2008
  const kvPair = KeyValuePair.fromBytes(kvPairBytes, this.header.hashSize);
@@ -2032,7 +2049,7 @@ class Database3 {
2032
2049
  const nextIndexPos = await this.core.length();
2033
2050
  await this.core.seek(nextIndexPos);
2034
2051
  await writer.write(indexBlock);
2035
- const nextSlotPos = nextIndexPos + BigInt(Slot.LENGTH * i);
2052
+ const nextSlotPos = nextIndexPos + Slot.LENGTH * i;
2036
2053
  await this.core.seek(nextSlotPos);
2037
2054
  await writer.write(nextSlot.toBytes());
2038
2055
  return new Slot(nextIndexPos, 1 /* INDEX */);
@@ -2049,8 +2066,8 @@ class Database3 {
2049
2066
  const writer = this.core.writer();
2050
2067
  let indexPos = header.ptr;
2051
2068
  const key = header.size;
2052
- const prevShift = key < BigInt(SLOT_COUNT) ? 0 : Math.floor(Math.log(Number(key - 1n)) / Math.log(SLOT_COUNT));
2053
- const nextShift = key < BigInt(SLOT_COUNT) ? 0 : Math.floor(Math.log(Number(key)) / Math.log(SLOT_COUNT));
2069
+ const prevShift = key < SLOT_COUNT ? 0 : Math.floor(Math.log(key - 1) / Math.log(SLOT_COUNT));
2070
+ const nextShift = key < SLOT_COUNT ? 0 : Math.floor(Math.log(key) / Math.log(SLOT_COUNT));
2054
2071
  if (prevShift !== nextShift) {
2055
2072
  const nextIndexPos = await this.core.length();
2056
2073
  await this.core.seek(nextIndexPos);
@@ -2060,14 +2077,14 @@ class Database3 {
2060
2077
  indexPos = nextIndexPos;
2061
2078
  }
2062
2079
  const slotPtr = await this.readArrayListSlot(indexPos, key, nextShift, writeMode, isTopLevel);
2063
- return new ArrayListAppendResult(new ArrayListHeader(indexPos, header.size + 1n), slotPtr);
2080
+ return new ArrayListAppendResult(new ArrayListHeader(indexPos, header.size + 1), slotPtr);
2064
2081
  }
2065
2082
  async readArrayListSlot(indexPos, key, shift, writeMode, isTopLevel) {
2066
2083
  if (shift >= MAX_BRANCH_LENGTH)
2067
2084
  throw new MaxShiftExceededException;
2068
2085
  const reader = this.core.reader();
2069
- const i = Number(key >> BigInt(shift * BIT_COUNT) & MASK);
2070
- const slotPos = indexPos + BigInt(Slot.LENGTH * i);
2086
+ const i = key >>> shift * BIT_COUNT & SLOT_COUNT - 1;
2087
+ const slotPos = indexPos + Slot.LENGTH * i;
2071
2088
  await this.core.seek(slotPos);
2072
2089
  const slotBytes = new Uint8Array(Slot.LENGTH);
2073
2090
  await reader.readFully(slotBytes);
@@ -2075,7 +2092,7 @@ class Database3 {
2075
2092
  if (shift === 0) {
2076
2093
  return new SlotPointer(slotPos, slot);
2077
2094
  }
2078
- const ptr = slot.value;
2095
+ const ptr = Number(slot.value);
2079
2096
  switch (slot.tag) {
2080
2097
  case 0 /* NONE */: {
2081
2098
  switch (writeMode) {
@@ -2088,7 +2105,7 @@ class Database3 {
2088
2105
  await writer.write(new Uint8Array(INDEX_BLOCK_SIZE));
2089
2106
  if (isTopLevel) {
2090
2107
  const fileSize = await this.core.length();
2091
- await this.core.seek(BigInt(Header.LENGTH + ArrayListHeader.LENGTH));
2108
+ await this.core.seek(Header.LENGTH + ArrayListHeader.LENGTH);
2092
2109
  await writer.writeLong(fileSize);
2093
2110
  }
2094
2111
  await this.core.seek(slotPos);
@@ -2126,11 +2143,11 @@ class Database3 {
2126
2143
  }
2127
2144
  async readArrayListSlice(header, size) {
2128
2145
  const reader = this.core.reader();
2129
- if (size > header.size || size < 0n) {
2146
+ if (size > header.size || size < 0) {
2130
2147
  throw new KeyNotFoundException;
2131
2148
  }
2132
- const prevShift = header.size < BigInt(SLOT_COUNT + 1) ? 0 : Math.floor(Math.log(Number(header.size - 1n)) / Math.log(SLOT_COUNT));
2133
- const nextShift = size < BigInt(SLOT_COUNT + 1) ? 0 : Math.floor(Math.log(Number(size - 1n)) / Math.log(SLOT_COUNT));
2149
+ const prevShift = header.size < SLOT_COUNT + 1 ? 0 : Math.floor(Math.log(header.size - 1) / Math.log(SLOT_COUNT));
2150
+ const nextShift = size < SLOT_COUNT + 1 ? 0 : Math.floor(Math.log(size - 1) / Math.log(SLOT_COUNT));
2134
2151
  if (prevShift === nextShift) {
2135
2152
  return new ArrayListHeader(header.ptr, size);
2136
2153
  } else {
@@ -2142,7 +2159,7 @@ class Database3 {
2142
2159
  await reader.readFully(slotBytes);
2143
2160
  const slot = Slot.fromBytes(slotBytes);
2144
2161
  shift -= 1;
2145
- indexPos = slot.value;
2162
+ indexPos = Number(slot.value);
2146
2163
  }
2147
2164
  return new ArrayListHeader(indexPos, size);
2148
2165
  }
@@ -2169,25 +2186,25 @@ class Database3 {
2169
2186
  throw e;
2170
2187
  }
2171
2188
  }
2172
- const newSlot = new Slot(0n, 0 /* NONE */, true);
2189
+ const newSlot = new Slot(0, 0 /* NONE */, true);
2173
2190
  slotPtr = slotPtr.withSlotPointer(slotPtr.slotPtr.withSlot(newSlot));
2174
2191
  if (slotPtr.slotPtr.position === null)
2175
2192
  throw new CursorNotWriteableException;
2176
2193
  const position = slotPtr.slotPtr.position;
2177
2194
  await this.core.seek(position);
2178
- await writer.write(new LinkedArrayListSlot2(0n, newSlot).toBytes());
2179
- if (header.size < BigInt(SLOT_COUNT) && shift > 0) {
2195
+ await writer.write(new LinkedArrayListSlot2(0, newSlot).toBytes());
2196
+ if (header.size < SLOT_COUNT && shift > 0) {
2180
2197
  throw new MustSetNewSlotsToFullException;
2181
2198
  }
2182
- return new LinkedArrayListAppendResult(new LinkedArrayListHeader(shift, ptr, header.size + 1n), slotPtr);
2199
+ return new LinkedArrayListAppendResult(new LinkedArrayListHeader(shift, ptr, header.size + 1), slotPtr);
2183
2200
  }
2184
2201
  static blockLeafCount(block, shift, i) {
2185
- let n = 0n;
2202
+ let n = 0;
2186
2203
  if (shift === 0) {
2187
2204
  for (let blockI = 0;blockI < block.length; blockI++) {
2188
2205
  const blockSlot = block[blockI];
2189
2206
  if (!blockSlot.slot.empty() || blockI === i) {
2190
- n += 1n;
2207
+ n += 1;
2191
2208
  }
2192
2209
  }
2193
2210
  } else {
@@ -2200,9 +2217,9 @@ class Database3 {
2200
2217
  static slotLeafCount(slot, shift) {
2201
2218
  if (shift === 0) {
2202
2219
  if (slot.slot.empty()) {
2203
- return 0n;
2220
+ return 0;
2204
2221
  } else {
2205
- return 1n;
2222
+ return 1;
2206
2223
  }
2207
2224
  } else {
2208
2225
  return slot.size;
@@ -2211,7 +2228,7 @@ class Database3 {
2211
2228
  static keyAndIndexForLinkedArrayList(slotBlock, key, shift) {
2212
2229
  let nextKey = key;
2213
2230
  let i = 0;
2214
- const maxLeafCount = shift === 0 ? 1n : BigInt(Math.pow(SLOT_COUNT, shift));
2231
+ const maxLeafCount = shift === 0 ? 1 : Math.pow(SLOT_COUNT, shift);
2215
2232
  while (true) {
2216
2233
  const slotLeafCount = Database3.slotLeafCount(slotBlock[i], shift);
2217
2234
  if (nextKey === slotLeafCount) {
@@ -2254,12 +2271,12 @@ class Database3 {
2254
2271
  const nextKey = keyAndIndex.key;
2255
2272
  const i = keyAndIndex.index;
2256
2273
  const slot = slotBlock[i];
2257
- const slotPos = indexPos + BigInt(LinkedArrayListSlot2.LENGTH * i);
2274
+ const slotPos = indexPos + LinkedArrayListSlot2.LENGTH * i;
2258
2275
  if (shift === 0) {
2259
2276
  const leafCount = Database3.blockLeafCount(slotBlock, shift, i);
2260
2277
  return new LinkedArrayListSlotPointer(new SlotPointer(slotPos, slot.slot), leafCount);
2261
2278
  }
2262
- const ptr = slot.slot.value;
2279
+ const ptr = Number(slot.slot.value);
2263
2280
  switch (slot.slot.tag) {
2264
2281
  case 0 /* NONE */: {
2265
2282
  switch (writeMode) {
@@ -2334,7 +2351,7 @@ class Database3 {
2334
2351
  case 0 /* NONE */:
2335
2352
  throw new EmptySlotException;
2336
2353
  case 1 /* INDEX */:
2337
- await this.readLinkedArrayListBlocks(slot.slot.value, nextKey, shift - 1, blocks);
2354
+ await this.readLinkedArrayListBlocks(Number(slot.slot.value), nextKey, shift - 1, blocks);
2338
2355
  break;
2339
2356
  default:
2340
2357
  throw new UnexpectedTagException;
@@ -2342,7 +2359,7 @@ class Database3 {
2342
2359
  }
2343
2360
  populateArray(arr) {
2344
2361
  for (let i = 0;i < arr.length; i++) {
2345
- arr[i] = new LinkedArrayListSlot2(0n, new Slot);
2362
+ arr[i] = new LinkedArrayListSlot2(0, new Slot);
2346
2363
  }
2347
2364
  }
2348
2365
  async readLinkedArrayListSlice(header, offset, size) {
@@ -2353,7 +2370,7 @@ class Database3 {
2353
2370
  const leftBlocks = [];
2354
2371
  await this.readLinkedArrayListBlocks(header.ptr, offset, header.shift, leftBlocks);
2355
2372
  const rightBlocks = [];
2356
- const rightKey = offset + size === 0n ? 0n : offset + size - 1n;
2373
+ const rightKey = offset + size === 0 ? 0 : offset + size - 1;
2357
2374
  await this.readLinkedArrayListBlocks(header.ptr, rightKey, header.shift, rightBlocks);
2358
2375
  const blockCount = leftBlocks.length;
2359
2376
  let nextSlots = [null, null];
@@ -2368,7 +2385,7 @@ class Database3 {
2368
2385
  let slotI = 0;
2369
2386
  const newRootBlock = new Array(SLOT_COUNT);
2370
2387
  this.populateArray(newRootBlock);
2371
- if (size > 0n) {
2388
+ if (size > 0) {
2372
2389
  if (nextSlots[0] !== null) {
2373
2390
  newRootBlock[slotI] = nextSlots[0];
2374
2391
  } else {
@@ -2376,7 +2393,7 @@ class Database3 {
2376
2393
  }
2377
2394
  slotI += 1;
2378
2395
  }
2379
- if (size > 1n) {
2396
+ if (size > 1) {
2380
2397
  for (let j = leftBlock.i + 1;j < rightBlock.i; j++) {
2381
2398
  const middleSlot = leftBlock.block[j];
2382
2399
  newRootBlock[slotI] = middleSlot;
@@ -2440,13 +2457,13 @@ class Database3 {
2440
2457
  nextSlots[j] = origBlockInfo.parentSlot;
2441
2458
  } else {
2442
2459
  const nextPtr = await this.core.position();
2443
- let leafCount = 0n;
2460
+ let leafCount = 0;
2444
2461
  for (let k = 0;k < blockMaybe.length; k++) {
2445
2462
  const blockSlot = blockMaybe[k];
2446
2463
  await writer.write(blockSlot.toBytes());
2447
2464
  if (isLeafNode) {
2448
2465
  if (!blockSlot.slot.empty()) {
2449
- leafCount += 1n;
2466
+ leafCount += 1;
2450
2467
  }
2451
2468
  } else {
2452
2469
  leafCount += blockSlot.size;
@@ -2463,15 +2480,15 @@ class Database3 {
2463
2480
  const rootSlot = nextSlots[0];
2464
2481
  if (rootSlot === null)
2465
2482
  throw new ExpectedRootNodeException;
2466
- return new LinkedArrayListHeader(nextShift, rootSlot.slot.value, size);
2483
+ return new LinkedArrayListHeader(nextShift, Number(rootSlot.slot.value), size);
2467
2484
  }
2468
2485
  async readLinkedArrayListConcat(headerA, headerB) {
2469
2486
  const writer = this.core.writer();
2470
2487
  const blocksA = [];
2471
- const keyA = headerA.size === 0n ? 0n : headerA.size - 1n;
2488
+ const keyA = headerA.size === 0 ? 0 : headerA.size - 1;
2472
2489
  await this.readLinkedArrayListBlocks(headerA.ptr, keyA, headerA.shift, blocksA);
2473
2490
  const blocksB = [];
2474
- await this.readLinkedArrayListBlocks(headerB.ptr, 0n, headerB.shift, blocksB);
2491
+ await this.readLinkedArrayListBlocks(headerB.ptr, 0, headerB.shift, blocksB);
2475
2492
  let nextSlots = [null, null];
2476
2493
  let nextShift = 0;
2477
2494
  for (let i = 0;i < Math.max(blocksA.length, blocksB.length); i++) {
@@ -2565,12 +2582,12 @@ class Database3 {
2565
2582
  break;
2566
2583
  }
2567
2584
  const nextPtr = await this.core.position();
2568
- let leafCount = 0n;
2585
+ let leafCount = 0;
2569
2586
  for (const blockSlot of block) {
2570
2587
  await writer.write(blockSlot.toBytes());
2571
2588
  if (isLeafNode) {
2572
2589
  if (!blockSlot.slot.empty()) {
2573
- leafCount += 1n;
2590
+ leafCount += 1;
2574
2591
  }
2575
2592
  } else {
2576
2593
  leafCount += blockSlot.size;
@@ -2595,7 +2612,7 @@ class Database3 {
2595
2612
  nextShift += 1;
2596
2613
  rootPtr = newPtr;
2597
2614
  } else {
2598
- rootPtr = nextSlots[0].slot.value;
2615
+ rootPtr = Number(nextSlots[0].slot.value);
2599
2616
  }
2600
2617
  } else {
2601
2618
  rootPtr = headerA.ptr;
@@ -2632,7 +2649,7 @@ var init_database = __esm(() => {
2632
2649
  const view = new DataView(buffer);
2633
2650
  const arr = new Uint8Array(buffer);
2634
2651
  arr.set(this.parent.toBytes(), 0);
2635
- view.setBigInt64(ArrayListHeader.LENGTH, this.fileSize, false);
2652
+ view.setBigInt64(ArrayListHeader.LENGTH, BigInt(this.fileSize), false);
2636
2653
  return arr;
2637
2654
  }
2638
2655
  };
@@ -2652,14 +2669,15 @@ var init_database = __esm(() => {
2652
2669
  const view = new DataView(buffer);
2653
2670
  const arr = new Uint8Array(buffer);
2654
2671
  arr.set(this.slot.toBytes(), 0);
2655
- view.setBigInt64(Slot.LENGTH, this.size, false);
2672
+ view.setBigInt64(Slot.LENGTH, BigInt(this.size), false);
2656
2673
  return arr;
2657
2674
  }
2658
2675
  static fromBytes(bytes) {
2659
2676
  const slotBytes = bytes.slice(0, Slot.LENGTH);
2660
2677
  const slot = Slot.fromBytes(slotBytes);
2661
2678
  const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
2662
- const size = checkLong(view.getBigInt64(Slot.LENGTH, false));
2679
+ const size = Number(view.getBigInt64(Slot.LENGTH, false));
2680
+ checkLong(size);
2663
2681
  return new LinkedArrayListSlot2(size, slot);
2664
2682
  }
2665
2683
  };
@@ -2671,7 +2689,34 @@ init_slot();
2671
2689
  init_writeable_data();
2672
2690
  init_exceptions();
2673
2691
 
2674
- // src/random-access-memory.ts
2692
+ // src/core-memory.ts
2693
+ class CoreMemory {
2694
+ memory;
2695
+ constructor() {
2696
+ this.memory = new RandomAccessMemory;
2697
+ }
2698
+ reader() {
2699
+ return this.memory;
2700
+ }
2701
+ writer() {
2702
+ return this.memory;
2703
+ }
2704
+ async length() {
2705
+ return this.memory.size();
2706
+ }
2707
+ async seek(pos) {
2708
+ this.memory.seek(pos);
2709
+ }
2710
+ position() {
2711
+ return this.memory.getPosition();
2712
+ }
2713
+ async setLength(len) {
2714
+ this.memory.setLength(len);
2715
+ }
2716
+ async flush() {}
2717
+ async sync() {}
2718
+ }
2719
+
2675
2720
  class RandomAccessMemory {
2676
2721
  buffer;
2677
2722
  _position = 0;
@@ -2754,7 +2799,7 @@ class RandomAccessMemory {
2754
2799
  async writeLong(v) {
2755
2800
  const buffer = new ArrayBuffer(8);
2756
2801
  const view = new DataView(buffer);
2757
- view.setBigInt64(0, v, false);
2802
+ view.setBigInt64(0, BigInt(v), false);
2758
2803
  await this.write(new Uint8Array(buffer));
2759
2804
  }
2760
2805
  async readFully(b) {
@@ -2786,58 +2831,68 @@ class RandomAccessMemory {
2786
2831
  const bytes = new Uint8Array(8);
2787
2832
  await this.readFully(bytes);
2788
2833
  const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
2789
- return view.getBigInt64(0, false);
2834
+ return Number(view.getBigInt64(0, false));
2790
2835
  }
2791
2836
  }
2792
- // src/core-memory.ts
2793
- class CoreMemory {
2794
- memory;
2795
- constructor(memory) {
2796
- this.memory = memory;
2837
+ // src/core-file.ts
2838
+ import * as fs from "fs/promises";
2839
+
2840
+ class CoreFile {
2841
+ filePath;
2842
+ _position = 0;
2843
+ fileHandle;
2844
+ constructor(filePath, fileHandle) {
2845
+ this.filePath = filePath;
2846
+ this.fileHandle = fileHandle;
2847
+ }
2848
+ static async create(filePath) {
2849
+ try {
2850
+ await fs.access(filePath);
2851
+ } catch {
2852
+ await fs.writeFile(filePath, new Uint8Array(0));
2853
+ }
2854
+ const fileHandle = await fs.open(filePath, "r+");
2855
+ return new CoreFile(filePath, fileHandle);
2797
2856
  }
2798
2857
  reader() {
2799
- return this.memory;
2858
+ return new FileDataReader(this);
2800
2859
  }
2801
2860
  writer() {
2802
- return this.memory;
2861
+ return new FileDataWriter(this);
2803
2862
  }
2804
2863
  async length() {
2805
- return BigInt(this.memory.size());
2864
+ const stats = await this.fileHandle.stat();
2865
+ return stats.size;
2806
2866
  }
2807
2867
  async seek(pos) {
2808
- this.memory.seek(Number(pos));
2868
+ this._position = pos;
2809
2869
  }
2810
- async position() {
2811
- return BigInt(this.memory.getPosition());
2870
+ position() {
2871
+ return this._position;
2812
2872
  }
2813
2873
  async setLength(len) {
2814
- this.memory.setLength(Number(len));
2874
+ await this.fileHandle.truncate(len);
2815
2875
  }
2816
2876
  async flush() {}
2817
- async sync() {}
2818
- }
2819
- // src/core-file.ts
2820
- class FileDataReader {
2821
- file;
2822
- position;
2823
- buffer = null;
2824
- bufferStart = 0;
2825
- bufferEnd = 0;
2826
- constructor(file, position) {
2827
- this.file = file;
2828
- this.position = position;
2877
+ async sync() {
2878
+ await this.fileHandle.sync();
2829
2879
  }
2830
- setPosition(pos) {
2831
- this.position = pos;
2880
+ [Symbol.dispose]() {
2881
+ import("fs").then((fs2) => {
2882
+ fs2.closeSync(this.fileHandle.fd);
2883
+ });
2832
2884
  }
2833
- getPosition() {
2834
- return this.position;
2885
+ }
2886
+
2887
+ class FileDataReader {
2888
+ core;
2889
+ constructor(core) {
2890
+ this.core = core;
2835
2891
  }
2836
2892
  async readFully(b) {
2837
- const slice = this.file.slice(this.position, this.position + b.length);
2838
- const data = await slice.arrayBuffer();
2839
- b.set(new Uint8Array(data));
2840
- this.position += b.length;
2893
+ const position = this.core.position();
2894
+ await this.core.fileHandle.readv([b], position);
2895
+ this.core.seek(position + b.length);
2841
2896
  }
2842
2897
  async readByte() {
2843
2898
  const bytes = new Uint8Array(1);
@@ -2860,42 +2915,19 @@ class FileDataReader {
2860
2915
  const bytes = new Uint8Array(8);
2861
2916
  await this.readFully(bytes);
2862
2917
  const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
2863
- return view.getBigInt64(0, false);
2918
+ return Number(view.getBigInt64(0, false));
2864
2919
  }
2865
2920
  }
2866
2921
 
2867
2922
  class FileDataWriter {
2868
- filePath;
2869
- position;
2870
- pendingWrites = [];
2871
- constructor(filePath, position) {
2872
- this.filePath = filePath;
2873
- this.position = position;
2874
- }
2875
- setPosition(pos) {
2876
- this.position = pos;
2877
- }
2878
- getPosition() {
2879
- return this.position;
2923
+ core;
2924
+ constructor(core) {
2925
+ this.core = core;
2880
2926
  }
2881
2927
  async write(buffer) {
2882
- const file = Bun.file(this.filePath);
2883
- const currentSize = file.size;
2884
- if (this.position >= currentSize) {
2885
- const existingData = currentSize > 0 ? new Uint8Array(await file.arrayBuffer()) : new Uint8Array(0);
2886
- const newData = new Uint8Array(this.position + buffer.length);
2887
- newData.set(existingData);
2888
- newData.set(buffer, this.position);
2889
- await Bun.write(this.filePath, newData);
2890
- } else {
2891
- const existingData = new Uint8Array(await file.arrayBuffer());
2892
- const newSize = Math.max(existingData.length, this.position + buffer.length);
2893
- const newData = new Uint8Array(newSize);
2894
- newData.set(existingData);
2895
- newData.set(buffer, this.position);
2896
- await Bun.write(this.filePath, newData);
2897
- }
2898
- this.position += buffer.length;
2928
+ const position = this.core.position();
2929
+ await this.core.fileHandle.writev([buffer], position);
2930
+ this.core.seek(position + buffer.length);
2899
2931
  }
2900
2932
  async writeByte(v) {
2901
2933
  await this.write(new Uint8Array([v & 255]));
@@ -2909,54 +2941,185 @@ class FileDataWriter {
2909
2941
  async writeLong(v) {
2910
2942
  const buffer = new ArrayBuffer(8);
2911
2943
  const view = new DataView(buffer);
2912
- view.setBigInt64(0, v, false);
2944
+ view.setBigInt64(0, BigInt(v), false);
2913
2945
  await this.write(new Uint8Array(buffer));
2914
2946
  }
2915
2947
  }
2916
-
2917
- class CoreFile {
2918
- filePath;
2919
- _position = 0;
2920
- _reader;
2921
- _writer;
2922
- constructor(filePath) {
2923
- this.filePath = filePath;
2924
- this._reader = new FileDataReader(Bun.file(filePath), 0);
2925
- this._writer = new FileDataWriter(filePath, 0);
2948
+ // src/core-buffered-file.ts
2949
+ class CoreBufferedFile {
2950
+ file;
2951
+ constructor(file) {
2952
+ this.file = file;
2926
2953
  }
2927
- static async create(filePath) {
2928
- const file = Bun.file(filePath);
2929
- if (!await file.exists()) {
2930
- await Bun.write(filePath, new Uint8Array(0));
2931
- }
2932
- return new CoreFile(filePath);
2954
+ static async create(filePath, bufferSize) {
2955
+ const file = await RandomAccessBufferedFile.create(filePath, bufferSize);
2956
+ return new CoreBufferedFile(file);
2933
2957
  }
2934
2958
  reader() {
2935
- return this._reader;
2959
+ return this.file;
2936
2960
  }
2937
2961
  writer() {
2938
- return this._writer;
2962
+ return this.file;
2939
2963
  }
2940
2964
  async length() {
2941
- const file = Bun.file(this.filePath);
2942
- return BigInt(file.size);
2965
+ return await this.file.length();
2943
2966
  }
2944
2967
  async seek(pos) {
2945
- this._position = Number(pos);
2946
- this._reader.setPosition(this._position);
2947
- this._writer.setPosition(this._position);
2968
+ await this.file.seek(pos);
2948
2969
  }
2949
- async position() {
2950
- return BigInt(this._reader.getPosition());
2970
+ position() {
2971
+ return this.file.position();
2951
2972
  }
2952
2973
  async setLength(len) {
2953
- const file = Bun.file(this.filePath);
2954
- const currentData = new Uint8Array(await file.arrayBuffer());
2955
- const newData = currentData.slice(0, Number(len));
2956
- await Bun.write(this.filePath, newData);
2974
+ await this.file.setLength(len);
2975
+ }
2976
+ async flush() {
2977
+ await this.file.flush();
2978
+ }
2979
+ async sync() {
2980
+ await this.file.sync();
2981
+ }
2982
+ [Symbol.dispose]() {
2983
+ this.file.file[Symbol.dispose]();
2984
+ }
2985
+ }
2986
+ var DEFAULT_BUFFER_SIZE = 8 * 1024 * 1024;
2987
+
2988
+ class RandomAccessBufferedFile {
2989
+ file;
2990
+ memory;
2991
+ bufferSize;
2992
+ filePos;
2993
+ memoryPos;
2994
+ constructor(file, bufferSize) {
2995
+ this.file = file;
2996
+ this.memory = new CoreMemory;
2997
+ this.bufferSize = bufferSize;
2998
+ this.filePos = 0;
2999
+ this.memoryPos = 0;
3000
+ }
3001
+ static async create(filePath, bufferSize = DEFAULT_BUFFER_SIZE) {
3002
+ const file = await CoreFile.create(filePath);
3003
+ return new RandomAccessBufferedFile(file, bufferSize);
3004
+ }
3005
+ async seek(pos) {
3006
+ if (pos > this.memoryPos + await this.memory.length()) {
3007
+ await this.flush();
3008
+ }
3009
+ this.filePos = pos;
3010
+ if (await this.memory.length() === 0) {
3011
+ this.memoryPos = pos;
3012
+ }
3013
+ }
3014
+ async length() {
3015
+ return Math.max(this.memoryPos + await this.memory.length(), await this.file.length());
3016
+ }
3017
+ position() {
3018
+ return this.filePos;
3019
+ }
3020
+ async setLength(len) {
3021
+ await this.flush();
3022
+ await this.file.setLength(len);
3023
+ this.filePos = Math.min(len, this.filePos);
3024
+ }
3025
+ async flush() {
3026
+ if (await this.memory.length() > 0) {
3027
+ await this.file.seek(this.memoryPos);
3028
+ await this.file.writer().write(this.memory.memory.toByteArray());
3029
+ this.memoryPos = 0;
3030
+ this.memory.memory.reset();
3031
+ }
3032
+ }
3033
+ async sync() {
3034
+ await this.flush();
3035
+ await this.file.sync();
3036
+ }
3037
+ async write(buffer) {
3038
+ if (await this.memory.length() + buffer.length > this.bufferSize) {
3039
+ await this.flush();
3040
+ }
3041
+ if (this.filePos >= this.memoryPos && this.filePos <= this.memoryPos + await this.memory.length()) {
3042
+ this.memory.seek(this.filePos - this.memoryPos);
3043
+ await this.memory.memory.write(buffer);
3044
+ } else {
3045
+ await this.file.seek(this.filePos);
3046
+ await this.file.writer().write(buffer);
3047
+ }
3048
+ this.filePos += buffer.length;
3049
+ }
3050
+ async writeByte(v) {
3051
+ await this.write(new Uint8Array([v & 255]));
3052
+ }
3053
+ async writeShort(v) {
3054
+ const buffer = new ArrayBuffer(2);
3055
+ const view = new DataView(buffer);
3056
+ view.setInt16(0, v & 65535, false);
3057
+ await this.write(new Uint8Array(buffer));
3058
+ }
3059
+ async writeLong(v) {
3060
+ const buffer = new ArrayBuffer(8);
3061
+ const view = new DataView(buffer);
3062
+ view.setBigInt64(0, BigInt(v), false);
3063
+ await this.write(new Uint8Array(buffer));
3064
+ }
3065
+ async readFully(buffer) {
3066
+ let pos = 0;
3067
+ if (this.filePos < this.memoryPos) {
3068
+ const sizeBeforeMem = Math.min(this.memoryPos - this.filePos, buffer.length);
3069
+ const tempBuffer = new Uint8Array(sizeBeforeMem);
3070
+ await this.file.seek(this.filePos);
3071
+ await this.file.reader().readFully(tempBuffer);
3072
+ buffer.set(tempBuffer, pos);
3073
+ pos += sizeBeforeMem;
3074
+ this.filePos += sizeBeforeMem;
3075
+ }
3076
+ if (pos === buffer.length)
3077
+ return;
3078
+ if (this.filePos >= this.memoryPos && this.filePos < this.memoryPos + await this.memory.length()) {
3079
+ const memPos = this.filePos - this.memoryPos;
3080
+ const sizeInMem = Math.min(await this.memory.length() - memPos, buffer.length - pos);
3081
+ this.memory.seek(memPos);
3082
+ const memBuffer = new Uint8Array(sizeInMem);
3083
+ await this.memory.memory.readFully(memBuffer);
3084
+ buffer.set(memBuffer, pos);
3085
+ pos += sizeInMem;
3086
+ this.filePos += sizeInMem;
3087
+ }
3088
+ if (pos === buffer.length)
3089
+ return;
3090
+ if (this.filePos >= this.memoryPos + await this.memory.length()) {
3091
+ const sizeAfterMem = buffer.length - pos;
3092
+ const tempBuffer = new Uint8Array(sizeAfterMem);
3093
+ await this.file.seek(this.filePos);
3094
+ await this.file.reader().readFully(tempBuffer);
3095
+ buffer.set(tempBuffer, pos);
3096
+ pos += sizeAfterMem;
3097
+ this.filePos += sizeAfterMem;
3098
+ }
3099
+ }
3100
+ async readByte() {
3101
+ const bytes = new Uint8Array(1);
3102
+ await this.readFully(bytes);
3103
+ return bytes[0];
3104
+ }
3105
+ async readShort() {
3106
+ const bytes = new Uint8Array(2);
3107
+ await this.readFully(bytes);
3108
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
3109
+ return view.getInt16(0, false);
3110
+ }
3111
+ async readInt() {
3112
+ const bytes = new Uint8Array(4);
3113
+ await this.readFully(bytes);
3114
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
3115
+ return view.getInt32(0, false);
3116
+ }
3117
+ async readLong() {
3118
+ const bytes = new Uint8Array(8);
3119
+ await this.readFully(bytes);
3120
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
3121
+ return Number(view.getBigInt64(0, false));
2957
3122
  }
2958
- async flush() {}
2959
- async sync() {}
2960
3123
  }
2961
3124
  // src/hasher.ts
2962
3125
  class Hasher {
@@ -3637,7 +3800,6 @@ export {
3637
3800
  ReadCountedHashSet,
3638
3801
  ReadCountedHashMap,
3639
3802
  ReadArrayList,
3640
- RandomAccessMemory,
3641
3803
  PathPartMustBeAtEndException,
3642
3804
  NotImplementedException,
3643
3805
  NoAvailableSlotsException,
@@ -3690,6 +3852,7 @@ export {
3690
3852
  CursorIterator,
3691
3853
  CoreMemory,
3692
3854
  CoreFile,
3855
+ CoreBufferedFile,
3693
3856
  Context,
3694
3857
  Bytes,
3695
3858
  BIT_COUNT,