@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
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import B from "benchmark";
|
|
2
|
-
import { field, option, serialize, variant } from "@dao-xyz/borsh";
|
|
3
|
-
import { v4 as uuid } from "uuid";
|
|
4
|
-
import crypto from "crypto";
|
|
5
|
-
import { Log } from "../log.js";
|
|
6
|
-
import { AnyBlockStore } from "@peerbit/blocks";
|
|
7
|
-
import { Ed25519Keypair } from "@peerbit/crypto";
|
|
8
|
-
import { BORSH_ENCODING } from "../encoding.js";
|
|
9
|
-
import { Entry } from "../entry.js";
|
|
10
|
-
|
|
11
|
-
// Run with "node --loader ts-node/esm ./src/__benchmark__/index.ts"
|
|
12
|
-
|
|
13
|
-
@variant("document")
|
|
14
|
-
class Document {
|
|
15
|
-
@field({ type: "string" })
|
|
16
|
-
id: string;
|
|
17
|
-
|
|
18
|
-
@field({ type: option("string") })
|
|
19
|
-
name?: string;
|
|
20
|
-
|
|
21
|
-
@field({ type: option("u64") })
|
|
22
|
-
number?: bigint;
|
|
23
|
-
|
|
24
|
-
@field({ type: Uint8Array })
|
|
25
|
-
bytes: Uint8Array;
|
|
26
|
-
|
|
27
|
-
constructor(opts: Document) {
|
|
28
|
-
if (opts) {
|
|
29
|
-
this.id = opts.id;
|
|
30
|
-
this.name = opts.name;
|
|
31
|
-
this.number = opts.number;
|
|
32
|
-
this.bytes = opts.bytes;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
let log: Log<Document>;
|
|
37
|
-
let store: AnyBlockStore;
|
|
38
|
-
const key = await Ed25519Keypair.create();
|
|
39
|
-
|
|
40
|
-
const reset = async () => {
|
|
41
|
-
log = new Log<Document>();
|
|
42
|
-
store = new AnyBlockStore();
|
|
43
|
-
await log.open(store, key, { encoding: BORSH_ENCODING(Document) });
|
|
44
|
-
};
|
|
45
|
-
await reset();
|
|
46
|
-
|
|
47
|
-
class NestedEntry {
|
|
48
|
-
@field({ type: Entry })
|
|
49
|
-
entry: Entry<any>;
|
|
50
|
-
|
|
51
|
-
constructor(entry: Entry<any>) {
|
|
52
|
-
this.entry = entry;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
const suite = new B.Suite({ delay: 100 });
|
|
56
|
-
suite
|
|
57
|
-
.add("1e3", {
|
|
58
|
-
fn: async (deferred) => {
|
|
59
|
-
const doc = new Document({
|
|
60
|
-
id: uuid(),
|
|
61
|
-
name: "hello",
|
|
62
|
-
number: 1n,
|
|
63
|
-
bytes: crypto.randomBytes(1e3)
|
|
64
|
-
});
|
|
65
|
-
await log.append(doc);
|
|
66
|
-
|
|
67
|
-
deferred.resolve();
|
|
68
|
-
},
|
|
69
|
-
defer: true
|
|
70
|
-
})
|
|
71
|
-
.add("1e4", {
|
|
72
|
-
fn: async (deferred) => {
|
|
73
|
-
const doc = new Document({
|
|
74
|
-
id: uuid(),
|
|
75
|
-
name: "hello",
|
|
76
|
-
number: 1n,
|
|
77
|
-
bytes: crypto.randomBytes(1e4)
|
|
78
|
-
});
|
|
79
|
-
await log.append(doc);
|
|
80
|
-
|
|
81
|
-
deferred.resolve();
|
|
82
|
-
},
|
|
83
|
-
defer: true
|
|
84
|
-
})
|
|
85
|
-
.add("1e5", {
|
|
86
|
-
fn: async (deferred) => {
|
|
87
|
-
const doc = new Document({
|
|
88
|
-
id: uuid(),
|
|
89
|
-
name: "hello",
|
|
90
|
-
number: 1n,
|
|
91
|
-
bytes: crypto.randomBytes(1e5)
|
|
92
|
-
});
|
|
93
|
-
const entry = await log.append(doc);
|
|
94
|
-
serialize(new NestedEntry(entry.entry));
|
|
95
|
-
serialize(new NestedEntry(entry.entry));
|
|
96
|
-
serialize(new NestedEntry(entry.entry));
|
|
97
|
-
serialize(new NestedEntry(entry.entry));
|
|
98
|
-
serialize(new NestedEntry(entry.entry));
|
|
99
|
-
serialize(new NestedEntry(entry.entry));
|
|
100
|
-
serialize(new NestedEntry(entry.entry));
|
|
101
|
-
serialize(new NestedEntry(entry.entry));
|
|
102
|
-
serialize(new NestedEntry(entry.entry));
|
|
103
|
-
serialize(new NestedEntry(entry.entry));
|
|
104
|
-
|
|
105
|
-
deferred.resolve();
|
|
106
|
-
},
|
|
107
|
-
defer: true
|
|
108
|
-
})
|
|
109
|
-
.add("1e6", {
|
|
110
|
-
fn: async (deferred) => {
|
|
111
|
-
const doc = new Document({
|
|
112
|
-
id: uuid(),
|
|
113
|
-
name: "hello",
|
|
114
|
-
number: 1n,
|
|
115
|
-
bytes: crypto.randomBytes(1e5)
|
|
116
|
-
});
|
|
117
|
-
await log.append(doc);
|
|
118
|
-
|
|
119
|
-
deferred.resolve();
|
|
120
|
-
},
|
|
121
|
-
defer: true
|
|
122
|
-
})
|
|
123
|
-
.on("cycle", async (event: any) => {
|
|
124
|
-
console.log(String(event.target));
|
|
125
|
-
await reset();
|
|
126
|
-
})
|
|
127
|
-
.on("error", (err) => {
|
|
128
|
-
throw err;
|
|
129
|
-
})
|
|
130
|
-
.run();
|
package/src/heads.ts
DELETED
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
import { Entry } from "./entry.js";
|
|
2
|
-
import { AnyStore } from "@peerbit/any-store";
|
|
3
|
-
import { HeadsCache } from "./heads-cache.js";
|
|
4
|
-
import { Blocks } from "@peerbit/blocks-interface";
|
|
5
|
-
import { Keychain } from "@peerbit/keychain";
|
|
6
|
-
import { Encoding } from "./encoding.js";
|
|
7
|
-
import { Values } from "./values.js";
|
|
8
|
-
import { logger } from "./logger.js";
|
|
9
|
-
import { EntryIndex } from "./entry-index.js";
|
|
10
|
-
|
|
11
|
-
export type CacheUpdateOptions = {
|
|
12
|
-
cache?: { update?: false; reset?: false } | { update: true; reset?: boolean };
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
interface Log<T> {
|
|
16
|
-
blocks: Blocks;
|
|
17
|
-
keychain?: Keychain;
|
|
18
|
-
memory?: AnyStore;
|
|
19
|
-
encoding: Encoding<any>;
|
|
20
|
-
entryIndex: EntryIndex<T>;
|
|
21
|
-
values: Values<T>;
|
|
22
|
-
}
|
|
23
|
-
export class HeadsIndex<T> {
|
|
24
|
-
private _id: Uint8Array;
|
|
25
|
-
private _index: Set<string> = new Set();
|
|
26
|
-
private _gids: Map<string, Map<string, Entry<T>>>; // gid -> hash -> entry
|
|
27
|
-
private _headsCache: HeadsCache<T> | undefined;
|
|
28
|
-
private _config: Log<T>;
|
|
29
|
-
private _onGidRemoved?: (gid: string[]) => Promise<void> | void;
|
|
30
|
-
constructor(id: Uint8Array) {
|
|
31
|
-
this._gids = new Map();
|
|
32
|
-
this._id = id;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async init(
|
|
36
|
-
log: Log<T>,
|
|
37
|
-
options: {
|
|
38
|
-
entries?: Entry<T>[];
|
|
39
|
-
onGidRemoved?: (gid: string[]) => Promise<void> | void;
|
|
40
|
-
} = {}
|
|
41
|
-
) {
|
|
42
|
-
this._config = log;
|
|
43
|
-
this._onGidRemoved = options.onGidRemoved;
|
|
44
|
-
await this.reset(options?.entries || []);
|
|
45
|
-
if (log.memory) {
|
|
46
|
-
this._headsCache = new HeadsCache(this);
|
|
47
|
-
return this._headsCache.init(await log.memory.sublevel("heads"));
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async load(
|
|
52
|
-
options?: {
|
|
53
|
-
timeout?: number;
|
|
54
|
-
replicate?: boolean;
|
|
55
|
-
reload?: boolean;
|
|
56
|
-
ignoreMissing?: boolean;
|
|
57
|
-
} & CacheUpdateOptions
|
|
58
|
-
): Promise<Entry<T>[] | undefined> {
|
|
59
|
-
if (!this._headsCache || (this._headsCache.loaded && !options?.reload)) {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// TODO make below into a promise that concurrenct caklls can wait on?
|
|
64
|
-
const heads = await this._headsCache?.load();
|
|
65
|
-
if (!heads) {
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
const entries = await Promise.all(
|
|
69
|
-
heads.map(async (x) => {
|
|
70
|
-
const entry = await this._config.entryIndex.get(x, { load: true });
|
|
71
|
-
if (!entry) {
|
|
72
|
-
if (options?.ignoreMissing) {
|
|
73
|
-
logger.error("Failed to load entry from head with hash: " + x);
|
|
74
|
-
return;
|
|
75
|
-
} else {
|
|
76
|
-
throw new Error("Failed to load entry from head with hash: " + x);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
await entry.getMeta(); // TODO types,decrypt gid
|
|
80
|
-
return entry;
|
|
81
|
-
})
|
|
82
|
-
);
|
|
83
|
-
const filtered = entries.filter((x): x is Entry<any> => !!x);
|
|
84
|
-
await this.reset(filtered);
|
|
85
|
-
return filtered;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
get headsCache(): HeadsCache<T> | undefined {
|
|
89
|
-
return this._headsCache;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
close() {
|
|
93
|
-
return this._headsCache?.close();
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
drop() {
|
|
97
|
-
return this._headsCache?.drop();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
get id(): Uint8Array {
|
|
101
|
-
return this._id;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
get index() {
|
|
105
|
-
return this._index;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
get gids(): Map<string, Map<string, Entry<T>>> {
|
|
109
|
-
return this._gids;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
get size() {
|
|
113
|
-
return this._index.size;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
async reset(
|
|
117
|
-
entries: Entry<T>[],
|
|
118
|
-
options: CacheUpdateOptions = { cache: { reset: true, update: true } }
|
|
119
|
-
) {
|
|
120
|
-
this._index.clear();
|
|
121
|
-
const gidKeys = [...this._gids.keys()];
|
|
122
|
-
|
|
123
|
-
this._gids = new Map();
|
|
124
|
-
if (entries?.length > 0) {
|
|
125
|
-
await this.putAll(entries, options); // reset cache = true
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (gidKeys.length > 0) {
|
|
129
|
-
this._onGidRemoved?.(gidKeys);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
has(cid: string) {
|
|
134
|
-
return this._index.has(cid);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
async put(entry: Entry<T>, options?: CacheUpdateOptions) {
|
|
138
|
-
await this._putOne(entry);
|
|
139
|
-
if (!options?.cache || options?.cache?.update) {
|
|
140
|
-
await this._headsCache?.queue({ added: [entry] }, options?.cache?.reset);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
async putAll(entries: Entry<T>[], options?: CacheUpdateOptions) {
|
|
145
|
-
await this._putAll(entries);
|
|
146
|
-
if (!options?.cache || options?.cache?.update) {
|
|
147
|
-
await this._headsCache?.queue({ added: entries }, options?.cache?.reset);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
async resetHeadsCache() {
|
|
152
|
-
await this._headsCache?.queue(
|
|
153
|
-
{ added: [...this._index], removed: [] },
|
|
154
|
-
true
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
async updateHeadsCache(
|
|
158
|
-
change: {
|
|
159
|
-
added?: (Entry<T> | string)[];
|
|
160
|
-
removed?: (Entry<T> | string)[];
|
|
161
|
-
} = {},
|
|
162
|
-
reset?: boolean
|
|
163
|
-
) {
|
|
164
|
-
await this._headsCache?.queue(change, reset);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
private async _putOne(entry: Entry<T>) {
|
|
168
|
-
if (!entry.hash) {
|
|
169
|
-
throw new Error("Missing hash");
|
|
170
|
-
}
|
|
171
|
-
if (this._index.has(entry.hash)) {
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
this._index.add(entry.hash);
|
|
176
|
-
const map = this._gids.get(entry.meta.gid);
|
|
177
|
-
if (!map) {
|
|
178
|
-
const newMap = new Map();
|
|
179
|
-
this._gids.set(entry.meta.gid, newMap);
|
|
180
|
-
newMap.set(entry.hash, entry);
|
|
181
|
-
} else {
|
|
182
|
-
map.set(entry.hash, entry);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
for (const next of entry.next) {
|
|
186
|
-
const indexedEntry = this._config.entryIndex.getShallow(next);
|
|
187
|
-
if (indexedEntry) {
|
|
188
|
-
await this.del(indexedEntry, { cache: { update: false } }); // we dont update cache here because the put will update the cache that is calling _putOne
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
private async _putAll(entries: Entry<T>[]) {
|
|
194
|
-
for (const entry of entries) {
|
|
195
|
-
await this._putOne(entry);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
async del(
|
|
200
|
-
entry: { hash: string; meta: { gid: string } },
|
|
201
|
-
options?: CacheUpdateOptions
|
|
202
|
-
): Promise<boolean> {
|
|
203
|
-
const wasHead = this._index.delete(entry.hash);
|
|
204
|
-
if (!wasHead) {
|
|
205
|
-
return false;
|
|
206
|
-
}
|
|
207
|
-
let removedGids: Set<string> | undefined = undefined;
|
|
208
|
-
const map = this._gids.get(entry.meta.gid)!;
|
|
209
|
-
map.delete(entry.hash);
|
|
210
|
-
if (map.size <= 0) {
|
|
211
|
-
this._gids.delete(entry.meta.gid);
|
|
212
|
-
(removedGids || (removedGids = new Set<string>())).add(entry.meta.gid);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
if (!entry.hash) {
|
|
216
|
-
throw new Error("Missing hash");
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
if (removedGids) {
|
|
220
|
-
await this._onGidRemoved?.([...removedGids]);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
if (wasHead && !(options?.cache || options?.cache?.update)) {
|
|
224
|
-
await this._headsCache?.queue(
|
|
225
|
-
{ removed: [entry.hash] },
|
|
226
|
-
options?.cache?.reset
|
|
227
|
-
);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return wasHead;
|
|
231
|
-
// this._headsCache = undefined; // TODO do smarter things here, only remove the element needed (?)
|
|
232
|
-
}
|
|
233
|
-
}
|
package/src/types.ts
DELETED
package/src/values.ts
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
import { Entry, ShallowEntry } from "./entry";
|
|
2
|
-
import { ISortFunction } from "./log-sorting";
|
|
3
|
-
import yallist from "yallist";
|
|
4
|
-
import { EntryIndex } from "./entry-index";
|
|
5
|
-
|
|
6
|
-
export type EntryNode = yallist.Node<string>;
|
|
7
|
-
|
|
8
|
-
export class Values<T> {
|
|
9
|
-
/**
|
|
10
|
-
* Keep track of sorted elements in descending sort order (i.e. newest elements)
|
|
11
|
-
*/
|
|
12
|
-
private _values: yallist<string>;
|
|
13
|
-
private _sortFn: ISortFunction;
|
|
14
|
-
private _byteLength: number;
|
|
15
|
-
private _entryIndex: EntryIndex<T>;
|
|
16
|
-
|
|
17
|
-
constructor(
|
|
18
|
-
entryIndex: EntryIndex<T>,
|
|
19
|
-
sortFn: ISortFunction,
|
|
20
|
-
entries: Entry<T>[] = []
|
|
21
|
-
) {
|
|
22
|
-
this._values = yallist.create(
|
|
23
|
-
entries
|
|
24
|
-
.slice()
|
|
25
|
-
.sort(sortFn)
|
|
26
|
-
.reverse()
|
|
27
|
-
.map((x) => {
|
|
28
|
-
if (!x.hash) throw new Error("Unexpected");
|
|
29
|
-
return x.hash; /* {
|
|
30
|
-
hash: x.hash,
|
|
31
|
-
byteLength: x._payload.byteLength,
|
|
32
|
-
meta: {
|
|
33
|
-
gids: x.gids,
|
|
34
|
-
gid: x.gid,
|
|
35
|
-
data: x.meta.data,
|
|
36
|
-
},
|
|
37
|
-
}; */
|
|
38
|
-
})
|
|
39
|
-
);
|
|
40
|
-
this._byteLength = 0;
|
|
41
|
-
entries.forEach((entry) => {
|
|
42
|
-
this._byteLength += entry._payload.byteLength;
|
|
43
|
-
});
|
|
44
|
-
this._sortFn = sortFn;
|
|
45
|
-
this._entryIndex = entryIndex;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
toArray(): Promise<Entry<T>[]> {
|
|
49
|
-
return Promise.all(
|
|
50
|
-
this._values.toArrayReverse().map((x) => this._entryIndex.get(x))
|
|
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
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
get head() {
|
|
55
|
-
return this._values.head;
|
|
56
|
-
}
|
|
57
|
-
get tail() {
|
|
58
|
-
return this._values.tail;
|
|
59
|
-
}
|
|
60
|
-
get length() {
|
|
61
|
-
return this._values.length;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
get entryIndex(): EntryIndex<T> {
|
|
65
|
-
return this._entryIndex;
|
|
66
|
-
}
|
|
67
|
-
put(value: Entry<T>) {
|
|
68
|
-
return this._put(value);
|
|
69
|
-
}
|
|
70
|
-
_put(value: Entry<T>) {
|
|
71
|
-
// assume we want to insert at head (or somehere close)
|
|
72
|
-
let walker = this._values.head;
|
|
73
|
-
let last: EntryNode | undefined = undefined;
|
|
74
|
-
while (walker) {
|
|
75
|
-
const walkerValue = this._entryIndex.getShallow(walker.value);
|
|
76
|
-
if (!walkerValue) {
|
|
77
|
-
throw new Error("Missing walker value");
|
|
78
|
-
}
|
|
79
|
-
if (walkerValue.hash === value.hash) {
|
|
80
|
-
return; // already exist!
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (this._sortFn(walkerValue, value) < 0) {
|
|
84
|
-
break;
|
|
85
|
-
}
|
|
86
|
-
last = walker;
|
|
87
|
-
walker = walker.next;
|
|
88
|
-
continue;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
this._byteLength += value._payload.byteLength;
|
|
92
|
-
if (!value.hash) {
|
|
93
|
-
throw new Error("Unexpected");
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
_insertAfter(this._values, last, value.hash);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
async delete(value: Entry<T> | string) {
|
|
100
|
-
const hash = typeof value === "string" ? value : value.hash;
|
|
101
|
-
// Assume we want to delete at tail (or somwhere close)
|
|
102
|
-
|
|
103
|
-
let walker = this._values.tail;
|
|
104
|
-
while (walker) {
|
|
105
|
-
const walkerValue = this._entryIndex.getShallow(walker.value);
|
|
106
|
-
|
|
107
|
-
if (!walkerValue) {
|
|
108
|
-
throw new Error("Missing walker value");
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if (walkerValue.hash === hash) {
|
|
112
|
-
this._values.removeNode(walker);
|
|
113
|
-
this._byteLength -= walkerValue.payloadByteLength;
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
walker = walker.prev; // prev will be undefined if you do removeNode(walker)
|
|
117
|
-
}
|
|
118
|
-
throw new Error(
|
|
119
|
-
"Failed to delete, entry does not exist" +
|
|
120
|
-
" ??? " +
|
|
121
|
-
this.length +
|
|
122
|
-
" ??? " +
|
|
123
|
-
hash
|
|
124
|
-
);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
deleteNode(node: EntryNode) {
|
|
128
|
-
this._values.removeNode(node);
|
|
129
|
-
this._byteLength -= this._entryIndex.getShallow(
|
|
130
|
-
node.value
|
|
131
|
-
)!.payloadByteLength;
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
pop() {
|
|
136
|
-
const value = this._values.pop();
|
|
137
|
-
if (value) {
|
|
138
|
-
this._byteLength -= this._entryIndex.getShallow(value)!.payloadByteLength;
|
|
139
|
-
}
|
|
140
|
-
return value;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
get byteLength() {
|
|
144
|
-
return this._byteLength;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
function _insertAfter(
|
|
149
|
-
self: yallist<any>,
|
|
150
|
-
node: EntryNode | undefined,
|
|
151
|
-
value: string
|
|
152
|
-
) {
|
|
153
|
-
const inserted = !node
|
|
154
|
-
? new yallist.Node(
|
|
155
|
-
value,
|
|
156
|
-
null as any,
|
|
157
|
-
self.head as EntryNode | undefined,
|
|
158
|
-
self
|
|
159
|
-
)
|
|
160
|
-
: new yallist.Node(value, node, node.next as EntryNode | undefined, self);
|
|
161
|
-
|
|
162
|
-
// is tail
|
|
163
|
-
if (inserted.next === null) {
|
|
164
|
-
self.tail = inserted;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// is head
|
|
168
|
-
if (inserted.prev === null) {
|
|
169
|
-
self.head = inserted;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
self.length++;
|
|
173
|
-
return inserted;
|
|
174
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|