Skip to content

Commit

Permalink
0.5.3. fix get progress (#173)
Browse files Browse the repository at this point in the history
* progress

* fmt

* and clippy

* remove old comments

* mod

* gets

* return b

* fmt

* get

* get

* fmt

* 0.5.3
  • Loading branch information
sergey-shandar committed May 14, 2024
1 parent 8f8f2fd commit 8e575da
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 49 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## 0.5.3

- fix progress for `get`. PR [#173](https://github.com/datablockset/blockset/pull/173).

## 0.5.2

- fix progress for `add`. PR [#172](https://github.com/datablockset/blockset/pull/172).
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ members = ["blockset", "blockset-lib"]
resolver = "2"

[workspace.package]
version = "0.5.2"
version = "0.5.3"
edition = "2021"
license = "GPL-3.0-or-later"
authors = ["Sergey Shandar"]
repository = "https://github.com/datablockset/blockset"

[workspace.dependencies]
blockset-lib = { path = "blockset-lib", version = "0.5.2" }
blockset-lib = { path = "blockset-lib", version = "0.5.3" }
io-trait = "0.11.0"
io-impl = "0.11.0"
io-test = "0.11.0"
Expand Down
72 changes: 66 additions & 6 deletions blockset-lib/src/app/get.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
use std::io::{self, Write};
use std::io::{self, Cursor, Write};

use io_trait::Io;
use nanvm_lib::{
common::default::default,
js::any::Any,
mem::manager::Manager,
js::{any::Any, js_object::JsObjectRef},
mem::{global::GLOBAL, manager::Manager},
parser::{parse_with_tokens, Context, ParseError, ParseResult},
tokenizer::tokenize,
};

use crate::{
cdt::node_type::NodeType,
common::status_line::{mb, StatusLine},
forest::{file::FileForest, node_id::ForestNodeId, Forest},
uint::u224::U224,
};

use super::invalid_input;
use super::{add::posix_path, get_hash, invalid_input, js_string_to_string, str_to_hash, try_move};

pub fn restore(io: &impl Io, hash: &U224, w: &mut impl Write) -> io::Result<()> {
FileForest(io).restore(&ForestNodeId::new(NodeType::Root, hash), w, io)
pub fn restore(
io: &impl Io,
hash: &U224,
w: &mut impl Write,
progress: &mut impl FnMut(u64, f64) -> io::Result<()>,
) -> io::Result<u64> {
FileForest(io).restore(&ForestNodeId::new(NodeType::Root, hash), w, progress)
}

fn tokenize_and_parse<M: Manager>(
Expand Down Expand Up @@ -51,3 +57,57 @@ pub fn create_file_recursively<T: Io>(io: &T, path: &str) -> io::Result<T::File>
create_file_path_recursively(io, path)?;
io.create(path)
}

fn set_progress(
state: &mut StatusLine<impl Io>,
progress_b: u64,
progress_p: f64,
) -> io::Result<()> {
state.set_progress(&(mb(progress_b) + ", "), progress_p)
}

fn get_if(d: &U224, path: &str, io: &impl Io) -> io::Result<()> {
let mut state = StatusLine::new(io);
if path.ends_with('/') {
let mut buffer = Vec::default();
let mut w = Cursor::new(&mut buffer);
restore(io, d, &mut w, &mut |_, _| Ok(()))?;
let json = try_move::<_, JsObjectRef<_>>(parse_json(io, GLOBAL, buffer)?)?;
let items = json.items();
let t = items.len();
let mut b = 0;
for (offset, (k, v)) in items.iter().enumerate() {
let file = js_string_to_string(k)?;
let hash = js_string_to_string(&try_move(v.clone())?)?;
b += restore(
io,
&str_to_hash(&hash)?,
&mut create_file_recursively(io, (path.to_owned() + &file).as_str())?,
&mut |progress_b, progress_p| {
set_progress(
&mut state,
b + progress_b,
(offset as f64 + progress_p) / t as f64,
)
},
)?;
}
Ok(())
} else {
restore(
io,
d,
&mut create_file_recursively(io, path)?,
&mut |progress_b, progress_p| set_progress(&mut state, progress_b, progress_p),
)?;
Ok(())
}
}

pub fn get<T: Io>(io: &T, a: &mut T::Args) -> io::Result<()> {
get_if(
&get_hash(a)?,
&posix_path(a.next().ok_or(invalid_input("missing file name"))?.as_str()),
io,
)
}
36 changes: 5 additions & 31 deletions blockset-lib/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ mod add;
mod add_entry;
mod get;

use std::io::{self, Cursor, ErrorKind, Read, Write};
use std::io::{self, ErrorKind, Read, Write};

use add_entry::add_entry;
use get::get;

use io_trait::Io;
use nanvm_lib::{
js::{any::Any, any_cast::AnyCast, js_object::JsObjectRef, js_string::JsStringRef},
mem::{global::GLOBAL, manager::Dealloc},
js::{any::Any, any_cast::AnyCast, js_string::JsStringRef},
mem::manager::Dealloc,
};

use crate::{
Expand All @@ -26,11 +27,6 @@ use crate::{
uint::u224::U224,
};

use self::{
add::posix_path,
get::{create_file_recursively, parse_json, restore},
};

fn set_progress(
state: &mut StatusLine<'_, impl Io>,
display_new: bool,
Expand Down Expand Up @@ -161,29 +157,7 @@ pub fn run(io: &impl Io) -> io::Result<()> {
"validate" => validate(&mut a, stdout),
"hash" => add_entry(io, &mut a, &|_| (), false),
"add" => add_entry(io, &mut a, &|io| ForestTreeAdd::new(FileForest(io)), true),
"get" => {
let d = get_hash(&mut a)?;
let path = posix_path(a.next().ok_or(invalid_input("missing file name"))?.as_str());
if path.ends_with('/') {
let mut buffer = Vec::default();
let mut w = Cursor::new(&mut buffer);
restore(io, &d, &mut w)?;
let json: JsObjectRef<_> = try_move(parse_json(io, GLOBAL, buffer)?)?;
for (k, v) in json.items() {
let file = js_string_to_string(k)?;
let hash = js_string_to_string(&try_move(v.clone())?)?;
restore(
io,
&str_to_hash(&hash)?,
&mut create_file_recursively(io, (path.to_owned() + &file).as_str())?,
)?;
// stdout.println([&path, ": ", &hash])?;
}
Ok(())
} else {
restore(io, &d, &mut create_file_recursively(io, &path)?)
}
}
"get" => get(io, &mut a),
"info" => stdout.println(["size: ", calculate_total(io)?.to_string().as_str(), " B."]),
_ => Err(invalid_input("unknown command")),
}
Expand Down
20 changes: 11 additions & 9 deletions blockset-lib/src/forest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use std::io::{self, Write};

use io_trait::Io;

use crate::{
cdt::{node_id::root, node_type::NodeType},
common::status_line::{mb, StatusLine},
uint::{u224::U224, u32::from_u8x4},
};

Expand Down Expand Up @@ -42,17 +39,21 @@ pub trait Forest {
Ok(true)
}
// we should extract a state machine from the function and remove `set_progress`.
fn restore(&self, id: &ForestNodeId, w: &mut impl Write, io: &impl Io) -> io::Result<()> {
fn restore(
&self,
id: &ForestNodeId,
w: &mut impl Write,
mut progress: impl FnMut(u64, f64) -> io::Result<()>,
) -> io::Result<u64> {
if id.hash == EMPTY {
return Ok(());
return Ok(0);
}
let mut tail = Vec::default();
let mut keys = [(id.hash, 1.0)].to_vec();
let mut progress_p = 0.0;
let mut progress_b = 0;
let mut state = StatusLine::new(io);
let mut t = id.node_type;
state.set_progress("", 0.0)?;
progress(0, 0.0)?;
while let Some((key, size)) = keys.pop() {
let v = self.get_block(&ForestNodeId::new(t, &key))?;
if let Some(len) = get_len(&v) {
Expand Down Expand Up @@ -80,10 +81,11 @@ pub trait Forest {
w.write_all(buf)?;
progress_p += size;
progress_b += buf.len() as u64;
state.set_progress(&(mb(progress_b) + ", "), progress_p)?;
progress(progress_b, progress_p)?;
}
t = NodeType::Child;
}
w.write_all(&tail)
w.write_all(&tail)?;
Ok(progress_b)
}
}
8 changes: 7 additions & 1 deletion blockset-lib/src/forest/tree_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ mod test {

use crate::{
cdt::{main_tree::MainTreeAdd, node_type::NodeType, tree_add::TreeAdd},
common::status_line::{mb, StatusLine},
forest::{mem::MemForest, node_id::ForestNodeId, Forest},
uint::u224::U224,
};
Expand Down Expand Up @@ -174,8 +175,13 @@ mod test {
let mut v = Vec::default();
let mut cursor = Cursor::new(&mut v);
let io = VirtualIo::new(&[]);
let mut state = StatusLine::new(&io);
table
.restore(&ForestNodeId::new(NodeType::Root, &k), &mut cursor, &io)
.restore(
&ForestNodeId::new(NodeType::Root, &k),
&mut cursor,
|progress_b, progress_p| state.set_progress(&(mb(progress_b) + ", "), progress_p),
)
.unwrap();
assert_eq!(v, c.as_bytes());
}
Expand Down

0 comments on commit 8e575da

Please sign in to comment.