vantage_dataset/im/
mod.rs1use indexmap::IndexMap;
4use std::collections::HashMap;
5use std::sync::{Arc, Mutex};
6use vantage_types::Record;
7
8pub mod dataset_insertable;
9pub mod dataset_readable;
10pub mod dataset_writable;
11pub mod im_table;
12
13pub mod valueset_insertable;
14pub mod valueset_readable;
15pub mod valueset_writable;
16pub use im_table::ImTable;
17
18type TableStorage<V> = Arc<Mutex<HashMap<String, IndexMap<String, Record<V>>>>>;
20
21#[derive(Debug)]
28pub struct ImDataSource<V = serde_json::Value> {
29 tables: TableStorage<V>,
31}
32
33impl<V> Clone for ImDataSource<V> {
34 fn clone(&self) -> Self {
35 Self {
36 tables: self.tables.clone(),
37 }
38 }
39}
40
41impl<V> ImDataSource<V> {
42 pub fn new() -> Self {
43 Self {
44 tables: Arc::new(Mutex::new(HashMap::new())),
45 }
46 }
47}
48
49impl<V: Clone> ImDataSource<V> {
50 pub(super) fn with_table<R>(
54 &self,
55 table_name: &str,
56 f: impl FnOnce(&IndexMap<String, Record<V>>) -> R,
57 ) -> R {
58 let tables = self.tables.lock().unwrap();
59 match tables.get(table_name) {
60 Some(table) => f(table),
61 None => f(&IndexMap::new()),
62 }
63 }
64
65 pub fn table_len(&self, table_name: &str) -> usize {
70 self.with_table(table_name, |table| table.len())
71 }
72
73 pub(super) fn with_table_mut<R>(
77 &self,
78 table_name: &str,
79 f: impl FnOnce(&mut IndexMap<String, Record<V>>) -> R,
80 ) -> R {
81 let mut tables = self.tables.lock().unwrap();
82 let table = tables.entry(table_name.to_string()).or_default();
83 f(table)
84 }
85}
86
87impl<V> Default for ImDataSource<V> {
88 fn default() -> Self {
89 Self::new()
90 }
91}