Skip to content

Commit 547e2c2

Browse files
committed
Improving loadDB
1 parent db5d615 commit 547e2c2

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

src/class/SimpleDB.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ export default class SimpleDB extends Simple {
435435
}
436436

437437
/**
438-
* Loads a database from a file into memory. The original file will not be modified. If one or more columns has geometries, the method will automatically look for a file with projections in the same folder as the database file.
438+
* Loads a database from a file into memory. The original file will not be modified. If one or more columns has geometries, the method will automatically look for files with projections and indexes in the same folder as the database file.
439439
*
440440
* @example
441441
* Basic usage
@@ -456,6 +456,24 @@ export default class SimpleDB extends Simple {
456456
async loadDB(file: string): Promise<void> {
457457
const path = cleanPath(file);
458458
const extension = getExtension(path);
459+
460+
const allIndexesFile = `${path.replace(`.${extension}`, "")}_indexes.json`;
461+
let vssIndex = false;
462+
if (existsSync(allIndexesFile)) {
463+
const indexes = JSON.parse(readFileSync(allIndexesFile, "utf-8"));
464+
for (const table of Object.keys(indexes)) {
465+
for (const index of indexes[table]) {
466+
if (index.startsWith("vss_")) {
467+
vssIndex = true;
468+
}
469+
}
470+
}
471+
}
472+
473+
if (vssIndex) {
474+
await this.customQuery(`INSTALL vss; LOAD vss;`);
475+
}
476+
459477
if (extension === "db") {
460478
await queryDB(
461479
this,
@@ -507,11 +525,21 @@ DETACH my_database;`,
507525
}
508526
await this.customQuery(`INSTALL spatial; LOAD spatial;`);
509527
}
528+
529+
if (existsSync(allIndexesFile)) {
530+
const indexes = JSON.parse(readFileSync(allIndexesFile, "utf-8"));
531+
for (const table of this.tables) {
532+
if (indexes[table.name]) {
533+
table.indexes = indexes[table.name];
534+
}
535+
}
536+
}
537+
510538
this.tableIncrement = Math.round(Math.random() * 1000000);
511539
}
512540

513541
/**
514-
* Writes the database to a file. The file will be created if it doesn't exist, and overwritten if it does. If one or more tables have geometries, a .json file will be created with the projections.
542+
* Writes the database to a file. The file will be created if it doesn't exist, and overwritten if it does. If one or more tables have geometries, a .json file will be created with the projections. If one or more indexes are present, a .json file will be created with the indexes.
515543
*
516544
* @example
517545
* Basic usage
@@ -551,6 +579,20 @@ DETACH my_database;`,
551579
writeFileSync(allProjectionsFile, JSON.stringify(allProjections));
552580
}
553581

582+
const allIndexes: { [key: string]: string[] } = {};
583+
for (const table of this.tables) {
584+
if (table.indexes.length > 0) {
585+
allIndexes[table.name] = table.indexes;
586+
}
587+
}
588+
const allIndexesFile = `${path.replace(`.${extension}`, "")}_indexes.json`;
589+
if (existsSync(allIndexesFile)) {
590+
rmSync(allIndexesFile);
591+
}
592+
if (Object.keys(allIndexes).length > 0) {
593+
writeFileSync(allIndexesFile, JSON.stringify(allIndexes));
594+
}
595+
554596
if (extension === "db") {
555597
await queryDB(
556598
this,

src/methods/aiEmbeddings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ export default async function aiEmbeddings(
8686
);
8787
await simpleTable.sdb.customQuery(
8888
`INSTALL vss; LOAD vss;
89-
CREATE INDEX my_hnsw_cosine_index${
89+
CREATE INDEX vss_cosine_index${
9090
camelCase(simpleTable.name)
9191
} ON "${simpleTable.name}" USING HNSW ("${newColumn}") WITH (metric = 'cosine');`,
9292
);
9393
simpleTable.indexes.push(
94-
`my_hnsw_cosine_index${camelCase(simpleTable.name)}`,
94+
`vss_cosine_index${camelCase(simpleTable.name)}`,
9595
);
9696
}
9797
}

src/methods/aiVectorSimilarity.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,19 @@ export default async function aiVectorSimilarity(
4040
);
4141
if (
4242
simpleTable.indexes.includes(
43-
`my_hnsw_cosine_index${camelCase(simpleTable.name)}`,
43+
`vss_cosine_index${camelCase(simpleTable.name)}`,
4444
)
4545
) {
4646
options.verbose && console.log("Index already exists.");
4747
} else {
4848
await simpleTable.sdb.customQuery(
4949
`INSTALL vss; LOAD vss;
50-
CREATE INDEX my_hnsw_cosine_index${
50+
CREATE INDEX vss_cosine_index${
5151
camelCase(simpleTable.name)
5252
} ON "${simpleTable.name}" USING HNSW ("${column}") WITH (metric = 'cosine');`,
5353
);
5454
simpleTable.indexes.push(
55-
`my_hnsw_cosine_index${camelCase(simpleTable.name)}`,
55+
`vss_cosine_index${camelCase(simpleTable.name)}`,
5656
);
5757
}
5858
}

0 commit comments

Comments
 (0)