use std::sync::Arc;
use anyhow::Result;
use async_trait::async_trait;
use mf_model::node_pool::NodePool;
use mf_state::plugin::{
Plugin, PluginMetadata, PluginSpec, PluginTrait, StateField,
};
use mf_state::state::State;
use mf_state::transaction::Transaction;
use crate::service::{IndexEvent, IndexService};
use crate::step_registry::ensure_default_step_indexers;
use crate::create_tantivy_service;
pub struct TantivySearchIndexResource {
pub service: Arc<IndexService>,
}
impl mf_state::resource::Resource for TantivySearchIndexResource {}
struct TantivyIndexStateField {
service: Arc<IndexService>,
}
impl std::fmt::Debug for TantivyIndexStateField {
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
f.debug_struct("TantivyIndexStateField").finish()
}
}
#[async_trait]
impl StateField for TantivyIndexStateField {
type Value = TantivySearchIndexResource;
async fn init(
&self,
_config: &mf_state::state::StateConfig,
_instance: &State,
) -> Arc<Self::Value> {
Arc::new(TantivySearchIndexResource { service: self.service.clone() })
}
async fn apply(
&self,
tr: &Transaction,
value: Arc<Self::Value>,
old_state: &State,
new_state: &State,
) -> Arc<Self::Value> {
let svc = value.service.clone();
let steps: Vec<Arc<dyn mf_transform::step::Step>> =
tr.steps.iter().cloned().collect();
let pool_before: Arc<NodePool> = old_state.doc();
let pool_after: Arc<NodePool> = new_state.doc();
drop(svc.handle(IndexEvent::TransactionCommitted {
pool_before: Some(pool_before),
pool_after,
steps,
}));
value
}
}
#[derive(Debug)]
struct TantivyIndexPluginTrait {}
impl PluginTrait for TantivyIndexPluginTrait {
fn metadata(&self) -> PluginMetadata {
PluginMetadata {
name: "tantivy_index".to_string(),
version: "1.0.0".to_string(),
description: "Tantivy 索引插件".to_string(),
author: "Tantivy".to_string(),
dependencies: vec![],
conflicts: vec![],
state_fields: vec![],
tags: vec![],
}
}
}
pub fn create_tantivy_index_plugin(
index_dir: &std::path::Path
) -> Result<Arc<Plugin>> {
ensure_default_step_indexers();
let service = Arc::new(create_tantivy_service(index_dir)?);
let field = Arc::new(TantivyIndexStateField { service });
let spec = PluginSpec {
state_field: Some(field),
tr: Arc::new(TantivyIndexPluginTrait {}),
};
Ok(Arc::new(Plugin::new(spec)))
}