xitdb 0.12.0 → 0.14.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.
- package/README.md +120 -118
- package/dist/core-buffered-file.d.ts +23 -25
- package/dist/core-buffered-file.js +182 -0
- package/dist/core-file.d.ts +8 -10
- package/dist/core-file.js +105 -0
- package/dist/core-memory.d.ts +15 -15
- package/dist/core-memory.js +152 -0
- package/dist/core.d.ts +14 -14
- package/dist/core.js +1 -0
- package/dist/database.d.ts +204 -80
- package/dist/database.js +3047 -0
- package/dist/exceptions.d.ts +4 -0
- package/dist/exceptions.js +62 -0
- package/dist/hasher.d.ts +2 -1
- package/dist/hasher.js +49 -0
- package/dist/index.d.ts +30 -26
- package/dist/index.js +35 -3986
- package/dist/read-array-list.d.ts +8 -8
- package/dist/read-array-list.js +35 -0
- package/dist/read-counted-hash-map.d.ts +3 -3
- package/dist/read-counted-hash-map.js +19 -0
- package/dist/read-counted-hash-set.d.ts +3 -3
- package/dist/read-counted-hash-set.js +19 -0
- package/dist/read-cursor.d.ts +29 -23
- package/dist/read-cursor.js +577 -0
- package/dist/read-hash-map.d.ts +22 -22
- package/dist/read-hash-map.js +65 -0
- package/dist/read-hash-set.d.ts +13 -13
- package/dist/read-hash-set.js +47 -0
- package/dist/read-linked-array-list.d.ts +8 -8
- package/dist/read-linked-array-list.js +35 -0
- package/dist/read-sorted-map.d.ts +21 -0
- package/dist/read-sorted-map.js +73 -0
- package/dist/read-sorted-set.d.ts +19 -0
- package/dist/read-sorted-set.js +65 -0
- package/dist/slot-pointer.d.ts +1 -1
- package/dist/slot-pointer.js +11 -0
- package/dist/slot.d.ts +2 -2
- package/dist/slot.js +41 -0
- package/dist/slotted.d.ts +1 -1
- package/dist/slotted.js +1 -0
- package/dist/tag.d.ts +3 -1
- package/dist/tag.js +25 -0
- package/dist/write-array-list.d.ts +13 -14
- package/dist/write-array-list.js +42 -0
- package/dist/write-counted-hash-map.d.ts +4 -5
- package/dist/write-counted-hash-map.js +18 -0
- package/dist/write-counted-hash-set.d.ts +4 -5
- package/dist/write-counted-hash-set.js +18 -0
- package/dist/write-cursor.d.ts +15 -15
- package/dist/write-cursor.js +124 -0
- package/dist/write-hash-map.d.ts +22 -23
- package/dist/write-hash-map.js +90 -0
- package/dist/write-hash-set.d.ts +16 -17
- package/dist/write-hash-set.js +59 -0
- package/dist/write-linked-array-list.d.ts +16 -17
- package/dist/write-linked-array-list.js +52 -0
- package/dist/write-sorted-map.d.ts +12 -0
- package/dist/write-sorted-map.js +37 -0
- package/dist/write-sorted-set.d.ts +10 -0
- package/dist/write-sorted-set.js +29 -0
- package/dist/writeable-data.js +68 -0
- package/package.json +6 -6
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { Slot } from './slot.js';
|
|
2
|
+
import { WriteMode, WriteData, } from './database.js';
|
|
3
|
+
import { ReadCursor, KeyValuePairCursor, CursorIterator, } from './read-cursor.js';
|
|
4
|
+
import { CursorNotWriteableException, EndOfStreamException, UnexpectedWriterPositionException, } from './exceptions.js';
|
|
5
|
+
export class WriteKeyValuePairCursor extends KeyValuePairCursor {
|
|
6
|
+
valueCursor;
|
|
7
|
+
keyCursor;
|
|
8
|
+
constructor(valueCursor, keyCursor, hash) {
|
|
9
|
+
super(valueCursor, keyCursor, hash);
|
|
10
|
+
this.valueCursor = valueCursor;
|
|
11
|
+
this.keyCursor = keyCursor;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export class WriteCursor extends ReadCursor {
|
|
15
|
+
constructor(slotPtr, db) {
|
|
16
|
+
super(slotPtr, db);
|
|
17
|
+
}
|
|
18
|
+
writePath(path) {
|
|
19
|
+
const slotPtr = this.db.readSlotPointer(WriteMode.READ_WRITE, path, 0, this.slotPtr);
|
|
20
|
+
if (this.db.txStart === null) {
|
|
21
|
+
this.db.core.sync();
|
|
22
|
+
}
|
|
23
|
+
return new WriteCursor(slotPtr, this.db);
|
|
24
|
+
}
|
|
25
|
+
write(data) {
|
|
26
|
+
const cursor = this.writePath([new WriteData(data)]);
|
|
27
|
+
this.slotPtr = cursor.slotPtr;
|
|
28
|
+
}
|
|
29
|
+
writeIfEmpty(data) {
|
|
30
|
+
if (this.slotPtr.slot.empty()) {
|
|
31
|
+
this.write(data);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
readKeyValuePair() {
|
|
35
|
+
const kvPairCursor = super.readKeyValuePair();
|
|
36
|
+
return new WriteKeyValuePairCursor(new WriteCursor(kvPairCursor.valueCursor.slotPtr, this.db), new WriteCursor(kvPairCursor.keyCursor.slotPtr, this.db), kvPairCursor.hash);
|
|
37
|
+
}
|
|
38
|
+
writer() {
|
|
39
|
+
const writer = this.db.core.writer();
|
|
40
|
+
const ptrPos = this.db.core.length();
|
|
41
|
+
this.db.core.seek(ptrPos);
|
|
42
|
+
writer.writeLong(0);
|
|
43
|
+
const startPosition = this.db.core.length();
|
|
44
|
+
return new Writer(this, 0, new Slot(ptrPos, 6 /* Tag.BYTES */), startPosition, 0);
|
|
45
|
+
}
|
|
46
|
+
*[Symbol.iterator]() {
|
|
47
|
+
const iterator = this.iterator();
|
|
48
|
+
while (iterator.hasNext()) {
|
|
49
|
+
const next = iterator.next();
|
|
50
|
+
if (next !== null) {
|
|
51
|
+
yield next;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
iterator() {
|
|
56
|
+
const iterator = new WriteCursorIterator(this);
|
|
57
|
+
iterator.init();
|
|
58
|
+
return iterator;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export class Writer {
|
|
62
|
+
parent;
|
|
63
|
+
size;
|
|
64
|
+
slot;
|
|
65
|
+
startPosition;
|
|
66
|
+
relativePosition;
|
|
67
|
+
formatTag = null;
|
|
68
|
+
constructor(parent, size, slot, startPosition, relativePosition) {
|
|
69
|
+
this.parent = parent;
|
|
70
|
+
this.size = size;
|
|
71
|
+
this.slot = slot;
|
|
72
|
+
this.startPosition = startPosition;
|
|
73
|
+
this.relativePosition = relativePosition;
|
|
74
|
+
}
|
|
75
|
+
write(buffer) {
|
|
76
|
+
if (this.size < this.relativePosition)
|
|
77
|
+
throw new EndOfStreamException();
|
|
78
|
+
this.parent.db.core.seek(this.startPosition + this.relativePosition);
|
|
79
|
+
const writer = this.parent.db.core.writer();
|
|
80
|
+
writer.write(buffer);
|
|
81
|
+
this.relativePosition += buffer.length;
|
|
82
|
+
if (this.relativePosition > this.size) {
|
|
83
|
+
this.size = this.relativePosition;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
finish() {
|
|
87
|
+
const writer = this.parent.db.core.writer();
|
|
88
|
+
if (this.formatTag !== null) {
|
|
89
|
+
this.slot = this.slot.withFull(true);
|
|
90
|
+
const formatTagPos = this.parent.db.core.length();
|
|
91
|
+
this.parent.db.core.seek(formatTagPos);
|
|
92
|
+
if (this.startPosition + this.size !== formatTagPos)
|
|
93
|
+
throw new UnexpectedWriterPositionException();
|
|
94
|
+
writer.write(this.formatTag);
|
|
95
|
+
}
|
|
96
|
+
this.parent.db.core.seek(Number(this.slot.value));
|
|
97
|
+
writer.writeLong(this.size);
|
|
98
|
+
if (this.parent.slotPtr.position === null)
|
|
99
|
+
throw new CursorNotWriteableException();
|
|
100
|
+
const position = this.parent.slotPtr.position;
|
|
101
|
+
this.parent.db.core.seek(position);
|
|
102
|
+
writer.write(this.slot.toBytes());
|
|
103
|
+
this.parent.slotPtr = this.parent.slotPtr.withSlot(this.slot);
|
|
104
|
+
}
|
|
105
|
+
seek(position) {
|
|
106
|
+
if (position <= this.size) {
|
|
107
|
+
this.relativePosition = position;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
export class WriteCursorIterator extends CursorIterator {
|
|
112
|
+
constructor(cursor) {
|
|
113
|
+
super(cursor);
|
|
114
|
+
}
|
|
115
|
+
next() {
|
|
116
|
+
const readCursor = super.next();
|
|
117
|
+
if (readCursor !== null) {
|
|
118
|
+
return new WriteCursor(readCursor.slotPtr, readCursor.db);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
package/dist/write-hash-map.d.ts
CHANGED
|
@@ -1,27 +1,26 @@
|
|
|
1
|
-
import { ReadHashMap } from './read-hash-map';
|
|
2
|
-
import { WriteCursor, WriteCursorIterator } from './write-cursor';
|
|
3
|
-
import type { WriteableData } from './writeable-data';
|
|
4
|
-
import { Bytes } from './writeable-data';
|
|
1
|
+
import { ReadHashMap } from './read-hash-map.js';
|
|
2
|
+
import { WriteCursor, WriteCursorIterator } from './write-cursor.js';
|
|
3
|
+
import type { WriteableData } from './writeable-data.js';
|
|
4
|
+
import { Bytes } from './writeable-data.js';
|
|
5
5
|
export declare class WriteHashMap extends ReadHashMap {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
iterator():
|
|
9
|
-
|
|
10
|
-
put(key:
|
|
11
|
-
put(
|
|
12
|
-
|
|
13
|
-
putCursor(key:
|
|
14
|
-
putCursor(
|
|
15
|
-
|
|
16
|
-
putKey(key:
|
|
17
|
-
putKey(
|
|
18
|
-
|
|
19
|
-
putKeyCursor(key:
|
|
20
|
-
putKeyCursor(
|
|
21
|
-
|
|
22
|
-
remove(key:
|
|
23
|
-
remove(
|
|
24
|
-
remove(hash: Uint8Array): Promise<boolean>;
|
|
6
|
+
constructor(cursor: WriteCursor, counted?: boolean);
|
|
7
|
+
iterator(): WriteCursorIterator;
|
|
8
|
+
[Symbol.iterator](): Iterator<WriteCursor>;
|
|
9
|
+
put(key: string, data: WriteableData): void;
|
|
10
|
+
put(key: Bytes, data: WriteableData): void;
|
|
11
|
+
put(hash: Uint8Array, data: WriteableData): void;
|
|
12
|
+
putCursor(key: string): WriteCursor;
|
|
13
|
+
putCursor(key: Bytes): WriteCursor;
|
|
14
|
+
putCursor(hash: Uint8Array): WriteCursor;
|
|
15
|
+
putKey(key: string, data: WriteableData): void;
|
|
16
|
+
putKey(key: Bytes, data: WriteableData): void;
|
|
17
|
+
putKey(hash: Uint8Array, data: WriteableData): void;
|
|
18
|
+
putKeyCursor(key: string): WriteCursor;
|
|
19
|
+
putKeyCursor(key: Bytes): WriteCursor;
|
|
20
|
+
putKeyCursor(hash: Uint8Array): WriteCursor;
|
|
21
|
+
remove(key: string): boolean;
|
|
22
|
+
remove(key: Bytes): boolean;
|
|
23
|
+
remove(hash: Uint8Array): boolean;
|
|
25
24
|
private putInternal;
|
|
26
25
|
private putCursorInternal;
|
|
27
26
|
private putKeyInternal;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { ReadHashMap } from './read-hash-map.js';
|
|
2
|
+
import { HashMapInit, HashMapGet, HashMapGetValue, HashMapGetKey, HashMapRemove, WriteData, } from './database.js';
|
|
3
|
+
import { Bytes } from './writeable-data.js';
|
|
4
|
+
import { KeyNotFoundException } from './exceptions.js';
|
|
5
|
+
export class WriteHashMap extends ReadHashMap {
|
|
6
|
+
constructor(cursor, counted = false) {
|
|
7
|
+
super();
|
|
8
|
+
this.cursor = cursor.writePath([new HashMapInit(counted, false)]);
|
|
9
|
+
}
|
|
10
|
+
iterator() {
|
|
11
|
+
return this.cursor.iterator();
|
|
12
|
+
}
|
|
13
|
+
*[Symbol.iterator]() {
|
|
14
|
+
yield* this.cursor;
|
|
15
|
+
}
|
|
16
|
+
put(key, data) {
|
|
17
|
+
if (typeof key === 'string') {
|
|
18
|
+
const hash = this.cursor.db.hasher.digest(new TextEncoder().encode(key));
|
|
19
|
+
this.putKeyInternal(hash, new Bytes(key));
|
|
20
|
+
this.putInternal(hash, data);
|
|
21
|
+
}
|
|
22
|
+
else if (key instanceof Bytes) {
|
|
23
|
+
const hash = this.cursor.db.hasher.digest(key.value);
|
|
24
|
+
this.putKeyInternal(hash, key);
|
|
25
|
+
this.putInternal(hash, data);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
this.putInternal(key, data);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
putCursor(key) {
|
|
32
|
+
if (typeof key === 'string') {
|
|
33
|
+
const hash = this.cursor.db.hasher.digest(new TextEncoder().encode(key));
|
|
34
|
+
this.putKeyInternal(hash, new Bytes(key));
|
|
35
|
+
return this.putCursorInternal(hash);
|
|
36
|
+
}
|
|
37
|
+
else if (key instanceof Bytes) {
|
|
38
|
+
const hash = this.cursor.db.hasher.digest(key.value);
|
|
39
|
+
this.putKeyInternal(hash, key);
|
|
40
|
+
return this.putCursorInternal(hash);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
return this.putCursorInternal(key);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
putKey(key, data) {
|
|
47
|
+
const hash = this.resolveHash(key);
|
|
48
|
+
this.putKeyInternal(hash, data);
|
|
49
|
+
}
|
|
50
|
+
putKeyCursor(key) {
|
|
51
|
+
const hash = this.resolveHash(key);
|
|
52
|
+
return this.putKeyCursorInternal(hash);
|
|
53
|
+
}
|
|
54
|
+
remove(key) {
|
|
55
|
+
const hash = this.resolveHash(key);
|
|
56
|
+
try {
|
|
57
|
+
this.cursor.writePath([new HashMapRemove(hash)]);
|
|
58
|
+
}
|
|
59
|
+
catch (e) {
|
|
60
|
+
if (e instanceof KeyNotFoundException) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
throw e;
|
|
64
|
+
}
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
// Internal methods that take hash directly
|
|
68
|
+
putInternal(hash, data) {
|
|
69
|
+
this.cursor.writePath([
|
|
70
|
+
new HashMapGet(new HashMapGetValue(hash)),
|
|
71
|
+
new WriteData(data),
|
|
72
|
+
]);
|
|
73
|
+
}
|
|
74
|
+
putCursorInternal(hash) {
|
|
75
|
+
return this.cursor.writePath([
|
|
76
|
+
new HashMapGet(new HashMapGetValue(hash)),
|
|
77
|
+
]);
|
|
78
|
+
}
|
|
79
|
+
putKeyInternal(hash, data) {
|
|
80
|
+
const cursor = this.cursor.writePath([
|
|
81
|
+
new HashMapGet(new HashMapGetKey(hash)),
|
|
82
|
+
]);
|
|
83
|
+
cursor.writeIfEmpty(data);
|
|
84
|
+
}
|
|
85
|
+
putKeyCursorInternal(hash) {
|
|
86
|
+
return this.cursor.writePath([
|
|
87
|
+
new HashMapGet(new HashMapGetKey(hash)),
|
|
88
|
+
]);
|
|
89
|
+
}
|
|
90
|
+
}
|
package/dist/write-hash-set.d.ts
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
import { ReadHashSet } from './read-hash-set';
|
|
2
|
-
import { WriteCursor, WriteCursorIterator } from './write-cursor';
|
|
3
|
-
import type { WriteableData } from './writeable-data';
|
|
4
|
-
import { Bytes } from './writeable-data';
|
|
1
|
+
import { ReadHashSet } from './read-hash-set.js';
|
|
2
|
+
import { WriteCursor, WriteCursorIterator } from './write-cursor.js';
|
|
3
|
+
import type { WriteableData } from './writeable-data.js';
|
|
4
|
+
import { Bytes } from './writeable-data.js';
|
|
5
5
|
export declare class WriteHashSet extends ReadHashSet {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
iterator():
|
|
9
|
-
|
|
10
|
-
put(key:
|
|
11
|
-
put(
|
|
12
|
-
|
|
13
|
-
putCursor(key:
|
|
14
|
-
putCursor(
|
|
15
|
-
|
|
16
|
-
remove(key:
|
|
17
|
-
remove(
|
|
18
|
-
remove(hash: Uint8Array): Promise<boolean>;
|
|
6
|
+
constructor(cursor: WriteCursor, counted?: boolean);
|
|
7
|
+
iterator(): WriteCursorIterator;
|
|
8
|
+
[Symbol.iterator](): Iterator<WriteCursor>;
|
|
9
|
+
put(key: string): void;
|
|
10
|
+
put(key: Bytes): void;
|
|
11
|
+
put(hash: Uint8Array, data: WriteableData): void;
|
|
12
|
+
putCursor(key: string): WriteCursor;
|
|
13
|
+
putCursor(key: Bytes): WriteCursor;
|
|
14
|
+
putCursor(hash: Uint8Array): WriteCursor;
|
|
15
|
+
remove(key: string): boolean;
|
|
16
|
+
remove(key: Bytes): boolean;
|
|
17
|
+
remove(hash: Uint8Array): boolean;
|
|
19
18
|
private putInternal;
|
|
20
19
|
private putCursorInternal;
|
|
21
20
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { ReadHashSet } from './read-hash-set.js';
|
|
2
|
+
import { HashMapInit, HashMapGet, HashMapGetKey, HashMapRemove } from './database.js';
|
|
3
|
+
import { Bytes } from './writeable-data.js';
|
|
4
|
+
import { KeyNotFoundException } from './exceptions.js';
|
|
5
|
+
export class WriteHashSet extends ReadHashSet {
|
|
6
|
+
constructor(cursor, counted = false) {
|
|
7
|
+
super();
|
|
8
|
+
this.cursor = cursor.writePath([new HashMapInit(counted, true)]);
|
|
9
|
+
}
|
|
10
|
+
iterator() {
|
|
11
|
+
return this.cursor.iterator();
|
|
12
|
+
}
|
|
13
|
+
*[Symbol.iterator]() {
|
|
14
|
+
yield* this.cursor;
|
|
15
|
+
}
|
|
16
|
+
put(key, data) {
|
|
17
|
+
if (typeof key === 'string') {
|
|
18
|
+
const bytes = new TextEncoder().encode(key);
|
|
19
|
+
const hash = this.cursor.db.hasher.digest(bytes);
|
|
20
|
+
this.putInternal(hash, new Bytes(bytes));
|
|
21
|
+
}
|
|
22
|
+
else if (key instanceof Bytes) {
|
|
23
|
+
const hash = this.cursor.db.hasher.digest(key.value);
|
|
24
|
+
this.putInternal(hash, key);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
this.putInternal(key, data);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
putCursor(key) {
|
|
31
|
+
const hash = this.resolveHash(key);
|
|
32
|
+
return this.putCursorInternal(hash);
|
|
33
|
+
}
|
|
34
|
+
remove(key) {
|
|
35
|
+
const hash = this.resolveHash(key);
|
|
36
|
+
try {
|
|
37
|
+
this.cursor.writePath([new HashMapRemove(hash)]);
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
if (e instanceof KeyNotFoundException) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
throw e;
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
// Internal methods that take hash directly
|
|
48
|
+
putInternal(hash, data) {
|
|
49
|
+
const cursor = this.cursor.writePath([
|
|
50
|
+
new HashMapGet(new HashMapGetKey(hash)),
|
|
51
|
+
]);
|
|
52
|
+
cursor.writeIfEmpty(data);
|
|
53
|
+
}
|
|
54
|
+
putCursorInternal(hash) {
|
|
55
|
+
return this.cursor.writePath([
|
|
56
|
+
new HashMapGet(new HashMapGetKey(hash)),
|
|
57
|
+
]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
import { Slot } from './slot';
|
|
2
|
-
import { ReadLinkedArrayList } from './read-linked-array-list';
|
|
3
|
-
import { WriteCursor, WriteCursorIterator } from './write-cursor';
|
|
4
|
-
import type { WriteableData } from './writeable-data';
|
|
1
|
+
import { Slot } from './slot.js';
|
|
2
|
+
import { ReadLinkedArrayList } from './read-linked-array-list.js';
|
|
3
|
+
import { WriteCursor, WriteCursorIterator } from './write-cursor.js';
|
|
4
|
+
import type { WriteableData } from './writeable-data.js';
|
|
5
5
|
export declare class WriteLinkedArrayList extends ReadLinkedArrayList {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
iterator():
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
remove(index: number): Promise<void>;
|
|
6
|
+
constructor(cursor: WriteCursor);
|
|
7
|
+
iterator(): WriteCursorIterator;
|
|
8
|
+
[Symbol.iterator](): Iterator<WriteCursor>;
|
|
9
|
+
put(index: number, data: WriteableData): void;
|
|
10
|
+
putCursor(index: number): WriteCursor;
|
|
11
|
+
append(data: WriteableData): void;
|
|
12
|
+
appendCursor(): WriteCursor;
|
|
13
|
+
slice(offset: number, size: number): void;
|
|
14
|
+
concat(list: Slot): void;
|
|
15
|
+
insert(index: number, data: WriteableData): void;
|
|
16
|
+
insertCursor(index: number): WriteCursor;
|
|
17
|
+
remove(index: number): void;
|
|
19
18
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { ReadLinkedArrayList } from './read-linked-array-list.js';
|
|
2
|
+
import { LinkedArrayListInit, LinkedArrayListGet, LinkedArrayListAppend, LinkedArrayListSlice, LinkedArrayListConcat, LinkedArrayListInsert, LinkedArrayListRemove, WriteData, } from './database.js';
|
|
3
|
+
export class WriteLinkedArrayList extends ReadLinkedArrayList {
|
|
4
|
+
constructor(cursor) {
|
|
5
|
+
super();
|
|
6
|
+
this.cursor = cursor.writePath([new LinkedArrayListInit()]);
|
|
7
|
+
}
|
|
8
|
+
iterator() {
|
|
9
|
+
return this.cursor.iterator();
|
|
10
|
+
}
|
|
11
|
+
*[Symbol.iterator]() {
|
|
12
|
+
yield* this.cursor;
|
|
13
|
+
}
|
|
14
|
+
put(index, data) {
|
|
15
|
+
this.cursor.writePath([
|
|
16
|
+
new LinkedArrayListGet(index),
|
|
17
|
+
new WriteData(data),
|
|
18
|
+
]);
|
|
19
|
+
}
|
|
20
|
+
putCursor(index) {
|
|
21
|
+
return this.cursor.writePath([new LinkedArrayListGet(index)]);
|
|
22
|
+
}
|
|
23
|
+
append(data) {
|
|
24
|
+
this.cursor.writePath([
|
|
25
|
+
new LinkedArrayListAppend(),
|
|
26
|
+
new WriteData(data),
|
|
27
|
+
]);
|
|
28
|
+
}
|
|
29
|
+
appendCursor() {
|
|
30
|
+
return this.cursor.writePath([new LinkedArrayListAppend()]);
|
|
31
|
+
}
|
|
32
|
+
slice(offset, size) {
|
|
33
|
+
this.cursor.writePath([
|
|
34
|
+
new LinkedArrayListSlice(offset, size),
|
|
35
|
+
]);
|
|
36
|
+
}
|
|
37
|
+
concat(list) {
|
|
38
|
+
this.cursor.writePath([new LinkedArrayListConcat(list)]);
|
|
39
|
+
}
|
|
40
|
+
insert(index, data) {
|
|
41
|
+
this.cursor.writePath([
|
|
42
|
+
new LinkedArrayListInsert(index),
|
|
43
|
+
new WriteData(data),
|
|
44
|
+
]);
|
|
45
|
+
}
|
|
46
|
+
insertCursor(index) {
|
|
47
|
+
return this.cursor.writePath([new LinkedArrayListInsert(index)]);
|
|
48
|
+
}
|
|
49
|
+
remove(index) {
|
|
50
|
+
this.cursor.writePath([new LinkedArrayListRemove(index)]);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ReadSortedMap } from './read-sorted-map.js';
|
|
2
|
+
import { WriteCursor, WriteCursorIterator } from './write-cursor.js';
|
|
3
|
+
import type { WriteableData } from './writeable-data.js';
|
|
4
|
+
import { Bytes } from './writeable-data.js';
|
|
5
|
+
export declare class WriteSortedMap extends ReadSortedMap {
|
|
6
|
+
constructor(cursor: WriteCursor);
|
|
7
|
+
iterator(): WriteCursorIterator;
|
|
8
|
+
[Symbol.iterator](): Iterator<WriteCursor>;
|
|
9
|
+
put(key: string | Bytes | Uint8Array, data: WriteableData): void;
|
|
10
|
+
putCursor(key: string | Bytes | Uint8Array): WriteCursor;
|
|
11
|
+
remove(key: string | Bytes | Uint8Array): boolean;
|
|
12
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ReadSortedMap } from './read-sorted-map.js';
|
|
2
|
+
import { SortedMapInit, SortedMapGet, SortedMapGetValue, SortedMapRemove, WriteData, } from './database.js';
|
|
3
|
+
import { KeyNotFoundException } from './exceptions.js';
|
|
4
|
+
export class WriteSortedMap extends ReadSortedMap {
|
|
5
|
+
constructor(cursor) {
|
|
6
|
+
super();
|
|
7
|
+
this.cursor = cursor.writePath([new SortedMapInit(false)]);
|
|
8
|
+
}
|
|
9
|
+
iterator() {
|
|
10
|
+
return this.cursor.iterator();
|
|
11
|
+
}
|
|
12
|
+
*[Symbol.iterator]() {
|
|
13
|
+
yield* this.cursor;
|
|
14
|
+
}
|
|
15
|
+
put(key, data) {
|
|
16
|
+
this.cursor.writePath([
|
|
17
|
+
new SortedMapGet(new SortedMapGetValue(this.resolveKey(key))),
|
|
18
|
+
new WriteData(data),
|
|
19
|
+
]);
|
|
20
|
+
}
|
|
21
|
+
putCursor(key) {
|
|
22
|
+
return this.cursor.writePath([
|
|
23
|
+
new SortedMapGet(new SortedMapGetValue(this.resolveKey(key))),
|
|
24
|
+
]);
|
|
25
|
+
}
|
|
26
|
+
remove(key) {
|
|
27
|
+
try {
|
|
28
|
+
this.cursor.writePath([new SortedMapRemove(this.resolveKey(key))]);
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
if (e instanceof KeyNotFoundException)
|
|
32
|
+
return false;
|
|
33
|
+
throw e;
|
|
34
|
+
}
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ReadSortedSet } from './read-sorted-set.js';
|
|
2
|
+
import { WriteCursor, WriteCursorIterator } from './write-cursor.js';
|
|
3
|
+
import { Bytes } from './writeable-data.js';
|
|
4
|
+
export declare class WriteSortedSet extends ReadSortedSet {
|
|
5
|
+
constructor(cursor: WriteCursor);
|
|
6
|
+
iterator(): WriteCursorIterator;
|
|
7
|
+
[Symbol.iterator](): Iterator<WriteCursor>;
|
|
8
|
+
put(key: string | Bytes | Uint8Array): void;
|
|
9
|
+
remove(key: string | Bytes | Uint8Array): boolean;
|
|
10
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ReadSortedSet } from './read-sorted-set.js';
|
|
2
|
+
import { SortedMapInit, SortedMapGet, SortedMapGetKey, SortedMapRemove } from './database.js';
|
|
3
|
+
import { KeyNotFoundException } from './exceptions.js';
|
|
4
|
+
export class WriteSortedSet extends ReadSortedSet {
|
|
5
|
+
constructor(cursor) {
|
|
6
|
+
super();
|
|
7
|
+
this.cursor = cursor.writePath([new SortedMapInit(true)]);
|
|
8
|
+
}
|
|
9
|
+
iterator() {
|
|
10
|
+
return this.cursor.iterator();
|
|
11
|
+
}
|
|
12
|
+
*[Symbol.iterator]() {
|
|
13
|
+
yield* this.cursor;
|
|
14
|
+
}
|
|
15
|
+
put(key) {
|
|
16
|
+
this.cursor.writePath([new SortedMapGet(new SortedMapGetKey(this.resolveKey(key)))]);
|
|
17
|
+
}
|
|
18
|
+
remove(key) {
|
|
19
|
+
try {
|
|
20
|
+
this.cursor.writePath([new SortedMapRemove(this.resolveKey(key))]);
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
if (e instanceof KeyNotFoundException)
|
|
24
|
+
return false;
|
|
25
|
+
throw e;
|
|
26
|
+
}
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { InvalidFormatTagSizeException, Uint64OverflowException, Int64OverflowException } from './exceptions.js';
|
|
2
|
+
const UINT64_MAX = 2n ** 64n - 1n;
|
|
3
|
+
const INT64_MIN = -(2n ** 63n);
|
|
4
|
+
const INT64_MAX = 2n ** 63n - 1n;
|
|
5
|
+
export class Uint {
|
|
6
|
+
value;
|
|
7
|
+
constructor(value) {
|
|
8
|
+
const bigintValue = BigInt(value);
|
|
9
|
+
if (bigintValue < 0n || bigintValue > UINT64_MAX) {
|
|
10
|
+
throw new Uint64OverflowException();
|
|
11
|
+
}
|
|
12
|
+
this.value = bigintValue;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export class Int {
|
|
16
|
+
value;
|
|
17
|
+
constructor(value) {
|
|
18
|
+
const bigintValue = BigInt(value);
|
|
19
|
+
if (bigintValue < INT64_MIN || bigintValue > INT64_MAX) {
|
|
20
|
+
throw new Int64OverflowException();
|
|
21
|
+
}
|
|
22
|
+
this.value = bigintValue;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export class Float {
|
|
26
|
+
value;
|
|
27
|
+
constructor(value) {
|
|
28
|
+
this.value = value;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export class Bytes {
|
|
32
|
+
value;
|
|
33
|
+
formatTag;
|
|
34
|
+
constructor(value, formatTag) {
|
|
35
|
+
if (typeof value === 'string') {
|
|
36
|
+
this.value = new TextEncoder().encode(value);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
this.value = value;
|
|
40
|
+
}
|
|
41
|
+
if (formatTag === undefined || formatTag === null) {
|
|
42
|
+
this.formatTag = null;
|
|
43
|
+
}
|
|
44
|
+
else if (typeof formatTag === 'string') {
|
|
45
|
+
const encoded = new TextEncoder().encode(formatTag);
|
|
46
|
+
if (encoded.length !== 2) {
|
|
47
|
+
throw new InvalidFormatTagSizeException();
|
|
48
|
+
}
|
|
49
|
+
this.formatTag = encoded;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
if (formatTag.length !== 2) {
|
|
53
|
+
throw new InvalidFormatTagSizeException();
|
|
54
|
+
}
|
|
55
|
+
this.formatTag = formatTag;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
isShort() {
|
|
59
|
+
const totalSize = this.formatTag !== null ? 6 : 8;
|
|
60
|
+
if (this.value.length > totalSize)
|
|
61
|
+
return false;
|
|
62
|
+
for (const b of this.value) {
|
|
63
|
+
if (b === 0)
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xitdb",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "An immutable database",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -21,14 +21,14 @@
|
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
23
|
"scripts": {
|
|
24
|
-
"build": "
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"test": "bun test",
|
|
24
|
+
"build": "tsc",
|
|
25
|
+
"prepublishOnly": "npm run build",
|
|
26
|
+
"test": "tsx --test tests/*.test.ts",
|
|
28
27
|
"typecheck": "tsc --noEmit"
|
|
29
28
|
},
|
|
30
29
|
"devDependencies": {
|
|
31
|
-
"@types/
|
|
30
|
+
"@types/node": "^22.0.0",
|
|
31
|
+
"tsx": "^4.19.0",
|
|
32
32
|
"typescript": "^5.3.0"
|
|
33
33
|
}
|
|
34
34
|
}
|