@peerbit/log 3.0.34 → 4.0.0-55cebfe
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/append.d.ts +2 -0
- package/dist/benchmark/append.d.ts.map +1 -0
- package/dist/benchmark/append.js +40 -0
- package/dist/benchmark/append.js.map +1 -0
- package/dist/benchmark/memory/index.d.ts +2 -0
- package/dist/benchmark/memory/index.d.ts.map +1 -0
- package/dist/benchmark/memory/index.js +122 -0
- package/dist/benchmark/memory/index.js.map +1 -0
- package/dist/benchmark/memory/insert.d.ts +2 -0
- package/dist/benchmark/memory/insert.d.ts.map +1 -0
- package/dist/benchmark/memory/insert.js +59 -0
- package/dist/benchmark/memory/insert.js.map +1 -0
- package/dist/benchmark/memory/utils.d.ts +13 -0
- package/dist/benchmark/memory/utils.d.ts.map +1 -0
- package/dist/benchmark/memory/utils.js +2 -0
- package/dist/benchmark/memory/utils.js.map +1 -0
- package/dist/benchmark/payload.d.ts +2 -0
- package/dist/benchmark/payload.d.ts.map +1 -0
- package/{lib/esm/__benchmark__/index.js → dist/benchmark/payload.js} +20 -22
- package/dist/benchmark/payload.js.map +1 -0
- package/dist/src/change.d.ts +6 -0
- package/dist/src/change.d.ts.map +1 -0
- package/{lib/esm → dist/src}/clock.d.ts +3 -26
- package/dist/src/clock.d.ts.map +1 -0
- package/{lib/esm → dist/src}/clock.js +30 -39
- package/dist/src/clock.js.map +1 -0
- package/{lib/esm → dist/src}/difference.d.ts +1 -0
- package/dist/src/difference.d.ts.map +1 -0
- package/{lib/esm → dist/src}/encoding.d.ts +2 -1
- package/dist/src/encoding.d.ts.map +1 -0
- package/{lib/esm → dist/src}/encoding.js +2 -2
- package/{lib/esm → dist/src}/encoding.js.map +1 -1
- package/dist/src/entry-index.d.ts +78 -0
- package/dist/src/entry-index.d.ts.map +1 -0
- package/dist/src/entry-index.js +316 -0
- package/dist/src/entry-index.js.map +1 -0
- package/{lib/esm → dist/src}/entry-with-refs.d.ts +2 -1
- package/dist/src/entry-with-refs.d.ts.map +1 -0
- package/{lib/esm → dist/src}/entry.d.ts +22 -18
- package/dist/src/entry.d.ts.map +1 -0
- package/{lib/esm → dist/src}/entry.js +69 -42
- package/dist/src/entry.js.map +1 -0
- package/{lib/esm → dist/src}/find-uniques.d.ts +1 -0
- package/dist/src/find-uniques.d.ts.map +1 -0
- package/{lib/esm → dist/src}/heads-cache.d.ts +4 -3
- package/dist/src/heads-cache.d.ts.map +1 -0
- package/{lib/esm → dist/src}/heads-cache.js +9 -10
- package/dist/src/heads-cache.js.map +1 -0
- package/{lib/esm → dist/src}/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -0
- package/{lib/esm → dist/src}/log-errors.d.ts +1 -0
- package/dist/src/log-errors.d.ts.map +1 -0
- package/dist/src/log-sorting.d.ts +35 -0
- package/dist/src/log-sorting.d.ts.map +1 -0
- package/dist/src/log-sorting.js +105 -0
- package/dist/src/log-sorting.js.map +1 -0
- package/{lib/esm → dist/src}/log.d.ts +78 -56
- package/dist/src/log.d.ts.map +1 -0
- package/{lib/esm → dist/src}/log.js +355 -465
- package/dist/src/log.js.map +1 -0
- package/{lib/esm → dist/src}/logger.d.ts +1 -0
- package/dist/src/logger.d.ts.map +1 -0
- package/{lib/esm → dist/src}/logger.js.map +1 -1
- package/{lib/esm → dist/src}/snapshot.d.ts +5 -4
- package/dist/src/snapshot.d.ts.map +1 -0
- package/{lib/esm → dist/src}/snapshot.js +10 -10
- package/dist/src/snapshot.js.map +1 -0
- package/{lib/esm → dist/src}/trim.d.ts +11 -9
- package/dist/src/trim.d.ts.map +1 -0
- package/{lib/esm → dist/src}/trim.js +46 -35
- package/dist/src/trim.js.map +1 -0
- package/dist/src/utils.d.ts +4 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +12 -0
- package/dist/src/utils.js.map +1 -0
- package/package.json +104 -78
- package/src/change.ts +3 -2
- package/src/clock.ts +22 -22
- package/src/encoding.ts +4 -4
- package/src/entry-index.ts +451 -52
- package/src/entry-with-refs.ts +1 -1
- package/src/entry.ts +95 -81
- package/src/heads-cache.ts +33 -29
- package/src/log-sorting.ts +116 -94
- package/src/log.ts +482 -571
- package/src/logger.ts +1 -0
- package/src/snapshot.ts +15 -17
- package/src/trim.ts +81 -50
- package/src/utils.ts +10 -0
- package/lib/esm/__benchmark__/index.d.ts +0 -1
- package/lib/esm/__benchmark__/index.js.map +0 -1
- package/lib/esm/change.d.ts +0 -5
- package/lib/esm/clock.js.map +0 -1
- package/lib/esm/entry-index.d.ts +0 -24
- package/lib/esm/entry-index.js +0 -74
- package/lib/esm/entry-index.js.map +0 -1
- package/lib/esm/entry.js.map +0 -1
- package/lib/esm/heads-cache.js.map +0 -1
- package/lib/esm/heads.d.ts +0 -69
- package/lib/esm/heads.js +0 -157
- package/lib/esm/heads.js.map +0 -1
- package/lib/esm/log-sorting.d.ts +0 -44
- package/lib/esm/log-sorting.js +0 -86
- package/lib/esm/log-sorting.js.map +0 -1
- package/lib/esm/log.js.map +0 -1
- package/lib/esm/snapshot.js.map +0 -1
- package/lib/esm/trim.js.map +0 -1
- package/lib/esm/types.d.ts +0 -6
- package/lib/esm/types.js +0 -23
- package/lib/esm/types.js.map +0 -1
- package/lib/esm/utils.d.ts +0 -2
- package/lib/esm/utils.js +0 -3
- package/lib/esm/utils.js.map +0 -1
- package/lib/esm/values.d.ts +0 -26
- package/lib/esm/values.js +0 -131
- package/lib/esm/values.js.map +0 -1
- package/src/__benchmark__/index.ts +0 -130
- package/src/heads.ts +0 -233
- package/src/types.ts +0 -12
- package/src/values.ts +0 -174
- /package/{lib/esm → dist/src}/change.js +0 -0
- /package/{lib/esm → dist/src}/change.js.map +0 -0
- /package/{lib/esm → dist/src}/difference.js +0 -0
- /package/{lib/esm → dist/src}/difference.js.map +0 -0
- /package/{lib/esm → dist/src}/entry-with-refs.js +0 -0
- /package/{lib/esm → dist/src}/entry-with-refs.js.map +0 -0
- /package/{lib/esm → dist/src}/find-uniques.js +0 -0
- /package/{lib/esm → dist/src}/find-uniques.js.map +0 -0
- /package/{lib/esm → dist/src}/index.js +0 -0
- /package/{lib/esm → dist/src}/index.js.map +0 -0
- /package/{lib/esm → dist/src}/log-errors.js +0 -0
- /package/{lib/esm → dist/src}/log-errors.js.map +0 -0
- /package/{lib/esm → dist/src}/logger.js +0 -0
package/src/log-sorting.ts
CHANGED
|
@@ -1,112 +1,134 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
Compare,
|
|
3
|
+
IntegerCompare,
|
|
4
|
+
Sort,
|
|
5
|
+
SortDirection,
|
|
6
|
+
type StateFieldQuery,
|
|
7
|
+
} from "@peerbit/indexer-interface";
|
|
8
|
+
import type { Timestamp } from "./clock";
|
|
4
9
|
|
|
5
|
-
|
|
10
|
+
export type SortableEntry = {
|
|
11
|
+
meta: {
|
|
12
|
+
clock: {
|
|
13
|
+
timestamp: Timestamp;
|
|
14
|
+
};
|
|
15
|
+
gid: string;
|
|
16
|
+
};
|
|
17
|
+
hash: string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const ENTRY_SORT_SHAPE = {
|
|
21
|
+
hash: true,
|
|
22
|
+
meta: { gid: true, clock: true },
|
|
23
|
+
} as const;
|
|
24
|
+
|
|
25
|
+
export type SortFn = {
|
|
26
|
+
sort: Sort[];
|
|
27
|
+
before: (entry: SortableEntry) => StateFieldQuery[];
|
|
28
|
+
after: (entry: SortableEntry) => StateFieldQuery[];
|
|
29
|
+
};
|
|
6
30
|
|
|
7
|
-
export type ISortFunction = <T>(
|
|
8
|
-
a: ShallowEntry,
|
|
9
|
-
b: ShallowEntry,
|
|
10
|
-
resolveConflict?: (a: ShallowEntry, b: ShallowEntry) => number
|
|
11
|
-
) => number;
|
|
12
31
|
/**
|
|
13
32
|
* Sort two entries as Last-Write-Wins (LWW).
|
|
14
33
|
*
|
|
15
34
|
* Last Write Wins is a conflict resolution strategy for sorting elements
|
|
16
35
|
* where the element with a greater clock (latest) is chosen as the winner.
|
|
17
36
|
*
|
|
18
|
-
* @param {Entry} a First entry
|
|
19
|
-
* @param {Entry} b Second entry
|
|
20
|
-
* @returns {number} 1 if a is latest, -1 if b is latest
|
|
21
37
|
*/
|
|
22
|
-
export const LastWriteWins:
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
SortByClockId(a, b, First);
|
|
30
|
-
// Sort two entries by their clock time, if concurrent,
|
|
31
|
-
// determine sorting using provided conflict resolution function
|
|
32
|
-
const sortByEntryClocks = (a: ShallowEntry, b: ShallowEntry) =>
|
|
33
|
-
SortByClocks(a, b, sortById);
|
|
34
|
-
// Sort entries by clock time as the primary sort criteria
|
|
35
|
-
return sortByEntryClocks(a, b);
|
|
36
|
-
};
|
|
38
|
+
export const LastWriteWins: SortFn = {
|
|
39
|
+
sort: [
|
|
40
|
+
// sort first by clock
|
|
41
|
+
new Sort({
|
|
42
|
+
key: ["meta", "clock", "timestamp", "wallTime"],
|
|
43
|
+
direction: SortDirection.ASC,
|
|
44
|
+
}),
|
|
37
45
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
46
|
+
// then by logical
|
|
47
|
+
new Sort({
|
|
48
|
+
key: ["meta", "clock", "timestamp", "logical"],
|
|
49
|
+
direction: SortDirection.ASC,
|
|
50
|
+
}),
|
|
51
|
+
],
|
|
52
|
+
before: (entry: SortableEntry) => [
|
|
53
|
+
new IntegerCompare({
|
|
54
|
+
key: ["meta", "clock", "timestamp", "wallTime"],
|
|
55
|
+
value: entry.meta.clock.timestamp.wallTime,
|
|
56
|
+
compare: Compare.Less,
|
|
57
|
+
}),
|
|
58
|
+
],
|
|
59
|
+
after: (entry: SortableEntry) => [
|
|
60
|
+
new IntegerCompare({
|
|
61
|
+
key: ["meta", "clock", "timestamp", "wallTime"],
|
|
62
|
+
value: entry.meta.clock.timestamp.wallTime,
|
|
63
|
+
compare: Compare.Greater,
|
|
64
|
+
}),
|
|
65
|
+
],
|
|
58
66
|
};
|
|
59
67
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
*/
|
|
67
|
-
export const SortByClocks: ISortFunction = <T>(
|
|
68
|
-
a: ShallowEntry,
|
|
69
|
-
b: ShallowEntry,
|
|
70
|
-
resolveConflict?: (a: ShallowEntry, b: ShallowEntry) => number
|
|
71
|
-
) => {
|
|
72
|
-
// Compare the clocks
|
|
73
|
-
const diff = Clock.compare(a.meta.clock, b.meta.clock);
|
|
74
|
-
// If the clocks are concurrent, use the provided
|
|
75
|
-
// conflict resolution function to determine which comes first
|
|
76
|
-
return diff === 0 ? (resolveConflict || First)(a, b) : diff;
|
|
77
|
-
};
|
|
68
|
+
export const FirstWriteWins: SortFn = {
|
|
69
|
+
sort: [
|
|
70
|
+
new Sort({
|
|
71
|
+
key: ["meta", "clock", "timestamp", "wallTime"],
|
|
72
|
+
direction: SortDirection.DESC,
|
|
73
|
+
}),
|
|
78
74
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
75
|
+
// then by logical
|
|
76
|
+
new Sort({
|
|
77
|
+
key: ["meta", "clock", "timestamp", "logical"],
|
|
78
|
+
direction: SortDirection.DESC,
|
|
79
|
+
}),
|
|
80
|
+
],
|
|
81
|
+
before: (entry: SortableEntry) => [
|
|
82
|
+
new IntegerCompare({
|
|
83
|
+
key: ["meta", "clock", "timestamp", "wallTime"],
|
|
84
|
+
value: entry.meta.clock.timestamp.wallTime,
|
|
85
|
+
compare: Compare.Greater,
|
|
86
|
+
}),
|
|
87
|
+
],
|
|
88
|
+
after: (entry: SortableEntry) => [
|
|
89
|
+
new IntegerCompare({
|
|
90
|
+
key: ["meta", "clock", "timestamp", "wallTime"],
|
|
91
|
+
value: entry.meta.clock.timestamp.wallTime,
|
|
92
|
+
compare: Compare.Less,
|
|
93
|
+
}),
|
|
94
|
+
],
|
|
95
|
+
};
|
|
96
|
+
export const SortByEntryHash: SortFn = {
|
|
97
|
+
sort: [
|
|
98
|
+
// sort first by clock
|
|
99
|
+
new Sort({
|
|
100
|
+
key: ["hash"],
|
|
101
|
+
direction: SortDirection.ASC,
|
|
102
|
+
}),
|
|
103
|
+
],
|
|
104
|
+
before: (entry: SortableEntry) => [
|
|
105
|
+
new IntegerCompare({
|
|
106
|
+
key: ["hash"],
|
|
107
|
+
value: entry.meta.clock.timestamp.wallTime,
|
|
108
|
+
compare: Compare.Less,
|
|
109
|
+
}),
|
|
110
|
+
],
|
|
111
|
+
after: (entry: SortableEntry) => [
|
|
112
|
+
new IntegerCompare({
|
|
113
|
+
key: ["hash"],
|
|
114
|
+
value: entry.meta.clock.timestamp.wallTime,
|
|
115
|
+
compare: Compare.Greater,
|
|
116
|
+
}),
|
|
117
|
+
],
|
|
91
118
|
};
|
|
92
119
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const comparator = <T>(a: ShallowEntry, b: ShallowEntry) => {
|
|
101
|
-
// Validate by calling the function
|
|
102
|
-
const result = func(a, b, (a, b) => -1);
|
|
103
|
-
if (result === 0) {
|
|
104
|
-
throw Error(
|
|
105
|
-
`Your log's tiebreaker function, ${func.name}, has returned zero and therefore cannot be`
|
|
106
|
-
);
|
|
120
|
+
export const compare = (a: SortableEntry, b: SortableEntry, sortFn: SortFn) => {
|
|
121
|
+
for (const sort of sortFn.sort) {
|
|
122
|
+
const aVal = sort.key.reduce((prev, curr) => (prev as any)[curr], a);
|
|
123
|
+
const bVal = sort.key.reduce((prev, curr) => (prev as any)[curr], b);
|
|
124
|
+
let multiplier = sort.direction === SortDirection.ASC ? 1 : -1;
|
|
125
|
+
if (aVal < bVal) {
|
|
126
|
+
return -1 * multiplier;
|
|
107
127
|
}
|
|
108
|
-
|
|
109
|
-
|
|
128
|
+
if (aVal > bVal) {
|
|
129
|
+
return 1 * multiplier;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
110
132
|
|
|
111
|
-
return
|
|
133
|
+
return 0;
|
|
112
134
|
};
|