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
use crate::storage::{ResourceStorage, Stream};

use std::{
    cell::RefCell,
    collections::BTreeMap,
    fmt,
    io::{self, Cursor},
    path::PathBuf,
    rc::Rc,
    slice,
};

type MemoryStorageStream = Rc<RefCell<Cursor<Vec<u8>>>>;

/// Internal storage of data in memory.
#[derive(Default, Clone)]
struct MemoryStorage {
    // Streams of resources that were written.
    streams: Rc<RefCell<BTreeMap<PathBuf, MemoryStorageStream>>>,
    // Data of resources that were opened for reading.
    resources: Rc<RefCell<BTreeMap<PathBuf, Rc<Vec<u8>>>>>,
}

impl fmt::Debug for MemoryStorage {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "MemoryStorage {{ streams: {:?}, resources: {:?} }}",
            self.streams
                .borrow()
                .iter()
                .map(|(path, _)| path.display())
                .collect::<Vec<_>>(),
            self.resources
                .borrow()
                .iter()
                .map(|(path, _)| path.display())
                .collect::<Vec<_>>(),
        )
    }
}

/// Resource storage in memory.
///
/// Used to create and read archives from memory, e.g. for writing tests.
///
/// # Examples
///
/// ```rust
/// use flatdata::{MemoryResourceStorage,  Vector};
/// use flatdata::test::{X, XBuilder};
///
/// let storage = MemoryResourceStorage::new("/root/to/my/archive/in/memory");
/// let builder = XBuilder::new(storage.clone()).expect("failed to create builder");
/// // Write some data and store it archive, e.g.
/// let v = Vector::new();
/// builder.set_data(&v.as_view());
///
/// let archive = X::open(storage).expect("failed to open");
/// // read data
/// archive.data();
/// ```
#[derive(Debug)]
pub struct MemoryResourceStorage {
    storage: MemoryStorage,
    path: PathBuf,
}

impl MemoryResourceStorage {
    /// Create an empty memory resource storage at a given virtual path.
    ///
    /// Resources will be placed in ephemeral memory with prefix `path`. A path
    /// has to be provided to unify the interface with `FileResourceStorage`.
    #[allow(clippy::new_ret_no_self)]
    pub fn new<P: Into<PathBuf>>(path: P) -> Rc<Self> {
        Rc::new(Self {
            storage: MemoryStorage::default(),
            path: path.into(),
        })
    }
}

impl ResourceStorage for MemoryResourceStorage {
    fn subdir(&self, dir: &str) -> Rc<dyn ResourceStorage> {
        Rc::new(Self {
            storage: self.storage.clone(),
            path: self.path.join(dir),
        })
    }

    fn exists(&self, resource_name: &str) -> bool {
        let resource_path = self.path.join(resource_name);
        self.storage.resources.borrow().contains_key(&resource_path)
            || self.storage.streams.borrow().contains_key(&resource_path)
    }

    fn read_resource(&self, resource_name: &str) -> Result<&[u8], io::Error> {
        let resource_path = self.path.join(resource_name);
        if !self.storage.resources.borrow().contains_key(&resource_path) {
            let streams = self.storage.streams.borrow();
            let stream = streams.get(&resource_path);
            match stream {
                Some(stream) => {
                    // Resource is not yet opened, but there is a stream it was written to
                    // => copy the stream as resource data.
                    let data = Rc::new(stream.borrow().get_ref().clone());
                    self.storage
                        .resources
                        .borrow_mut()
                        .insert(resource_path.clone(), data);
                }
                None => {
                    return Err(io::Error::new(
                        io::ErrorKind::NotFound,
                        String::from(resource_path.to_str().unwrap_or(resource_name)),
                    ));
                }
            }
        }
        let data = &self.storage.resources.borrow()[&resource_path];
        // We cannot prove to Rust that the buffer will live as long as the storage
        // (we never delete mappings), so we need to manually extend lifetime
        let extended_lifetime_data = unsafe { slice::from_raw_parts(data.as_ptr(), data.len()) };
        Ok(&extended_lifetime_data)
    }

    fn create_output_stream(
        &self,
        resource_name: &str,
    ) -> Result<Rc<RefCell<dyn Stream>>, io::Error> {
        let resource_path = self.path.join(resource_name);
        let stream = self
            .storage
            .streams
            .borrow_mut()
            .entry(resource_path)
            .or_insert_with(|| Rc::new(RefCell::new(Cursor::new(Vec::new()))))
            .clone();
        Ok(stream)
    }
}