@atproto/repo
Version:
atproto repo and MST implementation
57 lines • 1.5 kB
JavaScript
import { BlockMap } from '../block-map.js';
import { ReadableBlockstore } from './readable-blockstore.js';
export class MemoryBlockstore extends ReadableBlockstore {
constructor(blocks) {
super();
this.root = null;
this.rev = null;
this.blocks = new BlockMap();
if (blocks) {
this.blocks.addMap(blocks);
}
}
async getRoot() {
return this.root;
}
async getBytes(cid) {
return this.blocks.get(cid) || null;
}
async has(cid) {
return this.blocks.has(cid);
}
async getBlocks(cids) {
return this.blocks.getMany(cids);
}
async putBlock(cid, block) {
this.blocks.set(cid, block);
}
async putMany(blocks) {
this.blocks.addMap(blocks);
}
async updateRoot(cid, rev) {
this.root = cid;
this.rev = rev;
}
async applyCommit(commit) {
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() {
let total = 0;
this.blocks.forEach((bytes) => {
total += bytes.byteLength;
});
return total;
}
async destroy() {
this.blocks.clear();
}
}
export default MemoryBlockstore;
//# sourceMappingURL=memory-blockstore.js.map