#![allow(clippy::module_name_repetitions)]
use super::Id;
#[cfg(not(feature = "db"))]
use super::RecordId;
use mecomp_analysis::NUMBER_FEATURES;
#[cfg(feature = "db")]
use surrealdb::RecordId;
#[cfg(feature = "db")]
use surrealqlx::{migrations::M, traits::Table};
pub type AnalysisId = RecordId;
pub const TABLE_NAME: &str = "analysis";
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Analysis {
pub id: AnalysisId,
pub features: [f32; NUMBER_FEATURES],
pub embedding: [f32; mecomp_analysis::DIM_EMBEDDING],
}
#[cfg(feature = "db")]
impl Table for Analysis {
const TABLE_NAME: &'static str = TABLE_NAME;
fn migrations() -> Vec<M<'static>> {
use surrealqlx::surrql;
vec![
M::up(surrql!("
DEFINE TABLE IF NOT EXISTS analysis SCHEMAFULL;
DEFINE FIELD IF NOT EXISTS id ON analysis TYPE record;
DEFINE FIELD IF NOT EXISTS features ON analysis TYPE array<float>;
DEFINE INDEX IF NOT EXISTS analysis_features_vector_index ON analysis FIELDS features MTREE DIMENSION 20;")
)
.comment("Initial version"),
M::up(surrql!("DELETE analysis;")).comment("Clear the existing analyses"),
M::up(surrql!("DEFINE INDEX OVERWRITE analysis_features_vector_index ON analysis FIELDS features MTREE DIMENSION 23;"))
.comment("Update analysis features size from 20 to 23"),
M::up(surrql!("DEFINE TABLE IF NOT EXISTS analysis_to_song TYPE RELATION IN analysis OUT song ENFORCED;DELETE analysis_to_song;"))
.down(surrql!("DEFINE TABLE IF NOT EXISTS analysis_to_song TYPE RELATION IN analysis OUT song ENFORCED;DELETE analysis_to_song;"))
.comment("Clear analysis_to_song relations to prevent dangling relations"),
M::up(surrql!("DELETE analysis;DELETE analysis_to_song;"))
.comment("Clear existing analyses so we can modify indexes properly"),
M::up(surrql!("DEFINE FIELD IF NOT EXISTS embedding ON analysis TYPE array<float>;"))
.down(surrql!("REMOVE FIELD embedding ON analysis;"))
.comment("Add embedding field to analysis table"),
M::up(surrql!("DEFINE INDEX IF NOT EXISTS analysis_embeddings_vector_index ON analysis FIELDS embedding MTREE DIMENSION 32;"))
.down(surrql!("REMOVE INDEX analysis_embeddings_vector_index ON analysis;"))
.comment("Define analysis embeddings index after adding embedding field"),
]
}
}
impl Analysis {
#[must_use]
#[inline]
pub fn generate_id() -> AnalysisId {
RecordId::from_table_key(TABLE_NAME, Id::ulid())
}
}
impl From<&Analysis> for mecomp_analysis::Analysis {
#[inline]
fn from(analysis: &Analysis) -> Self {
Self::new(analysis.features)
}
}
impl From<Analysis> for mecomp_analysis::Analysis {
#[inline]
fn from(analysis: Analysis) -> Self {
Self::new(analysis.features)
}
}