From acfbb16eb2ce7232441b07ba8246d42b1a2f3ee5 Mon Sep 17 00:00:00 2001 From: Julian Ganz Date: Sun, 24 Jan 2016 22:26:25 +0100 Subject: [PATCH] Fix Store::retrieve() The previous version of `Store::retrieve()` required the entry to be already registered in the store's hashmap. --- libimagstore/src/store.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 3ff1b26b..406bac62 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -142,14 +142,25 @@ impl Store { /// Borrow a given Entry. When the `FileLockEntry` is either `update`d or /// dropped, the new Entry is written to disk pub fn retrieve<'a>(&'a self, id: StoreId) -> Result> { + // get hold of the hashmap let hsmap = self.entries.write(); if hsmap.is_err() { return Err(StoreError::new(StoreErrorKind::LockPoisoned, None)) } - hsmap.unwrap().get_mut(&id) - .ok_or(StoreError::new(StoreErrorKind::IdNotFound, None)) - .and_then(|store_entry| store_entry.get_entry()) - .and_then(|entry| Ok(FileLockEntry::new(self, entry, id))) + + // either get the existing entry ot create it + let mut entries = hsmap.unwrap(); + let mut store_entry = entries.entry(id.clone()).or_insert_with(|| StoreEntry::new(id.clone())); + + // make sure only one user gets a borrowed entry at a time + if store_entry.is_borrowed() { + return Err(StoreError::new(StoreErrorKind::IdLocked, None)); + } + + // TODO: update the store entry status + + // make a file lock entry from the store entry + store_entry.get_entry().and_then(|entry| Ok(FileLockEntry::new(self, entry, id))) } /// Iterate over all StoreIds for one module name