@peerbit/log 4.0.4 → 4.0.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/dist/src/clock.d.ts.map +1 -1
- package/dist/src/clock.js +20 -11
- package/dist/src/clock.js.map +1 -1
- package/dist/src/entry-create.d.ts +26 -0
- package/dist/src/entry-create.d.ts.map +1 -0
- package/dist/src/entry-create.js +5 -0
- package/dist/src/entry-create.js.map +1 -0
- package/dist/src/entry-index.d.ts +2 -1
- package/dist/src/entry-index.d.ts.map +1 -1
- package/dist/src/entry-index.js +2 -1
- package/dist/src/entry-index.js.map +1 -1
- package/dist/src/entry-shallow.d.ts +29 -0
- package/dist/src/entry-shallow.d.ts.map +1 -0
- package/dist/src/entry-shallow.js +75 -0
- package/dist/src/entry-shallow.js.map +1 -0
- package/dist/src/entry-type.d.ts +5 -0
- package/dist/src/entry-type.d.ts.map +1 -0
- package/dist/src/entry-type.js +6 -0
- package/dist/src/entry-type.js.map +1 -0
- package/dist/src/entry-v0.d.ts +121 -0
- package/dist/src/entry-v0.d.ts.map +1 -0
- package/dist/src/entry-v0.js +465 -0
- package/dist/src/entry-v0.js.map +1 -0
- package/dist/src/entry.d.ts +31 -153
- package/dist/src/entry.d.ts.map +1 -1
- package/dist/src/entry.js +24 -581
- package/dist/src/entry.js.map +1 -1
- package/dist/src/heads-cache.d.ts +1 -1
- package/dist/src/heads-cache.d.ts.map +1 -1
- package/dist/src/heads-cache.js +0 -1
- package/dist/src/heads-cache.js.map +1 -1
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +5 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/log.d.ts +6 -4
- package/dist/src/log.d.ts.map +1 -1
- package/dist/src/log.js +22 -82
- package/dist/src/log.js.map +1 -1
- package/dist/src/payload.d.ts +17 -0
- package/dist/src/payload.d.ts.map +1 -0
- package/dist/src/payload.js +53 -0
- package/dist/src/payload.js.map +1 -0
- package/dist/src/trim.d.ts +2 -1
- package/dist/src/trim.d.ts.map +1 -1
- package/dist/src/trim.js.map +1 -1
- package/package.json +3 -3
- package/src/clock.ts +3 -2
- package/src/entry-create.ts +30 -0
- package/src/entry-index.ts +3 -6
- package/src/entry-shallow.ts +61 -0
- package/src/entry-type.ts +4 -0
- package/src/entry-v0.ts +594 -0
- package/src/entry.ts +50 -693
- package/src/heads-cache.ts +1 -1
- package/src/index.ts +5 -0
- package/src/log.ts +23 -94
- package/src/payload.ts +44 -0
- package/src/trim.ts +2 -1
package/src/heads-cache.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { logger as loggerFn } from "@peerbit/logger";
|
|
|
11
11
|
import PQueue from "p-queue";
|
|
12
12
|
import path from "path-browserify";
|
|
13
13
|
import { v4 as uuid } from "uuid";
|
|
14
|
-
import { Entry } from "./entry
|
|
14
|
+
import type { Entry } from "./entry";
|
|
15
15
|
|
|
16
16
|
export const logger = loggerFn({ module: "heads-cache" });
|
|
17
17
|
export class CachedValue {}
|
package/src/index.ts
CHANGED
|
@@ -3,9 +3,14 @@ export * from "./log-sorting.js";
|
|
|
3
3
|
export * from "./log-errors.js";
|
|
4
4
|
export * from "./snapshot.js";
|
|
5
5
|
export * from "./entry.js";
|
|
6
|
+
export * from "./entry-type.js";
|
|
7
|
+
export * from "./entry-with-refs.js";
|
|
8
|
+
export * from "./entry-shallow.js";
|
|
6
9
|
export * from "./utils.js";
|
|
7
10
|
export * from "./clock.js";
|
|
8
11
|
export * from "./encoding.js";
|
|
9
12
|
export * from "./trim.js";
|
|
10
13
|
export * from "./change.js";
|
|
14
|
+
export * from "./entry-v0.js";
|
|
15
|
+
export * from "./entry-create.js";
|
|
11
16
|
export type { TrimToByteLengthOption, TrimToLengthOption } from "./trim.js";
|
package/src/log.ts
CHANGED
|
@@ -25,19 +25,15 @@ import {
|
|
|
25
25
|
type ResultsIterator,
|
|
26
26
|
type ReturnTypeFromResolveOptions,
|
|
27
27
|
} from "./entry-index.js";
|
|
28
|
+
import { ShallowEntry } from "./entry-shallow.js";
|
|
29
|
+
import { EntryType } from "./entry-type.js";
|
|
30
|
+
import { type EncryptionTemplateMaybeEncrypted, EntryV0 } from "./entry-v0.js";
|
|
28
31
|
import { type EntryWithRefs } from "./entry-with-refs.js";
|
|
29
|
-
import {
|
|
30
|
-
type CanAppend,
|
|
31
|
-
type EncryptionTemplateMaybeEncrypted,
|
|
32
|
-
Entry,
|
|
33
|
-
EntryType,
|
|
34
|
-
Payload,
|
|
35
|
-
ShallowEntry,
|
|
36
|
-
type ShallowOrFullEntry,
|
|
37
|
-
} from "./entry.js";
|
|
32
|
+
import { type CanAppend, Entry, type ShallowOrFullEntry } from "./entry.js";
|
|
38
33
|
import { findUniques } from "./find-uniques.js";
|
|
39
34
|
import * as LogError from "./log-errors.js";
|
|
40
35
|
import * as Sorting from "./log-sorting.js";
|
|
36
|
+
import type { Payload } from "./payload.js";
|
|
41
37
|
import { Trim, type TrimOptions } from "./trim.js";
|
|
42
38
|
|
|
43
39
|
const { LastWriteWins } = Sorting;
|
|
@@ -370,78 +366,10 @@ export class Log<T> {
|
|
|
370
366
|
* Get a entry with shallow representation
|
|
371
367
|
* @param {string} [hash] The hashes of the entry
|
|
372
368
|
*/
|
|
373
|
-
async getShallow(
|
|
374
|
-
hash: string,
|
|
375
|
-
options?: { timeout?: number },
|
|
376
|
-
): Promise<ShallowEntry | undefined> {
|
|
369
|
+
async getShallow(hash: string): Promise<ShallowEntry | undefined> {
|
|
377
370
|
return (await this._entryIndex.getShallow(hash))?.value;
|
|
378
371
|
}
|
|
379
372
|
|
|
380
|
-
/*
|
|
381
|
-
async traverse(
|
|
382
|
-
rootEntries: Entry<T>[],
|
|
383
|
-
amount = -1,
|
|
384
|
-
endHash?: string
|
|
385
|
-
): Promise<{ [key: string]: Entry<T> }> {
|
|
386
|
-
// Sort the given given root entries and use as the starting stack
|
|
387
|
-
let stack: Entry<T>[] = rootEntries.sort(this._sortFn).reverse();
|
|
388
|
-
|
|
389
|
-
// Cache for checking if we've processed an entry already
|
|
390
|
-
let traversed: { [key: string]: boolean } = {};
|
|
391
|
-
// End result
|
|
392
|
-
const result: { [key: string]: Entry<T> } = {};
|
|
393
|
-
let count = 0;
|
|
394
|
-
// Named function for getting an entry from the log
|
|
395
|
-
const getEntry = (e: string) => this.get(e);
|
|
396
|
-
|
|
397
|
-
// Add an entry to the stack and traversed nodes index
|
|
398
|
-
const addToStack = (entry: Entry<T>) => {
|
|
399
|
-
// If we've already processed the Entry<T>, don't add it to the stack
|
|
400
|
-
if (!entry || traversed[entry.hash]) {
|
|
401
|
-
return;
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
// Add the entry in front of the stack and sort
|
|
405
|
-
stack = [entry, ...stack].sort(this._sortFn).reverse();
|
|
406
|
-
// Add to the cache of processed entries
|
|
407
|
-
traversed[entry.hash] = true;
|
|
408
|
-
};
|
|
409
|
-
|
|
410
|
-
const addEntry = (rootEntry: Entry<T>) => {
|
|
411
|
-
result[rootEntry.hash] = rootEntry;
|
|
412
|
-
traversed[rootEntry.hash] = true;
|
|
413
|
-
count++;
|
|
414
|
-
};
|
|
415
|
-
|
|
416
|
-
// Start traversal
|
|
417
|
-
// Process stack until it's empty (traversed the full log)
|
|
418
|
-
// or when we have the requested amount of entries
|
|
419
|
-
// If requested entry amount is -1, traverse all
|
|
420
|
-
while (stack.length > 0 && (count < amount || amount < 0)) {
|
|
421
|
-
// eslint-disable-line no-unmodified-loop-condition
|
|
422
|
-
// Get the next element from the stack
|
|
423
|
-
const entry = stack.shift();
|
|
424
|
-
if (!entry) {
|
|
425
|
-
throw new Error("Unexpected");
|
|
426
|
-
}
|
|
427
|
-
// Add to the result
|
|
428
|
-
addEntry(entry);
|
|
429
|
-
// If it is the specified end hash, break out of the while loop
|
|
430
|
-
if (endHash && endHash === entry.hash) break;
|
|
431
|
-
|
|
432
|
-
// Add entry's next references to the stack
|
|
433
|
-
const entries = (await Promise.all(entry.next.map(getEntry))).filter(
|
|
434
|
-
(x) => !!x
|
|
435
|
-
) as Entry<any>[];
|
|
436
|
-
entries.forEach(addToStack);
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
stack = [];
|
|
440
|
-
traversed = {};
|
|
441
|
-
// End result
|
|
442
|
-
return result;
|
|
443
|
-
}
|
|
444
|
-
*/
|
|
445
373
|
async getReferenceSamples(
|
|
446
374
|
from: Entry<T>,
|
|
447
375
|
options?: { pointerCount?: number; memoryLimit?: number },
|
|
@@ -454,9 +382,9 @@ export class Log<T> {
|
|
|
454
382
|
return [];
|
|
455
383
|
}
|
|
456
384
|
hashes.add(from.hash);
|
|
457
|
-
let memoryCounter = from.
|
|
458
|
-
if (from.next?.length > 0 && pointerCount >= 2) {
|
|
459
|
-
let next = new Set(from.next);
|
|
385
|
+
let memoryCounter = from.payload.byteLength;
|
|
386
|
+
if (from.meta.next?.length > 0 && pointerCount >= 2) {
|
|
387
|
+
let next = new Set(from.meta.next);
|
|
460
388
|
let prev = 2;
|
|
461
389
|
outer: for (let i = 2; i <= maxDistance - 1; i *= 2) {
|
|
462
390
|
for (let j = prev; j < i; j++) {
|
|
@@ -466,13 +394,14 @@ export class Log<T> {
|
|
|
466
394
|
const nextNext = new Set<string>();
|
|
467
395
|
for (const n of next) {
|
|
468
396
|
const nentry = await this.get(n);
|
|
469
|
-
nentry
|
|
470
|
-
|
|
471
|
-
|
|
397
|
+
if (nentry) {
|
|
398
|
+
for (const n2 of nentry.meta.next) {
|
|
399
|
+
nextNext.add(n2);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
472
402
|
}
|
|
473
403
|
next = nextNext;
|
|
474
404
|
}
|
|
475
|
-
|
|
476
405
|
prev = i;
|
|
477
406
|
if (next) {
|
|
478
407
|
for (const n of next) {
|
|
@@ -483,7 +412,7 @@ export class Log<T> {
|
|
|
483
412
|
if (!entry) {
|
|
484
413
|
break outer;
|
|
485
414
|
}
|
|
486
|
-
memoryCounter += entry.
|
|
415
|
+
memoryCounter += entry.payload.byteLength;
|
|
487
416
|
if (memoryCounter > memoryLimit) {
|
|
488
417
|
break outer;
|
|
489
418
|
}
|
|
@@ -541,7 +470,7 @@ export class Log<T> {
|
|
|
541
470
|
timestamp: options?.meta?.timestamp || this._hlc.now(),
|
|
542
471
|
});
|
|
543
472
|
|
|
544
|
-
const entry = await
|
|
473
|
+
const entry = await EntryV0.create<T>({
|
|
545
474
|
store: this._storage,
|
|
546
475
|
identity: options.identity || this._identity,
|
|
547
476
|
signers: options.signers,
|
|
@@ -821,7 +750,7 @@ export class Log<T> {
|
|
|
821
750
|
}
|
|
822
751
|
|
|
823
752
|
const headsWithGid: JoinableEntry[] = await this.entryIndex
|
|
824
|
-
.getHeads(entry.gid, { type: "shape", shape: ENTRY_JOIN_SHAPE })
|
|
753
|
+
.getHeads(entry.meta.gid, { type: "shape", shape: ENTRY_JOIN_SHAPE })
|
|
825
754
|
.all();
|
|
826
755
|
if (headsWithGid) {
|
|
827
756
|
for (const v of headsWithGid) {
|
|
@@ -838,7 +767,7 @@ export class Log<T> {
|
|
|
838
767
|
}
|
|
839
768
|
|
|
840
769
|
if (entry.meta.type !== EntryType.CUT) {
|
|
841
|
-
for (const a of entry.next) {
|
|
770
|
+
for (const a of entry.meta.next) {
|
|
842
771
|
if (!(await this.has(a))) {
|
|
843
772
|
const nested =
|
|
844
773
|
options.references?.get(a) ||
|
|
@@ -1111,7 +1040,7 @@ export class Log<T> {
|
|
|
1111
1040
|
entry: Entry<any>,
|
|
1112
1041
|
) => {
|
|
1113
1042
|
const addToResult = (e: string) => (res[e] = entry.hash);
|
|
1114
|
-
entry.next.forEach(addToResult);
|
|
1043
|
+
entry.meta.next.forEach(addToResult);
|
|
1115
1044
|
return res;
|
|
1116
1045
|
};
|
|
1117
1046
|
|
|
@@ -1133,7 +1062,7 @@ export class Log<T> {
|
|
|
1133
1062
|
let nexts: string[] = [];
|
|
1134
1063
|
|
|
1135
1064
|
const addToIndex = (e: Entry<T>) => {
|
|
1136
|
-
if (e.next.length === 0) {
|
|
1065
|
+
if (e.meta.next.length === 0) {
|
|
1137
1066
|
nullIndex.push(e);
|
|
1138
1067
|
}
|
|
1139
1068
|
const addToReverseIndex = (a: any) => {
|
|
@@ -1143,9 +1072,9 @@ export class Log<T> {
|
|
|
1143
1072
|
};
|
|
1144
1073
|
|
|
1145
1074
|
// Add all entries and their parents to the reverse index
|
|
1146
|
-
e.next.forEach(addToReverseIndex);
|
|
1075
|
+
e.meta.next.forEach(addToReverseIndex);
|
|
1147
1076
|
// Get all next references
|
|
1148
|
-
nexts = nexts.concat(e.next);
|
|
1077
|
+
nexts = nexts.concat(e.meta.next);
|
|
1149
1078
|
// Get the hashes of input entries
|
|
1150
1079
|
hashes[e.hash] = true;
|
|
1151
1080
|
};
|
|
@@ -1189,7 +1118,7 @@ export class Log<T> {
|
|
|
1189
1118
|
res.splice(0, 0, e);
|
|
1190
1119
|
}
|
|
1191
1120
|
};
|
|
1192
|
-
entry.next.reverse().forEach(addToResult);
|
|
1121
|
+
entry.meta.next.reverse().forEach(addToResult);
|
|
1193
1122
|
return res;
|
|
1194
1123
|
};
|
|
1195
1124
|
|
package/src/payload.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { field, variant } from "@dao-xyz/borsh";
|
|
2
|
+
import { type Encoding, NO_ENCODING } from "./encoding.js";
|
|
3
|
+
import { equals } from "./utils.js";
|
|
4
|
+
|
|
5
|
+
@variant(0)
|
|
6
|
+
export class Payload<T> {
|
|
7
|
+
@field({ type: Uint8Array })
|
|
8
|
+
data: Uint8Array;
|
|
9
|
+
|
|
10
|
+
encoding: Encoding<T>;
|
|
11
|
+
|
|
12
|
+
private _value?: T;
|
|
13
|
+
|
|
14
|
+
constructor(props: { data: Uint8Array; value?: T; encoding: Encoding<T> }) {
|
|
15
|
+
this.data = props.data;
|
|
16
|
+
this._value = props.value;
|
|
17
|
+
this.encoding = props?.encoding;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
equals(other: Payload<T>): boolean {
|
|
21
|
+
return equals(this.data, other.data);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get isDecoded(): boolean {
|
|
25
|
+
return this._value != null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
get value(): T {
|
|
29
|
+
if (this._value == null) {
|
|
30
|
+
throw new Error("Value not decoded. Invoke: .getValue once");
|
|
31
|
+
}
|
|
32
|
+
return this._value;
|
|
33
|
+
}
|
|
34
|
+
getValue(encoding: Encoding<T> = this.encoding || NO_ENCODING): T {
|
|
35
|
+
if (this._value !== undefined) {
|
|
36
|
+
return this._value;
|
|
37
|
+
}
|
|
38
|
+
return encoding.decoder(this.data);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
get byteLength() {
|
|
42
|
+
return this.data.byteLength;
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/trim.ts
CHANGED
|
@@ -2,7 +2,8 @@ import { Cache } from "@peerbit/cache";
|
|
|
2
2
|
import { SumRequest } from "@peerbit/indexer-interface";
|
|
3
3
|
import PQueue from "p-queue";
|
|
4
4
|
import type { EntryIndex } from "./entry-index.js";
|
|
5
|
-
import type {
|
|
5
|
+
import type { ShallowEntry } from "./entry-shallow.js";
|
|
6
|
+
import type { Entry } from "./entry.js";
|
|
6
7
|
import type { SortFn } from "./log-sorting.js";
|
|
7
8
|
|
|
8
9
|
const trimOptionsEqual = (a: TrimOptions, b: TrimOptions) => {
|