@requestnetwork/data-access
Version:
Main package for the Request Network data access layer.
112 lines • 5.42 kB
JavaScript
;
var _InMemoryIndexer_channelToLocationsIndex, _InMemoryIndexer_topicToChannelsIndex;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InMemoryIndexer = void 0;
const tslib_1 = require("tslib");
const types_1 = require("@requestnetwork/types");
const block_1 = require("./block");
/** Helper class to manage a map with arrays as values */
class ArrayMap extends Map {
add(key, value) {
return this.set(key, this.get(key).concat(value));
}
get(key) {
return super.get(key) || [];
}
}
/**
* InMemory implementation to index Request storage transactions, for testing and development.
* The data itself is not indexed, only references to its location
*/
class InMemoryIndexer {
constructor(storageRead) {
this.storageRead = storageRead;
// these fields must be private (#) or jest's matcher won't work.
_InMemoryIndexer_channelToLocationsIndex.set(this, new ArrayMap());
_InMemoryIndexer_topicToChannelsIndex.set(this, new ArrayMap());
}
/** Adds the indexed data for easy retrieval */
addIndex(channelId, topics, location) {
tslib_1.__classPrivateFieldGet(this, _InMemoryIndexer_channelToLocationsIndex, "f").add(channelId, location);
for (const topic of topics || []) {
tslib_1.__classPrivateFieldGet(this, _InMemoryIndexer_topicToChannelsIndex, "f").add(topic, channelId);
}
}
initialize() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return;
});
}
getTransactionsByStorageLocation(hash) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const transactions = yield this.parseDocuments([hash]);
return {
blockNumber: 0,
transactions,
};
});
}
getTransactionsByChannelId(channelId) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const locations = tslib_1.__classPrivateFieldGet(this, _InMemoryIndexer_channelToLocationsIndex, "f").get(channelId);
const transactions = yield this.parseDocuments(locations);
return {
blockNumber: 0,
transactions: transactions.filter((x) => x.channelId === channelId),
};
});
}
getTransactionsByTopics(topics) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
// Efficiently get total count without creating intermediate array
const channelIdsSet = new Set(topics.flatMap((topic) => tslib_1.__classPrivateFieldGet(this, _InMemoryIndexer_topicToChannelsIndex, "f").get(topic)));
const channelIds = Array.from(channelIdsSet);
const locations = channelIds
.map((channel) => tslib_1.__classPrivateFieldGet(this, _InMemoryIndexer_channelToLocationsIndex, "f").get(channel))
.flat();
const transactions = yield this.parseDocuments(locations);
return {
blockNumber: 0,
transactions,
};
});
}
parseDocuments(locations) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const entries = yield this.storageRead.readMany(locations);
return entries
.filter((x) => x.meta.state === types_1.StorageTypes.ContentState.CONFIRMED)
.map((curr) => {
const { id, meta, content } = curr;
const block = block_1.default.parseBlock(content);
return Object.entries(block.header.channelIds).map(([channelId, [index]]) => ({
locationId: id,
channelId,
meta,
transaction: block.transactions[index],
}));
})
.flat()
.map((item) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
return ({
blockNumber: (_b = (_a = item.meta.ethereum) === null || _a === void 0 ? void 0 : _a.blockNumber) !== null && _b !== void 0 ? _b : 0,
blockTimestamp: (_d = (_c = item.meta.ethereum) === null || _c === void 0 ? void 0 : _c.blockTimestamp) !== null && _d !== void 0 ? _d : 0,
channelId: item.channelId,
hash: item.locationId,
size: String((_f = (_e = item.meta.ipfs) === null || _e === void 0 ? void 0 : _e.size) !== null && _f !== void 0 ? _f : 0),
smartContractAddress: (_h = (_g = item.meta.ethereum) === null || _g === void 0 ? void 0 : _g.smartContractAddress) !== null && _h !== void 0 ? _h : '',
topics: [],
transactionHash: (_k = (_j = item.meta.ethereum) === null || _j === void 0 ? void 0 : _j.transactionHash) !== null && _k !== void 0 ? _k : '',
data: item.transaction.data,
encryptedData: item.transaction.encryptedData,
encryptionMethod: item.transaction.encryptionMethod,
keys: item.transaction.keys,
});
});
});
}
}
exports.InMemoryIndexer = InMemoryIndexer;
_InMemoryIndexer_channelToLocationsIndex = new WeakMap(), _InMemoryIndexer_topicToChannelsIndex = new WeakMap();
//# sourceMappingURL=in-memory-indexer.js.map