sliftutils 1.7.3 → 1.7.5
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/index.d.ts
CHANGED
|
@@ -1717,6 +1717,7 @@ declare module "sliftutils/storage/DiskCollection" {
|
|
|
1717
1717
|
writeDelay?: number | undefined;
|
|
1718
1718
|
cbor?: boolean | undefined;
|
|
1719
1719
|
noPrompt?: boolean | undefined;
|
|
1720
|
+
resyncFromDisk?: boolean | undefined;
|
|
1720
1721
|
freeze?: "deep" | "shallow" | undefined;
|
|
1721
1722
|
beforeWrite?: ((update: {
|
|
1722
1723
|
newValue: T;
|
|
@@ -2177,12 +2178,13 @@ declare module "sliftutils/storage/TransactionStorage" {
|
|
|
2177
2178
|
private rawStorage;
|
|
2178
2179
|
private debugName;
|
|
2179
2180
|
private writeDelay;
|
|
2181
|
+
private resyncFromDisk;
|
|
2180
2182
|
cache: Map<string, TransactionEntry>;
|
|
2181
2183
|
private diskFiles;
|
|
2182
2184
|
private currentChunk;
|
|
2183
2185
|
private entryCount;
|
|
2184
2186
|
private static allStorage;
|
|
2185
|
-
constructor(rawStorage: IStorageRaw, debugName: string, writeDelay?: number);
|
|
2187
|
+
constructor(rawStorage: IStorageRaw, debugName: string, writeDelay?: number, resyncFromDisk?: boolean);
|
|
2186
2188
|
static compressAll(): Promise<void>;
|
|
2187
2189
|
private resyncCallbacks;
|
|
2188
2190
|
watchResync(callback: () => void): void;
|
package/package.json
CHANGED
|
@@ -150,7 +150,11 @@ export class BlockCache {
|
|
|
150
150
|
let lastMeta = index.blocks[runEnd - 1];
|
|
151
151
|
let compEnd = index.offsets[runEnd - 1] + lastMeta.len;
|
|
152
152
|
let runCompressed = rawGetRange(compStart, compEnd);
|
|
153
|
-
//
|
|
153
|
+
// Hold this run's promises locally. Registering them in the LRU can evict earlier
|
|
154
|
+
// blocks of the same run (or of a concurrent run) before we get to await them, so
|
|
155
|
+
// reading them back out of the cache would silently drop blocks and return a short
|
|
156
|
+
// buffer. We still register in the LRU so concurrent reads dedupe onto them.
|
|
157
|
+
let runPromises: Promise<Buffer>[] = [];
|
|
154
158
|
for (let i = runStart; i < runEnd; i++) {
|
|
155
159
|
let meta = index.blocks[i];
|
|
156
160
|
let relOffset = index.offsets[i] - compStart;
|
|
@@ -159,17 +163,20 @@ export class BlockCache {
|
|
|
159
163
|
if (meta.c) return LZ4.decompress(stored);
|
|
160
164
|
return stored;
|
|
161
165
|
});
|
|
166
|
+
runPromises.push(blockPromise);
|
|
162
167
|
this.touch(`${fileId}:${i}`, blockPromise);
|
|
163
168
|
}
|
|
164
|
-
for (let
|
|
165
|
-
let promise = this.blocks.get(`${fileId}:${i}`);
|
|
166
|
-
if (promise) parts.push(await promise);
|
|
167
|
-
}
|
|
169
|
+
for (let promise of runPromises) parts.push(await promise);
|
|
168
170
|
block = runEnd;
|
|
169
171
|
}
|
|
170
172
|
|
|
171
173
|
let combined = parts.length === 1 && parts[0] || Buffer.concat(parts);
|
|
172
174
|
let sliceStart = start - firstBlock * blockSize;
|
|
175
|
+
// A short combined buffer means we lost blocks; subarray would clamp and hand back
|
|
176
|
+
// truncated data that every caller treats as valid. Fail loudly instead.
|
|
177
|
+
if (combined.length < sliceStart + (end - start)) {
|
|
178
|
+
throw new Error(`Expected ${sliceStart + (end - start)} bytes of blocks for range [${start}, ${end}) of ${fileId}, was ${combined.length}`);
|
|
179
|
+
}
|
|
173
180
|
return combined.subarray(sliceStart, sliceStart + (end - start));
|
|
174
181
|
};
|
|
175
182
|
}
|
|
@@ -12,6 +12,7 @@ export declare class DiskCollection<T> implements IStorageSync<T> {
|
|
|
12
12
|
writeDelay?: number | undefined;
|
|
13
13
|
cbor?: boolean | undefined;
|
|
14
14
|
noPrompt?: boolean | undefined;
|
|
15
|
+
resyncFromDisk?: boolean | undefined;
|
|
15
16
|
freeze?: "deep" | "shallow" | undefined;
|
|
16
17
|
beforeWrite?: ((update: {
|
|
17
18
|
newValue: T;
|
|
@@ -34,6 +34,8 @@ export class DiskCollection<T> implements IStorageSync<T> {
|
|
|
34
34
|
writeDelay?: number;
|
|
35
35
|
cbor?: boolean;
|
|
36
36
|
noPrompt?: boolean;
|
|
37
|
+
// Poll disk and reload when another process/tab writes to this collection. Off by default.
|
|
38
|
+
resyncFromDisk?: boolean;
|
|
37
39
|
freeze?: "shallow" | "deep";
|
|
38
40
|
// May mutate newValue in order to change what will be written
|
|
39
41
|
beforeWrite?: (update: { newValue: T; key: string; collection: DiskCollection<T> }) => void;
|
|
@@ -52,7 +54,7 @@ export class DiskCollection<T> implements IStorageSync<T> {
|
|
|
52
54
|
let collections = await fileStorage.folder.getStorage("collections");
|
|
53
55
|
curCollection = await collections.folder.getStorage(this.collectionName);
|
|
54
56
|
}
|
|
55
|
-
let baseStorage = new TransactionStorage(curCollection, this.collectionName, this.config?.writeDelay);
|
|
57
|
+
let baseStorage = new TransactionStorage(curCollection, this.collectionName, this.config?.writeDelay, this.config?.resyncFromDisk);
|
|
56
58
|
this.transactionStorage = baseStorage;
|
|
57
59
|
return this.config?.cbor ? new CBORStorage<T>(baseStorage) : new JSONStorage<T>(baseStorage);
|
|
58
60
|
}
|
|
@@ -11,12 +11,13 @@ export declare class TransactionStorage implements IStorage<Buffer> {
|
|
|
11
11
|
private rawStorage;
|
|
12
12
|
private debugName;
|
|
13
13
|
private writeDelay;
|
|
14
|
+
private resyncFromDisk;
|
|
14
15
|
cache: Map<string, TransactionEntry>;
|
|
15
16
|
private diskFiles;
|
|
16
17
|
private currentChunk;
|
|
17
18
|
private entryCount;
|
|
18
19
|
private static allStorage;
|
|
19
|
-
constructor(rawStorage: IStorageRaw, debugName: string, writeDelay?: number);
|
|
20
|
+
constructor(rawStorage: IStorageRaw, debugName: string, writeDelay?: number, resyncFromDisk?: boolean);
|
|
20
21
|
static compressAll(): Promise<void>;
|
|
21
22
|
private resyncCallbacks;
|
|
22
23
|
watchResync(callback: () => void): void;
|
|
@@ -92,7 +92,11 @@ export class TransactionStorage implements IStorage<Buffer> {
|
|
|
92
92
|
constructor(
|
|
93
93
|
private rawStorage: IStorageRaw,
|
|
94
94
|
private debugName: string,
|
|
95
|
-
private writeDelay = WRITE_DELAY
|
|
95
|
+
private writeDelay = WRITE_DELAY,
|
|
96
|
+
// When set, we poll the underlying storage and reload if another process/tab writes new
|
|
97
|
+
// chunk files. Off by default, as it costs a periodic disk scan and most collections are
|
|
98
|
+
// only written by a single process.
|
|
99
|
+
private resyncFromDisk = false
|
|
96
100
|
) {
|
|
97
101
|
TransactionStorage.allStorage.push(this);
|
|
98
102
|
// VERY useful for debugging.
|
|
@@ -282,7 +286,7 @@ export class TransactionStorage implements IStorage<Buffer> {
|
|
|
282
286
|
private async loadAllTransactions(initialLoad?: boolean): Promise<string[]> {
|
|
283
287
|
if (isInBuild()) return [];
|
|
284
288
|
|
|
285
|
-
if (initialLoad) {
|
|
289
|
+
if (initialLoad && this.resyncFromDisk) {
|
|
286
290
|
runInfinitePoll(DISK_CHECK_INTERVAL, () => this.checkDisk());
|
|
287
291
|
}
|
|
288
292
|
|