@llamaindex/core
Version:
LlamaIndex Core Module
81 lines (77 loc) • 3.04 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
var env = require('@llamaindex/env');
var index_cjs$1 = require('../../../data-structs/dist/index.cjs');
var index_cjs = require('../../../global/dist/index.cjs');
var index_cjs$2 = require('../../kv-store/dist/index.cjs');
const DEFAULT_PERSIST_PATH = env.path.join(index_cjs.DEFAULT_PERSIST_DIR, index_cjs.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 = index_cjs.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 index_cjs$1.jsonToIndexStruct(json);
}
}
async getIndexStructs() {
const jsons = await this._kvStore.getAll(this._collection);
return Object.values(jsons).map((json)=>index_cjs$1.jsonToIndexStruct(json));
}
}
class SimpleIndexStore extends KVIndexStore {
constructor(kvStore){
kvStore = kvStore || new index_cjs$2.SimpleKVStore();
super(kvStore);
this.kvStore = kvStore;
}
static async fromPersistDir(persistDir = index_cjs.DEFAULT_PERSIST_DIR) {
const persistPath = env.path.join(persistDir, index_cjs.DEFAULT_INDEX_STORE_PERSIST_FILENAME);
return this.fromPersistPath(persistPath);
}
static async fromPersistPath(persistPath) {
const simpleKVStore = await index_cjs$2.SimpleKVStore.fromPersistPath(persistPath);
return new SimpleIndexStore(simpleKVStore);
}
async persist(persistPath = index_cjs.DEFAULT_PERSIST_DIR) {
this.kvStore.persist(persistPath);
}
static fromDict(saveDict) {
const simpleKVStore = index_cjs$2.SimpleKVStore.fromDict(saveDict);
return new SimpleIndexStore(simpleKVStore);
}
toDict() {
if (!(this.kvStore instanceof index_cjs$2.SimpleKVStore)) {
throw new Error("KVStore is not a SimpleKVStore");
}
return this.kvStore.toDict();
}
}
exports.BaseIndexStore = BaseIndexStore;
exports.DEFAULT_PERSIST_PATH = DEFAULT_PERSIST_PATH;
exports.KVIndexStore = KVIndexStore;
exports.SimpleIndexStore = SimpleIndexStore;