deno.land / x / replicache@v10.0.0-beta.0 / db / read.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
import {IndexRead} from './index';import type * as dag from '../dag/mod';import { Commit, DEFAULT_HEAD_NAME, fromHash as commitFromHash, Meta,} from './commit';import type {ReadonlyJSONValue} from '../json';import {BTreeRead, BTreeWrite} from '../btree/mod';import type {Hash} from '../hash';
export class Read { private readonly _dagRead: dag.Read; map: BTreeRead; readonly indexes: Map<string, IndexRead>; shouldDeepClone = false;
constructor( dagRead: dag.Read, map: BTreeRead, indexes: Map<string, IndexRead>, ) { this._dagRead = dagRead; this.map = map; this.indexes = indexes; }
has(key: string): Promise<boolean> { return this.map.has(key); }
get(key: string): Promise<ReadonlyJSONValue | undefined> { return this.map.get(key); }
isEmpty(): Promise<boolean> { return this.map.isEmpty(); }
async getMapForIndex(indexName: string): Promise<BTreeRead> { const idx = this.indexes.get(indexName); if (idx === undefined) { throw new Error(`Unknown index name: ${indexName}`); } return idx.withMap(this._dagRead, map => map); }
get closed(): boolean { return this._dagRead.closed; }
close(): void { this._dagRead.close(); }}
const enum WhenceType { Head, Hash,}
export type Whence = | { type: WhenceType.Hash; hash: Hash; } | { type: WhenceType.Head; name: string; };
export function whenceHead(name: string): Whence { return { type: WhenceType.Head, name, };}
export function whenceHash(hash: Hash): Whence { return { type: WhenceType.Hash, hash, };}
export function readFromDefaultHead(dagRead: dag.Read): Promise<Read> { return fromWhence(whenceHead(DEFAULT_HEAD_NAME), dagRead);}
export async function fromWhence( whence: Whence, dagRead: dag.Read,): Promise<Read> { const [, basis, map] = await readCommitForBTreeRead(whence, dagRead); const indexes = readIndexesForRead(basis); return new Read(dagRead, map, indexes);}
export async function readCommit( whence: Whence, dagRead: dag.Read,): Promise<[Hash, Commit<Meta>]> { let hash: Hash; switch (whence.type) { case WhenceType.Hash: hash = whence.hash; break; case WhenceType.Head: { const h = await dagRead.getHead(whence.name); if (h === undefined) { throw new Error(`Unknown head: ${whence.name}`); } hash = h; break; } }
const commit = await commitFromHash(hash, dagRead); return [hash, commit];}
export async function readCommitForBTreeRead( whence: Whence, dagRead: dag.Read,): Promise<[Hash, Commit<Meta>, BTreeRead]> { const [hash, commit] = await readCommit(whence, dagRead); return [hash, commit, new BTreeRead(dagRead, commit.valueHash)];}
export async function readCommitForBTreeWrite( whence: Whence, dagWrite: dag.Write,): Promise<[Hash, Commit<Meta>, BTreeWrite]> { const [hash, commit] = await readCommit(whence, dagWrite); return [hash, commit, new BTreeWrite(dagWrite, commit.valueHash)];}
export function readIndexesForRead( commit: Commit<Meta>,): Map<string, IndexRead> { const m = new Map(); for (const index of commit.indexes) { m.set(index.definition.name, new IndexRead(index, undefined)); } return m;}
replicache

Version Info

Tagged at
2 years ago