memquery 0.1.1

MemQuery is simple library for creating, querying, and updating in memory documents that are represented as JSON objects and queried using Mongodb like operators.
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
use super::{errors::Error, utils::*};
use serde_json::{json, Value};
use std::sync::Arc;

#[cfg(feature = "sync")]
use std::sync::Mutex;

#[cfg(not(feature = "sync"))]
use tokio::sync::Mutex;

pub type Documents = Vec<Value>;

pub type DocumentCollection = Arc<Mutex<Documents>>;

enum MathOpType {
  Inc,
  Mul,
}

pub fn has_update_operations(update: &Value) -> Result<bool, Error> {
  let update = update.as_object().unwrap();
  let all_operators = update.keys().map(|k| k.starts_with("$")).all(|o| o == true);
  if !all_operators && update.keys().map(|k| k.starts_with("$")).any(|o| o == true) {
    return Err(Error::MQInvalidOp(String::from(
      "Cannot mix update operators with keys.",
    )));
  }
  Ok(all_operators)
}

fn is_key_valid_op(key: &str) -> Result<(), Error> {
  // if key contains comparison op check if it is valid
  let (is_embedded, _) = is_embedded_query(key);
  if is_embedded && key.contains("$") {
    return Err(Error::MQOpNotAllowedInMultipartKey);
  }

  if is_op(key) && !matches!(key, EQ | GT | GTE | LT | LTE | NE | IN | NIN | AND | OR) {
    return Err(Error::MQInvalidOp(format!("Op {} is not supported.", key)));
  }

  Ok(())
}

fn sum<T>(x: T, y: T) -> T
where
  T: std::ops::Add<Output = T>,
{
  let result: T = x + y;
  result
}

fn mul<T>(x: T, y: T) -> T
where
  T: std::ops::Mul<Output = T>,
{
  let result: T = x * y;
  result
}

pub struct Engine {
  docs: DocumentCollection,
}

impl Engine {
  pub fn with_collection(docs: DocumentCollection) -> Engine {
    Engine { docs }
  }

  #[cfg(feature = "sync")]
  pub fn find(&self, query: &Value) -> Result<Documents, Error> {
    let mut result: Documents = Vec::new();

    for document in self.docs.lock().unwrap().iter() {
      if self.perform_query(&query, document)? {
        result.push(document.clone());
      }
    }
    Ok(result)
  }

  #[cfg(not(feature = "sync"))]
  pub async fn find(&self, query: &Value) -> Result<Documents, Error> {
    let mut result: Documents = Vec::new();

    for document in self.docs.lock().await.iter() {
      if self.perform_query(&query, document)? {
        result.push(document.clone());
      }
    }
    Ok(result)
  }

  #[cfg(not(feature = "sync"))]
  pub async fn find_and_update(&self, query: &Value, update: &Value) -> Result<u64, Error> {
    let mut documents_updated: u64 = 0;
    for document in self.docs.lock().await.iter_mut() {
      if self.perform_query(&query, document)? {
        self.perform_update(update, document)?;
        documents_updated += 1;
      }
    }
    Ok(documents_updated)
  }

  #[cfg(feature = "sync")]
  pub fn find_and_update(&self, query: &Value, update: &Value) -> Result<u64, Error> {
    let mut documents_updated: u64 = 0;
    for document in self.docs.lock().unwrap().iter_mut() {
      if self.perform_query(&query, document)? {
        self.perform_update(update, document)?;
        documents_updated += 1;
      }
    }
    Ok(documents_updated)
  }

  #[cfg(not(feature = "sync"))]
  pub async fn find_and_delete(&self, query: &Value) -> Result<Documents, Error> {
    let mut docs_deleted: Documents = Vec::new();
    let mut docs_guard = self.docs.lock().await;

    docs_guard.retain(|document| {
      if self.perform_query(&query, &document).unwrap_or(true) {
        docs_deleted.push(document.clone());
        return false;
      }
      true
    });
    Ok(docs_deleted)
  }

  #[cfg(feature = "sync")]
  pub fn find_and_delete(&self, query: &Value) -> Result<Documents, Error> {
    let mut docs_deleted: Documents = Vec::new();
    let mut docs_guard = self.docs.lock().unwrap();

    docs_guard.retain(|document| {
      if self.perform_query(&query, &document).unwrap_or(true) {
        docs_deleted.push(document.clone());
        return false;
      }
      true
    });
    Ok(docs_deleted)
  }

  fn perform_update<'d>(
    &self,
    update: &Value,
    document: &'d mut Value,
  ) -> Result<&'d mut Value, Error> {
    if has_update_operations(update)? {
      self.perform_update_operations(update, document)?;
    } else {
      *document = update.clone();
    }

    Ok(document)
  }

  fn perform_update_operations(&self, update: &Value, document: &mut Value) -> Result<(), Error> {
    let update = update.as_object().unwrap();
    for key in update.keys() {
      match key.as_str() {
        SET => self.handle_set(&update[key], document)?,
        UNSET => self.hanlde_unset(&update[key], document)?,
        INC => self.handle_inc(&update[key], document)?,
        MUL => self.handle_mul(&update[key], document)?,
        _ => {
          return Err(Error::MQInvalidOp(format!(
            "{} is invalid update operator.",
            key
          )))
        }
      }
    }

    Ok(())
  }

  fn run_op_on_value<F>(&self, key: &str, document: &mut Value, op: F) -> Result<(), Error>
  where
    F: Fn(&str, &mut Value) -> Result<(), Error>,
  {
    let (is_embedded, key_parts) = is_embedded_query(key);

    if !is_embedded {
      return op(key, document);
    }

    let key_parts_rest = &key_parts[1..];
    self.run_op_on_value(&key_parts_rest.join("."), &mut document[key_parts[0]], op)?;
    Ok(())
  }

  fn handle_set(&self, update: &Value, document: &mut Value) -> Result<(), Error> {
    let update = match update.as_object() {
      Some(u) => u,
      None => {
        return Err(Error::MQInvalidValue(String::from(
          "$set operator value must be JSON object",
        )))
      }
    };

    for (k, v) in update {
      if has_ops(k) {
        return Err(Error::MQOpNotAllowedInMultipartKey);
      }
      let handler = |k: &str, d: &mut Value| {
        d[k] = v.clone();
        Ok(())
      };
      self.run_op_on_value(k, document, handler)?;
    }

    Ok(())
  }

  fn hanlde_unset(&self, update: &Value, document: &mut Value) -> Result<(), Error> {
    let update = match update.as_object() {
      Some(u) => u,
      None => {
        return Err(Error::MQInvalidValue(String::from(
          "$unset operator value must be JSON object",
        )))
      }
    };

    let handler = |k: &str, d: &mut Value| {
      let document = d.as_object_mut().unwrap();
      document.remove(k);
      Ok(())
    };
    for key in update.keys() {
      self.run_op_on_value(key, document, handler)?;
    }

    Ok(())
  }

  fn handle_inc(&self, update: &Value, document: &mut Value) -> Result<(), Error> {
    self.handle_math_ops(update, document, MathOpType::Inc)
  }

  fn handle_mul(&self, update: &Value, document: &mut Value) -> Result<(), Error> {
    self.handle_math_ops(update, document, MathOpType::Mul)
  }

  fn handle_math_ops(
    &self,
    update: &Value,
    document: &mut Value,
    op_type: MathOpType,
  ) -> Result<(), Error> {
    let update = match update.as_object() {
      Some(u) => u,
      None => {
        return Err(Error::MQInvalidValue(String::from(
          "$inc operator value must be JSON object",
        )))
      }
    };

    for (k, v) in update {
      if has_ops(k) {
        return Err(Error::MQOpNotAllowedInMultipartKey);
      }

      if !v.is_number() {
        return Err(Error::MQInvalidType);
      }

      let handler = |k: &str, d: &mut Value| {
        match (&d[k], &v.clone()) {
          (Value::Number(dk), Value::Number(v)) => {
            if let Some(d_f) = dk.as_f64() {
              if let Some(v_f) = v.as_f64() {
                match op_type {
                  MathOpType::Inc => d[k] = json!(sum(d_f, v_f)),
                  MathOpType::Mul => d[k] = json!(mul(d_f, v_f)),
                };
              }
            } else if let Some(d_i) = dk.as_i64() {
              if let Some(v_i) = v.as_i64() {
                match op_type {
                  MathOpType::Inc => d[k] = json!(sum(d_i, v_i)),
                  MathOpType::Mul => d[k] = json!(mul(d_i, v_i)),
                };
              }
            } else if let Some(d_u) = dk.as_u64() {
              if let Some(v_u) = v.as_u64() {
                match op_type {
                  MathOpType::Inc => d[k] = json!(sum(d_u, v_u)),
                  MathOpType::Mul => d[k] = json!(mul(d_u, v_u)),
                };
              }
            }
          }
          _ => {
            return Err(Error::MQInvalidType);
          }
        };

        Ok(())
      };

      self.run_op_on_value(k, document, handler)?;
    }

    Ok(())
  }

  fn get_document_value<'d>(&self, key: &str, document: &'d Value) -> Result<&'d Value, Error> {
    // find if the value should be the immediate value of the key or embedded document
    let (is_embedded, key_parts) = is_embedded_query(key);
    let mut doc_value = &document[key];
    if is_embedded {
      doc_value = self.get_nested_document_value(key_parts, document)?;
    }

    Ok(doc_value)
  }

  fn perform_query(&self, query: &Value, document: &Value) -> Result<bool, Error> {
    let query_obj = query.as_object().unwrap();
    let mut is_found = false;

    if query_obj.keys().len() == 0 {
      return Ok(true);
    }

    for key in query_obj.keys() {
      is_key_valid_op(key)?;
      if is_logical_op(key) {
        let logical_op_list = &query[key];
        is_found = self.perform_logical_op(key, logical_op_list, document)?;
        break;
      } else if is_comparison_op(&query[key]) {
        let (_, op, comp_value) = has_comparison_op(&query[key]);
        is_found =
          self.perform_comparison_op(op, comp_value, self.get_document_value(key, document)?)?;
        continue;
      } else {
        is_found = &query[key] == self.get_document_value(key, document)?;
        if !is_found {
          break;
        }
      }
    }

    Ok(is_found)
  }

  fn perform_logical_op(
    &self,
    op: &str,
    logical_op_list: &Value,
    document: &Value,
  ) -> Result<bool, Error> {
    let op_list = match logical_op_list.as_array() {
      Some(l) => l,
      None => return Err(Error::MQError(String::from("Logical operation"))),
    };

    let mut op_success_list: Vec<bool> = Vec::new();

    for op_query in op_list {
      op_success_list.push(self.perform_query(op_query, document)?);
    }
    Ok(match op {
      OR => any(op_success_list),
      AND => all(op_success_list),
      _ => false,
    })
  }

  fn perform_comparison_op(
    &self,
    op: &str,
    compare_to_value: &Value,
    doc_value: &Value,
  ) -> Result<bool, Error> {
    match op {
      GT | GTE | LT | LTE | NE | EQ => self.perform_value_compares(op, compare_to_value, doc_value),
      IN | NIN => Ok(false),
      _ => Err(Error::MQInvalidOp(op.to_string())),
    }
  }

  fn perform_value_compares(
    &self,
    op: &str,
    compare_to_value: &Value,
    doc_value: &Value,
  ) -> Result<bool, Error> {
    if compare_to_value.is_object() {
      return Err(Error::MQInvalidValue(format!(
        "{} expects value not array or object.",
        op
      )));
    }

    match (doc_value, compare_to_value) {
      (Value::Number(d), Value::Number(c)) => {
        if let Some(doc_u) = d.as_u64() {
          if let Some(comp_u) = c.as_u64() {
            return self.compare(op, doc_u, comp_u);
          }
        } else if let Some(doc_i) = d.as_i64() {
          if let Some(comp_i) = d.as_i64() {
            return self.compare(op, doc_i, comp_i);
          }
        } else if let Some(doc_f) = d.as_f64() {
          if let Some(comp_f) = d.as_f64() {
            return self.compare(op, doc_f, comp_f);
          }
        }
      }
      (Value::String(d), Value::String(c)) => return self.compare(op, d, c),
      (Value::Array(d), Value::Array(c)) => {
        return self.perform_array_to_array_compare(op, d, c);
      }
      (Value::Array(d), Value::Number(c)) => {
        return self.perform_array_to_value_compare(op, d, &serde_json::json!(c));
      }
      (Value::Array(d), Value::String(c)) => {
        return self.perform_array_to_value_compare(op, d, &serde_json::json!(c))
      }
      _ => return Err(Error::MQInvalidType),
    }

    Err(Error::MQInvalidType)
  }

  fn compare<T: PartialOrd>(&self, op: &str, d: T, c: T) -> Result<bool, Error> {
    Ok(match op {
      GT => d > c,
      GTE => d >= c,
      LT => d < c,
      LTE => d <= c,
      NE => d != c,
      EQ => d == c,
      _ => {
        return Err(Error::MQInvalidOp(format!(
          "{} not supported for compare.",
          op
        )))
      }
    })
  }

  fn perform_array_to_array_compare(
    &self,
    op: &str,
    document_value: &Vec<Value>,
    compare_value: &Vec<Value>,
  ) -> Result<bool, Error> {
    if document_value == compare_value {
      return Ok(true);
    }

    let mut matches: Vec<bool> = Vec::new();
    for elem in document_value {
      let is_match = match op {
        NE => elem != &serde_json::json!(compare_value),
        EQ => elem == &serde_json::json!(compare_value),
        GT | GTE | LT | LTE | _ => {
          return Err(Error::MQInvalidOp(format!(
            "{} is not valid for array comparison.",
            op
          )));
        }
      };
      matches.push(is_match);
    }

    Ok(any(matches))
  }

  fn perform_array_to_value_compare(
    &self,
    op: &str,
    document_value: &Vec<Value>,
    compare_value: &Value,
  ) -> Result<bool, Error> {
    let mut matches: Vec<bool> = Vec::new();
    for elem in document_value {
      let mut is_match = false;
      if !elem.is_array() {
        // do not check nested arrays
        is_match = self.perform_value_compares(op, compare_value, elem)?
      }
      matches.push(is_match);
    }

    Ok(any(matches))
  }

  fn get_nested_document_value<'b>(
    &self,
    nested_keys: Vec<&str>,
    document: &'b Value,
  ) -> Result<&'b Value, Error> {
    let mut current_value: &Value = document;

    for key in nested_keys {
      if is_op(key) {
        return Err(Error::MQInvalidOp(format!(
          "{} operators not allowed in nested paths.",
          key
        )));
      }
      current_value = &current_value[key];
    }

    Ok(current_value)
  }
}