import { Cid } from '@atproto/lex-data' import { BlockMap } from '../block-map' import { CommitData } from '../types' import { ReadableBlockstore } from './readable-blockstore' import { RepoStorage } from './types' export class MemoryBlockstore extends ReadableBlockstore implements RepoStorage { blocks: BlockMap root: Cid | null = null rev: string | null = null constructor(blocks?: BlockMap) { super() this.blocks = new BlockMap() if (blocks) { this.blocks.addMap(blocks) } } async getRoot(): Promise { return this.root } async getBytes(cid: Cid): Promise { return this.blocks.get(cid) || null } async has(cid: Cid): Promise { return this.blocks.has(cid) } async getBlocks(cids: Cid[]): Promise<{ blocks: BlockMap; missing: Cid[] }> { return this.blocks.getMany(cids) } async putBlock(cid: Cid, block: Uint8Array): Promise { this.blocks.set(cid, block) } async putMany(blocks: BlockMap): Promise { this.blocks.addMap(blocks) } async updateRoot(cid: Cid, rev: string): Promise { this.root = cid this.rev = rev } async applyCommit(commit: CommitData): Promise { this.root = commit.cid const rmCids = commit.removedCids.toList() for (const cid of rmCids) { this.blocks.delete(cid) } commit.newBlocks.forEach((bytes, cid) => { this.blocks.set(cid, bytes) }) } async sizeInBytes(): Promise { let total = 0 this.blocks.forEach((bytes) => { total += bytes.byteLength }) return total } async destroy(): Promise { this.blocks.clear() } } export default MemoryBlockstore