use std::collections::HashMap;
use std::ops::Drop;
use std::path::PathBuf;
use std::result::Result as RResult;
use std::sync::Arc;
use std::sync::RwLock;
use std::io::Read;
use std::ops::Deref;
use std::ops::DerefMut;
use std::fmt::Formatter;
use std::fmt::Debug;
use std::fmt::Error as FMTError;
use libimagerror::errors::ErrorMsg as EM;
use toml::Value;
use toml_query::read::TomlValueReadExt;
use toml_query::read::TomlValueReadTypeExt;
use failure::Fallible as Result;
use failure::ResultExt;
use failure::err_msg;
use failure::Error;
use crate::storeid::{IntoStoreId, StoreId};
use crate::iter::Entries;
use crate::file_abstraction::FileAbstraction;
use crate::file_abstraction::FileAbstractionInstance;
use crate::file_abstraction::fs::FSFileAbstraction;
use crate::file_abstraction::inmemory::InMemoryFileAbstraction;
#[derive(Debug, PartialEq)]
enum StoreEntryStatus {
Present,
Borrowed
}
#[derive(Debug)]
struct StoreEntry {
id: StoreId,
store_base: PathBuf,
file: Box<dyn FileAbstractionInstance>,
status: StoreEntryStatus,
}
impl StoreEntry {
fn new(store_base: PathBuf, id: StoreId, backend: &Arc<dyn FileAbstraction>) -> Result<StoreEntry> {
let pb = id.clone().with_base(&store_base).into_pathbuf()?;
#[cfg(feature = "fs-lock")]
{
open_file(pb.clone())
.and_then(|f| f.lock_exclusive())
.with_context(|| EM::IO)?;
}
Ok(StoreEntry {
id,
store_base,
file: backend.new_instance(pb),
status: StoreEntryStatus::Present,
})
}
fn is_borrowed(&self) -> bool {
self.status == StoreEntryStatus::Borrowed
}
fn get_entry(&mut self) -> Result<Entry> {
if !self.is_borrowed() {
match self.file.get_file_content(self.id.clone().with_base(&self.store_base))? {
Some(file) => Ok(file),
None => Ok(Entry::new(self.id.clone()))
}
} else {
Err(format_err!("EntryAlreadyBorrowed: {}", self.id))
}
}
fn write_entry(&mut self, entry: &Entry) -> Result<()> {
if self.is_borrowed() {
assert_eq!(self.id, entry.location);
trace!("Writing entry...");
self.file
.write_file_content(entry)
.map(|_| ())
} else {
Ok(())
}
}
}
#[cfg(feature = "fs-lock")]
impl Drop for StoreEntry {
fn drop(self) {
self.get_entry()
.and_then(|entry| open_file(entry.get_location().clone()))
.and_then(|f| f.unlock())
}
}
pub struct Store {
location: PathBuf,
entries: Arc<RwLock<HashMap<StoreId, StoreEntry>>>,
backend: Arc<dyn FileAbstraction>,
}
impl Store {
pub fn new(location: PathBuf, store_config: &Option<Value>) -> Result<Store> {
let backend = Arc::new(FSFileAbstraction::default());
Store::new_with_backend(location, store_config, backend)
}
#[inline]
pub fn new_inmemory(location: PathBuf, store_config: &Option<Value>) -> Result<Store> {
let backend = Arc::new(InMemoryFileAbstraction::default());
Self::new_with_backend(location, store_config, backend)
}
pub(crate) fn new_with_backend(location: PathBuf,
store_config: &Option<Value>,
backend: Arc<dyn FileAbstraction>) -> Result<Store> {
use crate::configuration::*;
debug!("Building new Store object");
if !location.exists() {
if !config_implicit_store_create_allowed(store_config)? {
return Err(format_err!("CreateStoreDirDenied"))
.context(EM::FileError)
.context(EM::IO)
.map_err(Error::from)
}
backend
.create_dir_all(&location)
.context(format_err!("StorePathCreate: {}", location.display()))?;
} else if location.is_file() {
debug!("Store path exists as file");
return Err(format_err!("StorePathExists: {}", location.display()));
}
let store = Store {
location,
entries: Arc::new(RwLock::new(HashMap::new())),
backend,
};
debug!("Store building succeeded");
debug!("------------------------");
debug!("{:?}", store);
debug!("------------------------");
Ok(store)
}
pub fn create<'a, S: IntoStoreId>(&'a self, id: S) -> Result<FileLockEntry<'a>> {
let id = id.into_storeid()?;
debug!("Creating id: '{}'", id);
let exists = self.exists(id.clone())?;
if exists {
debug!("Entry exists: {:?}", id);
return Err(format_err!("EntryAlreadyExists: {}", id));
}
{
let mut hsmap = self
.entries
.write()
.map_err(|_| Error::from(EM::LockError))
.context(format_err!("CreateCallError: {}", id))?;
if hsmap.contains_key(&id) {
debug!("Cannot create, internal cache already contains: '{}'", id);
return Err(format_err!("EntryAlreadyExists: {}", id))
.context(format_err!("CreateCallError: {}", id))
.map_err(Error::from)
}
hsmap.insert(id.clone(), {
debug!("Creating: '{}'", id);
let mut se = StoreEntry::new(self.path().clone(), id.clone(), &self.backend)?;
se.status = StoreEntryStatus::Borrowed;
se
});
}
debug!("Constructing FileLockEntry: '{}'", id);
Ok(FileLockEntry::new(self, Entry::new(id)))
}
pub fn retrieve<'a, S: IntoStoreId>(&'a self, id: S) -> Result<FileLockEntry<'a>> {
let id = id.into_storeid()?;
debug!("Retrieving id: '{}'", id);
let entry = self
.entries
.write()
.map_err(|_| Error::from(EM::LockError))
.and_then(|mut es| {
let new_se = StoreEntry::new(self.path().clone(), id.clone(), &self.backend)?;
let se = es.entry(id.clone()).or_insert(new_se);
let entry = se.get_entry();
se.status = StoreEntryStatus::Borrowed;
entry
})
.context(format_err!("RetrieveCallError: {}", id))?;
debug!("Constructing FileLockEntry: '{}'", id);
Ok(FileLockEntry::new(self, entry))
}
pub fn get<'a, S: IntoStoreId + Clone>(&'a self, id: S) -> Result<Option<FileLockEntry<'a>>> {
let id = id.into_storeid()?;
debug!("Getting id: '{}'", id);
let exists = self.exists(id.clone())?;
if !exists {
debug!("Does not exist in internal cache or filesystem: {:?}", id);
return Ok(None);
}
self.retrieve(id.clone())
.map(Some)
.context(format_err!("GetCallError: {}", id))
.map_err(Error::from)
}
pub fn update<'a>(&'a self, entry: &mut FileLockEntry<'a>) -> Result<()> {
debug!("Updating FileLockEntry at '{}'", entry.get_location());
self._update(entry, false)
.context(format_err!("UpdateCallError: {}", entry.get_location()))
.map_err(Error::from)
}
fn _update<'a>(&'a self, entry: &mut FileLockEntry<'a>, modify_presence: bool) -> Result<()> {
let mut hsmap = self.entries.write()
.map_err(|_| Error::from(EM::LockError))?;
let se = hsmap.get_mut(&entry.location).ok_or_else(|| {
EM::EntryNotFound(entry.location.local_display_string())
})?;
assert!(se.is_borrowed(), "Tried to update a non borrowed entry.");
debug!("Verifying Entry");
entry.entry.verify()?;
debug!("Writing Entry");
se.write_entry(&entry.entry)?;
trace!("Entry written");
if modify_presence {
debug!("Modifying presence of {} -> Present", entry.get_location());
se.status = StoreEntryStatus::Present;
}
trace!("Entry updated successfully");
Ok(())
}
pub fn flush_cache(&self) -> Result<()> {
let mut hsmap = self.entries.write()
.map_err(|_| Error::from(EM::LockError))?;
let mut to_flush = vec![];
for (storeid, se) in hsmap.deref() {
if !se.is_borrowed() {
to_flush.push(storeid.clone());
}
}
for id in to_flush {
let _ = hsmap.remove(&id);
}
hsmap.shrink_to_fit();
Ok(())
}
pub fn cache_size(&self) -> Result<usize> {
let hsmap = self.entries.read().map_err(|_| Error::from(EM::LockError))?;
Ok(hsmap.iter().count())
}
pub fn cache_capacity(&self) -> Result<usize> {
let hsmap = self.entries.read().map_err(|_| Error::from(EM::LockError))?;
Ok(hsmap.capacity())
}
pub fn get_copy<S: IntoStoreId>(&self, id: S) -> Result<Entry> {
let id = id.into_storeid()?;
debug!("Retrieving copy of '{}'", id);
let entries = self.entries.write()
.map_err(|_| Error::from(EM::LockError))
.context(format_err!("RetrieveCopyCallError: {}", id))?;
if entries.get(&id).map(|e| e.is_borrowed()).unwrap_or(false) {
return Err(EM::IdLocked)
.context(format_err!("RetrieveCopyCallError: {}", id))
.map_err(Error::from)
}
StoreEntry::new(self.path().clone(), id, &self.backend)?.get_entry()
}
pub fn delete<S: IntoStoreId>(&self, id: S) -> Result<()> {
let id = id.into_storeid()?;
debug!("Deleting id: '{}'", id);
let pb = id.clone().with_base(self.path()).into_pathbuf()?;
{
let mut entries = self
.entries
.write()
.map_err(|_| Error::from(EM::LockError))
.context(format_err!("DeleteCallError: {}", id))?;
let do_remove = match entries.get(&id) {
Some(e) => if e.is_borrowed() {
return Err(Error::from(EM::LockError))
.context(format_err!("DeleteCallError: {}", id))
.map_err(Error::from)
} else {
true
},
None => {
debug!("Seems like {:?} is not in the internal cache", id);
if !self.backend.exists(&pb)? {
debug!("Seems like {:?} is not even on the FS", pb);
return Err(EM::FileNotFound)
.context(format_err!("DeleteCallError: {}", id))
.map_err(Error::from)
}
false
},
};
if do_remove {
let _ = entries.remove(&id);
}
}
debug!("Seems like {:?} is on the FS", pb);
self
.backend
.remove_file(&pb)
.context(EM::FileError)
.context(format_err!("DeleteCallError: {}", id))?;
debug!("Deleted");
Ok(())
}
pub fn save_to(&self, entry: &FileLockEntry, new_id: StoreId) -> Result<()> {
debug!("Saving '{}' to '{}'", entry.get_location(), new_id);
self.save_to_other_location(entry, new_id, false)
}
pub fn save_as(&self, entry: FileLockEntry, new_id: StoreId) -> Result<()> {
debug!("Saving '{}' as '{}'", entry.get_location(), new_id);
self.save_to_other_location(&entry, new_id, true)
}
fn save_to_other_location(&self, entry: &FileLockEntry, new_id: StoreId, remove_old: bool)
-> Result<()>
{
let hsmap = self
.entries
.write()
.map_err(|_| Error::from(EM::LockError))
.context(format_err!("MoveCallError: {} -> {}", entry.get_location(), new_id))?;
if hsmap.contains_key(&new_id) {
return Err(format_err!("Entry exists already: {}", new_id.clone()))
.context(format_err!("MoveCallError: {} -> {}", entry.get_location(), new_id))
.map_err(Error::from)
}
let old_id = entry.get_location().clone();
let old_id_as_path = old_id.clone().with_base(self.path()).into_pathbuf()?;
let new_id_as_path = new_id.clone().with_base(self.path()).into_pathbuf()?;
self.backend
.copy(&old_id_as_path, &new_id_as_path)
.and_then(|_| if remove_old {
debug!("Removing old '{:?}'", old_id_as_path);
self.backend.remove_file(&old_id_as_path)
} else {
Ok(())
})
.context(EM::FileError)
.context(format_err!("MoveCallError: {} -> {}", old_id, new_id))
.map_err(Error::from)
}
pub fn move_by_id(&self, old_id: StoreId, new_id: StoreId) -> Result<()> {
debug!("Moving '{}' to '{}'", old_id, new_id);
{
let mut hsmap = self.entries.write()
.map_err(|_| Error::from(EM::LockError))?;
if hsmap.contains_key(&new_id) {
return Err(format_err!("Entry already exists: {}", new_id));
}
debug!("New id does not exist in cache");
let old_id_pb = old_id.clone().with_base(self.path()).into_pathbuf()?;
let new_id_pb = new_id.clone().with_base(self.path()).into_pathbuf()?;
if !self.backend.exists(&old_id_pb)? {
return Err(format_err!("Entry does not exist: {}", old_id));
}
if hsmap.remove(&old_id).map(|e| e.is_borrowed()).unwrap_or(false) {
return Err(format_err!("Entry already borrowed: {}", old_id));
}
if self.backend.exists(&new_id_pb)? {
return Err(format_err!("Entry already exists: {}", new_id));
}
debug!("New entry does not yet exist on filesystem. Good.");
self
.backend
.rename(&old_id_pb, &new_id_pb)
.context({
let old = old_id_pb.display().to_string();
let new = new_id_pb.display().to_string();
format_err!("Rename error: {} -> {}", old, new)
})?;
debug!("Rename worked on filesystem");
}
debug!("Moved");
Ok(())
}
pub fn entries<'a>(&'a self) -> Result<Entries<'a>> {
trace!("Building 'Entries' iterator");
self.backend
.pathes_recursively(self.path().clone(), self.path(), self.backend.clone())
.map(|i| Entries::new(i, self))
}
pub fn exists(&self, id: StoreId) -> Result<bool> {
let cache_has_entry = |id: &StoreId|
self.entries
.read()
.map(|map| map.contains_key(id))
.map_err(|_| Error::from(EM::LockError))
.context(format_err!("CreateCallError: {}", id));
let backend_has_entry = |id: StoreId|
self.backend.exists(&id.with_base(self.path()).into_pathbuf()?);
Ok(cache_has_entry(&id)? || backend_has_entry(id)?)
}
pub fn path(&self) -> &PathBuf {
&self.location
}
}
impl Debug for Store {
fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FMTError> {
writeln!(fmt, "Store location = {:?}, entries = {:?}", self.location, self.entries)
}
}
pub struct FileLockEntry<'a> {
store: &'a Store,
entry: Entry,
}
impl<'a> FileLockEntry<'a, > {
fn new(store: &'a Store, entry: Entry) -> FileLockEntry<'a> {
FileLockEntry { store, entry }
}
}
impl<'a> Debug for FileLockEntry<'a> {
fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FMTError> {
write!(fmt,
"FileLockEntry(Store = {store}, location = {location:?})",
store = self.store.location.to_str().unwrap_or("Unknown Path"),
location = self.entry.get_location())
}
}
impl<'a> Deref for FileLockEntry<'a> {
type Target = Entry;
fn deref(&self) -> &Self::Target {
&self.entry
}
}
impl<'a> DerefMut for FileLockEntry<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.entry
}
}
#[cfg(not(test))]
impl<'a> Drop for FileLockEntry<'a> {
fn drop(&mut self) {
use libimagerror::trace::trace_error_dbg;
trace!("Dropping: {:?} - from FileLockEntry::drop()", self.get_location());
if let Err(e) = self.store._update(self, true) {
trace!("Error happened in FileLockEntry::drop() while Store::update()ing");
trace_error_dbg(&e);
if_cfg_panic!("ERROR WHILE DROPPING: {:?}", e);
}
}
}
#[cfg(test)]
impl<'a> Drop for FileLockEntry<'a> {
fn drop(&mut self) {
use libimagerror::trace::trace_error;
trace!("Dropping: {:?} - from FileLockEntry::drop() (test impl)", self.get_location());
let _ = self.store._update(self, true).map_err(|e| trace_error(&e));
}
}
pub type EntryContent = String;
#[derive(Debug, Clone)]
pub struct Entry {
location: StoreId,
header: Value,
content: EntryContent,
}
impl Entry {
pub fn new(loc: StoreId) -> Entry {
Entry {
location: loc,
header: Entry::default_header(),
content: EntryContent::new()
}
}
pub fn default_header() -> Value {
use toml::map::Map;
let mut m = Map::new();
m.insert(String::from("imag"), {
let mut imag_map = Map::new();
imag_map.insert(String::from("version"),
Value::String(String::from(env!("CARGO_PKG_VERSION"))));
Value::Table(imag_map)
});
Value::Table(m)
}
pub fn from_reader<S: IntoStoreId>(loc: S, file: &mut dyn Read) -> Result<Entry> {
let text = {
let mut s = String::new();
file.read_to_string(&mut s).context(EM::IO)?;
s
};
Self::from_str(loc, &text[..])
}
pub fn from_str<S: IntoStoreId>(loc: S, s: &str) -> Result<Entry> {
use crate::util::entry_buffer_to_header_content;
let (header, content) = entry_buffer_to_header_content(s)?;
Ok(Entry {
location: loc.into_storeid()?,
header,
content,
})
}
pub fn to_str(&self) -> Result<String> {
Ok(format!("---\n{header}---\n{content}",
header = ::toml::ser::to_string_pretty(&self.header)
.map_err(Error::from)
.context(err_msg("TOML Error"))?,
content = self.content))
}
pub fn get_location(&self) -> &StoreId {
&self.location
}
pub fn get_header(&self) -> &Value {
&self.header
}
pub fn get_header_mut(&mut self) -> &mut Value {
&mut self.header
}
pub fn get_content(&self) -> &EntryContent {
&self.content
}
pub fn set_content(&mut self, content: EntryContent) {
self.content = content;
}
pub fn get_content_mut(&mut self) -> &mut EntryContent {
&mut self.content
}
pub fn replace_from_buffer(&mut self, buf: &str) -> Result<()> {
let (header, content) = crate::util::entry_buffer_to_header_content(buf)?;
self.content = content;
self.header = header;
Ok(())
}
pub fn verify(&self) -> Result<()> {
if !has_main_section(&self.header)? {
Err(format_err!("MissingMainSection"))
} else if !has_imag_version_in_main_section(&self.header)? {
Err(format_err!("MissingVersionInfo"))
} else if !has_only_tables(&self.header)? {
debug!("Could not verify that it only has tables in its base table");
Err(format_err!("NonTableInBaseTable"))
} else {
Ok(())
}
}
}
impl PartialEq for Entry {
fn eq(&self, other: &Entry) -> bool {
self.location == other.location &&
self.header == other.header &&
self.content == other.content
}
}
fn has_only_tables(t: &Value) -> Result<bool> {
debug!("Verifying that table has only tables");
match *t {
Value::Table(ref tab) => Ok(tab.iter().all(|(_, x)| is_match!(*x, Value::Table(_)))),
_ => Err(format_err!("HeaderTypeFailure")),
}
}
fn has_main_section(t: &Value) -> Result<bool> {
t.read("imag")
.map_err(Error::from)
.context(EM::TomlQueryError)?
.ok_or_else(|| format_err!("ConfigKeyMissingError('imag')"))
.map(Value::is_table)
}
fn has_imag_version_in_main_section(t: &Value) -> Result<bool> {
t.read_string("imag.version")
.map_err(Error::from)
.context(EM::TomlQueryError)?
.ok_or_else(|| format_err!("ConfigKeyMissingError('imag.version')"))
.map_err(Error::from)
.map(String::from)
.map(|s: String| ::semver::Version::parse(&s).is_ok())
}
#[cfg(test)]
mod test {
extern crate env_logger;
use crate::storeid::StoreId;
use crate::store::has_main_section;
use crate::store::has_imag_version_in_main_section;
use toml::Value;
use toml::map::Map;
fn setup_logging() {
let _ = env_logger::try_init();
}
#[test]
fn test_imag_section() {
let mut map = Map::new();
map.insert("imag".into(), Value::Table(Map::new()));
assert!(has_main_section(&Value::Table(map)).unwrap());
}
#[test]
fn test_imag_abscent_main_section() {
let mut map = Map::new();
map.insert("not_imag".into(), Value::Boolean(false));
assert!(has_main_section(&Value::Table(map)).is_err());
}
#[test]
fn test_main_section_without_version() {
let mut map = Map::new();
map.insert("imag".into(), Value::Table(Map::new()));
assert!(has_imag_version_in_main_section(&Value::Table(map)).is_err());
}
#[test]
fn test_main_section_with_version() {
let mut map = Map::new();
let mut sub = Map::new();
sub.insert("version".into(), Value::String("0.0.0".into()));
map.insert("imag".into(), Value::Table(sub));
assert!(has_imag_version_in_main_section(&Value::Table(map)).unwrap());
}
#[test]
fn test_main_section_with_version_in_wrong_type() {
let mut map = Map::new();
let mut sub = Map::new();
sub.insert("version".into(), Value::Boolean(false));
map.insert("imag".into(), Value::Table(sub));
assert!(has_imag_version_in_main_section(&Value::Table(map)).is_err());
}
static TEST_ENTRY : &str = "---
[imag]
version = '0.0.3'
---
Hai";
static TEST_ENTRY_TNL : &str = "---
[imag]
version = '0.0.3'
---
Hai
";
#[test]
fn test_entry_from_str() {
use super::Entry;
use std::path::PathBuf;
setup_logging();
debug!("{}", TEST_ENTRY);
let entry = Entry::from_str(StoreId::new(PathBuf::from("test/foo~1.3")).unwrap(),
TEST_ENTRY).unwrap();
assert_eq!(entry.content, "Hai");
}
#[test]
fn test_entry_to_str() {
use super::Entry;
use std::path::PathBuf;
setup_logging();
debug!("{}", TEST_ENTRY);
let entry = Entry::from_str(StoreId::new(PathBuf::from("test/foo~1.3")).unwrap(),
TEST_ENTRY).unwrap();
let string = entry.to_str().unwrap();
assert_eq!(TEST_ENTRY, string);
}
#[test]
fn test_entry_to_str_trailing_newline() {
use super::Entry;
use std::path::PathBuf;
setup_logging();
debug!("{}", TEST_ENTRY_TNL);
let entry = Entry::from_str(StoreId::new(PathBuf::from("test/foo~1.3")).unwrap(),
TEST_ENTRY_TNL).unwrap();
let string = entry.to_str().unwrap();
assert_eq!(TEST_ENTRY_TNL, string);
}
}
#[cfg(test)]
mod store_tests {
extern crate env_logger;
use std::path::PathBuf;
use std::sync::Arc;
fn setup_logging() {
let _ = env_logger::try_init();
}
use super::Store;
pub fn get_store() -> Store {
use crate::file_abstraction::inmemory::InMemoryFileAbstraction;
let backend = Arc::new(InMemoryFileAbstraction::default());
Store::new_with_backend(PathBuf::from("/"), &None, backend).unwrap()
}
#[test]
fn test_store_instantiation() {
let store = get_store();
assert_eq!(store.location, PathBuf::from("/"));
assert!(store.entries.read().unwrap().is_empty());
}
#[test]
fn test_store_create() {
let store = get_store();
for n in 1..100 {
let s = format!("test-{}", n);
let entry = store.create(PathBuf::from(s.clone())).unwrap();
assert!(entry.verify().is_ok());
let loc = entry.get_location().clone().with_base(store.path()).into_pathbuf().unwrap();
assert!(loc.starts_with("/"));
assert!(loc.ends_with(s));
}
}
#[test]
fn test_store_get_create_get_delete_get() {
let store = get_store();
for n in 1..100 {
let res = store.get(PathBuf::from(format!("test-{}", n)));
assert!(match res { Ok(None) => true, _ => false, })
}
for n in 1..100 {
let s = format!("test-{}", n);
let entry = store.create(PathBuf::from(s.clone())).unwrap();
assert!(entry.verify().is_ok());
let loc = entry.get_location().clone().with_base(store.path()).into_pathbuf().unwrap();
assert!(loc.starts_with("/"));
assert!(loc.ends_with(s));
}
for n in 1..100 {
let res = store.get(PathBuf::from(format!("test-{}", n)));
assert!(match res { Ok(Some(_)) => true, _ => false, })
}
for n in 1..100 {
assert!(store.delete(PathBuf::from(format!("test-{}", n))).is_ok())
}
for n in 1..100 {
let res = store.get(PathBuf::from(format!("test-{}", n)));
assert!(match res { Ok(None) => true, _ => false, })
}
}
#[test]
fn test_store_create_twice() {
let store = get_store();
for n in 1..100 {
let s = format!("test-{}", n % 50);
if let Ok(entry) = store.create(PathBuf::from(s.clone())) {
assert!(entry.verify().is_ok());
let loc = entry.get_location().clone().with_base(store.path()).into_pathbuf().unwrap();
assert!(loc.starts_with("/"));
assert!(loc.ends_with(s));
}
}
}
#[test]
fn test_store_create_in_hm() {
use crate::storeid::StoreId;
let store = get_store();
for n in 1..100 {
let pb = StoreId::new(PathBuf::from(format!("test-{}", n))).unwrap();
assert!(store.entries.read().unwrap().get(&pb).is_none());
assert!(store.create(pb.clone()).is_ok());
assert!(store.entries.read().unwrap().get(&pb).is_some());
}
}
#[test]
fn test_store_retrieve_in_hm() {
use crate::storeid::StoreId;
let store = get_store();
for n in 1..100 {
let pb = StoreId::new(PathBuf::from(format!("test-{}", n))).unwrap();
assert!(store.entries.read().unwrap().get(&pb).is_none());
assert!(store.retrieve(pb.clone()).is_ok());
assert!(store.entries.read().unwrap().get(&pb).is_some());
}
}
#[test]
fn test_get_none() {
let store = get_store();
for n in 1..100 {
match store.get(PathBuf::from(format!("test-{}", n))) {
Ok(None) => {},
_ => panic!(),
}
}
}
#[test]
fn test_delete_none() {
let store = get_store();
for n in 1..100 {
match store.delete(PathBuf::from(format!("test-{}", n))) {
Err(_) => {},
_ => panic!(),
}
}
}
#[test]
fn test_store_move_moves_in_hm() {
use crate::storeid::StoreId;
setup_logging();
let store = get_store();
for n in 1..100 {
if n % 2 == 0 {
let id = StoreId::new(PathBuf::from(format!("t-{}", n))).unwrap();
let id_mv = StoreId::new(PathBuf::from(format!("t-{}", n - 1))).unwrap();
debug!("Trying to move: {} -> {}", id, id_mv);
{
debug!("Checking presence: {}", id);
assert!(store.entries.read().unwrap().get(&id).is_none());
}
{
debug!("Creating : {}", id);
assert!(store.create(id.clone()).is_ok());
}
{
debug!("Checking presence: {}", id);
assert!(store.entries.read().unwrap().get(&id).is_some());
}
debug!("Moving: {} -> {}", id, id_mv);
let r = store.move_by_id(id.clone(), id_mv.clone());
assert!(r.map_err(|e| debug!("ERROR: {:?}", e)).is_ok());
{
debug!("Checking presence: {}", id_mv);
assert!(store.entries.read().unwrap().get(&id_mv).is_none());
assert!(store.get(id_mv.clone()).unwrap().is_some());
assert!(store.entries.read().unwrap().get(&id_mv).is_some());
}
let res = store.get(id.clone());
assert!(match res { Ok(None) => true, _ => false },
"Moved id ({:?}) is still there: {:?}", id, res);
let res = store.get(id_mv.clone());
assert!(match res { Ok(Some(_)) => true, _ => false },
"New id ({:?}) is not in store: {:?}", id_mv, res);
}
}
}
#[test]
fn test_moving_entry_with_writing_before_and_after() {
use crate::storeid::StoreId;
setup_logging();
let store = get_store();
let old_name = StoreId::new(PathBuf::from("old")).unwrap();
let new_name = StoreId::new(PathBuf::from("new")).unwrap();
debug!("Creating old entry");
{
let mut entry = store.create(old_name.clone()).unwrap();
entry.get_content_mut().push_str("first line");
drop(entry);
}
debug!("Moving");
store.move_by_id(old_name.clone(), new_name.clone()).unwrap();
debug!("Getting old entry again (should not work)");
assert!(store.get(old_name).unwrap().is_none());
debug!("Getting new entry");
{
let mut entry = store.get(new_name.clone()).unwrap().unwrap();
assert_eq!(entry.get_content(), "first line");
entry.get_content_mut().push_str("second line");
drop(entry);
}
{
debug!("Getting new entry again");
debug!("Store = {:#?}", store);
let new_entry = store.get(new_name).unwrap().unwrap();
assert_eq!(new_entry.get_content(), "first linesecond line");
}
}
}