@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
63 lines • 2.79 kB
JavaScript
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
import { create, load } from 'prolly-trees/map';
import * as Block from 'multiformats/block';
import * as codec from '@ipld/dag-cbor';
import { sha256 as hasher } from 'multiformats/hashes/sha2';
import { bf, simpleCompare as compare } from 'prolly-trees/utils';
import { nocache, global as globalCache } from 'prolly-trees/cache';
const chunker = bf(12);
const cache = globalCache;
const opts = { cache, chunker, codec, hasher };
const indexStoreFactory = (blockStore) => {
const { put, get } = blockStore;
const indexCreate = async (values) => {
var _a, e_1, _b, _c;
const list = values.map((elem) => ({
key: elem.value,
value: elem.ref,
}));
let root;
try {
for (var _d = true, _e = __asyncValues(create(Object.assign({ get, compare, list }, opts))), _f; _f = await _e.next(), _a = _f.done, !_a;) {
_c = _f.value;
_d = false;
try {
const node = _c;
const address = await node.address;
const block = await node.block;
await put(block);
root = address;
}
finally {
_d = true;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
}
finally { if (e_1) throw e_1.error; }
}
return root;
};
const getDecoded = async (link) => {
const bytes = await get(link);
return await Block.decode({ bytes, codec, hasher });
};
const indexSearch = async (link, value) => {
const indexRoot = await load(Object.assign({ cid: link, get: getDecoded, compare }, opts));
const { result } = await indexRoot.get(value);
return { value, ref: result };
};
return { indexCreate, indexSearch };
};
export { indexStoreFactory };
//# sourceMappingURL=index-store-factory.js.map