deno.land / x / replicache@v10.0.0-beta.0 / dag / store-impl.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import type * as kv from '../kv/mod';import {Store, Read, Write, mustGetChunk} from './store';import { assertMeta, Chunk, createChunkWithHash, createChunk, ChunkHasher,} from './chunk';import {chunkDataKey, chunkMetaKey, headKey, chunkRefCountKey} from './key';import {assertHash, Hash} from '../hash';import {assertNumber} from '../asserts';import type {ReadonlyJSONValue} from '../json';import {computeRefCountUpdates} from './gc';
export class StoreImpl implements Store { private readonly _kv: kv.Store; private readonly _chunkHasher: ChunkHasher; private readonly _assertValidHash: (hash: Hash) => void;
constructor( kv: kv.Store, chunkHasher: ChunkHasher, assertValidHash: (hash: Hash) => void, ) { this._kv = kv; this._chunkHasher = chunkHasher; this._assertValidHash = assertValidHash; }
async read(): Promise<Read> { return new ReadImpl(await this._kv.read(), this._assertValidHash); }
async withRead<R>(fn: (read: Read) => R | Promise<R>): Promise<R> { return this._kv.withRead(kvr => fn(new ReadImpl(kvr, this._assertValidHash)), ); }
async write(): Promise<Write> { return new WriteImpl( await this._kv.write(), this._chunkHasher, this._assertValidHash, ); }
async withWrite<R>(fn: (Write: Write) => R | Promise<R>): Promise<R> { return this._kv.withWrite(kvw => fn(new WriteImpl(kvw, this._chunkHasher, this._assertValidHash)), ); }
async close(): Promise<void> { await this._kv.close(); }}
export class ReadImpl implements Read { protected readonly _tx: kv.Read; readonly assertValidHash: (hash: Hash) => void;
constructor(kv: kv.Read, assertValidHash: (hash: Hash) => void) { this._tx = kv; this.assertValidHash = assertValidHash; }
async hasChunk(hash: Hash): Promise<boolean> { return await this._tx.has(chunkDataKey(hash)); }
async getChunk(hash: Hash): Promise<Chunk | undefined> { const data = await this._tx.get(chunkDataKey(hash)); if (data === undefined) { return undefined; }
const refsVal = await this._tx.get(chunkMetaKey(hash)); let refs: readonly Hash[]; if (refsVal !== undefined) { assertMeta(refsVal); refs = refsVal; } else { refs = []; } return createChunkWithHash(hash, data, refs); }
async mustGetChunk(hash: Hash): Promise<Chunk> { return mustGetChunk(this, hash); }
async getHead(name: string): Promise<Hash | undefined> { const data = await this._tx.get(headKey(name)); if (data === undefined) { return undefined; } assertHash(data); return data; }
close(): void { this._tx.release(); }
get closed(): boolean { return this._tx.closed; }}
type HeadChange = { new: Hash | undefined; old: Hash | undefined;};
export class WriteImpl extends ReadImpl implements Write { protected declare readonly _tx: kv.Write; private readonly _chunkHasher: ChunkHasher;
private readonly _putChunks = new Set<Hash>(); private readonly _changedHeads = new Map<string, HeadChange>();
constructor( kvw: kv.Write, chunkHasher: ChunkHasher, assertValidHash: (hash: Hash) => void, ) { super(kvw, assertValidHash); this._chunkHasher = chunkHasher; }
createChunk = <V extends ReadonlyJSONValue>( data: V, refs: readonly Hash[], ): Chunk<V> => createChunk(data, refs, this._chunkHasher);
get kvWrite(): kv.Write { return this._tx; }
async putChunk(c: Chunk): Promise<void> { const {hash, data, meta} = c; // We never want to write temp hashes to the underlying store. this.assertValidHash(hash); const key = chunkDataKey(hash); const p1 = this._tx.put(key, data); let p2; if (meta.length > 0) { for (const h of meta) { this.assertValidHash(h); } p2 = this._tx.put(chunkMetaKey(hash), meta); } this._putChunks.add(hash); await p1; await p2; }
setHead(name: string, hash: Hash): Promise<void> { return this._setHead(name, hash); }
removeHead(name: string): Promise<void> { return this._setHead(name, undefined); }
private async _setHead(name: string, hash: Hash | undefined): Promise<void> { const oldHash = await this.getHead(name); const hk = headKey(name);
let p1: Promise<void>; if (hash === undefined) { p1 = this._tx.del(hk); } else { p1 = this._tx.put(hk, hash); }
const v = this._changedHeads.get(name); if (v === undefined) { this._changedHeads.set(name, {new: hash, old: oldHash}); } else { // Keep old if existing v.new = hash; }
await p1; }
async commit(): Promise<void> { const refCountUpdates = await computeRefCountUpdates( this._changedHeads.values(), this._putChunks, this, ); await this._applyRefCountUpdates(refCountUpdates); await this._tx.commit(); }
async getRefCount(hash: Hash): Promise<number | undefined> { const value = await this._tx.get(chunkRefCountKey(hash)); if (value === undefined) { return undefined; } assertNumber(value); if (value < 0 || value > 0xffff || value !== (value | 0)) { throw new Error( `Invalid ref count ${value}. We expect the value to be a Uint16`, ); } return value; }
async getRefs(hash: Hash): Promise<readonly Hash[] | undefined> { const meta = await this._tx.get(chunkMetaKey(hash)); if (meta !== undefined) { assertMeta(meta); } return meta; }
private async _applyRefCountUpdates( refCountCache: Map<Hash, number>, ): Promise<void> { const ps: Promise<void>[] = []; refCountCache.forEach((count, hash) => { if (count === 0) { ps.push(this._removeAllRelatedKeys(hash)); } else { const refCountKey = chunkRefCountKey(hash); ps.push(this._tx.put(refCountKey, count)); } }); await Promise.all(ps); }
private async _removeAllRelatedKeys(hash: Hash): Promise<void> { await Promise.all([ this._tx.del(chunkDataKey(hash)), this._tx.del(chunkMetaKey(hash)), this._tx.del(chunkRefCountKey(hash)), ]);
this._putChunks.delete(hash); }
close(): void { this._tx.release(); }}
replicache

Version Info

Tagged at
2 years ago