-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add FileStorage logic, example and documentation Co-Authored-by: Ishan Bhanuka <bhanuka.ishan@gmail.com> Co-Authored-by: Pushkar Mishra <pushkarmishra029@gmail.com> Co-Authored-by: Tarek <tareknaser360@gmail.com> Co-Authored-by: Kirill Taran <kirill@taran.space> * refactor done Signed-off-by: Pushkar Mishra <pushkarmishra029@gmail.com> * fix cargo.toml Signed-off-by: Pushkar Mishra <pushkarmishra029@gmail.com> * Update fs-storage/src/file_storage.rs Co-authored-by: Tarek Elsayed <60650661+tareknaser@users.noreply.github.com> * Update fs-storage/src/file_storage.rs Co-authored-by: Tarek Elsayed <60650661+tareknaser@users.noreply.github.com> * Update fs-storage/src/file_storage.rs Co-authored-by: Tarek Elsayed <60650661+tareknaser@users.noreply.github.com> * Add doc comment for erase * feat(fs-storage): refactor CLI write cmd to accept key-value pairs Signed-off-by: Tarek <tareknaser360@gmail.com> --------- Signed-off-by: Pushkar Mishra <pushkarmishra029@gmail.com> Signed-off-by: Tarek <tareknaser360@gmail.com> Co-authored-by: Pushkar Mishra <pushkarmishra029@gmail.com> Co-authored-by: Tarek <tareknaser360@gmail.com> Co-authored-by: Kirill Taran <kirill@taran.space> Co-authored-by: Tarek Elsayed <60650661+tareknaser@users.noreply.github.com>
- Loading branch information
1 parent
8e0a80b
commit 4856876
Showing
8 changed files
with
414 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Ark file system storage | ||
|
||
File system storage implementation for writing key value pairs to disk. | ||
|
||
## Steps to use CLI | ||
|
||
- Create a test.json file of key:values pairs you want to store. | ||
|
||
```json | ||
{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
"key3": "value3" | ||
} | ||
``` | ||
|
||
- Run Write Command | ||
|
||
```bash | ||
cargo run --example cli write /tmp/z test.json | ||
``` | ||
|
||
Alternatively, you can directly provide the input data as a comma-separated list of key-value pairs | ||
|
||
```bash | ||
cargo run --example cli write /tmp/z a:1,b:2,c:3 | ||
``` | ||
|
||
- Run Read Command | ||
|
||
```bash | ||
cargo run --example cli read /tmp/z key1,key2 | ||
``` | ||
|
||
- Get Output | ||
|
||
```bash | ||
key1: value1 | ||
key2: value2 | ||
``` | ||
|
||
- To get all key value pairs | ||
|
||
```bash | ||
cargo run --example cli read /tmp/z | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use anyhow::{Context, Result}; | ||
use fs_storage::file_storage::FileStorage; | ||
use serde_json::Value; | ||
use std::collections::BTreeMap; | ||
use std::env; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
fn main() { | ||
if let Err(e) = run() { | ||
eprintln!("Error: {}", e); | ||
} | ||
} | ||
|
||
fn run() -> Result<()> { | ||
let args: Vec<String> = env::args().collect(); | ||
if args.len() < 3 { | ||
println!("Usage:"); | ||
println!(" cargo run --example cli write <path> [JSON_FILE_PATH | KEY_VALUE_PAIRS]"); | ||
println!(" cargo run --example cli read <path> <key1,key2,...>"); | ||
return Ok(()); | ||
} | ||
|
||
let command = &args[1]; | ||
let path = &args[2]; | ||
match command.as_str() { | ||
"read" => read_command(&args, path), | ||
"write" => write_command(&args, path), | ||
_ => { | ||
eprintln!("Invalid command. Use 'read' or 'write'."); | ||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
fn read_command(args: &[String], path: &str) -> Result<()> { | ||
let keys = if args.len() > 3 { | ||
args[3] | ||
.split(',') | ||
.map(|s| s.to_string()) | ||
.collect::<Vec<String>>() | ||
} else { | ||
vec![] | ||
}; | ||
|
||
let mut fs = FileStorage::new("cli".to_string(), Path::new(path)); | ||
let map: BTreeMap<String, String> = | ||
fs.read_file().context("Failed to read file")?; | ||
|
||
if keys.is_empty() { | ||
for (key, value) in map { | ||
println!("{}: {}", key, value); | ||
} | ||
} else { | ||
for key in &keys { | ||
if let Some(value) = map.get(key) { | ||
println!("{}: {}", key, value); | ||
} else { | ||
eprintln!("Key '{}' not found", key); | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn write_command(args: &[String], path: &str) -> Result<()> { | ||
if args.len() < 4 { | ||
println!("Usage: cargo run --example cli write <path> [JSON_FILE_PATH | KEY_VALUE_PAIRS]"); | ||
return Ok(()); | ||
} | ||
|
||
let content = &args[3]; | ||
// Check if the content is a JSON file path | ||
let content_json = Path::new(content) | ||
.extension() | ||
.map_or(false, |ext| ext == "json"); | ||
|
||
let mut kv_pairs = BTreeMap::new(); | ||
if content_json { | ||
let content = | ||
fs::read_to_string(content).context("Failed to read JSON file")?; | ||
let json: Value = | ||
serde_json::from_str(&content).context("Failed to parse JSON")?; | ||
if let Value::Object(object) = json { | ||
for (key, value) in object { | ||
if let Value::String(value_str) = value { | ||
kv_pairs.insert(key, value_str); | ||
} else { | ||
println!( | ||
"Warning: Skipping non-string value for key '{}'", | ||
key | ||
); | ||
} | ||
} | ||
} else { | ||
println!("JSON value is not an object"); | ||
return Ok(()); | ||
} | ||
} else { | ||
let pairs = content.split(','); | ||
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()); | ||
} | ||
} | ||
} | ||
|
||
let mut fs = FileStorage::new("cli".to_string(), Path::new(path)); | ||
fs.write_file(&kv_pairs) | ||
.context("Failed to write file")?; | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.