#![allow(clippy::missing_errors_doc)]
use crate::error::{CommandError, Error};
use crate::helpers::require_vector_collection;
use crate::state::VelesDbState;
use tauri::{command, AppHandle, Runtime, State};
#[command]
pub async fn create_index<R: Runtime>(
_app: AppHandle<R>,
state: State<'_, VelesDbState>,
request: crate::types::CreateIndexRequest,
) -> std::result::Result<(), CommandError> {
state
.with_db(|db| {
let coll = require_vector_collection(&db, &request.collection)?;
coll.create_index(&request.field_name)
.map_err(|e| Error::InvalidConfig(e.to_string()))?;
Ok(())
})
.map_err(CommandError::from)
}
#[command]
pub async fn drop_index<R: Runtime>(
_app: AppHandle<R>,
state: State<'_, VelesDbState>,
request: crate::types::DropIndexRequest,
) -> std::result::Result<bool, CommandError> {
state
.with_db(|db| {
let coll = require_vector_collection(&db, &request.collection)?;
Ok(coll.drop_secondary_index(&request.field_name))
})
.map_err(CommandError::from)
}
#[command]
pub async fn list_indexes<R: Runtime>(
_app: AppHandle<R>,
state: State<'_, VelesDbState>,
request: crate::types::ListIndexesRequest,
) -> std::result::Result<Vec<crate::types::IndexInfoOutput>, CommandError> {
state
.with_db(|db| {
let coll = require_vector_collection(&db, &request.collection)?;
let indexes = coll.list_indexes();
Ok(indexes
.into_iter()
.map(|info| crate::types::IndexInfoOutput {
label: info.label,
property: info.property,
index_type: info.index_type,
cardinality: info.cardinality,
memory_bytes: info.memory_bytes,
})
.collect())
})
.map_err(CommandError::from)
}