1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::model::algorithms::{
    algorithm::Algorithm, algorithm_entry_point::AlgorithmEntryPoint,
    similarity_search::SimilaritySearch, RegisterFunction,
};
use async_graphql::{dynamic::FieldValue, Context};
use dynamic_graphql::internal::{OutputTypeName, Register, Registry, ResolveOwned, TypeName};
use once_cell::sync::Lazy;
use raphtory::vectors::vectorised_graph::DynamicVectorisedGraph;
use std::{
    borrow::Cow,
    collections::HashMap,
    sync::{Mutex, MutexGuard},
};

pub static VECTOR_ALGO_PLUGINS: Lazy<Mutex<HashMap<String, RegisterFunction>>> =
    Lazy::new(|| Mutex::new(HashMap::new()));

pub struct VectorAlgorithms {
    pub graph: DynamicVectorisedGraph,
}

impl From<DynamicVectorisedGraph> for VectorAlgorithms {
    fn from(graph: DynamicVectorisedGraph) -> Self {
        Self { graph }
    }
}

impl<'a> AlgorithmEntryPoint<'a> for VectorAlgorithms {
    fn predefined_algos() -> HashMap<&'static str, RegisterFunction> {
        HashMap::from([(
            "similaritySearch",
            Box::new(SimilaritySearch::register_algo) as RegisterFunction,
        )])
    }
    fn lock_plugins() -> MutexGuard<'static, HashMap<String, RegisterFunction>> {
        VECTOR_ALGO_PLUGINS.lock().unwrap()
    }
}

impl Register for VectorAlgorithms {
    fn register(registry: Registry) -> Registry {
        Self::register_algos(registry)
    }
}
impl TypeName for VectorAlgorithms {
    fn get_type_name() -> Cow<'static, str> {
        "VectorAlgorithms".into()
    }
}
impl OutputTypeName for VectorAlgorithms {}
impl<'a> ResolveOwned<'a> for VectorAlgorithms {
    fn resolve_owned(self, _ctx: &Context) -> dynamic_graphql::Result<Option<FieldValue<'a>>> {
        Ok(Some(FieldValue::owned_any(self)))
    }
}