@peerbit/shared-log 10.0.6 → 10.1.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/dist/benchmark/get-samples.js +1 -1
- package/dist/benchmark/get-samples.js.map +1 -1
- package/dist/benchmark/utils.js +1 -1
- package/dist/benchmark/utils.js.map +1 -1
- package/dist/src/index.d.ts +15 -10
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +138 -65
- package/dist/src/index.js.map +1 -1
- package/dist/src/ranges.d.ts +95 -11
- package/dist/src/ranges.d.ts.map +1 -1
- package/dist/src/ranges.js +437 -83
- package/dist/src/ranges.js.map +1 -1
- package/dist/src/replication-domain-hash.d.ts +2 -2
- package/dist/src/replication-domain-hash.d.ts.map +1 -1
- package/dist/src/replication-domain-hash.js +2 -17
- package/dist/src/replication-domain-hash.js.map +1 -1
- package/dist/src/replication-domain-time.d.ts +7 -2
- package/dist/src/replication-domain-time.d.ts.map +1 -1
- package/dist/src/replication-domain-time.js +7 -12
- package/dist/src/replication-domain-time.js.map +1 -1
- package/dist/src/replication-domain.d.ts +3 -20
- package/dist/src/replication-domain.d.ts.map +1 -1
- package/dist/src/replication-domain.js +0 -33
- package/dist/src/replication-domain.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +205 -107
- package/src/ranges.ts +669 -127
- package/src/replication-domain-hash.ts +16 -29
- package/src/replication-domain-time.ts +46 -40
- package/src/replication-domain.ts +7 -59
|
@@ -6,25 +6,10 @@ import { type EntryReplicated } from "./ranges.js";
|
|
|
6
6
|
import {
|
|
7
7
|
type Log,
|
|
8
8
|
type ReplicationDomain,
|
|
9
|
+
type ReplicationDomainConstructor,
|
|
9
10
|
type ReplicationDomainMapper,
|
|
10
11
|
} from "./replication-domain.js";
|
|
11
12
|
|
|
12
|
-
/* const hashToU32 = (hash: Uint8Array) => {
|
|
13
|
-
const seedNumber = new BinaryReader(
|
|
14
|
-
hash.subarray(hash.length - 4, hash.length),
|
|
15
|
-
).u32();
|
|
16
|
-
return seedNumber;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const hashToU64 = (hash: Uint8Array): bigint => {
|
|
21
|
-
const seedNumber = new BinaryReader(
|
|
22
|
-
hash.subarray(hash.length - 4, hash.length), //
|
|
23
|
-
).u64();
|
|
24
|
-
return seedNumber;
|
|
25
|
-
};
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
13
|
const hashTransformer = <R extends "u32" | "u64">(
|
|
29
14
|
resolution: R,
|
|
30
15
|
): ReplicationDomainMapper<any, R> => {
|
|
@@ -54,17 +39,19 @@ export type ReplicationDomainHash<R extends "u32" | "u64"> = ReplicationDomain<
|
|
|
54
39
|
R
|
|
55
40
|
>;
|
|
56
41
|
|
|
57
|
-
export const createReplicationDomainHash =
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
42
|
+
export const createReplicationDomainHash =
|
|
43
|
+
<R extends "u32" | "u64">(
|
|
44
|
+
resolution: R,
|
|
45
|
+
): ReplicationDomainConstructor<ReplicationDomainHash<R>> =>
|
|
46
|
+
(log: Log) => {
|
|
47
|
+
return {
|
|
48
|
+
resolution,
|
|
49
|
+
type: "hash",
|
|
50
|
+
fromEntry: hashTransformer<R>(resolution),
|
|
51
|
+
fromArgs: async (args: undefined) => {
|
|
52
|
+
return {
|
|
53
|
+
offset: log.node.identity.publicKey,
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
};
|
|
69
57
|
};
|
|
70
|
-
};
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import type { ShallowOrFullEntry } from "@peerbit/log";
|
|
2
|
-
import { type EntryReplicated } from "./ranges.js";
|
|
3
2
|
import {
|
|
3
|
+
type EntryReplicated,
|
|
4
|
+
type ReplicationRangeIndexable,
|
|
5
|
+
} from "./ranges.js";
|
|
6
|
+
import {
|
|
7
|
+
type Log,
|
|
4
8
|
type ReplicationDomain,
|
|
9
|
+
type ReplicationDomainConstructor,
|
|
5
10
|
type ReplicationDomainMapper,
|
|
6
11
|
} from "./replication-domain.js";
|
|
7
12
|
|
|
@@ -41,46 +46,47 @@ export type ReplicationDomainTime = ReplicationDomain<TimeRange, any, "u32"> & {
|
|
|
41
46
|
fromDuration: (duration: number) => number;
|
|
42
47
|
};
|
|
43
48
|
|
|
44
|
-
export const createReplicationDomainTime =
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
49
|
+
export const createReplicationDomainTime =
|
|
50
|
+
(properties: {
|
|
51
|
+
origin?: Date;
|
|
52
|
+
unit?: TimeUnit;
|
|
53
|
+
canMerge?: (
|
|
54
|
+
from: ReplicationRangeIndexable<"u32">,
|
|
55
|
+
into: ReplicationRangeIndexable<"u32">,
|
|
56
|
+
) => boolean;
|
|
57
|
+
}): ReplicationDomainConstructor<ReplicationDomainTime> =>
|
|
58
|
+
(log: Log) => {
|
|
59
|
+
const origin = properties.origin || new Date();
|
|
60
|
+
const unit = properties.unit || "milliseconds";
|
|
61
|
+
const originScaled = +origin * scalarMilliToUnit[unit];
|
|
62
|
+
const fromMilliToUnit = scalarMilliToUnit[unit];
|
|
63
|
+
const fromTime = (time: number | Date): number => {
|
|
64
|
+
return (
|
|
65
|
+
(typeof time === "number" ? time : +time * fromMilliToUnit) -
|
|
66
|
+
originScaled
|
|
67
|
+
);
|
|
68
|
+
};
|
|
55
69
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
70
|
+
const fromDuration = (duration: number): number => {
|
|
71
|
+
return duration;
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
resolution: "u32",
|
|
75
|
+
type: "time",
|
|
76
|
+
fromTime,
|
|
77
|
+
fromDuration,
|
|
78
|
+
fromEntry: fromEntry(origin, unit),
|
|
79
|
+
fromArgs: async (args: TimeRange | undefined) => {
|
|
80
|
+
if (!args) {
|
|
81
|
+
return {
|
|
82
|
+
offset: log.node.identity.publicKey,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
67
85
|
return {
|
|
68
|
-
offset:
|
|
86
|
+
offset: fromTime(args.from),
|
|
87
|
+
length: fromDuration(args.to - args.from),
|
|
69
88
|
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
length: fromDuration(args.to - args.from),
|
|
74
|
-
};
|
|
75
|
-
/* roleAge = roleAge ?? (await log.getDefaultMinRoleAge());
|
|
76
|
-
const ranges = await getCoverSet(
|
|
77
|
-
log.replicationIndex,
|
|
78
|
-
roleAge,
|
|
79
|
-
fromTime(args.from),
|
|
80
|
-
fromDuration(args.to - args.from),
|
|
81
|
-
undefined,
|
|
82
|
-
);
|
|
83
|
-
return [...ranges]; */
|
|
84
|
-
},
|
|
89
|
+
},
|
|
90
|
+
canMerge: properties.canMerge,
|
|
91
|
+
};
|
|
85
92
|
};
|
|
86
|
-
};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { PublicSignKey } from "@peerbit/crypto";
|
|
2
2
|
import { type Index } from "@peerbit/indexer-interface";
|
|
3
3
|
import type { Entry, ShallowEntry } from "@peerbit/log";
|
|
4
|
-
import { debounceAccumulator } from "./debounce.js";
|
|
5
4
|
import type { ReplicationRangeIndexable } from "./index.js";
|
|
6
5
|
import type { NumberFromType } from "./integers.js";
|
|
7
6
|
import type { EntryReplicated } from "./ranges.js";
|
|
@@ -31,63 +30,6 @@ type CoverRange<T extends number | bigint> = {
|
|
|
31
30
|
offset: T | PublicSignKey;
|
|
32
31
|
length?: T;
|
|
33
32
|
};
|
|
34
|
-
export type ReplicationChanges = ReplicationChange[];
|
|
35
|
-
export type ReplicationChange =
|
|
36
|
-
| {
|
|
37
|
-
type: "added";
|
|
38
|
-
range: ReplicationRangeIndexable<any>;
|
|
39
|
-
}
|
|
40
|
-
| {
|
|
41
|
-
type: "removed";
|
|
42
|
-
range: ReplicationRangeIndexable<any>;
|
|
43
|
-
}
|
|
44
|
-
| {
|
|
45
|
-
type: "updated";
|
|
46
|
-
range: ReplicationRangeIndexable<any>;
|
|
47
|
-
prev: ReplicationRangeIndexable<any>;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export const mergeReplicationChanges = (
|
|
51
|
-
changes: ReplicationChanges | ReplicationChanges[],
|
|
52
|
-
): ReplicationChanges => {
|
|
53
|
-
let first = changes[0];
|
|
54
|
-
if (!Array.isArray(first)) {
|
|
55
|
-
return changes as ReplicationChanges;
|
|
56
|
-
}
|
|
57
|
-
return (changes as ReplicationChanges[]).flat();
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
export const debounceAggregationChanges = (
|
|
61
|
-
fn: (changeOrChanges: ReplicationChange[]) => void,
|
|
62
|
-
delay: number,
|
|
63
|
-
) => {
|
|
64
|
-
return debounceAccumulator(
|
|
65
|
-
(result) => {
|
|
66
|
-
return fn([...result.values()]);
|
|
67
|
-
},
|
|
68
|
-
() => {
|
|
69
|
-
let aggregated: Map<string, ReplicationChange> = new Map();
|
|
70
|
-
return {
|
|
71
|
-
add: (change: ReplicationChange) => {
|
|
72
|
-
const prev = aggregated.get(change.range.idString);
|
|
73
|
-
if (prev) {
|
|
74
|
-
if (prev.range.timestamp < change.range.timestamp) {
|
|
75
|
-
aggregated.set(change.range.idString, change);
|
|
76
|
-
}
|
|
77
|
-
} else {
|
|
78
|
-
aggregated.set(change.range.idString, change);
|
|
79
|
-
}
|
|
80
|
-
},
|
|
81
|
-
delete: (key: string) => {
|
|
82
|
-
aggregated.delete(key);
|
|
83
|
-
},
|
|
84
|
-
size: () => aggregated.size,
|
|
85
|
-
value: aggregated,
|
|
86
|
-
};
|
|
87
|
-
},
|
|
88
|
-
delay,
|
|
89
|
-
);
|
|
90
|
-
};
|
|
91
33
|
|
|
92
34
|
export type ReplicationDomain<Args, T, R extends "u32" | "u64"> = {
|
|
93
35
|
resolution: R;
|
|
@@ -95,9 +37,15 @@ export type ReplicationDomain<Args, T, R extends "u32" | "u64"> = {
|
|
|
95
37
|
fromEntry: ReplicationDomainMapper<T, R>;
|
|
96
38
|
fromArgs: (
|
|
97
39
|
args: Args | undefined,
|
|
98
|
-
log: Log,
|
|
99
40
|
) => Promise<CoverRange<NumberFromType<R>>> | CoverRange<NumberFromType<R>>;
|
|
41
|
+
canMerge?: (
|
|
42
|
+
from: ReplicationRangeIndexable<R>,
|
|
43
|
+
into: ReplicationRangeIndexable<R>,
|
|
44
|
+
) => boolean;
|
|
100
45
|
};
|
|
101
46
|
|
|
47
|
+
export type ReplicationDomainConstructor<
|
|
48
|
+
D extends ReplicationDomain<any, any, any>,
|
|
49
|
+
> = (log: Log) => D;
|
|
102
50
|
export type ExtractDomainArgs<T> =
|
|
103
51
|
T extends ReplicationDomain<infer Args, any, any> ? Args : never;
|