@dstanesc/o-o-o-o-o-o-o
Version:
O-O-O-O-O-O-O is a collection of content addressed persistent data structures
55 lines • 1.78 kB
JavaScript
const memoryBlockStoreFactory = () => {
const blocks = {};
let readCounter = 0;
const put = async (block) => {
blocks[block.cid.toString()] = block.bytes;
};
const get = async (cid) => {
const bytes = blocks[cid.toString()];
if (bytes === undefined)
throw new Error('Block Not found for ' + cid.toString());
readCounter++;
return bytes;
};
const push = async (otherStore) => {
const cids = Object.keys(blocks);
for (const cid of cids) {
const bytes = blocks[cid];
await otherStore.put({ cid, bytes });
}
};
const diff = (otherStore) => {
const missingLocal = new Set();
const missingOther = new Set();
const intersection = new Set();
const localCids = content();
const otherCids = otherStore.content();
for (const cid of localCids) {
if (otherCids.has(cid)) {
intersection.add(cid);
}
else {
missingOther.add(cid);
}
}
for (const cid of otherCids) {
if (!localCids.has(cid)) {
missingLocal.add(cid);
}
}
return { missingLocal, missingOther, intersection };
};
const content = () => {
const out = new Set();
for (const cid of Object.keys(blocks)) {
out.add(cid.toString());
}
return out;
};
const countReads = () => readCounter;
const resetReads = () => (readCounter = 0);
const size = () => Object.keys(blocks).length;
return { get, put, push, countReads, resetReads, size, diff, content };
};
export { memoryBlockStoreFactory };
//# sourceMappingURL=block-store.js.map