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
188
use std::collections::BTreeSet;
use super::{Session, TemporaryDataEntity};
use crate::{search, Activity, Condition, Database};
pub struct SessionSearch<'a> {
session: &'a Session,
collection_id: i32,
conditions: Vec<Condition>,
}
impl<'a> SessionSearch<'a> {
pub fn new(session: &'a Session, collection_id: i32) -> Self {
Self {
session,
collection_id,
conditions: Vec::new(),
}
}
pub fn search_default(mut self) -> Self {
self.conditions.push(Condition::Term(search::Term::In(
chrono::Local::now().timestamp(),
)));
self.conditions.push(Condition::Activity(Activity::Active));
self
}
pub fn search_field(self, field_name: impl Into<String>, condition: search::Field) -> Self {
self.search(Condition::Field(field_name.into(), condition))
}
pub fn search_term(self, condition: search::Term) -> Self {
self.search(Condition::Term(condition))
}
pub fn search_activity(self, condition: Activity) -> Self {
self.search(Condition::Activity(condition))
}
pub fn search_row(self, condition: search::Number) -> Self {
self.search(Condition::Row(condition))
}
pub fn search(mut self, condition: Condition) -> Self {
self.conditions.push(condition);
self
}
fn temporary_data_match(ent: &TemporaryDataEntity, condition: &Condition) -> bool {
let mut is_match = true;
match condition {
Condition::Activity(activity) => {
if ent.activity != *activity {
is_match = false;
}
}
Condition::Term(cond) => match cond {
search::Term::In(c) => {
if !(ent.term_begin < *c && (ent.term_end == 0 || ent.term_end > *c)) {
is_match = false;
}
}
search::Term::Past(c) => {
if ent.term_end > *c {
is_match = false;
}
}
search::Term::Future(c) => {
if ent.term_begin < *c {
is_match = false;
}
}
},
Condition::Field(key, cond) => {
if let Some(field_tmp) = ent.fields.get(key) {
match cond {
search::Field::Match(v) => {
if field_tmp != v {
is_match = false;
}
}
search::Field::Range(min, max) => {
if min < field_tmp || max > field_tmp {
is_match = false;
}
}
search::Field::Min(min) => {
if min < field_tmp {
is_match = false;
}
}
search::Field::Max(max) => {
if max > field_tmp {
is_match = false;
}
}
search::Field::Forward(v) => {
if let Ok(str) = std::str::from_utf8(field_tmp) {
if !str.starts_with(v) {
is_match = false;
}
} else {
is_match = false;
}
}
search::Field::Partial(v) => {
if let Ok(str) = std::str::from_utf8(field_tmp) {
if !str.contains(v) {
is_match = false;
}
} else {
is_match = false;
}
}
search::Field::Backward(v) => {
if let Ok(str) = std::str::from_utf8(field_tmp) {
if !str.ends_with(v) {
is_match = false;
}
} else {
is_match = false;
}
}
}
} else {
is_match = false;
}
}
Condition::Narrow(conditions) => {
is_match = true;
for c in conditions {
is_match &= Self::temporary_data_match(ent, c);
if !is_match {
break;
}
}
}
Condition::Wide(conditions) => {
is_match = false;
for c in conditions {
is_match |= Self::temporary_data_match(ent, c);
if is_match {
break;
}
}
}
Condition::Depend(_) => {}
Condition::Row(_) => {}
Condition::LastUpdated(_) => {}
Condition::Uuid(_) => {}
}
is_match
}
pub(crate) fn result(self, database: &Database) -> BTreeSet<i64> {
let mut new_rows: BTreeSet<i64> = BTreeSet::new();
if let Some(collection) = database.collection(self.collection_id) {
let mut search = database.search(collection);
for c in &self.conditions {
search = search.search(c.clone());
}
let r = database.result(search);
if let Some(tmp) = self.session.temporary_data.get(&self.collection_id) {
for row in r {
if let Some(ent) = tmp.get(&(row as i64)) {
let mut is_match = true;
for c in &self.conditions {
is_match = Self::temporary_data_match(ent, c);
if !is_match {
break;
}
}
if is_match {
new_rows.insert(row as i64);
}
} else {
new_rows.insert(row as i64);
}
}
for (row, _) in tmp {
let row = *row;
if row < 0 {
new_rows.insert(row);
}
}
} else {
new_rows = r.into_iter().map(|x| x as i64).collect();
}
}
new_rows
}
}