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
extern crate bincode;
extern crate chrono;
extern crate fern;
#[macro_use]
extern crate log;
extern crate rayon;
extern crate remove_dir_all;
extern crate ron;
#[macro_use]
extern crate serde;
extern crate uuid;
use std::collections::HashMap;
use std::error::Error;
use std::path::PathBuf;
use std::sync::RwLock;
pub mod config;
pub mod core;
mod ephemeral;
pub mod fetch;
pub mod maintenance;
mod persistence;
pub mod prelude;
mod system;
use self::ephemeral::Data;
use crate::{config::Config, fetch::Fetchable};
use mycelium_command::node::Node;
type NodeId = DbId;
type PageId = DbId;
pub type DbId = [u8; 16];
pub(crate) const RESERVED: &str = "db_sys";
pub struct Db {
config: Config,
primary: Data,
working_dir: std::path::PathBuf,
system: system::System,
}
impl Db {
pub fn init(config: Option<Config>) -> std::io::Result<Db> {
if config.is_some() {
return Db::with_config(config.unwrap());
}
Db::new()
}
#[allow(clippy::new_without_default)]
pub fn new() -> std::io::Result<Db> {
let config = Config::default();
let path = PathBuf::from(config.get_data_dir().unwrap());
let system: crate::system::System =
crate::system::System::fetch(RESERVED, system::FILE_NAME, &path)
.expect("Failed to fetch system info.");
system.save_x(RESERVED, system::FILE_NAME, &path)?;
Ok(Db {
config,
primary: RwLock::new(HashMap::new()),
working_dir: std::path::PathBuf::new(),
system,
})
}
pub fn add(&self, item: &[u8], tag: &str) -> Result<[u8; 16], Box<dyn std::error::Error>> {
core::add(self, item, tag)
}
fn empty(&self) -> Db {
Db {
config: self.config.clone(),
primary: RwLock::new(HashMap::new()),
working_dir: self.working_dir.to_path_buf(),
system: self.system.clone(),
}
}
pub fn get(&self, tag: &str, id: DbId) -> Option<Node> {
match self.primary.read().expect("Lock Primary.").get(tag) {
Some(t) => t.get_node(id),
None => None,
}
}
pub fn get_reserved_sys(&self) -> PathBuf {
let mut reserved = self.working_dir.to_path_buf();
reserved.push(RESERVED);
reserved
}
pub fn get_tag(&self, tag: &str) -> Option<Vec<Node>> {
match self
.primary
.read()
.expect("Lock Primary core::get_tag()")
.get(tag)
{
Some(t) => Some(t.all_container_nodes()),
None => None,
}
}
pub fn get_tag_hashmap(&self, tag: &str) -> Option<HashMap<DbId, Node>> {
match self
.primary
.read()
.expect("Lock core::get_tag_hashmap()")
.get(tag)
{
Some(t) => Some(t.all_container_nodes_map()),
None => None,
}
}
pub fn get_working_dir(&self) -> PathBuf {
self.working_dir.to_path_buf()
}
pub fn get_system_id(&self) -> DbId {
self.system.db_id
}
pub fn list_tags(&self) -> Vec<String> {
let mut tags = vec![];
let tag_iter = self.primary.read().expect("Lock stew primary.");
for tag in tag_iter.keys() {
if tag == RESERVED {
continue;
}
tags.push(tag.to_string())
}
tags
}
pub fn load_tag(&self, tag: &str) -> std::io::Result<()> {
crate::core::validate_tag(self, tag).expect("Invalid container tag");
let mut primary = self.primary.write().expect("core::load_tag()");
let cont = primary.get_mut(tag);
if cont.is_some() {
let cont = cont.unwrap();
let cont = cont.load().with_page_item_idex();
primary.insert(tag.to_string(), cont);
}
Ok(())
}
pub fn node_history(
&mut self,
tag: &str,
node_id: DbId,
limit: usize,
) -> Option<(Option<Node>, Vec<Node>)> {
let mut nodes: Vec<Node> = vec![];
let mut primary = self.primary.write().expect("core::node_history lock()");
let cont = primary.get_mut(tag);
let mut current_node = None;
if cont.is_some() {
let cont = cont.unwrap();
if cont.is_loaded() {
current_node = cont.get_node(node_id);
let pages = cont.history.get_node_pages(node_id);
for page in pages {
match cont.history.load_page(page) {
Ok(_) => (),
Err(e) => panic!("Error loading page: {:?}", e),
};
}
let mut node_hist = cont.history.get_nodes(node_id, limit);
nodes.append(&mut node_hist);
} else {
let current_node = cont.get_node(node_id);
let node_hist = cont.history.get_nodes(node_id, limit);
return Some((current_node, node_hist));
}
}
Some((current_node, nodes))
}
#[allow(clippy::match_wild_err_arm)]
fn with_config(config: Config) -> std::io::Result<Db> {
let path = match config.get_data_dir() {
Some(_) => PathBuf::from(config.get_data_dir().unwrap()),
None => match std::env::current_dir() {
Ok(p) => p,
Err(_) => panic!("Couldn't get current executable directory."),
},
};
let sys_path = PathBuf::from(config.get_data_dir().unwrap());
let system: crate::system::System =
system::System::fetch(RESERVED, system::FILE_NAME, &sys_path)?;
system.save_x(RESERVED, system::FILE_NAME, &sys_path)?;
let max_page_size = config.get_max_page_size();
let primary = match core::load_tag_containers(&path, max_page_size) {
Ok(primary) => primary,
Err(e) => {
error!("Error loading tags: {}", e.description());
return Err(e);
}
};
let db = Db {
config,
primary,
working_dir: path,
system,
};
validate_probable_success(&db);
Ok(db)
}
pub fn refresh(&self, tag: &str) -> Result<(), Box<dyn std::error::Error>> {
let mut clear = false;
match self.primary.write().expect("Lock primary").get_mut(tag) {
Some(cont) => {
let cont = cont;
cont.clear().expect("Failed to clear tag on refresh.");
clear = true;
}
None => (),
}
if clear {
self.load_tag(tag)
.expect("Failed to reload tag on refresh.");
}
Ok(())
}
pub fn save_all(&self) -> Result<(), std::io::Error> {
crate::core::save_all(self)
}
pub fn update_node(
&self,
node_id: DbId,
item: &[u8],
tag: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let mut primary = self.primary.write().expect("core::update_node() lock()");
let cont = primary.get_mut(tag).unwrap();
let hist_node = cont.update_node(node_id, item)?;
cont.update_node_history(hist_node)
}
}
fn file_permission_check(path: std::path::PathBuf) -> Result<(), std::io::Error> {
use std::fs::File;
use std::io::Write;
use uuid::Uuid;
if !path.exists() {
std::fs::create_dir_all(path.to_path_buf())?;
}
let mut test = path.to_path_buf();
let path = format!("{}/{}", RESERVED, Uuid::new_v4());
test.push(&path);
std::fs::create_dir_all(&test)?;
test.push("file_test.txt");
let mut f = File::create(&test)?;
f.write_all(b"Beef, carrot, peas, and onion.")?;
match f.sync_all() {
Ok(_) => (),
Err(e) => {
error!("Issues writing files to disk in permission check.");
return Err(e);
}
}
use remove_dir_all::*;
remove_dir_all(test.parent().unwrap())
}
fn log_report() {
info!("Info level logging up");
trace!("trace level logging up");
debug!("debug level logging up");
error!("error level logging up");
warn!("warning level logging up");
}
pub fn setup_logging(verbosity: Option<usize>) {
#[allow(unused_assignments)]
let mut verbos = 3;
if verbosity.is_some() {
verbos = verbosity.unwrap()
} else {
verbos = if cfg!(debug_assertions) { 3 } else { 0 };
}
match crate::config::setup_logging(verbos) {
Ok(_) => {
log_report();
}
Err(e) => panic!("Couldn't init logging: {:?}", e),
}
}
fn validate_probable_success(db: &Db) {
match file_permission_check(db.working_dir.to_path_buf()) {
Ok(_) => (),
Err(e) => panic!("File permission check failure: {}", e.description()),
}
}
#[cfg(test)]
mod tests;