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
use std::collections::BTreeMap;
use std::collections::HashMap;
pub use versatile_data::{
Data
};
pub struct Collection{
id:u32
,data:Data
}
impl Collection{
pub fn new(id:u32,data:Data)->Collection{
Collection{
id
,data
}
}
pub fn data(&self)->&Data{
&self.data
}
pub fn data_mut(&mut self)->&mut Data{
&mut self.data
}
}
pub struct Database{
root_dir:String
,collections:HashMap<String,Collection>
,collections_id_map:BTreeMap<u32,String>
}
impl Database{
pub fn new(dir:&str)->Database{
Database{
root_dir:if dir.ends_with("/") || dir.ends_with("\\"){
let mut d=dir.to_string();
d.pop();
d
}else{
dir.to_string()
}
,collections:HashMap::new()
,collections_id_map:BTreeMap::new()
}
}
pub fn collection(&mut self,name:&str)->Option<&mut Collection>{
if !self.collections.contains_key(name){
let mut max_id=0;
let collections_dir=self.root_dir.to_string()+"/collection/";
if let Ok(dir)=std::fs::read_dir(&collections_dir){
for d in dir.into_iter(){
if let Ok(d)=d{
if let Ok(dt)=d.file_type(){
if dt.is_dir(){
if let Some(fname)=d.path().file_name(){
if let Some(fname)=fname.to_str(){
let s: Vec<&str>=fname.split("_").collect();
if s.len()>1{
if let Ok(i)=s[0].parse(){
max_id=std::cmp::max(max_id,i);
}
}
}
}
}
}
}
}
}
let collection_id=max_id+1;
if let Some(data)=Data::new(&(collections_dir+"/"+&collection_id.to_string()+"_"+name)){
self.collections_id_map.insert(collection_id,name.to_string());
self.collections.insert(name.to_string(),Collection::new(collection_id,data));
}
}
self.collections.get_mut(name)
}
}