Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Pushkar Mishra <[email protected]>
  • Loading branch information
Pushkarm029 committed Apr 14, 2024
1 parent 904b8d0 commit a01a07b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 39 deletions.
8 changes: 3 additions & 5 deletions fs-storage/examples/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn write_command(args: &[String], path: &str) -> Result<()> {
.extension()
.map_or(false, |ext| ext == "json");

let mut kv_pairs = BTreeMap::new();
let mut fs = FileStorage::new("cli".to_string(), Path::new(path));
if content_json {
let content =
fs::read_to_string(content).context("Failed to read JSON file")?;
Expand All @@ -86,7 +86,7 @@ fn write_command(args: &[String], path: &str) -> Result<()> {
if let Value::Object(object) = json {
for (key, value) in object {
if let Value::String(value_str) = value {
kv_pairs.insert(key, value_str);
fs.set(key, value_str);
} else {
println!(
"Warning: Skipping non-string value for key '{}'",
Expand All @@ -103,13 +103,11 @@ fn write_command(args: &[String], path: &str) -> Result<()> {
for pair in pairs {
let kv: Vec<&str> = pair.split(':').collect();
if kv.len() == 2 {
kv_pairs.insert(kv[0].to_string(), kv[1].to_string());
fs.set(kv[0].to_string(), kv[1].to_string());
}
}
}

let mut fs = FileStorage::new("cli".to_string(), Path::new(path));
fs.value_by_id = kv_pairs.clone();
fs.write_fs().context("Failed to write file")?;

Ok(())
Expand Down
10 changes: 5 additions & 5 deletions fs-storage/src/base_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ where
K: FromStr + Hash + Eq + Ord + Debug + Clone,
V: Debug + Clone,
{
fn get(&self, id: &K) -> Option<V>;
fn set(&mut self, id: K, value: V) -> Result<()>;
fn get(&self, id: &K) -> Option<&V>;
fn set(&mut self, id: K, value: V);
fn remove(&mut self, id: &K) -> Result<()>;

/// Remove file at stored path
fn erase(&mut self) -> Result<()>;
fn erase(&self) -> Result<()>;

/// Get immutable BTreeMap
///
/// This can be used to get most immutable BtreeMap related functions for free.
fn as_ref(&self) -> &BTreeMap<K, V>;

/// Check if underlying file has been updated
/// Check if storage is updated
///
/// This check can be used before reading the file.
fn is_file_updated(&self) -> Result<bool>;
fn is_storage_updated(&self) -> Result<bool>;

/// Read data from disk
///
Expand Down
52 changes: 23 additions & 29 deletions fs-storage/src/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct FileStorage<K, V> {
label: String,
path: PathBuf,
timestamp: SystemTime,
pub value_by_id: BTreeMap<K, V>,
data: BTreeMap<K, V>,
}

impl<K, V> FileStorage<K, V>
Expand All @@ -33,7 +33,7 @@ where
label,
path: PathBuf::from(path),
timestamp: SystemTime::now(),
value_by_id: BTreeMap::new(),
data: BTreeMap::new(),
}
}

Expand Down Expand Up @@ -81,33 +81,34 @@ where
+ serde::de::DeserializeOwned,
V: Debug + Clone + serde::Serialize + serde::de::DeserializeOwned,
{
fn get(&self, id: &K) -> Option<V> {
self.value_by_id.get(id).cloned()
fn get(&self, id: &K) -> Option<&V> {
self.data.get(id)
}

fn set(&mut self, id: K, value: V) -> Result<()> {
self.value_by_id.insert(id, value);
fn set(&mut self, id: K, value: V) {
self.data.insert(id, value);
self.timestamp = std::time::SystemTime::now();
Ok(())
}

fn remove(&mut self, id: &K) -> Result<()> {
self.value_by_id.remove(id);
self.data.remove(id).ok_or_else(|| {
ArklibError::Storage(self.label.clone(), "Key not found".to_owned())
})?;
self.timestamp = std::time::SystemTime::now();
Ok(())
}

fn erase(&mut self) -> Result<()> {
fn erase(&self) -> Result<()> {
fs::remove_file(&self.path).map_err(|err| {
ArklibError::Storage(self.label.clone(), err.to_string())
})
}

fn as_ref(&self) -> &BTreeMap<K, V> {
&self.value_by_id
&self.data
}

fn is_file_updated(&self) -> Result<bool> {
fn is_storage_updated(&self) -> Result<bool> {
let file_timestamp = fs::metadata(&self.path)?.modified()?;
Ok(self.timestamp < file_timestamp)
}
Expand All @@ -130,11 +131,9 @@ where
}
data.push_str(&line);
}
let value_by_id: BTreeMap<K, V> = serde_json::from_str(&data)?;
self.value_by_id = value_by_id.clone();

let data: BTreeMap<K, V> = serde_json::from_str(&data)?;
self.timestamp = new_timestamp;
Ok(value_by_id)
Ok(data)
}
None => Err(ArklibError::Storage(
self.label.clone(),
Expand All @@ -159,9 +158,9 @@ where
.as_bytes(),
)?;

let value_map = self.value_by_id.clone();
let data = serde_json::to_string(&value_map)?;
writer.write_all(data.as_bytes())?;
let value_map = self.data.clone();
let value_data = serde_json::to_string(&value_map)?;
writer.write_all(value_data.as_bytes())?;

let new_timestamp = fs::metadata(&self.path)?.modified()?;
if new_timestamp == self.timestamp {
Expand Down Expand Up @@ -194,11 +193,8 @@ mod tests {
let mut file_storage =
FileStorage::new("TestStorage".to_string(), &storage_path);

let mut data_to_write = BTreeMap::new();
data_to_write.insert("key1".to_string(), "value1".to_string());
data_to_write.insert("key2".to_string(), "value2".to_string());

file_storage.value_by_id = data_to_write.clone();
file_storage.set("key1".to_string(), "value1".to_string());
file_storage.set("key1".to_string(), "value2".to_string());

file_storage
.write_fs()
Expand All @@ -208,7 +204,8 @@ mod tests {
.read_fs()
.expect("Failed to read data from disk");

assert_eq!(data_read, data_to_write);
assert_eq!(data_read.len(), 1);
assert_eq!(data_read.get("key1").map(|v| v.as_str()), Some("value2"))
}

#[test]
Expand All @@ -220,11 +217,8 @@ mod tests {
let mut file_storage =
FileStorage::new("TestStorage".to_string(), &storage_path);

let mut data_to_write = BTreeMap::new();
data_to_write.insert("key1".to_string(), "value1".to_string());
data_to_write.insert("key2".to_string(), "value2".to_string());

file_storage.value_by_id = data_to_write.clone();
file_storage.set("key1".to_string(), "value1".to_string());
file_storage.set("key1".to_string(), "value2".to_string());

file_storage
.write_fs()
Expand Down

0 comments on commit a01a07b

Please sign in to comment.