Skip to main content

linkrs_sync/
types.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
4pub enum ChangeType {
5    Insert,
6    Update,
7    Delete,
8}
9
10#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
11pub struct IndexOpKey {
12    pub space_id: u64,
13    pub tag_name: String,
14    pub field_name: String,
15}
16
17impl IndexOpKey {
18    pub fn new(space_id: u64, tag_name: impl Into<String>, field_name: impl Into<String>) -> Self {
19        Self {
20            space_id,
21            tag_name: tag_name.into(),
22            field_name: field_name.into(),
23        }
24    }
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
28pub enum IndexType {
29    Fulltext,
30    Vector,
31}
32
33#[derive(Debug, Clone, Serialize, Deserialize)]
34pub enum IndexData {
35    Fulltext(String),
36    Vector(Vec<f32>),
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct IndexOperation {
41    pub key: IndexOpKey,
42    pub index_type: IndexType,
43    pub change_type: ChangeType,
44    pub id: String,
45    pub data: Option<IndexData>,
46}
47
48impl IndexOperation {
49    pub fn new_fulltext(
50        key: IndexOpKey,
51        change_type: ChangeType,
52        id: impl Into<String>,
53        text: Option<String>,
54    ) -> Self {
55        Self {
56            key,
57            index_type: IndexType::Fulltext,
58            change_type,
59            id: id.into(),
60            data: text.map(IndexData::Fulltext),
61        }
62    }
63
64    pub fn new_vector(
65        key: IndexOpKey,
66        change_type: ChangeType,
67        id: impl Into<String>,
68        vector: Option<Vec<f32>>,
69    ) -> Self {
70        Self {
71            key,
72            index_type: IndexType::Vector,
73            change_type,
74            id: id.into(),
75            data: vector.map(IndexData::Vector),
76        }
77    }
78
79    pub fn extract_index_key(&self) -> (u64, String, String) {
80        (
81            self.key.space_id,
82            self.key.tag_name.clone(),
83            self.key.field_name.clone(),
84        )
85    }
86
87    pub fn space_id(&self) -> u64 {
88        self.key.space_id
89    }
90
91    pub fn tag_name(&self) -> &str {
92        &self.key.tag_name
93    }
94
95    pub fn field_name(&self) -> &str {
96        &self.key.field_name
97    }
98
99    pub fn text(&self) -> Option<&str> {
100        match &self.data {
101            Some(IndexData::Fulltext(text)) => Some(text),
102            Some(IndexData::Vector(_)) | None => None,
103        }
104    }
105
106    pub fn vector(&self) -> Option<&[f32]> {
107        match &self.data {
108            Some(IndexData::Vector(vector)) => Some(vector),
109            Some(IndexData::Fulltext(_)) | None => None,
110        }
111    }
112}