@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.
Files changed (133) hide show
  1. package/dist/benchmark/append.d.ts +2 -0
  2. package/dist/benchmark/append.d.ts.map +1 -0
  3. package/dist/benchmark/append.js +40 -0
  4. package/dist/benchmark/append.js.map +1 -0
  5. package/dist/benchmark/memory/index.d.ts +2 -0
  6. package/dist/benchmark/memory/index.d.ts.map +1 -0
  7. package/dist/benchmark/memory/index.js +122 -0
  8. package/dist/benchmark/memory/index.js.map +1 -0
  9. package/dist/benchmark/memory/insert.d.ts +2 -0
  10. package/dist/benchmark/memory/insert.d.ts.map +1 -0
  11. package/dist/benchmark/memory/insert.js +59 -0
  12. package/dist/benchmark/memory/insert.js.map +1 -0
  13. package/dist/benchmark/memory/utils.d.ts +13 -0
  14. package/dist/benchmark/memory/utils.d.ts.map +1 -0
  15. package/dist/benchmark/memory/utils.js +2 -0
  16. package/dist/benchmark/memory/utils.js.map +1 -0
  17. package/dist/benchmark/payload.d.ts +2 -0
  18. package/dist/benchmark/payload.d.ts.map +1 -0
  19. package/{lib/esm/__benchmark__/index.js → dist/benchmark/payload.js} +20 -22
  20. package/dist/benchmark/payload.js.map +1 -0
  21. package/dist/src/change.d.ts +6 -0
  22. package/dist/src/change.d.ts.map +1 -0
  23. package/{lib/esm → dist/src}/clock.d.ts +3 -26
  24. package/dist/src/clock.d.ts.map +1 -0
  25. package/{lib/esm → dist/src}/clock.js +30 -39
  26. package/dist/src/clock.js.map +1 -0
  27. package/{lib/esm → dist/src}/difference.d.ts +1 -0
  28. package/dist/src/difference.d.ts.map +1 -0
  29. package/{lib/esm → dist/src}/encoding.d.ts +2 -1
  30. package/dist/src/encoding.d.ts.map +1 -0
  31. package/{lib/esm → dist/src}/encoding.js +2 -2
  32. package/{lib/esm → dist/src}/encoding.js.map +1 -1
  33. package/dist/src/entry-index.d.ts +78 -0
  34. package/dist/src/entry-index.d.ts.map +1 -0
  35. package/dist/src/entry-index.js +316 -0
  36. package/dist/src/entry-index.js.map +1 -0
  37. package/{lib/esm → dist/src}/entry-with-refs.d.ts +2 -1
  38. package/dist/src/entry-with-refs.d.ts.map +1 -0
  39. package/{lib/esm → dist/src}/entry.d.ts +22 -18
  40. package/dist/src/entry.d.ts.map +1 -0
  41. package/{lib/esm → dist/src}/entry.js +69 -42
  42. package/dist/src/entry.js.map +1 -0
  43. package/{lib/esm → dist/src}/find-uniques.d.ts +1 -0
  44. package/dist/src/find-uniques.d.ts.map +1 -0
  45. package/{lib/esm → dist/src}/heads-cache.d.ts +4 -3
  46. package/dist/src/heads-cache.d.ts.map +1 -0
  47. package/{lib/esm → dist/src}/heads-cache.js +9 -10
  48. package/dist/src/heads-cache.js.map +1 -0
  49. package/{lib/esm → dist/src}/index.d.ts +1 -0
  50. package/dist/src/index.d.ts.map +1 -0
  51. package/{lib/esm → dist/src}/log-errors.d.ts +1 -0
  52. package/dist/src/log-errors.d.ts.map +1 -0
  53. package/dist/src/log-sorting.d.ts +35 -0
  54. package/dist/src/log-sorting.d.ts.map +1 -0
  55. package/dist/src/log-sorting.js +105 -0
  56. package/dist/src/log-sorting.js.map +1 -0
  57. package/{lib/esm → dist/src}/log.d.ts +78 -56
  58. package/dist/src/log.d.ts.map +1 -0
  59. package/{lib/esm → dist/src}/log.js +355 -465
  60. package/dist/src/log.js.map +1 -0
  61. package/{lib/esm → dist/src}/logger.d.ts +1 -0
  62. package/dist/src/logger.d.ts.map +1 -0
  63. package/{lib/esm → dist/src}/logger.js.map +1 -1
  64. package/{lib/esm → dist/src}/snapshot.d.ts +5 -4
  65. package/dist/src/snapshot.d.ts.map +1 -0
  66. package/{lib/esm → dist/src}/snapshot.js +10 -10
  67. package/dist/src/snapshot.js.map +1 -0
  68. package/{lib/esm → dist/src}/trim.d.ts +11 -9
  69. package/dist/src/trim.d.ts.map +1 -0
  70. package/{lib/esm → dist/src}/trim.js +46 -35
  71. package/dist/src/trim.js.map +1 -0
  72. package/dist/src/utils.d.ts +4 -0
  73. package/dist/src/utils.d.ts.map +1 -0
  74. package/dist/src/utils.js +12 -0
  75. package/dist/src/utils.js.map +1 -0
  76. package/package.json +104 -78
  77. package/src/change.ts +3 -2
  78. package/src/clock.ts +22 -22
  79. package/src/encoding.ts +4 -4
  80. package/src/entry-index.ts +451 -52
  81. package/src/entry-with-refs.ts +1 -1
  82. package/src/entry.ts +95 -81
  83. package/src/heads-cache.ts +33 -29
  84. package/src/log-sorting.ts +116 -94
  85. package/src/log.ts +482 -571
  86. package/src/logger.ts +1 -0
  87. package/src/snapshot.ts +15 -17
  88. package/src/trim.ts +81 -50
  89. package/src/utils.ts +10 -0
  90. package/lib/esm/__benchmark__/index.d.ts +0 -1
  91. package/lib/esm/__benchmark__/index.js.map +0 -1
  92. package/lib/esm/change.d.ts +0 -5
  93. package/lib/esm/clock.js.map +0 -1
  94. package/lib/esm/entry-index.d.ts +0 -24
  95. package/lib/esm/entry-index.js +0 -74
  96. package/lib/esm/entry-index.js.map +0 -1
  97. package/lib/esm/entry.js.map +0 -1
  98. package/lib/esm/heads-cache.js.map +0 -1
  99. package/lib/esm/heads.d.ts +0 -69
  100. package/lib/esm/heads.js +0 -157
  101. package/lib/esm/heads.js.map +0 -1
  102. package/lib/esm/log-sorting.d.ts +0 -44
  103. package/lib/esm/log-sorting.js +0 -86
  104. package/lib/esm/log-sorting.js.map +0 -1
  105. package/lib/esm/log.js.map +0 -1
  106. package/lib/esm/snapshot.js.map +0 -1
  107. package/lib/esm/trim.js.map +0 -1
  108. package/lib/esm/types.d.ts +0 -6
  109. package/lib/esm/types.js +0 -23
  110. package/lib/esm/types.js.map +0 -1
  111. package/lib/esm/utils.d.ts +0 -2
  112. package/lib/esm/utils.js +0 -3
  113. package/lib/esm/utils.js.map +0 -1
  114. package/lib/esm/values.d.ts +0 -26
  115. package/lib/esm/values.js +0 -131
  116. package/lib/esm/values.js.map +0 -1
  117. package/src/__benchmark__/index.ts +0 -130
  118. package/src/heads.ts +0 -233
  119. package/src/types.ts +0 -12
  120. package/src/values.ts +0 -174
  121. /package/{lib/esm → dist/src}/change.js +0 -0
  122. /package/{lib/esm → dist/src}/change.js.map +0 -0
  123. /package/{lib/esm → dist/src}/difference.js +0 -0
  124. /package/{lib/esm → dist/src}/difference.js.map +0 -0
  125. /package/{lib/esm → dist/src}/entry-with-refs.js +0 -0
  126. /package/{lib/esm → dist/src}/entry-with-refs.js.map +0 -0
  127. /package/{lib/esm → dist/src}/find-uniques.js +0 -0
  128. /package/{lib/esm → dist/src}/find-uniques.js.map +0 -0
  129. /package/{lib/esm → dist/src}/index.js +0 -0
  130. /package/{lib/esm → dist/src}/index.js.map +0 -0
  131. /package/{lib/esm → dist/src}/log-errors.js +0 -0
  132. /package/{lib/esm → dist/src}/log-errors.js.map +0 -0
  133. /package/{lib/esm → dist/src}/logger.js +0 -0
@@ -1,112 +1,134 @@
1
- import { Entry, ShallowEntry } from "./entry.js";
2
- import { LamportClock as Clock } from "./clock.js";
3
- import { compare } from "@peerbit/uint8arrays";
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
- const First = (a: any, b: any) => a;
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: ISortFunction = <T>(
23
- a: ShallowEntry,
24
- b: ShallowEntry
25
- ) => {
26
- // Ultimate conflict resolution (take the first/left arg)
27
- // Sort two entries by their clock id, if the same always take the first
28
- const sortById = (a: ShallowEntry, b: ShallowEntry) =>
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
- * Sort two entries by their hash.
40
- *
41
- * @param {Entry} a First entry
42
- * @param {Entry} b Second entry
43
- * @returns {number} 1 if a is latest, -1 if b is latest
44
- */
45
- export const SortByEntryHash: ISortFunction = (a, b) => {
46
- // Ultimate conflict resolution (compare hashes)
47
- const compareHash = (a: ShallowEntry, b: ShallowEntry) =>
48
- a.hash < b.hash ? -1 : 1;
49
- // Sort two entries by their clock id, if the same then compare hashes
50
- const sortById = (a: ShallowEntry, b: ShallowEntry) =>
51
- SortByClockId(a, b, compareHash);
52
- // Sort two entries by their clock time, if concurrent,
53
- // determine sorting using provided conflict resolution function
54
- const sortByEntryClocks = (a: ShallowEntry, b: ShallowEntry) =>
55
- SortByClocks(a, b, sortById);
56
- // Sort entries by clock time as the primary sort criteria
57
- return sortByEntryClocks(a, b);
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
- * Sort two entries by their clock time.
62
- * @param {Entry} a First entry to compare
63
- * @param {Entry} b Second entry to compare
64
- * @param {function(a, b)} resolveConflict A function to call if entries are concurrent (happened at the same time). The function should take in two entries and return 1 if the first entry should be chosen and -1 if the second entry should be chosen.
65
- * @returns {number} 1 if a is greater, -1 if b is greater
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
- * Sort two entries by their clock id.
81
- * @param {Entry} a First entry to compare
82
- * @param {Entry} b Second entry to compare
83
- * @param {function(a, b)} resolveConflict A function to call if the clocks ids are the same. The function should take in two entries and return 1 if the first entry should be chosen and -1 if the second entry should be chosen.
84
- * @returns {number} 1 if a is greater, -1 if b is greater
85
- */
86
- export const SortByClockId: ISortFunction = (a, b, resolveConflict) => {
87
- // Sort by ID if clocks are concurrent,
88
- // take the entry with a "greater" clock id
89
- const clockCompare = compare(a.meta.clock.id, b.meta.clock.id);
90
- return clockCompare === 0 ? (resolveConflict || First)(a, b) : clockCompare;
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
- * A wrapper function to throw an error if the results of a passed function return zero
95
- * @param {function(a, b)} [tiebreaker] The tiebreaker function to validate.
96
- * @returns {function(a, b)} 1 if a is greater, -1 if b is greater
97
- * @throws {Error} if func ever returns 0
98
- */
99
- export const NoZeroes = (func: ISortFunction) => {
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
- return result;
109
- };
128
+ if (aVal > bVal) {
129
+ return 1 * multiplier;
130
+ }
131
+ }
110
132
 
111
- return comparator;
133
+ return 0;
112
134
  };