falkordb/graph/
mod.rs

1/*
2 * Copyright FalkorDB Ltd. 2023 - present
3 * Licensed under the MIT License.
4 */
5
6use crate::{EntityType, GraphSchema, IndexType};
7use std::{collections::HashMap, fmt::Display};
8
9pub(crate) mod blocking;
10pub(crate) mod query_builder;
11
12#[cfg(feature = "tokio")]
13pub(crate) mod asynchronous;
14
15pub trait HasGraphSchema {
16    fn get_graph_schema_mut(&mut self) -> &mut GraphSchema;
17}
18
19pub(crate) fn generate_create_index_query<P: Display>(
20    index_field_type: IndexType,
21    entity_type: EntityType,
22    label: &str,
23    properties: &[P],
24    options: Option<&HashMap<String, String>>,
25) -> String {
26    let properties_string = properties
27        .iter()
28        .map(|element| format!("l.{}", element))
29        .collect::<Vec<_>>()
30        .join(", ");
31
32    let pattern = match entity_type {
33        EntityType::Node => format!("(l:{})", label),
34        EntityType::Edge => format!("()-[l:{}]->()", label),
35    };
36
37    let idx_type = match index_field_type {
38        IndexType::Range => "",
39        IndexType::Vector => "VECTOR ",
40        IndexType::Fulltext => "FULLTEXT ",
41    };
42
43    let options_string = options
44        .map(|hashmap| {
45            hashmap
46                .iter()
47                .map(|(key, val)| format!("'{key}':'{val}'"))
48                .collect::<Vec<_>>()
49                .join(",")
50        })
51        .map(|options_string| format!(" OPTIONS {{ {} }}", options_string))
52        .unwrap_or_default();
53
54    format!(
55        "CREATE {idx_type}INDEX FOR {pattern} ON ({}){}",
56        properties_string, options_string
57    )
58}
59
60pub(crate) fn generate_drop_index_query<P: Display>(
61    index_field_type: IndexType,
62    entity_type: EntityType,
63    label: &str,
64    properties: &[P],
65) -> String {
66    let properties_string = properties
67        .iter()
68        .map(|element| format!("e.{}", element))
69        .collect::<Vec<_>>()
70        .join(", ");
71
72    let pattern = match entity_type {
73        EntityType::Node => format!("(e:{})", label),
74        EntityType::Edge => format!("()-[e:{}]->()", label),
75    };
76
77    let idx_type = match index_field_type {
78        IndexType::Range => "",
79        IndexType::Vector => "VECTOR",
80        IndexType::Fulltext => "FULLTEXT",
81    }
82    .to_string();
83
84    format!(
85        "DROP {idx_type} INDEX for {pattern} ON ({})",
86        properties_string
87    )
88}