v_common_module/
veda_backend.rs

1use crate::module::Module;
2use crate::ticket::Ticket;
3use crate::v_onto::individual::*;
4use crate::v_search::ft_client::FTClient;
5use ini::Ini;
6use std::env;
7use v_api::app::ResultCode;
8use v_api::*;
9use v_storage::storage::*;
10
11pub struct Backend {
12    pub storage: VStorage,
13    pub fts: FTClient,
14    pub api: APIClient,
15}
16
17impl Default for Backend {
18    fn default() -> Self {
19        Backend::create(StorageMode::ReadOnly, false)
20    }
21}
22
23impl Backend {
24    pub fn create(storage_mode: StorageMode, use_remote_storage: bool) -> Self {
25        let args: Vec<String> = env::args().collect();
26
27        let conf = Ini::load_from_file("veda.properties").expect("fail load veda.properties file");
28        let section = conf.section(None::<String>).expect("fail parse veda.properties");
29
30        let mut ft_query_service_url = String::default();
31
32        for el in args.iter() {
33            if el.starts_with("--ft_query_service_url") {
34                let p: Vec<&str> = el.split('=').collect();
35                ft_query_service_url = p[1].to_owned();
36            }
37        }
38
39        if ft_query_service_url.is_empty() {
40            ft_query_service_url = section.get("ft_query_service_url").expect("param [ft_query_service_url] not found in veda.properties").to_string();
41        }
42
43        info!("use ft_query_service_url={}", ft_query_service_url);
44
45        let storage: VStorage;
46
47        if !use_remote_storage {
48            storage = get_storage_use_prop(storage_mode);
49        } else {
50            let ro_storage_url = section.get("ro_storage_url").expect("param [ro_storage_url] not found in veda.properties");
51            storage = VStorage::new_remote(ro_storage_url);
52        }
53
54        let ft_client = FTClient::new(ft_query_service_url);
55
56        let param_name = "main_module_url";
57        let api = if let Some(url) = Module::get_property(param_name) {
58            APIClient::new(url)
59        } else {
60            error!("not found param {} in properties file", param_name);
61            APIClient::new("".to_owned())
62        };
63
64        Backend {
65            storage,
66            fts: ft_client,
67            api,
68        }
69    }
70
71    pub fn get_sys_ticket_id(&mut self) -> Result<String, i32> {
72        Module::get_sys_ticket_id_from_db(&mut self.storage)
73    }
74
75    pub fn get_literal_of_link(&mut self, indv: &mut Individual, link: &str, field: &str, to: &mut Individual) -> Option<String> {
76        if let Some(v) = indv.get_literals(link) {
77            for el in v {
78                if self.storage.get_individual(&el, to) {
79                    return to.get_first_literal(field);
80                }
81            }
82        }
83        None
84    }
85
86    pub fn get_literals_of_link(&mut self, indv: &mut Individual, link: &str, field: &str) -> Vec<String> {
87        let mut res = Vec::new();
88        if let Some(v) = indv.get_literals(link) {
89            for el in v {
90                let to = &mut Individual::default();
91                if self.storage.get_individual(&el, to) {
92                    if let Some(s) = to.get_first_literal(field) {
93                        res.push(s);
94                    }
95                }
96            }
97        }
98        res
99    }
100
101    pub fn get_datetime_of_link(&mut self, indv: &mut Individual, link: &str, field: &str, to: &mut Individual) -> Option<i64> {
102        if let Some(v) = indv.get_literals(link) {
103            for el in v {
104                if self.storage.get_individual(&el, to) {
105                    return to.get_first_datetime(field);
106                }
107            }
108        }
109        None
110    }
111
112    pub fn get_individual_h(&mut self, uri: &str) -> Option<Box<Individual>> {
113        let mut iraw = Box::new(Individual::default());
114        if !self.storage.get_individual(uri, &mut iraw) {
115            return None;
116        }
117        Some(iraw)
118    }
119
120    pub fn get_individual_s(&mut self, uri: &str) -> Option<Individual> {
121        let mut iraw = Individual::default();
122        if !self.storage.get_individual(uri, &mut iraw) {
123            return None;
124        }
125        Some(iraw)
126    }
127
128    pub fn get_individual<'a>(&mut self, uri: &str, iraw: &'a mut Individual) -> Option<&'a mut Individual> {
129        if uri.is_empty() || !self.storage.get_individual(uri, iraw) {
130            return None;
131        }
132        Some(iraw)
133    }
134
135    pub fn get_ticket_from_db(&mut self, id: &str) -> Ticket {
136        let mut dest = Ticket::default();
137        let mut indv = Individual::default();
138        if self.storage.get_individual_from_db(StorageId::Tickets, id, &mut indv) {
139            dest.update_from_individual(&mut indv);
140            dest.result = ResultCode::Ok;
141        }
142        dest
143    }
144}
145
146pub fn indv_apply_cmd(cmd: &IndvOp, prev_indv: &mut Individual, indv: &mut Individual) {
147    if !prev_indv.is_empty() {
148        let list_predicates = indv.get_predicates();
149
150        for predicate in list_predicates {
151            if predicate != "v-s:updateCounter" {
152                if cmd == &IndvOp::AddTo {
153                    // add value to set or ignore if exists
154                    prev_indv.apply_predicate_as_add_unique(&predicate, indv);
155                } else if cmd == &IndvOp::SetIn {
156                    // set value to predicate
157                    prev_indv.apply_predicate_as_set(&predicate, indv);
158                } else if cmd == &IndvOp::RemoveFrom {
159                    // remove predicate or value in set
160                    prev_indv.apply_predicate_as_remove(&predicate, indv);
161                }
162            }
163        }
164    }
165}
166
167pub fn get_storage_use_prop(mode: StorageMode) -> VStorage {
168    let conf = Ini::load_from_file("veda.properties").expect("fail load [veda.properties] file");
169    let section = conf.section(None::<String>).expect("fail parse veda.properties");
170    let tarantool_addr = if let Some(p) = section.get("tarantool_url") {
171        p.to_owned()
172    } else {
173        warn!("param [tarantool_url] not found in veda.properties");
174        "".to_owned()
175    };
176
177    let storage = if !tarantool_addr.is_empty() {
178        VStorage::new_tt(tarantool_addr, "veda6", "123456")
179    } else {
180        VStorage::new_lmdb("./data", mode)
181    };
182
183    storage
184}