2025-02-02 12:58:10 -05:00
|
|
|
pub mod database;
|
|
|
|
pub mod global;
|
|
|
|
pub mod id;
|
|
|
|
mod record;
|
|
|
|
pub mod table;
|
|
|
|
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Clone, Debug)]
|
|
|
|
struct IDPath {
|
|
|
|
path: Vec<Uuid>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IDPath {
|
|
|
|
fn new() -> Self {
|
|
|
|
let mut path = Vec::new();
|
2025-02-04 01:40:40 -05:00
|
|
|
path.push(Uuid::nil());
|
2025-02-02 12:58:10 -05:00
|
|
|
Self { path: path }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next() -> Uuid {
|
|
|
|
Uuid::new_v4()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extend(&self, addition: Uuid) -> Self {
|
|
|
|
let mut result = self.clone();
|
2025-02-04 01:40:40 -05:00
|
|
|
result.path.pop();
|
2025-02-02 12:58:10 -05:00
|
|
|
result.path.push(addition);
|
2025-02-04 01:40:40 -05:00
|
|
|
result.path.push(Uuid::nil());
|
2025-02-02 12:58:10 -05:00
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod ids {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_idpath() {
|
2025-02-04 01:40:40 -05:00
|
|
|
let expected = [Uuid::nil()];
|
2025-02-02 12:58:10 -05:00
|
|
|
let id = IDPath::new();
|
2025-02-04 01:40:40 -05:00
|
|
|
assert_eq!(id.path, expected);
|
2025-02-02 12:58:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn next_is_random() {
|
|
|
|
let mut ids: Vec<Uuid> = Vec::new();
|
|
|
|
for _ in 0..10 {
|
|
|
|
let id = IDPath::next();
|
|
|
|
assert!(!ids.contains(&id), "{} is a duplicate", id);
|
|
|
|
ids.push(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extend_idpath() {
|
|
|
|
let mut path: Vec<Uuid> = Vec::new();
|
2025-02-04 01:40:40 -05:00
|
|
|
path.push(Uuid::nil());
|
2025-02-02 12:58:10 -05:00
|
|
|
let mut id = IDPath::new();
|
2025-02-04 01:40:40 -05:00
|
|
|
for count in 1..5 {
|
|
|
|
assert_eq!(id.path.len(), count);
|
2025-02-02 12:58:10 -05:00
|
|
|
let extended = IDPath::next();
|
|
|
|
id = id.extend(extended.clone());
|
2025-02-04 01:40:40 -05:00
|
|
|
path.pop();
|
2025-02-02 12:58:10 -05:00
|
|
|
path.push(extended);
|
2025-02-04 01:40:40 -05:00
|
|
|
path.push(Uuid::nil());
|
2025-02-02 12:58:10 -05:00
|
|
|
assert_eq!(id.path, path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Cache {
|
|
|
|
data: BTreeMap<String, IDPath>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Cache {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
data: BTreeMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_database<S>(&mut self, name: S)
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
|
|
|
self.data.insert(name.into(), IDPath::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_databases(&self) -> Vec<String> {
|
|
|
|
self.data.keys().cloned().collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod caches {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_cache() {
|
|
|
|
let cache = Cache::new();
|
|
|
|
let dbs = cache.get_databases();
|
|
|
|
assert!(dbs.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_databases() {
|
|
|
|
let mut cache = Cache::new();
|
|
|
|
cache.add_database("zed");
|
|
|
|
cache.add_database("alpha".to_string());
|
|
|
|
cache.add_database("beta");
|
|
|
|
cache.add_database("gamma".to_string());
|
|
|
|
assert_eq!(cache.get_databases(), ["alpha", "beta", "gamma", "zed"]);
|
|
|
|
}
|
|
|
|
}
|