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;

///
/// Identifier for nodes which contain items
///
type NodeId = DbId;

///
/// Identifier for page which contains nodes
///
type PageId = DbId;

///
/// Unique Ids are from crate uuid and feature v4: Uuid::new_v4()
///
pub type DbId = [u8; 16];

pub(crate) const RESERVED: &str = "db_sys";

///
/// # Db - Tagged data storage.
/// HashMap <container_name, container>
///
/// * container \*
///
///     pages: HashMap<PageId, Page>
///
///     index: HashMap<NodeId, PageId>
///
/// *Simplified structure for the data store.
///
pub struct Db {
    config: Config,
    primary: Data,
    working_dir: std::path::PathBuf,
    system: system::System,
}

///
/// # Db
///
/// Initially load only tag you are interested in. Tags lazy
/// load with use.
///
/// Todo: prune infrequently used data from memory.
///
impl Db {
    ///
    /// # Database init - Primary use
    ///
    /// * Parameters
    ///     - config: Defaults to Config::default()
    ///
    /// * Returns
    ///     - Result<Db>: Instance of Db
    ///
    /// ```
    /// use crate::mycelium_core::Db;
    /// use crate::mycelium_core::prelude::*;
    ///
    /// // Builder pattern for config
    /// let config = Config::new()
    ///     .with_data_directory("./data")
    ///     .with_block_count(32) // blocks per page when run persistently
    ///     .with_block_size(1024); // size of blocks on page    ///
    ///
    /// // TODO: If block count/size are adjusted on an existing Db::vacuum()
    /// // should be run to update existing pages.
    ///
    /// match Db::init(Some(config)) {
    ///     Ok(_) => assert!(true),
    ///     _ => assert!(false, "Failed to create db from config")
    /// }
    ///
    /// ```
    ///
    pub fn init(config: Option<Config>) -> std::io::Result<Db> {
        if config.is_some() {
            return Db::with_config(config.unwrap());
        }
        Db::new()
    }

    // Fails early if there are issues reading from disk.
    ///
    /// # new db
    ///
    /// Db::new() will create a folder structure and system
    /// files used by the database or read existing ones. Nothing
    /// else is written to disk without specific calls to do so.
    ///
    /// * Returns
    ///     - Result<Db>: Instance of db with default config
    ///
    /// ```
    /// use crate::mycelium_core::prelude::*;
    ///
    /// match Db::new() {
    ///     Ok(_) => assert!(true),
    ///     _ => assert!(false, "failed to start db core.")
    /// }
    /// ```
    ///
    #[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,
        })
    }

    ///
    /// # Add item to container.
    /// * Parameters
    ///     - Item: Reference to byte array of item.
    ///     - Tag: Container for item.
    ///
    /// * Returns
    ///     - Result(DbId)
    ///
    /// ```
    /// use crate::mycelium_core::Db;
    ///
    /// // new db
    /// let db = Db::new().expect("Failed to create db");
    /// // add an item
    /// let id = db.add(b"new item", "tag").expect("Failed to add item");
    /// // should get a new id back. Id should not be [0;16] as that is
    /// // an empty id from Uuid.
    /// assert!(id != [0 as u8; 16])
    /// ```
    ///
    pub fn add(&self, item: &[u8], tag: &str) -> Result<[u8; 16], Box<dyn std::error::Error>> {
        core::add(self, item, tag)
    }

    // Create an emtpy structure to the same data directory
    // This is used by vacuum to get an empty structure to move all
    // nodes from one to the other.
    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(),
        }
    }

    ///
    /// # Get a node (struct that wraps the serialized item).
    ///
    /// * Parameters
    ///     - tag: name of container containing the item
    ///     - id: [u8;16] identifier for a head node
    ///
    /// * Returns
    ///     - Option<Node>: None if nothing found.
    ///
    /// ```
    /// use crate::mycelium_core::Db;
    ///
    /// //new db
    /// let db = Db::new().expect("Failed to create db.");
    /// //get an item that doesn't exist
    /// let item_none = db.get("no_exist", [0;16]);
    /// //should be none
    /// assert!(item_none.is_none());
    ///
    /// //add an item to tag tag
    /// let id = db.add(b"item bytes", "tag").expect("Failed to add item.");
    /// //get item just added with id
    /// let item_b = db.get("tag", id).unwrap();
    /// //item should have same contents
    /// assert!(b"item bytes".to_vec() == item_b.get_item())
    /// ```
    ///
    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,
        }
    }

    ///
    /// # Get reserved system folder
    ///
    pub fn get_reserved_sys(&self) -> PathBuf {
        let mut reserved = self.working_dir.to_path_buf();
        reserved.push(RESERVED);
        reserved
    }

    ///
    /// # Get all items in tag
    ///
    /// * Parameters
    ///     - tag: Tag identifier to fetch items of
    ///
    /// * Returns:
    ///     - Option<Vec<Node>>: list of nodes
    ///
    /// ```
    /// use crate::mycelium_core::Db;
    ///
    /// // new db
    /// let db = Db::new().expect("Failed to create db");
    /// // get items for non-existing tag
    /// let empty = db.get_tag("emtpy_tag");
    /// // should be empty
    /// assert!(empty.is_none());
    ///
    /// //add some items to tag dog
    /// db.add(b"shepherd", "dog").expect("Failed to add to db");
    /// db.add(b"pitbull", "dog").expect("Failed to add to db");
    /// //get a list of items in tag dog
    /// let dog_list = db.get_tag("dog").expect("Failed to get a result");
    /// //should get 2 back
    /// assert!(dog_list.len() == 2);
    ///
    /// ```
    ///
    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,
        }
    }

    ///
    /// # All nodes in tag as hashmap
    ///
    /// * Parameters:
    ///     - tag: container id
    ///
    ///  * Returns:
    ///     - Option<HashMap<DbId,Node>>>
    ///
    /// ```
    /// use crate::mycelium_core::Db;
    ///
    /// // new db
    /// let db = Db::new().expect("Failed to create db");
    /// // add an item (returns an id we are ignoring)
    /// db.add(b"Some bytes", "tag").expect("Failed to add to db");
    /// // get hashmap
    /// let hm = db.get_tag_hashmap("tag").expect("Failed to get map");
    ///
    /// assert!(hm.len() == 1);
    ///
    /// ```
    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,
        }
    }

    ///
    /// # Database working directory
    ///
    pub fn get_working_dir(&self) -> PathBuf {
        self.working_dir.to_path_buf()
    }

    pub fn get_system_id(&self) -> DbId {
        self.system.db_id
    }

    ///
    /// # Empty list if no tags
    ///
    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
    }

    ///
    /// # Load tag from disk
    ///
    /// Multiple calls will destroy in memory changes replacing with what is on disk.
    /// load_tag on emtpy tag will result in container being created.
    ///
    /// * Parameters
    ///     - tag: container name
    ///
    /// * Return
    ///     - Result: ()/Err
    ///
    /// ```
    /// use crate::mycelium_core::Db;
    ///
    /// //new db
    /// let db = Db::new().expect("Failed to create new db");
    ///
    /// match db.load_tag("no_exists") {
    ///     Ok(_) => assert!(true),
    ///     Err(_) => assert!(false, "Error on load non existent tag"),
    /// };
    ///
    /// ```
    ///
    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(())
    }

    ///
    /// # Load Node History
    ///
    /// Nodes are a lazily loaded linked list with the most recent
    /// version as the head. Load the past versions of a node into memory
    /// before retrieving them.
    ///
    /// * Parameters
    ///     - tag: container name containing node
    ///     - node_id: Id for node to load history list for
    ///
    /// * Returns
    ///     - Tuple.0: Head of node list. None if node has been archived
    ///     - Tuple.1: List of node versions
    ///
    /// ```
    /// use crate::mycelium_core::Db;
    ///
    /// // new db
    /// let mut db = Db::new().expect("Failed to create db");
    ///
    /// // add item
    /// let id = db.add(b"shepherd", "dog").expect("Failed to add item");
    /// // update node
    /// db.update_node(id, b"German Shepherd", "dog").expect("Failed to update");
    /// // query history
    /// let result = db.node_history("dog", id, 0).expect("Failed to get node history");
    ///
    /// assert!(result.0.is_some() && result.1.len() ==1);
    /// ```
    ///
    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);

                // Check if history is loaded
                let pages = cont.history.get_node_pages(node_id);
                // load
                for page in pages {
                    match cont.history.load_page(page) {
                        Ok(_) => (),
                        Err(e) => panic!("Error loading page: {:?}", e),
                    };
                }
                // get items
                let mut node_hist = cont.history.get_nodes(node_id, limit);
                nodes.append(&mut node_hist);
            } else {
                //load from memory (if there)
                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,
        };

        // Quick test for things like permissions. We try and fail as
        // early possible so there are no surprises if instantiation is
        // successful.
        validate_probable_success(&db);

        Ok(db)
    }

    ///
    /// # Refresh Tag
    ///
    /// Load tag from disk. Will drop any changes currently in memory.
    ///
    /// * Parameters
    ///     - tag: container name
    ///
    /// * Return
    ///     - Result: (), Box<Err>
    ///
    /// ```
    /// use crate::mycelium_core::Db;
    ///
    /// // new db
    /// let db = Db::new().expect("Failed to create db");
    ///
    /// // Loads from disk.
    /// match db.refresh("dog") {
    ///     Ok(_) => assert!(true),
    ///     Err(_) => assert!(false),
    /// }
    ///
    /// ```
    ///
    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(())
    }

    ///
    /// # Write all to disk/cache
    ///
    pub fn save_all(&self) -> Result<(), std::io::Error> {
        crate::core::save_all(self)
    }

    ///
    /// # Update Node
    /// On update new node is pushed onto head of list structure and
    /// original is pushed into history.
    ///
    /// * Parameters:
    ///     - node_id: [u8; 16] id of list head node
    ///     - item: &[u8] byte array of item
    ///     - tag: container id of item
    ///
    /// * Returns
    ///     - Result: (), Err.
    ///
    /// ```
    /// use crate::mycelium_core::Db;
    ///
    /// //new db
    /// let db = Db::new().expect("Failed to create db");
    ///
    /// // add item
    /// let id = db.add(b"item bytes", "item").expect("Failed to add item");
    /// // update item
    /// match db.update_node(id, b"better bytes", "item") {
    ///     Ok(_) => assert!(true),
    ///     Err(_) => assert!(false, "Failed to update")
    /// }
    ///
    /// ```
    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();
        // update_node returns original node replaced by new node
        let hist_node = cont.update_node(node_id, item)?;
        cont.update_node_history(hist_node)
    }
}

// Panics
// Tests run on nvme need to try on hdd's
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");
}

///
/// # Logging with crates log and fern
/// Call this once before you setup an instance of stew.
///  If setting up multiple instances calling this again will
///  cause a panic.
///
/// * Parameters:
///     - verbosity: 1 low, 2 med, 3 high. Default 3.
///
/// ```
/// use crate::mycelium_core::prelude::*;
///
/// // most verbose
/// setup_logging(1);
///
/// ```
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),
    }
}

// Add more as needed as a type of startup validation.
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;