-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations.rs
More file actions
1024 lines (929 loc) · 38.3 KB
/
operations.rs
File metadata and controls
1024 lines (929 loc) · 38.3 KB
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::graph::{all_simple_paths, GraphEdge, GraphNode, OperationGraph};
use crate::models::file_types::FileTypes;
use crate::models::traits::*;
use crate::operation_management::{
load_changeset, load_changeset_dependencies, load_changeset_models,
};
use crate::views::patch::get_change_graph;
use petgraph::graphmap::{DiGraphMap, UnGraphMap};
use petgraph::visit::{Dfs, Reversed};
use petgraph::Direction;
use rusqlite::session::ChangesetIter;
use rusqlite::types::Value;
use rusqlite::Error as SQLError;
use rusqlite::{params, params_from_iter, Connection, Result as SQLResult, Row};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::io::Read;
use std::string::ToString;
use thiserror::Error;
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct Operation {
pub hash: String,
pub db_uuid: String,
pub parent_hash: Option<String>,
pub branch_id: i64,
pub change_type: String,
}
#[derive(Debug, Error, PartialEq)]
pub enum OperationError {
#[error("SQLite Error: {0}")]
SqliteError(#[from] SQLError),
}
impl Operation {
pub fn create(
conn: &Connection,
db_uuid: &str,
change_type: &str,
hash: &str,
) -> SQLResult<Operation> {
let current_op = OperationState::get_operation(conn, db_uuid);
let current_branch_id =
OperationState::get_current_branch(conn, db_uuid).expect("No branch is checked out.");
// if we are in the middle of a branch's operations, and not on a new branch's creation point
// we cannot create a new operation as that would create a bifurcation in a branch's order
// of operations. We ensure there is no child operation in this branch of the current operation.
if let Some(op_hash) = current_op.clone() {
let count: i64 = conn
.query_row(
"select count(*) from operation where branch_id = ?1 AND parent_hash = ?2 AND hash not in (select operation_hash from branch_masked_operations where branch_id = ?1);",
(current_branch_id, op_hash),
|row| row.get(0),
)
.unwrap();
if count != 0 {
panic!("The current operation is in the middle of a branch. A new operation would create a bifurcation in the branch lineage. Create a new branch if you wish to bifurcate the current set of operations.");
}
}
let query = "INSERT INTO operation (hash, db_uuid, change_type, parent_hash, branch_id) VALUES (?1, ?2, ?3, ?4, ?5);";
let mut stmt = conn.prepare(query).unwrap();
stmt.execute(params_from_iter(vec![
Value::from(hash.to_string()),
Value::from(db_uuid.to_string()),
Value::from(change_type.to_string()),
Value::from(current_op.clone()),
Value::from(current_branch_id),
]))?;
let operation = Operation {
hash: hash.to_string(),
db_uuid: db_uuid.to_string(),
parent_hash: current_op.clone(),
branch_id: current_branch_id,
change_type: change_type.to_string(),
};
// TODO: error condition here where we can write to disk but transaction fails
OperationState::set_operation(conn, &operation.db_uuid, &operation.hash);
Branch::set_start_operation(conn, current_branch_id, &operation.hash);
Ok(operation)
}
pub fn add_file(
conn: &Connection,
operation_hash: &str,
file_addition_id: i64,
) -> SQLResult<()> {
let query =
"INSERT INTO operation_files (operation_hash, file_addition_id) VALUES (?1, ?2)";
let mut stmt = conn.prepare(query).unwrap();
stmt.execute(params![operation_hash.to_string(), file_addition_id])?;
Ok(())
}
pub fn get_upstream(conn: &Connection, operation_hash: String) -> Vec<String> {
let query = "WITH RECURSIVE operations(operation_hash, depth) AS ( \
select ?1, 0 UNION \
select parent_hash, depth + 1 from operation join operations ON hash=operation_hash \
) SELECT operation_hash, depth from operations where operation_hash is not null order by depth desc;";
let mut stmt = conn.prepare(query).unwrap();
stmt.query_map((operation_hash,), |row| row.get(0))
.unwrap()
.map(|id| id.unwrap())
.collect::<Vec<String>>()
}
pub fn get_operation_graph(conn: &Connection) -> OperationGraph {
let mut graph = OperationGraph::new();
let operations = Operation::query(conn, "select * from operation;", rusqlite::params![]);
for op in operations.iter() {
graph.add_node(&op.hash);
if let Some(v) = op.parent_hash.clone() {
graph.add_node(&v);
graph.add_edge(&v, &op.hash);
}
}
graph
}
pub fn get_change_graph(
conn: &Connection,
hash: &str,
) -> Result<HashMap<i64, DiGraphMap<GraphNode, GraphEdge>>, OperationError> {
let operation = Operation::get_by_hash(conn, hash)?;
let changeset = load_changeset(&operation);
let dependencies = load_changeset_dependencies(&operation);
let input: &mut dyn Read = &mut changeset.as_slice();
let mut iter = ChangesetIter::start_strm(&input).unwrap();
let new_models = load_changeset_models(&mut iter);
Ok(get_change_graph(&new_models, &dependencies))
}
pub fn get_path_between(
conn: &Connection,
source_id: &str,
target_id: &str,
) -> Vec<(String, Direction, String)> {
let directed_graph = Operation::get_operation_graph(conn);
let source_node = directed_graph.get_node(source_id);
let target_node = directed_graph.get_node(target_id);
let mut undirected_graph: UnGraphMap<usize, ()> = Default::default();
for node in directed_graph.graph.nodes() {
undirected_graph.add_node(node);
}
for (source, target, _weight) in directed_graph.graph.all_edges() {
undirected_graph.add_edge(source, target, ());
}
let mut patch_path: Vec<(String, Direction, String)> = vec![];
for path in all_simple_paths(&undirected_graph, source_node, target_node) {
let mut last_node = source_node;
for node in &path[1..] {
if *node != source_node {
for (_edge_src, edge_target, _edge_weight) in directed_graph
.graph
.edges_directed(last_node, Direction::Outgoing)
{
if edge_target == *node {
patch_path.push((
directed_graph.get_key(last_node),
Direction::Outgoing,
directed_graph.get_key(*node),
));
break;
}
}
for (edge_src, _edge_target, _edge_weight) in directed_graph
.graph
.edges_directed(last_node, Direction::Incoming)
{
if edge_src == *node {
patch_path.push((
directed_graph.get_key(last_node),
Direction::Incoming,
directed_graph.get_key(*node),
));
break;
}
}
}
last_node = *node;
}
}
patch_path
}
pub fn get_by_hash(conn: &Connection, op_hash: &str) -> SQLResult<Operation> {
Operation::get(
conn,
"select * from operation where hash LIKE ?1",
params![Value::from(format!("{op_hash}%"))],
)
}
}
impl Query for Operation {
type Model = Operation;
fn process_row(row: &Row) -> Self::Model {
Operation {
hash: row.get(0).unwrap(),
db_uuid: row.get(1).unwrap(),
parent_hash: row.get(2).unwrap(),
branch_id: row.get(3).unwrap(),
change_type: row.get(4).unwrap(),
}
}
}
pub struct OperationFile {
pub file_path: String,
pub file_type: FileTypes,
}
pub struct OperationInfo {
pub files: Vec<OperationFile>,
pub description: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct FileAddition {
pub id: i64,
pub file_path: String,
pub file_type: FileTypes,
}
impl Query for FileAddition {
type Model = FileAddition;
fn process_row(row: &Row) -> Self::Model {
Self::Model {
id: row.get(0).unwrap(),
file_path: row.get(1).unwrap(),
file_type: row.get(2).unwrap(),
}
}
}
impl FileAddition {
pub fn create(conn: &Connection, file_path: &str, file_type: FileTypes) -> FileAddition {
let query =
"INSERT INTO file_addition (file_path, file_type) VALUES (?1, ?2) RETURNING (id)";
let mut stmt = conn.prepare(query).unwrap();
let mut rows = stmt
.query_map(
params_from_iter(vec![
Value::from(file_path.to_string()),
Value::from(file_type),
]),
|row| {
Ok(FileAddition {
id: row.get(0)?,
file_path: file_path.to_string(),
file_type,
})
},
)
.unwrap();
rows.next().unwrap().unwrap()
}
pub fn get_files_for_operation(conn: &Connection, operation_hash: &str) -> Vec<FileAddition> {
let query = "select fa.* from file_addition fa left join operation_files of on (fa.id = of.file_addition_id) where of.operation_hash = ?1";
let mut stmt = conn.prepare(query).unwrap();
let rows = stmt
.query_map(params![operation_hash], |row| {
Ok(FileAddition::process_row(row))
})
.unwrap();
rows.map(|row| row.unwrap()).collect()
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct OperationSummary {
pub id: i64,
pub operation_hash: String,
pub summary: String,
}
impl Query for OperationSummary {
type Model = OperationSummary;
fn process_row(row: &Row) -> Self::Model {
Self::Model {
id: row.get(0).unwrap(),
operation_hash: row.get(1).unwrap(),
summary: row.get(2).unwrap(),
}
}
}
impl OperationSummary {
pub fn create(conn: &Connection, operation_hash: &str, summary: &str) -> OperationSummary {
let query =
"INSERT INTO operation_summary (operation_hash, summary) VALUES (?1, ?2) RETURNING (id)";
let mut stmt = conn.prepare(query).unwrap();
let operation_hash = operation_hash.to_string();
let mut rows = stmt
.query_map(
params_from_iter(vec![
Value::from(operation_hash.clone()),
Value::from(summary.to_string()),
]),
|row| {
Ok(OperationSummary {
id: row.get(0)?,
operation_hash: operation_hash.clone(),
summary: summary.to_string(),
})
},
)
.unwrap();
rows.next().unwrap().unwrap()
}
pub fn set_message(conn: &Connection, id: i64, message: &str) -> SQLResult<()> {
let query = "UPDATE operation_summary SET summary = ?2 where id = ?1";
let mut stmt = conn.prepare(query).unwrap();
stmt.execute(params![id, message])?;
Ok(())
}
}
#[derive(Clone, Debug)]
pub struct Branch {
pub id: i64,
pub db_uuid: String,
pub name: String,
pub start_operation_hash: Option<String>,
pub current_operation_hash: Option<String>,
}
impl Branch {
pub fn create(conn: &Connection, db_uuid: &str, branch_name: &str) -> Branch {
let current_operation_hash = OperationState::get_operation(conn, db_uuid);
let mut stmt = conn.prepare_cached("insert into branch (db_uuid, name, start_operation_hash, current_operation_hash) values (?1, ?2, ?3, ?3) returning (id);").unwrap();
let mut rows = stmt
.query_map(
(db_uuid, branch_name, current_operation_hash.clone()),
|row| {
Ok(Branch {
id: row.get(0)?,
db_uuid: db_uuid.to_string(),
name: branch_name.to_string(),
start_operation_hash: current_operation_hash.clone(),
current_operation_hash: current_operation_hash.clone(),
})
},
)
.unwrap();
match rows.next().unwrap() {
Ok(res) => res,
Err(rusqlite::Error::SqliteFailure(err, details)) => {
if err.code == rusqlite::ErrorCode::ConstraintViolation {
panic!("Branch already exists");
} else {
panic!("something bad happened querying the database {err:?} {details:?}");
}
}
Err(_) => {
panic!("something bad happened querying the database");
}
}
}
pub fn delete(conn: &Connection, db_uuid: &str, branch_name: &str) {
if branch_name == "main" {
panic!("Main branch cannot be deleted");
}
if let Some(branch) = Branch::get_by_name(conn, db_uuid, branch_name) {
let branch_id = branch.id;
if let Some(current_branch) = OperationState::get_current_branch(conn, db_uuid) {
if current_branch == branch_id {
panic!("Unable to delete the branch that is currently active.");
}
}
conn.execute("delete from branch where id = ?1", (branch_id,))
.expect("Error deleting from branch table.");
} else {
panic!("No branch named {branch_name} in database.");
}
}
pub fn query(conn: &Connection, query: &str, placeholders: Vec<Value>) -> Vec<Branch> {
let mut stmt = conn.prepare(query).unwrap();
let rows = stmt
.query_map(params_from_iter(placeholders), |row| {
Ok(Branch {
id: row.get(0)?,
db_uuid: row.get(1)?,
name: row.get(2)?,
start_operation_hash: row.get(3)?,
current_operation_hash: row.get(4)?,
})
})
.unwrap();
let mut objs = vec![];
for row in rows {
objs.push(row.unwrap());
}
objs
}
pub fn get_by_name(conn: &Connection, db_uuid: &str, branch_name: &str) -> Option<Branch> {
let mut branch: Option<Branch> = None;
for result in Branch::query(
conn,
"select * from branch where db_uuid = ?1 and name = ?2",
vec![
Value::from(db_uuid.to_string()),
Value::from(branch_name.to_string()),
],
)
.iter()
{
branch = Some(result.clone());
}
branch
}
pub fn get_by_id(conn: &Connection, branch_id: i64) -> Option<Branch> {
let mut branch: Option<Branch> = None;
for result in Branch::query(
conn,
"select * from branch where id = ?1",
vec![Value::from(branch_id)],
)
.iter()
{
branch = Some(result.clone());
}
branch
}
pub fn set_current_operation(conn: &Connection, branch_id: i64, operation_hash: &str) {
conn.execute(
"UPDATE branch set current_operation_hash = ?2 where id = ?1",
(branch_id, operation_hash.to_string()),
)
.unwrap();
}
pub fn set_start_operation(conn: &Connection, branch_id: i64, operation_hash: &str) {
conn.execute(
"UPDATE branch set start_operation_hash = ?2 where id = ?1 and start_operation_hash is null",
(branch_id, operation_hash.to_string()),
)
.unwrap();
}
pub fn get_operations(conn: &Connection, branch_id: i64) -> Vec<Operation> {
let branch = Branch::get_by_id(conn, branch_id)
.unwrap_or_else(|| panic!("No branch with id {branch_id}."));
let mut graph = Operation::get_operation_graph(conn);
let mut operations: Vec<Operation> = vec![];
let masked_operations = Branch::get_masked_operations(conn, branch_id);
for op in masked_operations.iter() {
graph.remove_key(op);
}
if let Some(creation_hash) = branch.start_operation_hash {
let rev_graph = Reversed(&graph.graph);
let creation_node = graph.get_node(&creation_hash);
let mut dfs = Dfs::new(rev_graph, creation_node);
while let Some(ancestor) = dfs.next(rev_graph) {
let ancestor_node = graph.get_key(ancestor);
operations.insert(
0,
Operation::get_by_hash(conn, &ancestor_node)
.unwrap_or_else(|_| panic!("Hash {ancestor_node} does not exist.")),
);
}
let mut branch_operations: HashSet<String> = HashSet::from_iter(
Operation::query(
conn,
"select * from operation where branch_id = ?1;",
rusqlite::params!(Value::from(branch_id)),
)
.iter()
.map(|op| op.hash.clone())
.collect::<Vec<String>>(),
);
branch_operations.extend(
operations
.iter()
.map(|op| op.hash.clone())
.collect::<Vec<String>>(),
);
// remove all nodes not in our branch operations. We do this here because upstream operations
// may be created in a different branch_id but shared with this branch.
for hash in graph.node_ids.clone().keys() {
if !branch_operations.contains(hash) {
graph.remove_key(hash);
}
}
// Now traverse down from our starting point, we should only have 1 valid path that is not
// cutoff and in our branch operations
let mut dfs = Dfs::new(&graph.graph, creation_node);
// get rid of the first node which is creation_id
dfs.next(&graph.graph);
while let Some(child) = dfs.next(&graph.graph) {
let child_hash = graph.get_key(child);
operations.push(
Operation::get_by_hash(conn, &child_hash)
.unwrap_or_else(|_| panic!("Hash {child_hash} does not exist.")),
);
}
}
operations
}
pub fn mask_operation(conn: &Connection, branch_id: i64, operation_hash: &str) {
conn.execute("INSERT OR IGNORE into branch_masked_operations (branch_id, operation_hash) values (?1, ?2);", (branch_id, operation_hash.to_string())).unwrap();
}
pub fn get_masked_operations(conn: &Connection, branch_id: i64) -> Vec<String> {
let mut stmt = conn
.prepare("select operation_hash from branch_masked_operations where branch_id = ?1")
.unwrap();
stmt.query_map((branch_id,), |row| row.get(0))
.unwrap()
.map(|res| res.unwrap())
.collect::<Vec<String>>()
}
}
pub struct OperationState {}
impl OperationState {
pub fn set_operation(conn: &Connection, db_uuid: &str, op_hash: &str) {
let mut stmt = conn
.prepare(
"INSERT INTO operation_state (db_uuid, operation_hash)
VALUES (?1, ?2)
ON CONFLICT (db_uuid) DO
UPDATE SET operation_hash=excluded.operation_hash;",
)
.unwrap();
stmt.execute((db_uuid.to_string(), op_hash.to_string()))
.unwrap();
let branch_id =
OperationState::get_current_branch(conn, db_uuid).expect("No current branch set.");
Branch::set_current_operation(conn, branch_id, op_hash);
}
pub fn get_operation(conn: &Connection, db_uuid: &str) -> Option<String> {
let mut hash: Option<String> = None;
let mut stmt = conn
.prepare("SELECT operation_hash from operation_state where db_uuid = ?1;")
.unwrap();
let rows = stmt
.query_map((db_uuid.to_string(),), |row| row.get(0))
.unwrap();
for row in rows {
hash = row.unwrap();
}
hash
}
pub fn set_branch(conn: &Connection, db_uuid: &str, branch_name: &str) {
let branch = Branch::get_by_name(conn, db_uuid, branch_name)
.unwrap_or_else(|| panic!("No branch named {branch_name}."));
let mut stmt = conn
.prepare(
"INSERT INTO operation_state (db_uuid, branch_id)
VALUES (?1, ?2)
ON CONFLICT (db_uuid) DO
UPDATE SET branch_id=excluded.branch_id;",
)
.unwrap();
stmt.execute(params_from_iter(vec![
Value::from(db_uuid.to_string()),
Value::from(branch.id),
]))
.unwrap();
if let Some(current_branch_id) = OperationState::get_current_branch(conn, db_uuid) {
if current_branch_id != branch.id {
panic!("Failed to set branch to {branch_name}");
}
} else {
panic!("Failed to set branch.");
}
}
pub fn get_current_branch(conn: &Connection, db_uuid: &str) -> Option<i64> {
let mut id: Option<i64> = None;
let mut stmt = conn
.prepare("SELECT branch_id from operation_state where db_uuid = ?1;")
.unwrap();
let rows = stmt
.query_map((db_uuid.to_string(),), |row| row.get(0))
.unwrap();
for row in rows {
id = row.unwrap();
}
id
}
}
pub fn setup_db(conn: &Connection, db_uuid: &str) {
// check if the database is known. If not, initialize it.
if Branch::query(
conn,
"select * from branch where db_uuid = ?1",
vec![Value::from(db_uuid.to_string())],
)
.is_empty()
{
Branch::create(conn, db_uuid, "main");
OperationState::set_branch(conn, db_uuid, "main");
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::models::metadata;
use crate::operation_management;
use crate::test_helpers::{
create_operation, get_connection, get_operation_connection, keys_match, setup_gen_dir,
};
#[test]
fn test_gets_operations_of_branch() {
setup_gen_dir();
let conn = &get_connection(None);
let db_uuid = &metadata::get_db_uuid(conn);
let op_conn = &get_operation_connection(None);
setup_db(op_conn, db_uuid);
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-1");
// operations will be made in ascending order.
// The branch topology is as follows. () indicate where a branch starts
//
// -> 4 -> 5
// /
// -> 2 -> 3 (branch-1-sub-1)
// /
// branch-1
// /
// 1 (main, branch-1, branch-2)
// \
// branch-2
// \
// -> 6 -> 7 (branch-2-midpoint-1) -> 8 (branch-2-sub-1)
// \ \
// -> 12 -> 13 9 -> 10 -> 11
//
//
//
//
let branch_1 = Branch::create(op_conn, db_uuid, "branch-1");
let branch_2 = Branch::create(op_conn, db_uuid, "branch-2");
OperationState::set_branch(op_conn, db_uuid, "branch-1");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-2");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-3");
let branch_1_sub_1 = Branch::create(op_conn, db_uuid, "branch-1-sub-1");
OperationState::set_branch(op_conn, db_uuid, "branch-1-sub-1");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-4");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-5");
// TODO: We should merge the set branch/operation stuff, now that operations track branches we likely don't need set_branch
OperationState::set_branch(op_conn, db_uuid, "branch-2");
OperationState::set_operation(op_conn, db_uuid, "op-1");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-6");
let branch_2_midpoint =
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-7");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-8");
let branch_2_sub_1 = Branch::create(op_conn, db_uuid, "branch-2-sub-1");
OperationState::set_branch(op_conn, db_uuid, "branch-2-sub-1");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-9");
create_operation(
conn,
op_conn,
"test.fasta",
FileTypes::Fasta,
"foo",
"op-10",
);
create_operation(
conn,
op_conn,
"test.fasta",
FileTypes::Fasta,
"foo",
"op-11",
);
OperationState::set_operation(op_conn, db_uuid, &branch_2_midpoint.hash);
OperationState::set_branch(op_conn, db_uuid, &branch_2.name);
let branch_2_midpoint_1 = Branch::create(op_conn, db_uuid, "branch-2-midpoint-1");
OperationState::set_branch(op_conn, db_uuid, &branch_2_midpoint_1.name);
create_operation(
conn,
op_conn,
"test.fasta",
FileTypes::Fasta,
"foo",
"op-12",
);
create_operation(
conn,
op_conn,
"test.fasta",
FileTypes::Fasta,
"foo",
"op-13",
);
let ops = Branch::get_operations(op_conn, branch_2_midpoint_1.id)
.iter()
.map(|f| f.hash.clone())
.collect::<Vec<String>>();
assert_eq!(
ops,
vec![
"op-1".to_string(),
"op-6".to_string(),
"op-7".to_string(),
"op-12".to_string(),
"op-13".to_string()
]
);
let ops = Branch::get_operations(op_conn, branch_1.id)
.iter()
.map(|f| f.hash.clone())
.collect::<Vec<String>>();
assert_eq!(
ops,
vec!["op-1".to_string(), "op-2".to_string(), "op-3".to_string()]
);
let ops = Branch::get_operations(op_conn, branch_2.id)
.iter()
.map(|f| f.hash.clone())
.collect::<Vec<String>>();
assert_eq!(
ops,
vec![
"op-1".to_string(),
"op-6".to_string(),
"op-7".to_string(),
"op-8".to_string()
]
);
let ops = Branch::get_operations(op_conn, branch_1_sub_1.id)
.iter()
.map(|f| f.hash.clone())
.collect::<Vec<String>>();
assert_eq!(
ops,
vec![
"op-1".to_string(),
"op-2".to_string(),
"op-3".to_string(),
"op-4".to_string(),
"op-5".to_string()
]
);
let ops = Branch::get_operations(op_conn, branch_2_sub_1.id)
.iter()
.map(|f| f.hash.clone())
.collect::<Vec<String>>();
assert_eq!(
ops,
vec![
"op-1".to_string(),
"op-6".to_string(),
"op-7".to_string(),
"op-8".to_string(),
"op-9".to_string(),
"op-10".to_string(),
"op-11".to_string()
]
);
}
#[test]
fn test_graph_representation() {
setup_gen_dir();
let conn = &get_connection(None);
let db_uuid = &metadata::get_db_uuid(conn);
let op_conn = &get_operation_connection(None);
setup_db(op_conn, db_uuid);
// operations will be made in ascending order.
// The branch topology is as follows. () indicate where a branch starts
//
//
//
// branch-3 /-> 7
// main 1 -> 2 -> 3
// branch-1 \-> 4 -> 5
// branch-2 \-> 6
let mut expected_graph = OperationGraph::new();
expected_graph.add_edge("op-1", "op-2");
expected_graph.add_edge("op-2", "op-3");
expected_graph.add_edge("op-3", "op-4");
expected_graph.add_edge("op-4", "op-5");
expected_graph.add_edge("op-4", "op-6");
expected_graph.add_edge("op-1", "op-7");
let _ = Operation::create(op_conn, db_uuid, "vcf_addition", "op-1").unwrap();
let _ = Operation::create(op_conn, db_uuid, "vcf_addition", "op-2").unwrap();
let _ = Operation::create(op_conn, db_uuid, "vcf_addition", "op-3").unwrap();
Branch::create(op_conn, db_uuid, "branch-1");
OperationState::set_branch(op_conn, db_uuid, "branch-1");
let _ = Operation::create(op_conn, db_uuid, "vcf_addition", "op-4").unwrap();
let _ = Operation::create(op_conn, db_uuid, "vcf_addition", "op-5").unwrap();
OperationState::set_operation(op_conn, db_uuid, "op-4");
Branch::create(op_conn, db_uuid, "branch-2");
OperationState::set_branch(op_conn, db_uuid, "branch-2");
let _ = Operation::create(op_conn, db_uuid, "vcf_addition", "op-6").unwrap();
OperationState::set_operation(op_conn, db_uuid, "op-1");
Branch::create(op_conn, db_uuid, "branch-3");
OperationState::set_branch(op_conn, db_uuid, "branch-3");
let _ = Operation::create(op_conn, db_uuid, "vcf_addition", "op-7").unwrap();
let graph = Operation::get_operation_graph(op_conn);
assert!(keys_match(&graph.node_ids, &expected_graph.node_ids));
assert_eq!(
graph
.graph
.all_edges()
.map(|(src, dest, _)| (graph.get_key(src), graph.get_key(dest)))
.collect::<Vec<(String, String)>>(),
expected_graph
.graph
.all_edges()
.map(|(src, dest, _)| (expected_graph.get_key(src), expected_graph.get_key(dest)))
.collect::<Vec<(String, String)>>()
);
}
#[test]
fn test_path_between() {
setup_gen_dir();
let conn = &get_connection(None);
let db_uuid = &metadata::get_db_uuid(conn);
let op_conn = &get_operation_connection(None);
setup_db(op_conn, db_uuid);
// operations will be made in ascending order.
// The branch topology is as follows. () indicate where a branch starts
//
//
//
// branch-3 /-> 7
// main 1 -> 2 -> 3
// branch-1 \-> 4 -> 5
// branch-2 \-> 6
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-1");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-2");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-3");
Branch::create(op_conn, db_uuid, "branch-1");
OperationState::set_branch(op_conn, db_uuid, "branch-1");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-4");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-5");
OperationState::set_operation(op_conn, db_uuid, "op-4");
Branch::create(op_conn, db_uuid, "branch-2");
OperationState::set_branch(op_conn, db_uuid, "branch-2");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-6");
OperationState::set_operation(op_conn, db_uuid, "op-1");
Branch::create(op_conn, db_uuid, "branch-3");
OperationState::set_branch(op_conn, db_uuid, "branch-3");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-7");
assert_eq!(
Operation::get_path_between(op_conn, "op-1", "op-6"),
vec![
("op-1".to_string(), Direction::Outgoing, "op-2".to_string()),
("op-2".to_string(), Direction::Outgoing, "op-3".to_string()),
("op-3".to_string(), Direction::Outgoing, "op-4".to_string()),
("op-4".to_string(), Direction::Outgoing, "op-6".to_string()),
]
);
assert_eq!(
Operation::get_path_between(op_conn, "op-7", "op-1"),
vec![("op-7".to_string(), Direction::Incoming, "op-1".to_string()),]
);
assert_eq!(
Operation::get_path_between(op_conn, "op-3", "op-7"),
vec![
("op-3".to_string(), Direction::Incoming, "op-2".to_string()),
("op-2".to_string(), Direction::Incoming, "op-1".to_string()),
("op-1".to_string(), Direction::Outgoing, "op-7".to_string()),
]
);
}
#[test]
#[should_panic(
expected = "The current operation is in the middle of a branch. A new operation would create a bifurcation in the branch lineage. Create a new branch if you wish to bifurcate the current set of operations."
)]
fn test_prevents_bifurcation() {
// We make a simple branch from 1 -> 2 -> 3 -> 4 and ensure we cannot checkout operation 2
// and create a new operation from that point on the same branch.
setup_gen_dir();
let conn = &get_connection(None);
let db_uuid = &metadata::get_db_uuid(conn);
let op_conn = &get_operation_connection(None);
setup_db(op_conn, db_uuid);
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-1");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-2");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-3");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-4");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-5");
OperationState::set_operation(op_conn, db_uuid, "op-2");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-6");
}
#[test]
fn test_bifurcation_allowed_on_new_branch() {
// We make a simple branch from 1 -> 2 -> 3 -> 4 and ensure we can checkout operation 2
// because there is a new branch made
setup_gen_dir();
let conn = &get_connection(None);
let db_uuid = &metadata::get_db_uuid(conn);
let op_conn = &get_operation_connection(None);
setup_db(op_conn, db_uuid);
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-1");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-2");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-3");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-4");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-5");
OperationState::set_operation(op_conn, db_uuid, "op-2");
Branch::create(op_conn, db_uuid, "branch-1");
OperationState::set_branch(op_conn, db_uuid, "branch-1");
create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-6");
}
#[test]
fn test_bifurcation_allowed_on_reset() {
// We make a simple branch from 1 -> 2 -> 3 -> 4 and ensure we can reset to operation 2
// and create a new operation from that point on the same branch because we reset.
setup_gen_dir();
let conn = &get_connection(None);
let db_uuid = &metadata::get_db_uuid(conn);
let op_conn = &get_operation_connection(None);
setup_db(op_conn, db_uuid);
let op_1 = create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-1");
let op_2 = create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-2");
let _op_3 = create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-3");
let _op_4 = create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-4");
operation_management::reset(conn, op_conn, db_uuid, "op-2");
let op_5 = create_operation(conn, op_conn, "test.fasta", FileTypes::Fasta, "foo", "op-5");
assert_eq!(
Branch::get_operations(
op_conn,
OperationState::get_current_branch(op_conn, db_uuid).unwrap()
)
.iter()
.map(|op| op.hash.clone())
.collect::<Vec<String>>(),
vec![op_1.hash.clone(), op_2.hash.clone(), op_5.hash.clone()]
);
}
#[test]
fn test_sets_start_operation_hash_on_first_change() {
setup_gen_dir();
let conn = &get_connection(None);
let db_uuid = &metadata::get_db_uuid(conn);
let op_conn = &get_operation_connection("t3.db");
setup_db(op_conn, db_uuid);
let db_uuid2 = "another-thing";
setup_db(op_conn, db_uuid2);
let db1_main = Branch::get_by_name(op_conn, db_uuid, "main").unwrap().id;