Merge pull request #611 from matthiasbeyer/libimagstore/drop-wlocks-early

Libimagstore/drop wlocks early
This commit is contained in:
Matthias Beyer 2016-08-02 20:28:08 +02:00 committed by GitHub
commit 76d88e46ad

View file

@ -390,19 +390,21 @@ impl Store {
.map_err_into(SEK::CreateCallError) .map_err_into(SEK::CreateCallError)
} }
let mut hsmap = match self.entries.write() { {
Err(_) => return Err(SEK::LockPoisoned.into_error()).map_err_into(SEK::CreateCallError), let mut hsmap = match self.entries.write() {
Ok(s) => s, Err(_) => return Err(SEK::LockPoisoned.into_error()).map_err_into(SEK::CreateCallError),
}; Ok(s) => s,
};
if hsmap.contains_key(&id) { if hsmap.contains_key(&id) {
return Err(SEK::EntryAlreadyExists.into_error()).map_err_into(SEK::CreateCallError); return Err(SEK::EntryAlreadyExists.into_error()).map_err_into(SEK::CreateCallError);
}
hsmap.insert(id.clone(), {
let mut se = StoreEntry::new(id.clone());
se.status = StoreEntryStatus::Borrowed;
se
});
} }
hsmap.insert(id.clone(), {
let mut se = StoreEntry::new(id.clone());
se.status = StoreEntryStatus::Borrowed;
se
});
let mut fle = FileLockEntry::new(self, Entry::new(id)); let mut fle = FileLockEntry::new(self, Entry::new(id));
self.execute_hooks_for_mut_file(self.post_create_aspects.clone(), &mut fle) self.execute_hooks_for_mut_file(self.post_create_aspects.clone(), &mut fle)
@ -426,23 +428,25 @@ impl Store {
.map_err_into(SEK::RetrieveCallError) .map_err_into(SEK::RetrieveCallError)
} }
self.entries let entry = try!({
.write() self.entries
.map_err(|_| SE::new(SEK::LockPoisoned, None)) .write()
.and_then(|mut es| { .map_err(|_| SE::new(SEK::LockPoisoned, None))
let mut se = es.entry(id.clone()).or_insert_with(|| StoreEntry::new(id.clone())); .and_then(|mut es| {
let entry = se.get_entry(); let mut se = es.entry(id.clone()).or_insert_with(|| StoreEntry::new(id.clone()));
se.status = StoreEntryStatus::Borrowed; let entry = se.get_entry();
entry se.status = StoreEntryStatus::Borrowed;
}) entry
.map(|e| FileLockEntry::new(self, e)) })
.and_then(|mut fle| { .map_err_into(SEK::RetrieveCallError)
self.execute_hooks_for_mut_file(self.post_retrieve_aspects.clone(), &mut fle) });
.map_err_into(SEK::PostHookExecuteError)
.map_err_into(SEK::HookExecutionError) let mut fle = FileLockEntry::new(self, entry);
.and(Ok(fle)) self.execute_hooks_for_mut_file(self.post_retrieve_aspects.clone(), &mut fle)
}) .map_err_into(SEK::PostHookExecuteError)
.map_err_into(SEK::HookExecutionError)
.map_err_into(SEK::RetrieveCallError) .map_err_into(SEK::RetrieveCallError)
.and(Ok(fle))
} }
/// Get an entry from the store if it exists. /// Get an entry from the store if it exists.
@ -596,23 +600,25 @@ impl Store {
.map_err_into(SEK::DeleteCallError) .map_err_into(SEK::DeleteCallError)
} }
let mut entries = match self.entries.write() { {
Err(_) => return Err(SE::new(SEK::LockPoisoned, None)) let mut entries = match self.entries.write() {
.map_err_into(SEK::DeleteCallError), Err(_) => return Err(SE::new(SEK::LockPoisoned, None))
Ok(e) => e, .map_err_into(SEK::DeleteCallError),
}; Ok(e) => e,
};
// if the entry is currently modified by the user, we cannot drop it // if the entry is currently modified by the user, we cannot drop it
if entries.get(&id).map(|e| e.is_borrowed()).unwrap_or(false) { if entries.get(&id).map(|e| e.is_borrowed()).unwrap_or(false) {
return Err(SE::new(SEK::IdLocked, None)) return Err(SE::new(SEK::IdLocked, None))
.map_err_into(SEK::DeleteCallError); .map_err_into(SEK::DeleteCallError);
} }
// remove the entry first, then the file // remove the entry first, then the file
entries.remove(&id); entries.remove(&id);
if let Err(e) = remove_file(&id) { if let Err(e) = remove_file(&id) {
return Err(SEK::FileError.into_error_with_cause(Box::new(e))) return Err(SEK::FileError.into_error_with_cause(Box::new(e)))
.map_err_into(SEK::DeleteCallError); .map_err_into(SEK::DeleteCallError);
}
} }
self.execute_hooks_for_id(self.post_delete_aspects.clone(), &id) self.execute_hooks_for_id(self.post_delete_aspects.clone(), &id)
@ -680,19 +686,22 @@ impl Store {
.map_err_into(SEK::MoveByIdCallError) .map_err_into(SEK::MoveByIdCallError)
} }
let hsmap = self.entries.write(); {
if hsmap.is_err() { let hsmap = self.entries.write();
return Err(SE::new(SEK::LockPoisoned, None)) if hsmap.is_err() {
} return Err(SE::new(SEK::LockPoisoned, None))
if hsmap.unwrap().contains_key(&old_id) {
return Err(SE::new(SEK::EntryAlreadyBorrowed, None));
} else {
match rename(old_id, new_id.clone()) {
Err(e) => return Err(SEK::EntryRenameError.into_error_with_cause(Box::new(e))),
_ => {
debug!("Rename worked");
},
} }
if hsmap.unwrap().contains_key(&old_id) {
return Err(SE::new(SEK::EntryAlreadyBorrowed, None));
} else {
match rename(old_id, new_id.clone()) {
Err(e) => return Err(SEK::EntryRenameError.into_error_with_cause(Box::new(e))),
_ => {
debug!("Rename worked");
},
}
}
} }
self.execute_hooks_for_id(self.pre_move_aspects.clone(), &new_id) self.execute_hooks_for_id(self.pre_move_aspects.clone(), &new_id)