closevector-common
Version:
40 lines (39 loc) • 1.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SynchronousInMemoryDocstore = void 0;
class SynchronousInMemoryDocstore {
_docs;
constructor(docs) {
this._docs = docs ?? new Map();
}
/**
* Searches for a document in the store based on its ID.
* @param search The ID of the document to search for.
* @returns The document with the given ID.
*/
search(search) {
const result = this._docs.get(search);
if (!result) {
throw new Error(`ID ${search} not found.`);
}
else {
return result;
}
}
/**
* Adds new documents to the store.
* @param texts An object where the keys are document IDs and the values are the documents themselves.
* @returns Void
*/
add(texts) {
const keys = [...this._docs.keys()];
const overlapping = Object.keys(texts).filter(x => keys.includes(x));
if (overlapping.length > 0) {
throw new Error(`Tried to add ids that already exist: ${overlapping}`);
}
for (const [key, value] of Object.entries(texts)) {
this._docs.set(key, value);
}
}
}
exports.SynchronousInMemoryDocstore = SynchronousInMemoryDocstore;