@peerbit/log 1.0.14 → 2.0.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/lib/esm/clock.d.ts +1 -1
- package/lib/esm/clock.js +11 -13
- package/lib/esm/clock.js.map +1 -1
- package/lib/esm/encoding.js +1 -1
- package/lib/esm/entry-index.d.ts +5 -2
- package/lib/esm/entry-index.js +9 -3
- package/lib/esm/entry-index.js.map +1 -1
- package/lib/esm/entry.d.ts +40 -27
- package/lib/esm/entry.js +115 -115
- package/lib/esm/entry.js.map +1 -1
- package/lib/esm/heads-cache.js +1 -1
- package/lib/esm/heads.d.ts +13 -8
- package/lib/esm/heads.js +52 -44
- package/lib/esm/heads.js.map +1 -1
- package/lib/esm/log-sorting.d.ts +3 -3
- package/lib/esm/log-sorting.js +2 -2
- package/lib/esm/log-sorting.js.map +1 -1
- package/lib/esm/log.d.ts +10 -6
- package/lib/esm/log.js +40 -52
- package/lib/esm/log.js.map +1 -1
- package/lib/esm/trim.d.ts +2 -2
- package/lib/esm/trim.js +10 -9
- package/lib/esm/trim.js.map +1 -1
- package/lib/esm/values.d.ts +5 -11
- package/lib/esm/values.js +17 -17
- package/lib/esm/values.js.map +1 -1
- package/package.json +5 -5
- package/src/clock.ts +11 -13
- package/src/encoding.ts +1 -1
- package/src/entry-index.ts +13 -6
- package/src/entry.ts +139 -138
- package/src/heads-cache.ts +1 -1
- package/src/heads.ts +76 -57
- package/src/log-sorting.ts +20 -16
- package/src/log.ts +61 -59
- package/src/trim.ts +14 -12
- package/src/values.ts +24 -32
- package/lib/esm/is-defined.d.ts +0 -1
- package/lib/esm/is-defined.js +0 -2
- package/lib/esm/is-defined.js.map +0 -1
- package/src/is-defined.ts +0 -1
package/src/trim.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Cache } from "@peerbit/cache";
|
|
2
2
|
import PQueue from "p-queue";
|
|
3
|
-
import { Entry } from "./entry.js";
|
|
3
|
+
import { Entry, ShallowEntry } from "./entry.js";
|
|
4
4
|
import { EntryNode, Values } from "./values.js";
|
|
5
5
|
|
|
6
6
|
const trimOptionsEqual = (a: TrimOptions, b: TrimOptions) => {
|
|
@@ -68,9 +68,10 @@ export type TrimCondition =
|
|
|
68
68
|
| TrimToByteLengthOption
|
|
69
69
|
| TrimToLengthOption
|
|
70
70
|
| TrimToTime;
|
|
71
|
+
|
|
71
72
|
export type TrimCanAppendOption = {
|
|
72
73
|
filter?: {
|
|
73
|
-
canTrim: (
|
|
74
|
+
canTrim: (entry: ShallowEntry) => Promise<boolean> | boolean;
|
|
74
75
|
cacheId?: () => string | number;
|
|
75
76
|
};
|
|
76
77
|
};
|
|
@@ -98,7 +99,7 @@ export class Trim<T> {
|
|
|
98
99
|
}
|
|
99
100
|
|
|
100
101
|
deleteFromCache(entry: Entry<T>) {
|
|
101
|
-
if (this._canTrimCacheLastNode?.value
|
|
102
|
+
if (this._canTrimCacheLastNode?.value === entry.hash) {
|
|
102
103
|
this._canTrimCacheLastNode = this._canTrimCacheLastNode.prev;
|
|
103
104
|
}
|
|
104
105
|
}
|
|
@@ -116,7 +117,7 @@ export class Trim<T> {
|
|
|
116
117
|
/// TODO Make this method less ugly
|
|
117
118
|
const deleted: Entry<T>[] = [];
|
|
118
119
|
|
|
119
|
-
let done: () =>
|
|
120
|
+
let done: () => boolean;
|
|
120
121
|
const values = this._log.values();
|
|
121
122
|
if (option.type === "length") {
|
|
122
123
|
const to = option.to;
|
|
@@ -136,18 +137,18 @@ export class Trim<T> {
|
|
|
136
137
|
} else if (option.type == "time") {
|
|
137
138
|
const s0 = BigInt(+new Date() * 1e6);
|
|
138
139
|
const maxAge = option.maxAge * 1e6;
|
|
139
|
-
done =
|
|
140
|
+
done = () => {
|
|
140
141
|
if (!values.tail) {
|
|
141
142
|
return true;
|
|
142
143
|
}
|
|
143
144
|
|
|
144
|
-
const nodeValue =
|
|
145
|
+
const nodeValue = values.entryIndex.getShallow(values.tail.value);
|
|
145
146
|
|
|
146
147
|
if (!nodeValue) {
|
|
147
148
|
return true;
|
|
148
149
|
}
|
|
149
150
|
|
|
150
|
-
return s0 - nodeValue.
|
|
151
|
+
return s0 - nodeValue.meta.clock.timestamp.wallTime < maxAge;
|
|
151
152
|
};
|
|
152
153
|
} else {
|
|
153
154
|
return [];
|
|
@@ -194,7 +195,7 @@ export class Trim<T> {
|
|
|
194
195
|
// TODO only go through heads?
|
|
195
196
|
while (
|
|
196
197
|
node &&
|
|
197
|
-
!
|
|
198
|
+
!done() &&
|
|
198
199
|
values.length > 0 &&
|
|
199
200
|
node &&
|
|
200
201
|
(!looped || node !== startNode)
|
|
@@ -202,15 +203,16 @@ export class Trim<T> {
|
|
|
202
203
|
let deleteAble: boolean | undefined = true;
|
|
203
204
|
if (option.filter?.canTrim) {
|
|
204
205
|
canTrimByGid = canTrimByGid || new Map();
|
|
205
|
-
|
|
206
|
+
const indexedEntry = values.entryIndex.getShallow(node.value)!; // TODO check undefined
|
|
207
|
+
deleteAble = canTrimByGid.get(indexedEntry.meta.gid);
|
|
206
208
|
if (deleteAble === undefined) {
|
|
207
|
-
deleteAble = await option.filter?.canTrim(
|
|
208
|
-
canTrimByGid.set(
|
|
209
|
+
deleteAble = await option.filter?.canTrim(indexedEntry);
|
|
210
|
+
canTrimByGid.set(indexedEntry.meta.gid, deleteAble);
|
|
209
211
|
}
|
|
210
212
|
|
|
211
213
|
if (!deleteAble && cacheProgress) {
|
|
212
214
|
// ignore it
|
|
213
|
-
this._canTrimCacheHashBreakpoint.add(node.value
|
|
215
|
+
this._canTrimCacheHashBreakpoint.add(node.value, true);
|
|
214
216
|
}
|
|
215
217
|
}
|
|
216
218
|
|
package/src/values.ts
CHANGED
|
@@ -1,25 +1,15 @@
|
|
|
1
|
-
import { Entry } from "./entry";
|
|
1
|
+
import { Entry, ShallowEntry } from "./entry";
|
|
2
2
|
import { ISortFunction } from "./log-sorting";
|
|
3
3
|
import yallist from "yallist";
|
|
4
4
|
import { EntryIndex } from "./entry-index";
|
|
5
5
|
|
|
6
|
-
type
|
|
7
|
-
hash: string
|
|
8
|
-
) => Promise<Entry<T> | undefined> | Entry<T> | undefined;
|
|
9
|
-
|
|
10
|
-
interface Value {
|
|
11
|
-
hash: string;
|
|
12
|
-
gid: string;
|
|
13
|
-
byteLength: number;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export type EntryNode = yallist.Node<Value>;
|
|
6
|
+
export type EntryNode = yallist.Node<string>;
|
|
17
7
|
|
|
18
8
|
export class Values<T> {
|
|
19
9
|
/**
|
|
20
10
|
* Keep track of sorted elements in descending sort order (i.e. newest elements)
|
|
21
11
|
*/
|
|
22
|
-
private _values: yallist<
|
|
12
|
+
private _values: yallist<string>;
|
|
23
13
|
private _sortFn: ISortFunction;
|
|
24
14
|
private _byteLength: number;
|
|
25
15
|
private _entryIndex: EntryIndex<T>;
|
|
@@ -36,11 +26,15 @@ export class Values<T> {
|
|
|
36
26
|
.reverse()
|
|
37
27
|
.map((x) => {
|
|
38
28
|
if (!x.hash) throw new Error("Unexpected");
|
|
39
|
-
return {
|
|
29
|
+
return x.hash; /* {
|
|
40
30
|
hash: x.hash,
|
|
41
31
|
byteLength: x._payload.byteLength,
|
|
42
|
-
|
|
43
|
-
|
|
32
|
+
meta: {
|
|
33
|
+
gids: x.gids,
|
|
34
|
+
gid: x.gid,
|
|
35
|
+
data: x.meta.data,
|
|
36
|
+
},
|
|
37
|
+
}; */
|
|
44
38
|
})
|
|
45
39
|
);
|
|
46
40
|
this._byteLength = 0;
|
|
@@ -53,7 +47,7 @@ export class Values<T> {
|
|
|
53
47
|
|
|
54
48
|
toArray(): Promise<Entry<T>[]> {
|
|
55
49
|
return Promise.all(
|
|
56
|
-
this._values.toArrayReverse().map((x) => this._entryIndex.get(x
|
|
50
|
+
this._values.toArrayReverse().map((x) => this._entryIndex.get(x))
|
|
57
51
|
).then((arr) => arr.filter((x) => !!x)) as Promise<Entry<T>[]>; // we do reverse because we assume the log is only meaningful if we read it from start to end
|
|
58
52
|
}
|
|
59
53
|
|
|
@@ -68,6 +62,10 @@ export class Values<T> {
|
|
|
68
62
|
}
|
|
69
63
|
|
|
70
64
|
private _putPromise: Map<string, Promise<any>> = new Map();
|
|
65
|
+
|
|
66
|
+
get entryIndex(): EntryIndex<T> {
|
|
67
|
+
return this._entryIndex;
|
|
68
|
+
}
|
|
71
69
|
async put(value: Entry<T>) {
|
|
72
70
|
let promise = this._putPromise.get(value.hash);
|
|
73
71
|
if (promise) {
|
|
@@ -85,7 +83,7 @@ export class Values<T> {
|
|
|
85
83
|
let walker = this._values.head;
|
|
86
84
|
let last: EntryNode | undefined = undefined;
|
|
87
85
|
while (walker) {
|
|
88
|
-
const walkerValue = await this.
|
|
86
|
+
const walkerValue = await this._entryIndex.getShallow(walker.value);
|
|
89
87
|
if (!walkerValue) {
|
|
90
88
|
throw new Error("Missing walker value");
|
|
91
89
|
}
|
|
@@ -106,11 +104,7 @@ export class Values<T> {
|
|
|
106
104
|
throw new Error("Unexpected");
|
|
107
105
|
}
|
|
108
106
|
|
|
109
|
-
_insertAfter(this._values, last,
|
|
110
|
-
byteLength: value._payload.byteLength,
|
|
111
|
-
gid: value.gid,
|
|
112
|
-
hash: value.hash,
|
|
113
|
-
});
|
|
107
|
+
_insertAfter(this._values, last, value.hash);
|
|
114
108
|
}
|
|
115
109
|
|
|
116
110
|
async delete(value: Entry<T> | string) {
|
|
@@ -119,7 +113,7 @@ export class Values<T> {
|
|
|
119
113
|
|
|
120
114
|
let walker = this._values.tail;
|
|
121
115
|
while (walker) {
|
|
122
|
-
const walkerValue =
|
|
116
|
+
const walkerValue = this._entryIndex.getShallow(walker.value);
|
|
123
117
|
|
|
124
118
|
if (!walkerValue) {
|
|
125
119
|
throw new Error("Missing walker value");
|
|
@@ -127,7 +121,7 @@ export class Values<T> {
|
|
|
127
121
|
|
|
128
122
|
if (walkerValue.hash === hash) {
|
|
129
123
|
this._values.removeNode(walker);
|
|
130
|
-
this._byteLength -= walkerValue.
|
|
124
|
+
this._byteLength -= walkerValue.payloadByteLength;
|
|
131
125
|
return;
|
|
132
126
|
}
|
|
133
127
|
walker = walker.prev; // prev will be undefined if you do removeNode(walker)
|
|
@@ -143,14 +137,16 @@ export class Values<T> {
|
|
|
143
137
|
|
|
144
138
|
deleteNode(node: EntryNode) {
|
|
145
139
|
this._values.removeNode(node);
|
|
146
|
-
this._byteLength -=
|
|
140
|
+
this._byteLength -= this._entryIndex.getShallow(
|
|
141
|
+
node.value
|
|
142
|
+
)!.payloadByteLength;
|
|
147
143
|
return;
|
|
148
144
|
}
|
|
149
145
|
|
|
150
146
|
pop() {
|
|
151
147
|
const value = this._values.pop();
|
|
152
148
|
if (value) {
|
|
153
|
-
this._byteLength -= value
|
|
149
|
+
this._byteLength -= this._entryIndex.getShallow(value)!.payloadByteLength;
|
|
154
150
|
}
|
|
155
151
|
return value;
|
|
156
152
|
}
|
|
@@ -158,16 +154,12 @@ export class Values<T> {
|
|
|
158
154
|
get byteLength() {
|
|
159
155
|
return this._byteLength;
|
|
160
156
|
}
|
|
161
|
-
|
|
162
|
-
async getEntry(node: EntryNode) {
|
|
163
|
-
return this._entryIndex.get(node.value.hash);
|
|
164
|
-
}
|
|
165
157
|
}
|
|
166
158
|
|
|
167
159
|
function _insertAfter(
|
|
168
160
|
self: yallist<any>,
|
|
169
161
|
node: EntryNode | undefined,
|
|
170
|
-
value:
|
|
162
|
+
value: string
|
|
171
163
|
) {
|
|
172
164
|
const inserted = !node
|
|
173
165
|
? new yallist.Node(
|
package/lib/esm/is-defined.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const isDefined: (arg: any) => boolean;
|
package/lib/esm/is-defined.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"is-defined.js","sourceRoot":"","sources":["../../src/is-defined.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,GAAQ,EAAE,EAAE,CAAC,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,IAAI,CAAC"}
|
package/src/is-defined.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const isDefined = (arg: any) => arg !== undefined && arg !== null;
|