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
use versatile_data::{
Activity
,KeyValue
,Update
,UpdateTerm
};
use crate::{
Database
,CollectionRow
};
pub enum UpdateParent<'a>{
Inherit
,Overwrite(Vec<(&'a str,CollectionRow)>)
}
pub struct TransactionRecord<'a>{
collection_id:u32
,update:Update
,activity:Activity
,term_begin:UpdateTerm
,term_end:UpdateTerm
,fields:Vec<KeyValue<'a>>
,parents:UpdateParent<'a>
,childs:Vec<(&'a str,Vec<TransactionRecord<'a>>)>
}
impl<'a> TransactionRecord<'a>{
pub fn new(
collection_id:u32
,update:Update
,activity:Activity
,term_begin:UpdateTerm
,term_end:UpdateTerm
,fields:Vec<KeyValue<'a>>
,parents:UpdateParent<'a>
,childs:Vec<(&'a str,Vec<TransactionRecord<'a>>)>
)->TransactionRecord<'a>{
TransactionRecord{
collection_id
,update
,activity
,term_begin
,term_end
,fields
,parents
,childs
}
}
}
pub struct Transaction<'a>{
database:&'a mut Database
,records:Vec<TransactionRecord<'a>>
,deletes:Vec<CollectionRow>
}
impl<'a> Transaction<'a>{
pub fn new(database:&'a mut Database)->Transaction{
Transaction{
database
,records:Vec::new()
,deletes:Vec::new()
}
}
pub fn update(&mut self,records:&mut Vec::<TransactionRecord<'a>>){
self.records.append(records);
}
pub fn delete(&mut self,collection_id:u32,row:u32){
self.deletes.push(CollectionRow::new(collection_id,row));
}
pub fn commit(&mut self){
Self::register_recursive(&mut self.database,&self.records,None);
for i in &self.deletes{
Self::delete_recursive(&mut self.database,&i);
}
self.records=Vec::new();
self.deletes=Vec::new();
}
fn delete_recursive(database:&mut Database,target:&CollectionRow){
let c=database.relation.index_parent().select_by_value(target);
for relation_row in c{
if let Some(child)=database.relation.index_child().value(relation_row){
Self::delete_recursive(database,&child);
if let Some(collection)=database.collection_mut(child.collection_id()){
collection.delete(child.row());
}
}
database.relation.delete(relation_row);
}
}
fn register_recursive(
database:&mut Database
,records:&Vec::<TransactionRecord>
,incidentally_parent:Option<(&str,CollectionRow)>
){
for r in records.iter(){
if let Some(data)=database.collection_mut(r.collection_id){
let row=data.update(r.update,r.activity,r.term_begin,r.term_end,&r.fields);
if let UpdateParent::Overwrite(ref parents)=r.parents{
if let Update::Row(_)=r.update{
let relations=database.relation().index_child().select_by_value(
&CollectionRow::new(r.collection_id,row)
);
for i in relations{
database.relation_mut().delete(i);
}
}
for (relation_key,parent) in parents{
database.relation_mut().insert(
relation_key
,*parent
,CollectionRow::new(r.collection_id,row)
);
}
}
if let Some((relation_key,parent_collection_row))=incidentally_parent{
database.relation_mut().insert(
relation_key
,parent_collection_row
,CollectionRow::new(r.collection_id,row)
);
}
for (relation_key,childs) in &r.childs{
Self::register_recursive(
database
,&childs
,Some((
relation_key
,CollectionRow::new(r.collection_id,row)
))
);
}
}
}
}
}