Implement store::create
This commit is contained in:
parent
fa2f1068d4
commit
ee8cf35fcc
2 changed files with 26 additions and 2 deletions
|
@ -22,6 +22,7 @@ pub enum StoreErrorKind {
|
||||||
StorePathCreate,
|
StorePathCreate,
|
||||||
LockPoisoned,
|
LockPoisoned,
|
||||||
EntryAlreadyBorrowed,
|
EntryAlreadyBorrowed,
|
||||||
|
EntryAlreadyExists,
|
||||||
MalformedEntry,
|
MalformedEntry,
|
||||||
// maybe more
|
// maybe more
|
||||||
}
|
}
|
||||||
|
@ -40,6 +41,7 @@ fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str {
|
||||||
&StoreErrorKind::LockPoisoned
|
&StoreErrorKind::LockPoisoned
|
||||||
=> "The internal Store Lock has been poisoned",
|
=> "The internal Store Lock has been poisoned",
|
||||||
&StoreErrorKind::EntryAlreadyBorrowed => "Entry is already borrowed",
|
&StoreErrorKind::EntryAlreadyBorrowed => "Entry is already borrowed",
|
||||||
|
&StoreErrorKind::EntryAlreadyExists => "Entry already exists",
|
||||||
&StoreErrorKind::MalformedEntry => "Entry has invalid formatting, missing header",
|
&StoreErrorKind::MalformedEntry => "Entry has invalid formatting, missing header",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,15 @@ struct StoreEntry {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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
|
/// The entry is currently borrowed, meaning that some thread is currently
|
||||||
/// mutating it
|
/// mutating it
|
||||||
fn is_borrowed(&self) -> bool {
|
fn is_borrowed(&self) -> bool {
|
||||||
|
@ -101,8 +110,21 @@ impl Store {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates the Entry at the given location (inside the entry)
|
/// Creates the Entry at the given location (inside the entry)
|
||||||
pub fn create(&self, entry: Entry) -> Result<()> {
|
pub fn create<'a>(&'a self, id: StoreId) -> Result<FileLockEntry<'a>> {
|
||||||
unimplemented!();
|
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
|
/// Borrow a given Entry. When the `FileLockEntry` is either `update`d or
|
||||||
|
|
Loading…
Reference in a new issue