summaryrefslogtreecommitdiff
path: root/src/store.rs
blob: a415d7ad5224a7dec452479c83181c929a5c7526 (plain)
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
use crate::chunk::DataChunk;
use crate::chunkid::ChunkId;
use crate::chunkmeta::ChunkMeta;
use anyhow::Context;
use std::path::{Path, PathBuf};

/// Store chunks, with metadata, persistently.
///
/// The chunks and their metadata are stored persistently on disk
/// under a directory specified as the Store struct is created. To
/// store or retrieve a chunk its identifier must be used.
pub struct Store {
    dir: PathBuf,
}

impl Store {
    /// Create a new Store to represent on-disk storage of chunks.x
    pub fn new(dir: &Path) -> Self {
        Store {
            dir: dir.to_path_buf(),
        }
    }

    // Construct name for a files in the store from chunk id.
    //
    // The name of directory containing the file is returned
    // separately to make it easier to create it if needed.
    fn filenames(&self, id: &ChunkId) -> (PathBuf, PathBuf, PathBuf) {
        let bytes = id.as_bytes();
        assert!(bytes.len() > 3);
        let a = bytes[0];
        let b = bytes[1];
        let c = bytes[2];
        let dir = self.dir.join(format!("{}/{}/{}", a, b, c));
        let meta = dir.join(format!("{}.{}", id, "meta"));
        let data = dir.join(format!("{}.{}", id, "data"));
        (dir, meta, data)
    }

    /// Save a chunk into a store.
    pub fn save(&self, id: &ChunkId, meta: &ChunkMeta, chunk: &DataChunk) -> anyhow::Result<()> {
        let (dir, metaname, dataname) = &self.filenames(id);

        if !dir.exists() {
            let res = std::fs::create_dir_all(dir).into();
            if let Err(_) = res {
                return res.with_context(|| format!("creating directory {}", dir.display()));
            }
        }

        std::fs::write(&metaname, meta.to_json())?;
        std::fs::write(&dataname, chunk.data())?;
        Ok(())
    }

    /// Load a chunk's metadata from a store.
    pub fn load_meta(&self, id: &ChunkId) -> anyhow::Result<ChunkMeta> {
        let (_, metaname, _) = &self.filenames(id);
        let meta = std::fs::read(&metaname)?;
        Ok(serde_json::from_slice(&meta)?)
    }

    /// Load a chunk from a store.
    pub fn load(&self, id: &ChunkId) -> anyhow::Result<(ChunkMeta, DataChunk)> {
        let (_, _, dataname) = &self.filenames(id);
        let meta = self.load_meta(id)?;
        let data = std::fs::read(&dataname)?;
        Ok((meta, DataChunk::new(data)))
    }

    /// Delete a chunk from a store.
    pub fn delete(&self, id: &ChunkId) -> anyhow::Result<()> {
        let (_, metaname, dataname) = &self.filenames(id);
        std::fs::remove_file(&metaname)?;
        std::fs::remove_file(&dataname)?;
        Ok(())
    }
}