@peerbit/log 1.0.2
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/LICENSE +23 -0
- package/README.md +11 -0
- package/lib/esm/change.d.ts +5 -0
- package/lib/esm/change.js +2 -0
- package/lib/esm/change.js.map +1 -0
- package/lib/esm/clock.d.ts +87 -0
- package/lib/esm/clock.js +260 -0
- package/lib/esm/clock.js.map +1 -0
- package/lib/esm/difference.d.ts +1 -0
- package/lib/esm/difference.js +20 -0
- package/lib/esm/difference.js.map +1 -0
- package/lib/esm/encoding.d.ts +7 -0
- package/lib/esm/encoding.js +20 -0
- package/lib/esm/encoding.js.map +1 -0
- package/lib/esm/entry-index.d.ts +21 -0
- package/lib/esm/entry-index.js +63 -0
- package/lib/esm/entry-index.js.map +1 -0
- package/lib/esm/entry-with-refs.d.ts +5 -0
- package/lib/esm/entry-with-refs.js +2 -0
- package/lib/esm/entry-with-refs.js.map +1 -0
- package/lib/esm/entry.d.ts +179 -0
- package/lib/esm/entry.js +591 -0
- package/lib/esm/entry.js.map +1 -0
- package/lib/esm/find-uniques.d.ts +1 -0
- package/lib/esm/find-uniques.js +12 -0
- package/lib/esm/find-uniques.js.map +1 -0
- package/lib/esm/heads-cache.d.ts +64 -0
- package/lib/esm/heads-cache.js +317 -0
- package/lib/esm/heads-cache.js.map +1 -0
- package/lib/esm/heads.d.ts +63 -0
- package/lib/esm/heads.js +143 -0
- package/lib/esm/heads.js.map +1 -0
- package/lib/esm/hrtime.d.ts +5 -0
- package/lib/esm/hrtime.js +71 -0
- package/lib/esm/hrtime.js.map +1 -0
- package/lib/esm/index.d.ts +11 -0
- package/lib/esm/index.js +11 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/is-defined.d.ts +1 -0
- package/lib/esm/is-defined.js +2 -0
- package/lib/esm/is-defined.js.map +1 -0
- package/lib/esm/log-errors.d.ts +5 -0
- package/lib/esm/log-errors.js +6 -0
- package/lib/esm/log-errors.js.map +1 -0
- package/lib/esm/log-sorting.d.ts +44 -0
- package/lib/esm/log-sorting.js +86 -0
- package/lib/esm/log-sorting.js.map +1 -0
- package/lib/esm/log.d.ts +205 -0
- package/lib/esm/log.js +1004 -0
- package/lib/esm/log.js.map +1 -0
- package/lib/esm/logger.d.ts +2 -0
- package/lib/esm/logger.js +4 -0
- package/lib/esm/logger.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/lib/esm/snapshot.d.ts +22 -0
- package/lib/esm/snapshot.js +83 -0
- package/lib/esm/snapshot.js.map +1 -0
- package/lib/esm/trim.d.ts +49 -0
- package/lib/esm/trim.js +203 -0
- package/lib/esm/trim.js.map +1 -0
- package/lib/esm/types.d.ts +6 -0
- package/lib/esm/types.js +23 -0
- package/lib/esm/types.js.map +1 -0
- package/lib/esm/utils.d.ts +2 -0
- package/lib/esm/utils.js +3 -0
- package/lib/esm/utils.js.map +1 -0
- package/lib/esm/values.d.ts +33 -0
- package/lib/esm/values.js +141 -0
- package/lib/esm/values.js.map +1 -0
- package/package.json +79 -0
- package/src/change.ts +2 -0
- package/src/clock.ts +280 -0
- package/src/difference.ts +22 -0
- package/src/encoding.ts +27 -0
- package/src/entry-index.ts +78 -0
- package/src/entry-with-refs.ts +6 -0
- package/src/entry.ts +749 -0
- package/src/find-uniques.ts +14 -0
- package/src/heads-cache.ts +400 -0
- package/src/heads.ts +208 -0
- package/src/hrtime.ts +78 -0
- package/src/index.ts +11 -0
- package/src/is-defined.ts +1 -0
- package/src/log-errors.ts +9 -0
- package/src/log-sorting.ts +108 -0
- package/src/log.ts +1262 -0
- package/src/logger.ts +3 -0
- package/src/snapshot.ts +103 -0
- package/src/trim.ts +269 -0
- package/src/types.ts +12 -0
- package/src/utils.ts +2 -0
- package/src/values.ts +193 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Entry } from "./entry";
|
|
2
|
+
import { SimpleLevel } from "@peerbit/lazy-level";
|
|
3
|
+
export declare const logger: import("pino").Logger<import("pino").LoggerOptions | import("pino").DestinationStream>;
|
|
4
|
+
export declare class CachedValue {
|
|
5
|
+
}
|
|
6
|
+
export declare class CachePath {
|
|
7
|
+
path: string;
|
|
8
|
+
constructor(path: string);
|
|
9
|
+
}
|
|
10
|
+
export declare class UnsfinishedReplication {
|
|
11
|
+
hashes: string[];
|
|
12
|
+
constructor(opts?: {
|
|
13
|
+
hashes: string[];
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
export declare class HeadsCacheToSerialize {
|
|
17
|
+
heads: string[];
|
|
18
|
+
last?: string;
|
|
19
|
+
counter: bigint;
|
|
20
|
+
constructor(heads: string[], counter: bigint, last?: string);
|
|
21
|
+
}
|
|
22
|
+
interface HeadsIndex {
|
|
23
|
+
id: Uint8Array;
|
|
24
|
+
size: number;
|
|
25
|
+
has(cid: string): boolean;
|
|
26
|
+
}
|
|
27
|
+
export declare class HeadsCache<T> {
|
|
28
|
+
headsPath: string;
|
|
29
|
+
removedHeadsPath: string;
|
|
30
|
+
initialized: boolean;
|
|
31
|
+
private _headsPathCounter;
|
|
32
|
+
private _lastHeadsPath?;
|
|
33
|
+
private _lastHeadsCount;
|
|
34
|
+
private _lastRemovedHeadsPath?;
|
|
35
|
+
private _lastRemovedHeadsCount;
|
|
36
|
+
private _cache?;
|
|
37
|
+
private _cacheWriteQueue?;
|
|
38
|
+
private _loaded;
|
|
39
|
+
private _index;
|
|
40
|
+
constructor(index: HeadsIndex);
|
|
41
|
+
get cache(): SimpleLevel | undefined;
|
|
42
|
+
get headsPathCounter(): number;
|
|
43
|
+
init(cache?: SimpleLevel): Promise<this>;
|
|
44
|
+
get loaded(): boolean;
|
|
45
|
+
private _updateCachedHeads;
|
|
46
|
+
idle(): Promise<void>;
|
|
47
|
+
getCachedHeads(lastHeadsPath?: string | undefined, lastRemovedHeadsPath?: string | undefined): Promise<string[]>;
|
|
48
|
+
get closed(): boolean;
|
|
49
|
+
close(): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Drops a database and removes local data
|
|
52
|
+
*/
|
|
53
|
+
drop(): Promise<void>;
|
|
54
|
+
private _loadHeads;
|
|
55
|
+
load(): Promise<string[]>;
|
|
56
|
+
loadLastHeadsPath(): Promise<void>;
|
|
57
|
+
getCachedHeadsCount(headPath?: string): Promise<bigint>;
|
|
58
|
+
waitForHeads(): Promise<void | string[]>;
|
|
59
|
+
queue(changes: {
|
|
60
|
+
added?: (Entry<T> | string)[];
|
|
61
|
+
removed?: (Entry<T> | string)[];
|
|
62
|
+
}, reset?: boolean): Promise<void> | undefined;
|
|
63
|
+
}
|
|
64
|
+
export {};
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import PQueue from "p-queue";
|
|
11
|
+
import { v4 as uuid } from "uuid";
|
|
12
|
+
import { variant, option, field, vec } from "@dao-xyz/borsh";
|
|
13
|
+
import { serialize, deserialize } from "@dao-xyz/borsh";
|
|
14
|
+
import { logger as loggerFn } from "@peerbit/logger";
|
|
15
|
+
import path from "path-browserify";
|
|
16
|
+
export const logger = loggerFn({ module: "heads-cache" });
|
|
17
|
+
export class CachedValue {
|
|
18
|
+
}
|
|
19
|
+
/* export type AppendOptions<T> = {
|
|
20
|
+
signers?: ((data: Uint8Array) => Promise<SignatureWithKey>)[];
|
|
21
|
+
nexts?: Entry<T>[];
|
|
22
|
+
reciever?: EncryptionTemplateMaybeEncrypted;
|
|
23
|
+
type?: EntryType;
|
|
24
|
+
};
|
|
25
|
+
*/
|
|
26
|
+
export let CachePath = class CachePath {
|
|
27
|
+
path;
|
|
28
|
+
constructor(path) {
|
|
29
|
+
this.path = path;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
__decorate([
|
|
33
|
+
field({ type: "string" }),
|
|
34
|
+
__metadata("design:type", String)
|
|
35
|
+
], CachePath.prototype, "path", void 0);
|
|
36
|
+
CachePath = __decorate([
|
|
37
|
+
variant(0),
|
|
38
|
+
__metadata("design:paramtypes", [String])
|
|
39
|
+
], CachePath);
|
|
40
|
+
export let UnsfinishedReplication = class UnsfinishedReplication {
|
|
41
|
+
hashes;
|
|
42
|
+
constructor(opts) {
|
|
43
|
+
if (opts) {
|
|
44
|
+
this.hashes = opts.hashes;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
__decorate([
|
|
49
|
+
field({ type: vec("string") }),
|
|
50
|
+
__metadata("design:type", Array)
|
|
51
|
+
], UnsfinishedReplication.prototype, "hashes", void 0);
|
|
52
|
+
UnsfinishedReplication = __decorate([
|
|
53
|
+
variant(0),
|
|
54
|
+
__metadata("design:paramtypes", [Object])
|
|
55
|
+
], UnsfinishedReplication);
|
|
56
|
+
export let HeadsCacheToSerialize = class HeadsCacheToSerialize {
|
|
57
|
+
heads;
|
|
58
|
+
last;
|
|
59
|
+
counter;
|
|
60
|
+
constructor(heads, counter, last) {
|
|
61
|
+
this.heads = heads;
|
|
62
|
+
this.last = last;
|
|
63
|
+
this.counter = counter;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
__decorate([
|
|
67
|
+
field({ type: vec("string") }),
|
|
68
|
+
__metadata("design:type", Array)
|
|
69
|
+
], HeadsCacheToSerialize.prototype, "heads", void 0);
|
|
70
|
+
__decorate([
|
|
71
|
+
field({ type: option("string") }),
|
|
72
|
+
__metadata("design:type", String)
|
|
73
|
+
], HeadsCacheToSerialize.prototype, "last", void 0);
|
|
74
|
+
__decorate([
|
|
75
|
+
field({ type: "u64" }),
|
|
76
|
+
__metadata("design:type", BigInt)
|
|
77
|
+
], HeadsCacheToSerialize.prototype, "counter", void 0);
|
|
78
|
+
HeadsCacheToSerialize = __decorate([
|
|
79
|
+
variant(0),
|
|
80
|
+
__metadata("design:paramtypes", [Array, BigInt, String])
|
|
81
|
+
], HeadsCacheToSerialize);
|
|
82
|
+
const updateHashes = async (headCache, headsPath, lastCid, lastCounter, hashes) => {
|
|
83
|
+
const newHeadsPath = path.join(headsPath, String(headCache.headsPathCounter), uuid());
|
|
84
|
+
const counter = lastCounter + BigInt(hashes.length);
|
|
85
|
+
await Promise.all([
|
|
86
|
+
headCache.cache?.put(headsPath, serialize(new CachePath(newHeadsPath.toString()))),
|
|
87
|
+
headCache.cache?.put(newHeadsPath, serialize(new HeadsCacheToSerialize(hashes, counter, lastCid))),
|
|
88
|
+
]);
|
|
89
|
+
return { counter, newPath: newHeadsPath };
|
|
90
|
+
};
|
|
91
|
+
export class HeadsCache {
|
|
92
|
+
// An access controller that is note part of the store manifest, usefull for circular store -> access controller -> store structures
|
|
93
|
+
headsPath;
|
|
94
|
+
removedHeadsPath;
|
|
95
|
+
initialized;
|
|
96
|
+
_headsPathCounter = 0;
|
|
97
|
+
_lastHeadsPath;
|
|
98
|
+
_lastHeadsCount = 0n;
|
|
99
|
+
_lastRemovedHeadsPath;
|
|
100
|
+
_lastRemovedHeadsCount = 0n;
|
|
101
|
+
_cache;
|
|
102
|
+
_cacheWriteQueue;
|
|
103
|
+
_loaded = false;
|
|
104
|
+
_index;
|
|
105
|
+
constructor(index) {
|
|
106
|
+
this._index = index;
|
|
107
|
+
}
|
|
108
|
+
get cache() {
|
|
109
|
+
return this._cache;
|
|
110
|
+
}
|
|
111
|
+
get headsPathCounter() {
|
|
112
|
+
return this._headsPathCounter;
|
|
113
|
+
}
|
|
114
|
+
async init(cache) {
|
|
115
|
+
if (this.initialized) {
|
|
116
|
+
throw new Error("Already initialized");
|
|
117
|
+
}
|
|
118
|
+
this._cache = cache;
|
|
119
|
+
// Set the options (we will use the topic property after thiis)
|
|
120
|
+
await this._cache?.open();
|
|
121
|
+
this.headsPath = "heads";
|
|
122
|
+
this.removedHeadsPath = "heads_removed";
|
|
123
|
+
await this.loadLastHeadsPath();
|
|
124
|
+
// append and log-joins queue. Adding ops and joins to the queue
|
|
125
|
+
// makes sure they get processed sequentially to avoid race conditions
|
|
126
|
+
this._cacheWriteQueue = new PQueue({ concurrency: 1 });
|
|
127
|
+
/* if (this._options.onOpen) {
|
|
128
|
+
await this._options.onOpen();
|
|
129
|
+
} */
|
|
130
|
+
this.initialized = true;
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
get loaded() {
|
|
134
|
+
return this._loaded;
|
|
135
|
+
}
|
|
136
|
+
async _updateCachedHeads(change, reset) {
|
|
137
|
+
if (typeof reset !== "boolean" && change.added) {
|
|
138
|
+
// Only reset all heads if loaded once, since we don't want too loose track of unloaded heads
|
|
139
|
+
if (this._loaded && this._index.size <= change.added.length) {
|
|
140
|
+
let addedIsAllHeads = true;
|
|
141
|
+
for (const entry of change.added) {
|
|
142
|
+
const hash = typeof entry === "string" ? entry : entry.hash;
|
|
143
|
+
if (!this._index.has(hash)) {
|
|
144
|
+
addedIsAllHeads = false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
reset = addedIsAllHeads;
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
// added size < head size, meaning we have not rewritten all heads
|
|
151
|
+
reset = false;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// If 'reset' then dont keep references to old heads caches, assume new cache will fully describe all heads
|
|
155
|
+
// TODO dont delete old before saving new
|
|
156
|
+
if (reset) {
|
|
157
|
+
await this.cache?.clear();
|
|
158
|
+
this._lastHeadsPath = undefined;
|
|
159
|
+
this._lastRemovedHeadsPath = undefined;
|
|
160
|
+
this._lastHeadsCount = 0n;
|
|
161
|
+
this._lastRemovedHeadsCount = 0n;
|
|
162
|
+
this._headsPathCounter += 1;
|
|
163
|
+
}
|
|
164
|
+
if (change.added && change.added.length > 0) {
|
|
165
|
+
const update = await updateHashes(this, this.headsPath, this._lastHeadsPath, this._lastHeadsCount, change.added.map((x) => (typeof x === "string" ? x : x.hash)));
|
|
166
|
+
this._lastHeadsPath = update.newPath;
|
|
167
|
+
this._lastHeadsCount = update.counter;
|
|
168
|
+
}
|
|
169
|
+
if (this._lastHeadsPath) {
|
|
170
|
+
// only add removed heads if we actually have added heads, else these are pointless
|
|
171
|
+
if (change.removed && change.removed.length > 0) {
|
|
172
|
+
const update = await updateHashes(this, this.removedHeadsPath, this._lastRemovedHeadsPath, this._lastRemovedHeadsCount, change.removed.map((x) => (typeof x === "string" ? x : x.hash)));
|
|
173
|
+
this._lastRemovedHeadsPath = update.newPath;
|
|
174
|
+
this._lastRemovedHeadsCount = update.counter;
|
|
175
|
+
if (update.counter > 0n &&
|
|
176
|
+
2n * update.counter >= this._lastHeadsCount) {
|
|
177
|
+
const resetToHeads = await this.getCachedHeads(this._lastHeadsPath, this._lastRemovedHeadsPath);
|
|
178
|
+
await this._updateCachedHeads({ added: resetToHeads, removed: [] }, true);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
async idle() {
|
|
184
|
+
// Wait for the operations queue to finish processing
|
|
185
|
+
// to make sure everything that all operations that have
|
|
186
|
+
// been queued will be written to disk
|
|
187
|
+
await this._cacheWriteQueue?.onIdle();
|
|
188
|
+
await this._cache?.idle?.();
|
|
189
|
+
}
|
|
190
|
+
async getCachedHeads(lastHeadsPath = this._lastHeadsPath, lastRemovedHeadsPath = this._lastRemovedHeadsPath) {
|
|
191
|
+
if (!this._cache) {
|
|
192
|
+
return [];
|
|
193
|
+
}
|
|
194
|
+
const getHashes = async (start, filter) => {
|
|
195
|
+
const result = [];
|
|
196
|
+
let next = start;
|
|
197
|
+
while (next) {
|
|
198
|
+
const cache = await this._cache
|
|
199
|
+
?.get(next)
|
|
200
|
+
.then((bytes) => bytes && deserialize(bytes, HeadsCacheToSerialize));
|
|
201
|
+
next = cache?.last;
|
|
202
|
+
cache?.heads.forEach((head) => {
|
|
203
|
+
if (filter && filter.has(head)) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
result.push(head);
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
return result;
|
|
210
|
+
};
|
|
211
|
+
const removedHeads = new Set(await getHashes(lastRemovedHeadsPath));
|
|
212
|
+
const heads = await getHashes(lastHeadsPath, removedHeads);
|
|
213
|
+
return heads; // Saved heads - removed heads
|
|
214
|
+
}
|
|
215
|
+
/* get logOptions(): LogOptions<T> {
|
|
216
|
+
return {
|
|
217
|
+
logId: this.id,
|
|
218
|
+
trim: this._options.trim && {
|
|
219
|
+
// I can trim if I am not a replicator of an entry
|
|
220
|
+
|
|
221
|
+
...this._options.trim,
|
|
222
|
+
filter: this.options.replicator && {
|
|
223
|
+
canTrim: async (gid) => !(await this.options.replicator!(gid)),
|
|
224
|
+
cacheId: this.options.replicatorsCacheId,
|
|
225
|
+
},
|
|
226
|
+
},
|
|
227
|
+
};
|
|
228
|
+
} */
|
|
229
|
+
get closed() {
|
|
230
|
+
return !this._cache || this._cache.status === "closed";
|
|
231
|
+
}
|
|
232
|
+
async close() {
|
|
233
|
+
await this.idle();
|
|
234
|
+
await this._cache?.close();
|
|
235
|
+
this._loaded = false;
|
|
236
|
+
this._lastHeadsPath = undefined;
|
|
237
|
+
this._lastRemovedHeadsPath = undefined;
|
|
238
|
+
this._lastRemovedHeadsCount = 0n;
|
|
239
|
+
this._lastHeadsCount = 0n;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Drops a database and removes local data
|
|
243
|
+
*/
|
|
244
|
+
async drop() {
|
|
245
|
+
this.initialized = false;
|
|
246
|
+
if (!this._cache) {
|
|
247
|
+
return; // already dropped
|
|
248
|
+
}
|
|
249
|
+
if (this._cache.status !== "open") {
|
|
250
|
+
await this._cache.open();
|
|
251
|
+
}
|
|
252
|
+
await this._cache.del(this.headsPath);
|
|
253
|
+
await this._cache.del(this.removedHeadsPath);
|
|
254
|
+
await this.close();
|
|
255
|
+
delete this._cache;
|
|
256
|
+
}
|
|
257
|
+
async _loadHeads() {
|
|
258
|
+
if (!this.initialized) {
|
|
259
|
+
throw new Error("Store needs to be initialized before loaded");
|
|
260
|
+
}
|
|
261
|
+
if (this._cache.status !== "open") {
|
|
262
|
+
await this._cache.open();
|
|
263
|
+
}
|
|
264
|
+
await this.loadLastHeadsPath();
|
|
265
|
+
return this.getCachedHeads();
|
|
266
|
+
}
|
|
267
|
+
async load() {
|
|
268
|
+
if (!this.initialized) {
|
|
269
|
+
throw new Error("Needs to be initialized before loaded");
|
|
270
|
+
}
|
|
271
|
+
if (this._cache.status !== "open") {
|
|
272
|
+
await this._cache.open();
|
|
273
|
+
}
|
|
274
|
+
const heads = await this._loadHeads();
|
|
275
|
+
this._loaded = true;
|
|
276
|
+
return heads;
|
|
277
|
+
}
|
|
278
|
+
async loadLastHeadsPath() {
|
|
279
|
+
this._lastHeadsPath = await this._cache
|
|
280
|
+
?.get(this.headsPath)
|
|
281
|
+
.then((bytes) => bytes && deserialize(bytes, CachePath).path);
|
|
282
|
+
this._lastRemovedHeadsPath = await this._cache
|
|
283
|
+
?.get(this.removedHeadsPath)
|
|
284
|
+
.then((bytes) => bytes && deserialize(bytes, CachePath).path);
|
|
285
|
+
this._lastHeadsCount = this._lastHeadsPath
|
|
286
|
+
? await this.getCachedHeadsCount(this._lastHeadsPath)
|
|
287
|
+
: 0n;
|
|
288
|
+
this._lastRemovedHeadsCount = this._lastRemovedHeadsPath
|
|
289
|
+
? await this.getCachedHeadsCount(this._lastRemovedHeadsPath)
|
|
290
|
+
: 0n;
|
|
291
|
+
}
|
|
292
|
+
async getCachedHeadsCount(headPath) {
|
|
293
|
+
if (!headPath) {
|
|
294
|
+
return 0n;
|
|
295
|
+
}
|
|
296
|
+
return ((await this._cache
|
|
297
|
+
?.get(headPath)
|
|
298
|
+
.then((bytes) => bytes && deserialize(bytes, HeadsCacheToSerialize)))?.counter || 0n);
|
|
299
|
+
}
|
|
300
|
+
async waitForHeads() {
|
|
301
|
+
if (this.closed) {
|
|
302
|
+
throw new Error("Store is closed");
|
|
303
|
+
}
|
|
304
|
+
if (!this._loaded) {
|
|
305
|
+
return this._cacheWriteQueue?.add(async () => {
|
|
306
|
+
if (this._loaded) {
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
return this.load();
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
queue(changes, reset) {
|
|
314
|
+
return this._cacheWriteQueue?.add(() => this._updateCachedHeads(changes, reset));
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
//# sourceMappingURL=heads-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"heads-cache.js","sourceRoot":"","sources":["../../src/heads-cache.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,MAAM,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAC;AAGlC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,MAAM,IAAI,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,IAAI,MAAM,iBAAiB,CAAC;AACnC,MAAM,CAAC,MAAM,MAAM,GAAG,QAAQ,CAAC,EAAE,MAAM,EAAE,aAAa,EAAE,CAAC,CAAC;AAC1D,MAAM,OAAO,WAAW;CAAG;AAC3B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gBAAgB,EAAE,GAAG,CAAC,KAAK,IAAI,EAAE;gBAC5C,IAAI,IAAI,CAAC,OAAO,EAAE;oBACjB,OAAO;iBACP;gBACD,OAAO,IAAI,CAAC,IAAI,EAAE,CAAC;YACpB,CAAC,CAAC,CAAC;SACH;IACF,CAAC;IAEM,KAAK,CACX,OAGC,EACD,KAAe;QAEf,OAAO,IAAI,CAAC,gBAAgB,EAAE,GAAG,CAAC,GAAG,EAAE,CACtC,IAAI,CAAC,kBAAkB,CAAC,OAAO,EAAE,KAAK,CAAC,CACvC,CAAC;IACH,CAAC;CACD"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Entry } from "./entry.js";
|
|
2
|
+
import { SimpleLevel } from "@peerbit/lazy-level";
|
|
3
|
+
import { HeadsCache } from "./heads-cache.js";
|
|
4
|
+
import { Blocks } from "@peerbit/blocks-interface";
|
|
5
|
+
import { Keychain } from "@peerbit/crypto";
|
|
6
|
+
import { Encoding } from "./encoding.js";
|
|
7
|
+
export type CacheUpdateOptions = {
|
|
8
|
+
cache?: {
|
|
9
|
+
update?: false;
|
|
10
|
+
reset?: false;
|
|
11
|
+
} | {
|
|
12
|
+
update: true;
|
|
13
|
+
reset?: boolean;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
interface Config {
|
|
17
|
+
storage: Blocks;
|
|
18
|
+
keychain?: Keychain;
|
|
19
|
+
memory?: SimpleLevel;
|
|
20
|
+
encoding: Encoding<any>;
|
|
21
|
+
}
|
|
22
|
+
export declare class HeadsIndex<T> {
|
|
23
|
+
private _id;
|
|
24
|
+
private _index;
|
|
25
|
+
private _gids;
|
|
26
|
+
private _headsCache;
|
|
27
|
+
private _config;
|
|
28
|
+
constructor(id: Uint8Array);
|
|
29
|
+
init(config: Config, options?: {
|
|
30
|
+
entries?: Entry<T>[];
|
|
31
|
+
}): Promise<HeadsCache<T> | undefined>;
|
|
32
|
+
load(options?: {
|
|
33
|
+
timeout?: number;
|
|
34
|
+
replicate?: boolean;
|
|
35
|
+
reload?: boolean;
|
|
36
|
+
} & CacheUpdateOptions): Promise<Entry<T>[] | undefined>;
|
|
37
|
+
get headsCache(): HeadsCache<T> | undefined;
|
|
38
|
+
close(): Promise<void> | undefined;
|
|
39
|
+
drop(): Promise<void> | undefined;
|
|
40
|
+
get id(): Uint8Array;
|
|
41
|
+
get index(): Set<string>;
|
|
42
|
+
get gids(): Map<string, number>;
|
|
43
|
+
get size(): number;
|
|
44
|
+
reset(entries: Entry<T>[], options?: CacheUpdateOptions): Promise<void>;
|
|
45
|
+
has(cid: string): boolean;
|
|
46
|
+
put(entry: Entry<T>, options?: CacheUpdateOptions): Promise<void>;
|
|
47
|
+
putAll(entries: Entry<T>[], options?: CacheUpdateOptions): Promise<void>;
|
|
48
|
+
resetHeadsCache(): Promise<void>;
|
|
49
|
+
updateHeadsCache(change?: {
|
|
50
|
+
added?: (Entry<T> | string)[];
|
|
51
|
+
removed?: (Entry<T> | string)[];
|
|
52
|
+
}, reset?: boolean): Promise<void>;
|
|
53
|
+
private _putOne;
|
|
54
|
+
private _putAll;
|
|
55
|
+
del(entry: {
|
|
56
|
+
hash: string;
|
|
57
|
+
gid: string;
|
|
58
|
+
}, options?: CacheUpdateOptions): Promise<{
|
|
59
|
+
removed: boolean;
|
|
60
|
+
lastWithGid: boolean;
|
|
61
|
+
}>;
|
|
62
|
+
}
|
|
63
|
+
export {};
|
package/lib/esm/heads.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Entry } from "./entry.js";
|
|
2
|
+
import { HeadsCache } from "./heads-cache.js";
|
|
3
|
+
export class HeadsIndex {
|
|
4
|
+
_id;
|
|
5
|
+
_index = new Set();
|
|
6
|
+
_gids;
|
|
7
|
+
_headsCache;
|
|
8
|
+
_config;
|
|
9
|
+
constructor(id) {
|
|
10
|
+
this._gids = new Map();
|
|
11
|
+
this._id = id;
|
|
12
|
+
}
|
|
13
|
+
async init(config, options = {}) {
|
|
14
|
+
this._config = config;
|
|
15
|
+
await this.reset(options?.entries || []);
|
|
16
|
+
if (config.memory) {
|
|
17
|
+
this._headsCache = new HeadsCache(this);
|
|
18
|
+
return this._headsCache.init(await config.memory.sublevel("heads"));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
async load(options) {
|
|
22
|
+
if (!this._headsCache || (this._headsCache.loaded && !options?.reload)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
// TODO make below into a promise that concurrenct caklls can wait on?
|
|
26
|
+
const heads = await this._headsCache?.load();
|
|
27
|
+
if (!heads) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
const entries = await Promise.all(heads.map(async (x) => {
|
|
32
|
+
const entry = await Entry.fromMultihash(this._config.storage, x, options);
|
|
33
|
+
entry.init(this._config);
|
|
34
|
+
await entry.getGid(); // decrypt gid
|
|
35
|
+
return entry;
|
|
36
|
+
}));
|
|
37
|
+
await this.reset(entries);
|
|
38
|
+
return entries;
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
const q = 123;
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
get headsCache() {
|
|
46
|
+
return this._headsCache;
|
|
47
|
+
}
|
|
48
|
+
close() {
|
|
49
|
+
return this._headsCache?.close();
|
|
50
|
+
}
|
|
51
|
+
drop() {
|
|
52
|
+
return this._headsCache?.drop();
|
|
53
|
+
}
|
|
54
|
+
get id() {
|
|
55
|
+
return this._id;
|
|
56
|
+
}
|
|
57
|
+
get index() {
|
|
58
|
+
return this._index;
|
|
59
|
+
}
|
|
60
|
+
get gids() {
|
|
61
|
+
return this._gids;
|
|
62
|
+
}
|
|
63
|
+
get size() {
|
|
64
|
+
return this._index.size;
|
|
65
|
+
}
|
|
66
|
+
async reset(entries, options = { cache: { reset: true, update: true } }) {
|
|
67
|
+
this._index.clear();
|
|
68
|
+
this._gids = new Map();
|
|
69
|
+
if (entries) {
|
|
70
|
+
await this.putAll(entries, options); // reset cache = true
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
has(cid) {
|
|
74
|
+
return this._index.has(cid);
|
|
75
|
+
}
|
|
76
|
+
async put(entry, options) {
|
|
77
|
+
this._putOne(entry);
|
|
78
|
+
if (options?.cache?.update) {
|
|
79
|
+
await this._headsCache?.queue({ added: [entry] }, options.cache.reset);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async putAll(entries, options) {
|
|
83
|
+
this._putAll(entries);
|
|
84
|
+
if (options?.cache?.update) {
|
|
85
|
+
await this._headsCache?.queue({ added: entries }, options.cache.reset);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async resetHeadsCache() {
|
|
89
|
+
await this._headsCache?.queue({ added: [...this._index], removed: [] }, true);
|
|
90
|
+
}
|
|
91
|
+
async updateHeadsCache(change = {}, reset) {
|
|
92
|
+
await this._headsCache?.queue(change, reset);
|
|
93
|
+
}
|
|
94
|
+
_putOne(entry) {
|
|
95
|
+
if (!entry.hash) {
|
|
96
|
+
throw new Error("Missing hash");
|
|
97
|
+
}
|
|
98
|
+
if (this._index.has(entry.hash)) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
this._index.add(entry.hash);
|
|
102
|
+
if (!this._gids.has(entry.gid)) {
|
|
103
|
+
this._gids.set(entry.gid, 1);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
this._gids.set(entry.gid, this._gids.get(entry.gid) + 1);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
_putAll(entries) {
|
|
110
|
+
for (const entry of entries) {
|
|
111
|
+
this._putOne(entry);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
async del(entry, options) {
|
|
115
|
+
const wasHead = this._index.delete(entry.hash);
|
|
116
|
+
if (!wasHead) {
|
|
117
|
+
return {
|
|
118
|
+
lastWithGid: false,
|
|
119
|
+
removed: false,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
const newValue = this._gids.get(entry.gid) - 1;
|
|
123
|
+
const lastWithGid = newValue <= 0;
|
|
124
|
+
if (newValue <= 0) {
|
|
125
|
+
this._gids.delete(entry.gid);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
this._gids.set(entry.gid, newValue);
|
|
129
|
+
}
|
|
130
|
+
if (!entry.hash) {
|
|
131
|
+
throw new Error("Missing hash");
|
|
132
|
+
}
|
|
133
|
+
if (wasHead && options?.cache?.update) {
|
|
134
|
+
await this._headsCache?.queue({ removed: [entry.hash] }, options.cache.reset);
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
removed: wasHead,
|
|
138
|
+
lastWithGid: lastWithGid,
|
|
139
|
+
};
|
|
140
|
+
// this._headsCache = undefined; // TODO do smarter things here, only remove the element needed (?)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=heads.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"heads.js","sourceRoot":"","sources":["../../src/heads.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAe9C,MAAM,OAAO,UAAU;IACd,GAAG,CAAa;IAChB,MAAM,GAAgB,IAAI,GAAG,EAAE,CAAC;IAChC,KAAK,CAAsB;IAC3B,WAAW,CAA4B;IACvC,OAAO,CAAS;IACxB,YAAY,EAAc;QACzB,IAAI,CAAC,KAAK,GAAG,IAAI,GAAG,EAAE,CAAC;QACvB,IAAI,CAAC,GAAG,GAAG,EAAE,CAAC;IACf,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,MAAc,EAAE,UAAoC,EAAE;QAChE,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,OAAO,IAAI,EAAE,CAAC,CAAC;QACzC,IAAI,MAAM,CAAC,MAAM,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