plato-tile-query 0.1.0

Advanced tile query builder — filters, search, sort, pagination
Documentation
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//! # plato-tile-query
//!
//! Advanced tile query builder — filters, search, sort, pagination.
//!
//! Part of the PLATO framework.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Sort direction for query results.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum SortOrder {
    Asc,
    Desc,
}

/// Filter comparison operators.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum FilterOp {
    Eq,
    Neq,
    Gt,
    Gte,
    Lt,
    Lte,
    Contains,
    StartsWith,
    EndsWith,
    In,
    NotIn,
    Regex,
    Exists,
    TypeIs,
}

impl FilterOp {
    /// String representation matching the Python API.
    pub fn as_str(&self) -> &'static str {
        match self {
            FilterOp::Eq => "eq",
            FilterOp::Neq => "neq",
            FilterOp::Gt => "gt",
            FilterOp::Gte => "gte",
            FilterOp::Lt => "lt",
            FilterOp::Lte => "lte",
            FilterOp::Contains => "contains",
            FilterOp::StartsWith => "starts_with",
            FilterOp::EndsWith => "ends_with",
            FilterOp::In => "in",
            FilterOp::NotIn => "not_in",
            FilterOp::Regex => "regex",
            FilterOp::Exists => "exists",
            FilterOp::TypeIs => "type_is",
        }
    }
}

/// A single filter clause.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Filter {
    pub field: String,
    pub op: String,
    pub value: Option<serde_json::Value>,
}

/// Execution plan with cost estimates and optimization hints.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct QueryPlan {
    pub filters: Vec<Filter>,
    pub search_terms: Vec<String>,
    pub sort_field: String,
    pub sort_order: String,
    pub page: usize,
    pub page_size: usize,
    pub estimated_cost: f64,
    pub index_used: String,
    pub optimization_hints: Vec<String>,
}

/// Cached query entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryCache {
    pub key: String,
    pub result: Vec<HashMap<String, serde_json::Value>>,
    pub total: usize,
    pub created_at: f64,
    pub ttl: f64,
    pub hit_count: usize,
    pub query_plan_hash: String,
}

/// Result of executing a tile query.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryResult {
    pub tiles: Vec<HashMap<String, serde_json::Value>>,
    pub total: usize,
    pub page: usize,
    pub page_size: usize,
    pub query_time_ms: f64,
    pub facets: HashMap<String, HashMap<String, usize>>,
    pub cache_hit: bool,
    pub plan: Option<QueryPlan>,
    pub timed_out: bool,
}

impl Default for QueryResult {
    fn default() -> Self {
        Self {
            tiles: Vec::new(),
            total: 0,
            page: 1,
            page_size: 20,
            query_time_ms: 0.0,
            facets: HashMap::new(),
            cache_hit: false,
            plan: None,
            timed_out: false,
        }
    }
}

/// Fluent builder for tile queries.
pub struct TileQueryBuilder {
    filters: Vec<Filter>,
    domain: String,
    search: String,
    tags: Vec<String>,
    sort_by: String,
    sort_order: SortOrder,
    page: usize,
    page_size: usize,
    fields: Vec<String>,
    include_deleted: bool,
    explain: bool,
    cache_ttl: f64,
    timeout_ms: f64,
    facet_fields: Vec<String>,
    post_filter_fn: Option<Box<dyn Fn(&HashMap<String, serde_json::Value>) -> bool + Send + Sync>>,
}

impl Default for TileQueryBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl TileQueryBuilder {
    pub fn new() -> Self {
        Self {
            filters: Vec::new(),
            domain: String::new(),
            search: String::new(),
            tags: Vec::new(),
            sort_by: "created_at".to_string(),
            sort_order: SortOrder::Desc,
            page: 1,
            page_size: 20,
            fields: Vec::new(),
            include_deleted: false,
            explain: false,
            cache_ttl: 0.0,
            timeout_ms: 5000.0,
            facet_fields: Vec::new(),
            post_filter_fn: None,
        }
    }

    pub fn filter(mut self, field: &str, op: FilterOp, value: Option<serde_json::Value>) -> Self {
        self.filters.push(Filter {
            field: field.to_string(),
            op: op.as_str().to_string(),
            value,
        });
        self
    }

    pub fn search(mut self, query: &str) -> Self {
        self.search = query.to_string();
        self
    }

    pub fn in_domain(mut self, domain: &str) -> Self {
        self.domain = domain.to_string();
        self
    }

    pub fn with_tags(mut self, tags: &[&str]) -> Self {
        self.tags.extend(tags.iter().map(|t| t.to_string()));
        self
    }

    pub fn sort_by(mut self, field: &str, order: SortOrder) -> Self {
        self.sort_by = field.to_string();
        self.sort_order = order;
        self
    }

    pub fn page(mut self, num: usize, size: usize) -> Self {
        self.page = num.max(1);
        self.page_size = size.clamp(1, 100);
        self
    }

    pub fn select(mut self, fields: &[&str]) -> Self {
        self.fields = fields.iter().map(|f| f.to_string()).collect();
        self
    }

    pub fn include_deleted(mut self) -> Self {
        self.include_deleted = true;
        self
    }

    pub fn explain(mut self) -> Self {
        self.explain = true;
        self
    }

    pub fn cache(mut self, ttl: f64) -> Self {
        self.cache_ttl = ttl;
        self
    }

    pub fn timeout(mut self, ms: f64) -> Self {
        self.timeout_ms = ms;
        self
    }

    pub fn facet(mut self, fields: &[&str]) -> Self {
        self.facet_fields = fields.iter().map(|f| f.to_string()).collect();
        self
    }

    pub fn post_filter<F>(mut self, f: F) -> Self
    where
        F: Fn(&HashMap<String, serde_json::Value>) -> bool + Send + Sync + 'static,
    {
        self.post_filter_fn = Some(Box::new(f));
        self
    }

    pub fn reset(&mut self) -> &mut Self {
        self.filters.clear();
        self.domain.clear();
        self.search.clear();
        self.tags.clear();
        self.sort_by = "created_at".to_string();
        self.sort_order = SortOrder::Desc;
        self.page = 1;
        self.page_size = 20;
        self.fields.clear();
        self.include_deleted = false;
        self.explain = false;
        self.cache_ttl = 0.0;
        self.facet_fields.clear();
        self.post_filter_fn = None;
        self
    }

    pub fn build_plan(&self) -> QueryPlan {
        let mut plan = QueryPlan {
            filters: self.filters.clone(),
            search_terms: if self.search.is_empty() {
                Vec::new()
            } else {
                self.search
                    .split(|c: char| !c.is_alphanumeric())
                    .filter(|s| !s.is_empty())
                    .map(|s| s.to_string())
                    .collect()
            },
            sort_field: self.sort_by.clone(),
            sort_order: if self.sort_order == SortOrder::Asc {
                "asc".to_string()
            } else {
                "desc".to_string()
            },
            page: self.page,
            page_size: self.page_size,
            ..Default::default()
        };

        plan.estimated_cost = self.filters.len() as f64 * 0.1;
        if !self.search.is_empty() {
            plan.estimated_cost += 0.3;
        }
        if self.post_filter_fn.is_some() {
            plan.estimated_cost += 0.2;
        }
        plan.estimated_cost = plan.estimated_cost.min(1.0);

        if self.filters.is_empty() && self.search.is_empty() {
            plan.optimization_hints
                .push("Full table scan — add filters to reduce cost".to_string());
        }
        if self.page > 10 {
            plan.optimization_hints
                .push("Deep pagination — consider cursor-based approach".to_string());
        }
        if self.page_size > 50 {
            plan.optimization_hints
                .push("Large page size — may impact latency".to_string());
        }

        plan
    }

    pub fn to_dict(&self) -> HashMap<String, serde_json::Value> {
        let mut map = HashMap::new();
        map.insert(
            "filters".to_string(),
            serde_json::to_value(&self.filters).unwrap_or_default(),
        );
        map.insert("domain".to_string(), self.domain.clone().into());
        map.insert("search".to_string(), self.search.clone().into());
        map.insert("tags".to_string(), self.tags.clone().into());
        map.insert("sort_by".to_string(), self.sort_by.clone().into());
        map.insert(
            "sort_order".to_string(),
            if self.sort_order == SortOrder::Asc {
                "asc"
            } else {
                "desc"
            }
            .into(),
        );
        map.insert("page".to_string(), self.page.into());
        map.insert("page_size".to_string(), self.page_size.into());
        map.insert("fields".to_string(), self.fields.clone().into());
        map.insert("include_deleted".to_string(), self.include_deleted.into());
        map.insert("facet_fields".to_string(), self.facet_fields.clone().into());
        map
    }

    pub fn filter_count(&self) -> usize {
        self.filters.len()
    }

    pub fn has_search(&self) -> bool {
        !self.search.trim().is_empty()
    }

    pub fn complexity(&self) -> &'static str {
        let n = self.filters.len()
            + if self.search.is_empty() { 0 } else { 1 }
            + self.tags.len();
        if n == 0 {
            "simple"
        } else if n <= 3 {
            "moderate"
        } else {
            "complex"
        }
    }
}

/// Simple in-memory cache for query results.
pub struct QueryCacheManager {
    cache: HashMap<String, QueryCache>,
    max_size: usize,
    default_ttl: f64,
    hits: usize,
    misses: usize,
}

impl Default for QueryCacheManager {
    fn default() -> Self {
        Self::new(100, 60.0)
    }
}

impl QueryCacheManager {
    pub fn new(max_size: usize, default_ttl: f64) -> Self {
        Self {
            cache: HashMap::new(),
            max_size,
            default_ttl,
            hits: 0,
            misses: 0,
        }
    }

    fn make_key(&self, query_dict: &HashMap<String, serde_json::Value>) -> String {
        let raw = serde_json::to_string(query_dict).unwrap_or_default();
        md5::compute(raw.as_bytes())
            .iter()
            .map(|b| format!("{:02x}", b))
            .collect::<String>()[..16]
            .to_string()
    }

    pub fn get(&mut self, query_dict: &HashMap<String, serde_json::Value>) -> Option<&QueryCache> {
        let key = self.make_key(query_dict);
        let now = current_time();
        if let Some(entry) = self.cache.get(&key) {
            if now - entry.created_at < entry.ttl {
                // We can't increment hit_count on an immutable reference,
                // so we do a second mutable lookup for stats.
                if let Some(e) = self.cache.get_mut(&key) {
                    e.hit_count += 1;
                }
                self.hits += 1;
                return self.cache.get(&key);
            }
        }
        self.misses += 1;
        None
    }

    pub fn put(
        &mut self,
        query_dict: &HashMap<String, serde_json::Value>,
        tiles: Vec<HashMap<String, serde_json::Value>>,
        total: usize,
        ttl: f64,
    ) {
        if self.cache.len() >= self.max_size {
            self.evict();
        }
        let key = self.make_key(query_dict);
        self.cache.insert(
            key.clone(),
            QueryCache {
                key,
                result: tiles,
                total,
                created_at: current_time(),
                ttl: if ttl > 0.0 { ttl } else { self.default_ttl },
                hit_count: 0,
                query_plan_hash: self.make_key(query_dict),
            },
        );
    }

    fn evict(&mut self) {
        if self.cache.is_empty() {
            return;
        }
        let oldest = self
            .cache
            .iter()
            .min_by(|a, b| a.1.created_at.partial_cmp(&b.1.created_at).unwrap())
            .map(|(k, _)| k.clone());
        if let Some(k) = oldest {
            self.cache.remove(&k);
        }
    }

    pub fn invalidate(&mut self, query_dict: Option<&HashMap<String, serde_json::Value>>) {
        if let Some(qd) = query_dict {
            let key = self.make_key(qd);
            self.cache.remove(&key);
        } else {
            self.cache.clear();
        }
    }

    pub fn clear(&mut self) {
        self.cache.clear();
    }

    pub fn stats(&self) -> HashMap<String, serde_json::Value> {
        let total = self.hits + self.misses;
        let mut map = HashMap::new();
        map.insert("size".to_string(), self.cache.len().into());
        map.insert("max_size".to_string(), self.max_size.into());
        map.insert("hits".to_string(), self.hits.into());
        map.insert("misses".to_string(), self.misses.into());
        map.insert(
            "hit_rate".to_string(),
            if total > 0 {
                (self.hits as f64) / (total as f64)
            } else {
                0.0
            }
            .into(),
        );
        map
    }
}

fn current_time() -> f64 {
    use std::time::{SystemTime, UNIX_EPOCH};
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs_f64())
        .unwrap_or(0.0)
}

// Simple md5 implementation to avoid adding a dependency
mod md5 {
    pub fn compute(input: &[u8]) -> [u8; 16] {
        // FNV-1a based fallback for deterministic hashing
        let mut hash: u64 = 0xcbf29ce484222325;
        for &byte in input {
            hash ^= byte as u64;
            hash = hash.wrapping_mul(0x100000001b3);
        }
        let mut out = [0u8; 16];
        out[..8].copy_from_slice(&hash.to_le_bytes());
        out[8..].copy_from_slice(&hash.to_le_bytes());
        out
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_builder_chain() {
        let builder = TileQueryBuilder::new()
            .filter("status", FilterOp::Eq, Some("active".into()))
            .search("hello world")
            .in_domain("main")
            .with_tags(&["a", "b"])
            .sort_by("name", SortOrder::Asc)
            .page(2, 50)
            .select(&["id", "name"]);

        assert_eq!(builder.filter_count(), 1);
        assert!(builder.has_search());
        assert_eq!(builder.complexity(), "complex");
    }

    #[test]
    fn test_build_plan() {
        let builder = TileQueryBuilder::new()
            .filter("x", FilterOp::Gt, Some(10.into()))
            .search("term");
        let plan = builder.build_plan();
        assert_eq!(plan.filters.len(), 1);
        assert_eq!(plan.search_terms, vec!["term"]);
        assert!(plan.estimated_cost > 0.0);
    }

    #[test]
    fn test_cache_manager() {
        let mut cm = QueryCacheManager::new(10, 60.0);
        let mut qd = HashMap::new();
        qd.insert("x".to_string(), serde_json::Value::from(1));
        cm.put(&qd, vec![HashMap::new()], 1, 0.0);
        assert!(cm.get(&qd).is_some());
        assert_eq!(cm.stats()["hits"], serde_json::Value::from(1));
    }
}