From ee8cf35fcc17163fa93fa5a2897da0e91168d81f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Sun, 24 Jan 2016 17:28:52 +0100 Subject: [PATCH] Implement store::create --- libimagstore/src/error.rs | 2 ++ libimagstore/src/store.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index 9cd19814..19ce8e35 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -22,6 +22,7 @@ pub enum StoreErrorKind { StorePathCreate, LockPoisoned, EntryAlreadyBorrowed, + EntryAlreadyExists, MalformedEntry, // maybe more } @@ -40,6 +41,7 @@ fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str { &StoreErrorKind::LockPoisoned => "The internal Store Lock has been poisoned", &StoreErrorKind::EntryAlreadyBorrowed => "Entry is already borrowed", + &StoreErrorKind::EntryAlreadyExists => "Entry already exists", &StoreErrorKind::MalformedEntry => "Entry has invalid formatting, missing header", } } diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index f6e78611..fb222fc5 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -37,6 +37,15 @@ struct StoreEntry { } impl StoreEntry { + + fn new(id: StoreId) -> StoreEntry { + StoreEntry { + id: id.clone(), + file: LazyFile::Absent(id), + status: StoreEntryStatus::Present, + } + } + /// The entry is currently borrowed, meaning that some thread is currently /// mutating it fn is_borrowed(&self) -> bool { @@ -101,8 +110,21 @@ impl Store { } /// Creates the Entry at the given location (inside the entry) - pub fn create(&self, entry: Entry) -> Result<()> { - unimplemented!(); + pub fn create<'a>(&'a self, id: StoreId) -> Result> { + let hsmap = self.entries.write(); + if hsmap.is_err() { + return Err(StoreError::new(StoreErrorKind::LockPoisoned, None)) + } + let mut hsmap = hsmap.unwrap(); + if hsmap.contains_key(&id) { + return Err(StoreError::new(StoreErrorKind::EntryAlreadyExists, None)) + } + hsmap.insert(id.clone(), { + let mut se = StoreEntry::new(id.clone()); + se.status = StoreEntryStatus::Borrowed; + se + }); + Ok(FileLockEntry::new(self, Entry::new(id.clone()), id)) } /// Borrow a given Entry. When the `FileLockEntry` is either `update`d or