deno.land / x / replicache@v10.0.0-beta.0 / db / read.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148import {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;}
Version Info