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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use std::{
    collections::BTreeSet,
    num::{NonZeroI32, NonZeroI64, NonZeroU32},
    ops::Deref,
    sync::Arc,
};

use async_recursion::async_recursion;
use hashbrown::HashMap;
use semilattice_database::{search, Collection, Condition, Depend, FieldName, SearchResult};

use crate::{Session, SessionDatabase};

use super::{SessionOperation, TemporaryDataEntity};

#[derive(Debug, Clone, PartialEq)]
pub struct SessionSearchResult {
    collection_id: i32,
    rows: BTreeSet<NonZeroI64>,
    join: HashMap<Arc<String>, HashMap<NonZeroI64, SessionSearchResult>>,
}

impl SessionSearchResult {
    pub fn rows(&self) -> &BTreeSet<NonZeroI64> {
        &self.rows
    }

    pub fn join(&self) -> &HashMap<Arc<String>, HashMap<NonZeroI64, SessionSearchResult>> {
        &self.join
    }
}

impl Session {
    fn temporary_data_match(
        row: NonZeroI64,
        ent: &TemporaryDataEntity,
        condition: &Condition,
    ) -> bool {
        match condition {
            Condition::Row(cond) => match cond {
                search::Number::In(c) => c.contains(&(row.get() as isize)),
                search::Number::Max(c) => row.get() <= *c as i64,
                search::Number::Min(c) => row.get() >= *c as i64,
                search::Number::Range(c) => c.contains(&(row.get() as isize)),
            },
            Condition::Uuid(uuid) => uuid.contains(&ent.uuid),
            Condition::Activity(activity) => ent.activity == *activity,
            Condition::Term(cond) => match cond {
                search::Term::In(c) => {
                    ent.term_begin < *c && (ent.term_end == 0 || ent.term_end > *c)
                }
                search::Term::Past(c) => ent.term_end >= *c,
                search::Term::Future(c) => ent.term_begin >= *c,
            },
            Condition::Field(field_id, cond) => {
                ent.fields.get(field_id).map_or(false, |f| match cond {
                    search::Field::Match(v) => f == v,
                    search::Field::Range(min, max) => min <= f && max >= f,
                    search::Field::Min(min) => min <= f,
                    search::Field::Max(max) => max >= f,
                    search::Field::Forward(v) => {
                        unsafe { std::str::from_utf8_unchecked(f) }.starts_with(v.as_ref())
                    }
                    search::Field::Partial(v) => {
                        unsafe { std::str::from_utf8_unchecked(f) }.contains(v.as_ref())
                    }
                    search::Field::Backward(v) => {
                        unsafe { std::str::from_utf8_unchecked(f) }.ends_with(v.as_ref())
                    }
                    search::Field::ValueForward(v) => {
                        v.starts_with(unsafe { std::str::from_utf8_unchecked(f) })
                    }
                    search::Field::ValueBackward(v) => {
                        v.ends_with(unsafe { std::str::from_utf8_unchecked(f) })
                    }
                    search::Field::ValuePartial(v) => {
                        v.contains(unsafe { std::str::from_utf8_unchecked(f) })
                    }
                })
            }
            Condition::Narrow(conditions) => {
                let mut is_match = true;
                for c in conditions.into_iter() {
                    is_match &= Self::temporary_data_match(row, ent, c);
                    if !is_match {
                        break;
                    }
                }
                is_match
            }
            Condition::Wide(conditions) => {
                let mut is_match = false;
                for c in conditions.into_iter() {
                    is_match |= Self::temporary_data_match(row, ent, c);
                    if is_match {
                        break;
                    }
                }
                is_match
            }
            Condition::Depend(key, collection_row) => {
                let mut is_match = true;
                for depend in &ent.depends {
                    is_match = key.as_ref().map_or(true, |key| key == depend.key())
                        && collection_row == depend.deref();
                    if is_match {
                        break;
                    }
                }
                is_match
            }
            Condition::LastUpdated(_) => true,
        }
    }

    fn temprary_data_match_conditions(
        conditions: &Vec<Condition>,
        row: NonZeroI64,
        ent: &TemporaryDataEntity,
    ) -> bool {
        for c in conditions.into_iter() {
            if !Self::temporary_data_match(row, ent, c) {
                return false;
            }
        }
        true
    }

    #[async_recursion(?Send)]
    async fn join(
        &self,
        join_result: &HashMap<Arc<String>, HashMap<NonZeroU32, SearchResult>>,
    ) -> HashMap<Arc<String>, HashMap<NonZeroI64, SessionSearchResult>> {
        let mut map = HashMap::new();
        for (name, row_join) in join_result {
            let mut map_inner = HashMap::new();
            for (row, result) in row_join {
                let result = self.result_with(result).await;
                map_inner.insert((*row).into(), result);
            }
            map.insert(name.to_owned(), map_inner);
        }
        map
    }

    pub async fn result_with(&self, search_result: &SearchResult) -> SessionSearchResult {
        let (collection_id, rows) = if let Some(search) = search_result.search() {
            let collection_id = search.collection_id();
            (
                collection_id.get(),
                if let Some(tmp) = self.temporary_data.get(&collection_id) {
                    let mut rows: BTreeSet<NonZeroI64> = BTreeSet::new();
                    for row in search_result.rows().into_iter() {
                        let row = NonZeroI64::from(*row);
                        if let Some(ent) = tmp.get(&row) {
                            if ent.operation != SessionOperation::Delete {
                                if Self::temprary_data_match_conditions(
                                    search.conditions(),
                                    row,
                                    ent,
                                ) {
                                    rows.insert(row);
                                }
                            }
                        } else {
                            rows.insert(row);
                        }
                    }
                    for (row, _) in tmp.into_iter() {
                        if row.get() < 0 {
                            if let Some(ent) = tmp.get(row) {
                                if ent.operation != SessionOperation::Delete {
                                    if Self::temprary_data_match_conditions(
                                        search.conditions(),
                                        *row,
                                        ent,
                                    ) {
                                        rows.insert(*row);
                                    }
                                }
                            }
                        }
                    }
                    rows
                } else {
                    search_result
                        .rows()
                        .into_iter()
                        .map(|x| NonZeroI64::from(*x))
                        .collect()
                },
            )
        } else {
            (0, BTreeSet::new())
        };
        let join = self.join(search_result.join()).await;

        SessionSearchResult {
            collection_id,
            rows,
            join,
        }
    }

    pub fn field_bytes<'a>(
        &'a self,
        database: &'a SessionDatabase,
        collection_id: NonZeroI32,
        row: NonZeroI64,
        field_name: &FieldName,
    ) -> &[u8] {
        if let Some(temporary_collection) = self.temporary_data.get(&collection_id) {
            if let Some(tmp_row) = temporary_collection.get(&row) {
                if let Some(val) = tmp_row.fields.get(field_name) {
                    return val;
                }
            }
        }
        if row.get() > 0 {
            if let Some(collection) = database.collection(collection_id) {
                return collection.field_bytes(row.try_into().unwrap(), field_name);
            }
        }
        b""
    }

    pub fn collection_field_bytes<'a>(
        &'a self,
        collection: &'a Collection,
        row: NonZeroI64,
        field_name: &FieldName,
    ) -> &[u8] {
        if let Some(temprary_collection) = self.temporary_data.get(&collection.id()) {
            if let Some(temprary_row) = temprary_collection.get(&row) {
                if let Some(val) = temprary_row.fields.get(field_name) {
                    return val;
                }
            }
        }
        if row.get() > 0 {
            collection.field_bytes(row.try_into().unwrap(), field_name)
        } else {
            b""
        }
    }

    pub fn temporary_collection(
        &self,
        collection_id: NonZeroI32,
    ) -> Option<&HashMap<NonZeroI64, TemporaryDataEntity>> {
        self.temporary_data.get(&collection_id)
    }

    pub fn depends(&self, key: Option<Arc<String>>, pend_row: NonZeroU32) -> Option<Vec<Depend>> {
        self.session_data.as_ref().and_then(move |session_data| {
            key.map_or_else(
                || {
                    Some(
                        session_data
                            .relation
                            .rows
                            .session_row
                            .iter_by(|v| v.cmp(&pend_row.get()))
                            .filter_map(|relation_row| {
                                if let (Some(key), Some(depend)) = (
                                    session_data.relation.rows.key.get(relation_row),
                                    session_data.relation.rows.depend.get(relation_row),
                                ) {
                                    return Some(Depend::new(
                                        Arc::new(
                                            unsafe {
                                                std::str::from_utf8_unchecked(
                                                    session_data
                                                        .relation
                                                        .key_names
                                                        .bytes(
                                                            NonZeroU32::new(*key.deref()).unwrap(),
                                                        )
                                                        .unwrap(),
                                                )
                                            }
                                            .into(),
                                        ),
                                        depend.deref().clone(),
                                    ));
                                }
                                None
                            })
                            .collect(),
                    )
                },
                |key_name| {
                    session_data
                        .relation
                        .key_names
                        .row(key_name.as_bytes())
                        .map(|key_id| {
                            session_data
                                .relation
                                .rows
                                .session_row
                                .iter_by(|v| v.cmp(&pend_row.get()))
                                .filter_map(|relation_row| {
                                    if let (Some(key), Some(depend)) = (
                                        session_data.relation.rows.key.get(relation_row),
                                        session_data.relation.rows.depend.get(relation_row),
                                    ) {
                                        if *key.deref() == key_id.get() {
                                            return Some(Depend::new(
                                                Arc::clone(&key_name),
                                                depend.deref().clone(),
                                            ));
                                        }
                                    }
                                    None
                                })
                                .collect()
                        })
                },
            )
        })
    }
}