closevector-common
Version:
42 lines (35 loc) • 1.29 kB
text/typescript
import { CloseVectorDocument } from './document';
export class SynchronousInMemoryDocstore {
_docs: Map<string, CloseVectorDocument>;
constructor(docs?: Map<string, CloseVectorDocument>) {
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: string): CloseVectorDocument {
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: Record<string, CloseVectorDocument>): void {
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);
}
}
}