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
// tar_file.rs
//
// Copyright (c) 2020 All The Music, LLC
//
// This work is licensed under the Creative Commons Attribution 4.0 International License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/ or send
// a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.

use crate::storage::{
    IntoInner,
    PathGenerator,
    StorageBackend,
    TarArchive,
    TarArchiveError,
};

/// Type alias for `TarFile` inner object
type InnerObject = std::io::BufWriter<std::fs::File>;

/// [Tar archive](https://en.wikipedia.org/wiki/Tar_(computing)) storage backend.
/// Use for datasets where compression isn't necessary. For reference, a tar archive
/// with 4096 entries will take up ~4.19MB on disk, as each entry requires a header of 512
/// bytes and at least 512 bytes of data.
pub struct TarFile<G: PathGenerator> {
    archive: TarArchive<InnerObject, G>,
}

impl<G: PathGenerator> TarFile<G> {
    /// Create new `TarFile` instance
    pub fn new<P: AsRef<std::path::Path>>(target_path: P, path_generator: G) -> Result<Self, TarArchiveError> {
        // Open filepath
        let archive = std::fs::File::create(target_path)?;
        // Wrap in BufWriter, optimized for many small writes
        // (see: https://doc.rust-lang.org/std/io/struct.BufWriter.html)
        let archive = std::io::BufWriter::new(archive);
        Ok(Self {
            archive: TarArchive::new(archive, path_generator),
        })
    }
}

impl<G: PathGenerator> StorageBackend for TarFile<G> {
    type Error = <TarArchive<InnerObject, G> as StorageBackend>::Error;

    fn append_file(&mut self, mfile: libatm::MIDIFile, mode: Option<u32>) -> Result<(), Self::Error> {
        self.archive.append_file(mfile, mode)
    }

    fn finish(&mut self) -> Result<(), Self::Error> {
        self.archive.finish()
    }
}

impl<G: PathGenerator> IntoInner for TarFile<G> {
    type Inner = InnerObject;

    fn into_inner(self) -> Result<Self::Inner, <Self as StorageBackend>::Error> {
        self.archive.into_inner()
    }
}