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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use crate::onto::onto_index::OntoIndex;
use crate::storage::async_storage::get_individual_from_db;
use crate::storage::async_storage::AStorage;
use crate::v_api::obj::ResultCode;
use futures::lock::Mutex;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Serialize, Deserialize, Debug)]
pub struct QueryResult {
pub result: Vec<String>,
pub count: i64,
pub estimated: i64,
pub processed: i64,
pub cursor: i64,
pub total_time: i64,
pub query_time: i64,
pub authorize_time: i64,
pub result_code: ResultCode,
}
impl Default for QueryResult {
fn default() -> Self {
QueryResult {
result: vec![],
count: 0,
estimated: 0,
processed: 0,
cursor: 0,
total_time: 0,
query_time: 0,
authorize_time: 0,
result_code: ResultCode::NotReady,
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct FTQuery {
pub ticket: String,
pub user: String,
pub query: String,
pub sort: String,
pub databases: String,
pub reopen: bool,
pub top: i32,
pub limit: i32,
pub from: i32,
}
impl FTQuery {
pub fn new_with_user(user: &str, query: &str) -> FTQuery {
FTQuery {
ticket: "".to_owned(),
user: user.to_owned(),
query: query.to_owned(),
sort: "".to_owned(),
databases: "".to_owned(),
reopen: false,
top: 10000,
limit: 10000,
from: 0,
}
}
pub fn new_with_ticket(ticket: &str, query: &str) -> FTQuery {
FTQuery {
ticket: ticket.to_owned(),
user: "".to_owned(),
query: query.to_owned(),
sort: "".to_owned(),
databases: "".to_owned(),
reopen: false,
top: 10000,
limit: 10000,
from: 0,
}
}
pub fn as_string(&self) -> String {
let mut s = String::new();
s.push_str("[\"");
if self.ticket.is_empty() {
if !self.user.is_empty() {
s.push_str("\"UU=");
s.push_str(&self.user);
}
} else {
s.push_str(&self.ticket);
}
s.push_str("\",\"");
s.push_str(&self.query);
s.push_str("\",\"");
s.push_str(&self.sort);
s.push_str("\",\"");
s.push_str(&self.databases);
s.push_str("\",");
s.push_str(&self.reopen.to_string());
s.push(',');
s.push_str(&self.top.to_string());
s.push(',');
s.push_str(&self.limit.to_string());
s.push(',');
s.push_str(&self.from.to_string());
s.push(']');
s
}
}
pub struct PrefixesCache {
pub full2short_r: evmap::ReadHandle<String, String>,
pub full2short_w: Arc<Mutex<evmap::WriteHandle<String, String>>>,
pub short2full_r: evmap::ReadHandle<String, String>,
pub short2full_w: Arc<Mutex<evmap::WriteHandle<String, String>>>,
}
pub fn split_full_prefix(v: &str) -> (&str, &str) {
let pos = if let Some(n) = v.rfind('/') {
n
} else {
v.rfind('#').unwrap_or_default()
};
v.split_at(pos + 1)
}
pub fn split_short_prefix(v: &str) -> Option<(&str, &str)> {
if let Some(pos) = v.rfind(':') {
let lr = v.split_at(pos);
if let Some(l) = lr.1.strip_prefix(':') {
return Some((lr.0, l));
}
}
None
}
pub fn get_short_prefix(full_prefix: &str, prefixes_cache: &PrefixesCache) -> String {
if let Some(v) = prefixes_cache.full2short_r.get(full_prefix) {
if let Some(t) = v.get_one() {
return t.to_string();
}
}
full_prefix.to_owned()
}
pub fn get_full_prefix(short_prefix: &str, prefixes_cache: &PrefixesCache) -> String {
if let Some(v) = prefixes_cache.short2full_r.get(short_prefix) {
if let Some(t) = v.get_one() {
return t.to_string();
}
}
short_prefix.to_owned()
}
pub async fn load_prefixes(storage: &AStorage, prefixes_cache: &PrefixesCache) {
let onto_index = OntoIndex::load();
let mut f2s = prefixes_cache.full2short_w.lock().await;
let mut s2f = prefixes_cache.short2full_w.lock().await;
for id in onto_index.data.keys() {
if let Ok((mut rindv, _res)) = get_individual_from_db(id, "", storage, None).await {
rindv.parse_all();
if rindv.any_exists("rdf:type", &["owl:Ontology"]) {
if let Some(full_url) = rindv.get_first_literal("v-s:fullUrl") {
debug!("prefix : {} -> {}", rindv.get_id(), full_url);
let short_prefix = rindv.get_id().trim_end_matches(':');
f2s.insert(full_url.to_owned(), short_prefix.to_owned());
s2f.insert(short_prefix.to_owned(), full_url.to_owned());
}
}
} else {
error!("failed to read individual {}", id);
}
f2s.refresh();
s2f.refresh();
}
}