@llamaindex/core
Version:
LlamaIndex Core Module
76 lines (73 loc) • 2.82 kB
JavaScript
import { path } from '@llamaindex/env';
import { jsonToIndexStruct } from '../../../data-structs/dist/index.js';
import { DEFAULT_PERSIST_DIR, DEFAULT_INDEX_STORE_PERSIST_FILENAME, DEFAULT_NAMESPACE } from '../../../global/dist/index.js';
import { SimpleKVStore } from '../../kv-store/dist/index.js';
const DEFAULT_PERSIST_PATH = path.join(DEFAULT_PERSIST_DIR, DEFAULT_INDEX_STORE_PERSIST_FILENAME);
class BaseIndexStore {
async persist(persistPath = DEFAULT_PERSIST_PATH) {
// Persist the index store to disk.
}
}
class KVIndexStore extends BaseIndexStore {
constructor(kvStore, namespace = DEFAULT_NAMESPACE){
super();
this._kvStore = kvStore;
this._collection = `${namespace}/data`;
}
async addIndexStruct(indexStruct) {
const key = indexStruct.indexId;
const data = indexStruct.toJson();
await this._kvStore.put(key, data, this._collection);
}
async deleteIndexStruct(key) {
await this._kvStore.delete(key, this._collection);
}
async getIndexStruct(structId) {
if (!structId) {
const structs = await this.getIndexStructs();
if (structs.length !== 1) {
throw new Error("More than one index struct found");
}
return structs[0];
} else {
const json = await this._kvStore.get(structId, this._collection);
if (json == null) {
return;
}
return jsonToIndexStruct(json);
}
}
async getIndexStructs() {
const jsons = await this._kvStore.getAll(this._collection);
return Object.values(jsons).map((json)=>jsonToIndexStruct(json));
}
}
class SimpleIndexStore extends KVIndexStore {
constructor(kvStore){
kvStore = kvStore || new SimpleKVStore();
super(kvStore);
this.kvStore = kvStore;
}
static async fromPersistDir(persistDir = DEFAULT_PERSIST_DIR) {
const persistPath = path.join(persistDir, DEFAULT_INDEX_STORE_PERSIST_FILENAME);
return this.fromPersistPath(persistPath);
}
static async fromPersistPath(persistPath) {
const simpleKVStore = await SimpleKVStore.fromPersistPath(persistPath);
return new SimpleIndexStore(simpleKVStore);
}
async persist(persistPath = DEFAULT_PERSIST_DIR) {
this.kvStore.persist(persistPath);
}
static fromDict(saveDict) {
const simpleKVStore = SimpleKVStore.fromDict(saveDict);
return new SimpleIndexStore(simpleKVStore);
}
toDict() {
if (!(this.kvStore instanceof SimpleKVStore)) {
throw new Error("KVStore is not a SimpleKVStore");
}
return this.kvStore.toDict();
}
}
export { BaseIndexStore, DEFAULT_PERSIST_PATH, KVIndexStore, SimpleIndexStore };