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
@@ -1,86 +0,0 @@
1
- import { ReadHashSet } from './read-hash-set';
2
- import { WriteCursor, WriteCursorIterator } from './write-cursor';
3
- import { HashMapInit, HashMapGet, HashMapGetKey, HashMapRemove } from './database';
4
- import type { WriteableData } from './writeable-data';
5
- import { Bytes } from './writeable-data';
6
- import { KeyNotFoundException } from './exceptions';
7
-
8
- export class WriteHashSet extends ReadHashSet {
9
- protected constructor() {
10
- super();
11
- }
12
-
13
- static async create(cursor: WriteCursor): Promise<WriteHashSet> {
14
- const set = new WriteHashSet();
15
- const newCursor = await cursor.writePath([new HashMapInit(false, true)]);
16
- set.cursor = newCursor;
17
- return set;
18
- }
19
-
20
- override iterator(): WriteCursorIterator {
21
- return (this.cursor as WriteCursor).iterator();
22
- }
23
-
24
- override async *[Symbol.asyncIterator](): AsyncIterator<WriteCursor> {
25
- yield* this.cursor as WriteCursor;
26
- }
27
-
28
- // Methods that take a string key and hash it
29
- async putByString(key: string): Promise<void> {
30
- const bytes = new TextEncoder().encode(key);
31
- const hash = await this.cursor.db.hasher.digest(bytes);
32
- await this.put(hash, new Bytes(bytes));
33
- }
34
-
35
- async putCursorByString(key: string): Promise<WriteCursor> {
36
- const hash = await this.cursor.db.hasher.digest(new TextEncoder().encode(key));
37
- return this.putCursor(hash);
38
- }
39
-
40
- async removeByString(key: string): Promise<boolean> {
41
- const hash = await this.cursor.db.hasher.digest(new TextEncoder().encode(key));
42
- return this.remove(hash);
43
- }
44
-
45
- // Methods that take Bytes key and hash it
46
- async putByBytes(key: Bytes): Promise<void> {
47
- const hash = await this.cursor.db.hasher.digest(key.value);
48
- await this.put(hash, key);
49
- }
50
-
51
- async putCursorByBytes(key: Bytes): Promise<WriteCursor> {
52
- const hash = await this.cursor.db.hasher.digest(key.value);
53
- return this.putCursor(hash);
54
- }
55
-
56
- async removeByBytes(key: Bytes): Promise<boolean> {
57
- const hash = await this.cursor.db.hasher.digest(key.value);
58
- return this.remove(hash);
59
- }
60
-
61
- // Methods that take hash directly
62
- async put(hash: Uint8Array, data: WriteableData): Promise<void> {
63
- const cursor = await (this.cursor as WriteCursor).writePath([
64
- new HashMapGet(new HashMapGetKey(hash)),
65
- ]);
66
- await cursor.writeIfEmpty(data);
67
- }
68
-
69
- async putCursor(hash: Uint8Array): Promise<WriteCursor> {
70
- return (this.cursor as WriteCursor).writePath([
71
- new HashMapGet(new HashMapGetKey(hash)),
72
- ]);
73
- }
74
-
75
- async remove(hash: Uint8Array): Promise<boolean> {
76
- try {
77
- await (this.cursor as WriteCursor).writePath([new HashMapRemove(hash)]);
78
- } catch (e) {
79
- if (e instanceof KeyNotFoundException) {
80
- return false;
81
- }
82
- throw e;
83
- }
84
- return true;
85
- }
86
- }
@@ -1,80 +0,0 @@
1
- import { Slot } from './slot';
2
- import { ReadLinkedArrayList } from './read-linked-array-list';
3
- import { WriteCursor, WriteCursorIterator } from './write-cursor';
4
- import {
5
- LinkedArrayListInit,
6
- LinkedArrayListGet,
7
- LinkedArrayListAppend,
8
- LinkedArrayListSlice,
9
- LinkedArrayListConcat,
10
- LinkedArrayListInsert,
11
- LinkedArrayListRemove,
12
- WriteData,
13
- } from './database';
14
- import type { WriteableData } from './writeable-data';
15
-
16
- export class WriteLinkedArrayList extends ReadLinkedArrayList {
17
- constructor(cursor: WriteCursor) {
18
- super(cursor);
19
- }
20
-
21
- static async create(cursor: WriteCursor): Promise<WriteLinkedArrayList> {
22
- const newCursor = await cursor.writePath([new LinkedArrayListInit()]);
23
- return new WriteLinkedArrayList(newCursor);
24
- }
25
-
26
- override iterator(): WriteCursorIterator {
27
- return (this.cursor as WriteCursor).iterator();
28
- }
29
-
30
- override async *[Symbol.asyncIterator](): AsyncIterator<WriteCursor> {
31
- yield* this.cursor as WriteCursor;
32
- }
33
-
34
- async put(index: number, data: WriteableData): Promise<void> {
35
- await (this.cursor as WriteCursor).writePath([
36
- new LinkedArrayListGet(index),
37
- new WriteData(data),
38
- ]);
39
- }
40
-
41
- async putCursor(index: number): Promise<WriteCursor> {
42
- return (this.cursor as WriteCursor).writePath([new LinkedArrayListGet(index)]);
43
- }
44
-
45
- async append(data: WriteableData): Promise<void> {
46
- await (this.cursor as WriteCursor).writePath([
47
- new LinkedArrayListAppend(),
48
- new WriteData(data),
49
- ]);
50
- }
51
-
52
- async appendCursor(): Promise<WriteCursor> {
53
- return (this.cursor as WriteCursor).writePath([new LinkedArrayListAppend()]);
54
- }
55
-
56
- async slice(offset: number, size: number): Promise<void> {
57
- await (this.cursor as WriteCursor).writePath([
58
- new LinkedArrayListSlice(offset, size),
59
- ]);
60
- }
61
-
62
- async concat(list: Slot): Promise<void> {
63
- await (this.cursor as WriteCursor).writePath([new LinkedArrayListConcat(list)]);
64
- }
65
-
66
- async insert(index: number, data: WriteableData): Promise<void> {
67
- await (this.cursor as WriteCursor).writePath([
68
- new LinkedArrayListInsert(index),
69
- new WriteData(data),
70
- ]);
71
- }
72
-
73
- async insertCursor(index: number): Promise<WriteCursor> {
74
- return (this.cursor as WriteCursor).writePath([new LinkedArrayListInsert(index)]);
75
- }
76
-
77
- async remove(index: number): Promise<void> {
78
- await (this.cursor as WriteCursor).writePath([new LinkedArrayListRemove(index)]);
79
- }
80
- }
@@ -1,67 +0,0 @@
1
- import { InvalidFormatTagSizeException } from './exceptions';
2
-
3
- export interface WriteableData {}
4
-
5
- export class Uint implements WriteableData {
6
- readonly value: number;
7
-
8
- constructor(value: number) {
9
- if (value < 0) {
10
- throw new Error('Uint must not be negative');
11
- }
12
- this.value = value;
13
- }
14
- }
15
-
16
- export class Int implements WriteableData {
17
- readonly value: number;
18
-
19
- constructor(value: number) {
20
- this.value = value;
21
- }
22
- }
23
-
24
- export class Float implements WriteableData {
25
- readonly value: number;
26
-
27
- constructor(value: number) {
28
- this.value = value;
29
- }
30
- }
31
-
32
- export class Bytes implements WriteableData {
33
- readonly value: Uint8Array;
34
- readonly formatTag: Uint8Array | null;
35
-
36
- constructor(value: Uint8Array | string, formatTag?: Uint8Array | string | null) {
37
- if (typeof value === 'string') {
38
- this.value = new TextEncoder().encode(value);
39
- } else {
40
- this.value = value;
41
- }
42
-
43
- if (formatTag === undefined || formatTag === null) {
44
- this.formatTag = null;
45
- } else if (typeof formatTag === 'string') {
46
- const encoded = new TextEncoder().encode(formatTag);
47
- if (encoded.length !== 2) {
48
- throw new InvalidFormatTagSizeException();
49
- }
50
- this.formatTag = encoded;
51
- } else {
52
- if (formatTag.length !== 2) {
53
- throw new InvalidFormatTagSizeException();
54
- }
55
- this.formatTag = formatTag;
56
- }
57
- }
58
-
59
- isShort(): boolean {
60
- const totalSize = this.formatTag !== null ? 6 : 8;
61
- if (this.value.length > totalSize) return false;
62
- for (const b of this.value) {
63
- if (b === 0) return false;
64
- }
65
- return true;
66
- }
67
- }