mirror of
https://github.com/perstarkse/minne.git
synced 2026-07-04 20:11:42 +02:00
fix: index creation at init
This commit is contained in:
@@ -84,6 +84,9 @@ async fn ensure_runtime_indexes_inner(
|
|||||||
create_fts_analyzer(db).await?;
|
create_fts_analyzer(db).await?;
|
||||||
|
|
||||||
for spec in fts_index_specs() {
|
for spec in fts_index_specs() {
|
||||||
|
if index_exists(db, spec.table, spec.index_name).await? {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
create_index_with_polling(
|
create_index_with_polling(
|
||||||
db,
|
db,
|
||||||
spec.definition(),
|
spec.definition(),
|
||||||
@@ -106,9 +109,18 @@ async fn ensure_hnsw_index(
|
|||||||
spec: &HnswIndexSpec,
|
spec: &HnswIndexSpec,
|
||||||
dimension: usize,
|
dimension: usize,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let definition = match hnsw_index_state(db, spec, dimension).await? {
|
match hnsw_index_state(db, spec, dimension).await? {
|
||||||
HnswIndexState::Missing => spec.definition_if_not_exists(dimension),
|
HnswIndexState::Missing => {
|
||||||
HnswIndexState::Matches(_) => spec.definition_if_not_exists(dimension),
|
create_index_with_polling(
|
||||||
|
db,
|
||||||
|
spec.definition_if_not_exists(dimension),
|
||||||
|
spec.index_name,
|
||||||
|
spec.table,
|
||||||
|
Some(spec.table),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
HnswIndexState::Matches(_) => Ok(()),
|
||||||
HnswIndexState::Different(existing) => {
|
HnswIndexState::Different(existing) => {
|
||||||
info!(
|
info!(
|
||||||
index = spec.index_name,
|
index = spec.index_name,
|
||||||
@@ -117,18 +129,16 @@ async fn ensure_hnsw_index(
|
|||||||
target_dimension = dimension,
|
target_dimension = dimension,
|
||||||
"Overwriting HNSW index to match new embedding dimension"
|
"Overwriting HNSW index to match new embedding dimension"
|
||||||
);
|
);
|
||||||
spec.definition_overwrite(dimension)
|
create_index_with_polling(
|
||||||
|
db,
|
||||||
|
spec.definition_overwrite(dimension),
|
||||||
|
spec.index_name,
|
||||||
|
spec.table,
|
||||||
|
Some(spec.table),
|
||||||
|
)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
create_index_with_polling(
|
|
||||||
db,
|
|
||||||
definition,
|
|
||||||
spec.index_name,
|
|
||||||
spec.table,
|
|
||||||
Some(spec.table),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn hnsw_index_state(
|
async fn hnsw_index_state(
|
||||||
@@ -409,6 +419,33 @@ async fn count_table_rows(db: &SurrealDbClient, table: &str) -> Result<u64> {
|
|||||||
Ok(rows.first().map(|r| r.count).unwrap_or(0))
|
Ok(rows.first().map(|r| r.count).unwrap_or(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn index_exists(db: &SurrealDbClient, table: &str, index_name: &str) -> Result<bool> {
|
||||||
|
let info_query = format!("INFO FOR TABLE {table};");
|
||||||
|
let mut response = db
|
||||||
|
.client
|
||||||
|
.query(info_query)
|
||||||
|
.await
|
||||||
|
.with_context(|| format!("fetching table info for {}", table))?;
|
||||||
|
|
||||||
|
let info: surrealdb::Value = response
|
||||||
|
.take(0)
|
||||||
|
.context("failed to take table info response")?;
|
||||||
|
|
||||||
|
let info_json: Value =
|
||||||
|
serde_json::to_value(info).context("serializing table info to JSON for parsing")?;
|
||||||
|
|
||||||
|
let Some(indexes) = info_json
|
||||||
|
.get("Object")
|
||||||
|
.and_then(|o| o.get("indexes"))
|
||||||
|
.and_then(|i| i.get("Object"))
|
||||||
|
.and_then(|i| i.as_object())
|
||||||
|
else {
|
||||||
|
return Ok(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(indexes.contains_key(index_name))
|
||||||
|
}
|
||||||
|
|
||||||
const fn hnsw_index_specs() -> [HnswIndexSpec; 2] {
|
const fn hnsw_index_specs() -> [HnswIndexSpec; 2] {
|
||||||
[
|
[
|
||||||
HnswIndexSpec {
|
HnswIndexSpec {
|
||||||
|
|||||||
Reference in New Issue
Block a user