Skip to content

Commit

Permalink
using NaNVM lib (#161)
Browse files Browse the repository at this point in the history
* nanvm-lib

* remove array

* nanvm-lib = 0.0.1

* default

* codecov

* fmt

* assert

* unchecked_shr

* shl128

* shl

* app

* progress

* 99

* add

* 99.82%

* 100%

* clippy

* clippy

* 99.65%

* 98.24%

* 98.95%

* 100%

* assert
  • Loading branch information
sergey-shandar authored Feb 22, 2024
1 parent 8caf2f9 commit 822570d
Show file tree
Hide file tree
Showing 20 changed files with 319 additions and 134 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ repository = "https://github.com/datablockset/blockset"
io-trait = "0.8.0"
io-impl = "0.8.1"
io-test = "0.8.1"
wasm-bindgen-test = "0.3.39"
wasm-bindgen-test = "0.3.41"
nanvm-lib = "0.0.1"
1 change: 1 addition & 0 deletions blockset-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository.workspace = true

[dependencies]
io-trait.workspace = true
nanvm-lib.workspace = true

[dev-dependencies]
io-test.workspace = true
Expand Down
151 changes: 89 additions & 62 deletions blockset-lib/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,43 @@ use crate::{
uint::u224::U224,
};

fn set_progress(
state: &mut StatusLine<'_, impl Io>,
display_new: bool,
new: u64,
progress::State { current, total }: progress::State,
) -> io::Result<()> {
let p = if total == 0 {
1.0
} else {
(current as f64) / (total as f64)
};
let s = if display_new {
"New data: ".to_owned() + &mb(new) + ". "
} else {
String::new()
} + "Processed: "
+ &mb(current)
+ ", ";
state.set_progress(&s, p)
}

fn file_read(
file: &mut (impl Read + Progress),
tree: &mut MainTreeAdd<impl TreeAdd>,
new: &mut u64,
) -> io::Result<bool> {
let mut buf = [0; 1024];
let size = file.read(buf.as_mut())?;
if size == 0 {
return Ok(true);
}
for c in buf[0..size].iter() {
*new += tree.push(*c)?;
}
Ok(false)
}

fn read_to_tree<T: TreeAdd>(
s: T,
mut file: impl Read + Progress,
Expand All @@ -26,28 +63,10 @@ fn read_to_tree<T: TreeAdd>(
let mut new = 0;
loop {
let pr = file.progress();
let progress::State { current, total } = pr?;
let mut buf = [0; 1024];
let p = if total == 0 {
1.0
} else {
(current as f64) / (total as f64)
};
let s = if display_new {
"New data: ".to_owned() + &mb(new) + ". "
} else {
String::new()
} + "Processed: "
+ &mb(current)
+ ", ";
state.set_progress(&s, p)?;
let size = file.read(buf.as_mut())?;
if size == 0 {
set_progress(&mut state, display_new, new, pr?)?;
if file_read(&mut file, &mut tree, &mut new)? {
break;
}
for c in buf[0..size].iter() {
new += tree.push(*c)?;
}
}
Ok(tree.end()?.0.to_base32())
}
Expand All @@ -65,35 +84,58 @@ fn invalid_input(s: &str) -> io::Error {
io::Error::new(ErrorKind::InvalidInput, s)
}

fn add<'a, T: Io, S: 'a + TreeAdd>(
io: &'a T,
a: &mut T::Args,
storage: impl Fn(&'a T) -> S,
display_new: bool,
) -> io::Result<()> {
let stdout = &mut io.stdout();
let path = a.next().ok_or(invalid_input("missing file name"))?;
let to_posix_eol = if let Some(option) = a.next() {
fn is_to_posix_eol(a: &mut impl Iterator<Item = String>) -> io::Result<bool> {
Ok(if let Some(option) = a.next() {
if option != "--to-posix-eol" {
return Err(invalid_input("unknown option"));
}
true
} else {
false
};
// let len = io.metadata(&path)?.len();
let f = io.open(&path)?;
let s = storage(io);
let k = if to_posix_eol {
})
}

fn read_to_tree_file(
to_posix_eol: bool,
s: impl TreeAdd,
f: impl Read + Progress,
io: &impl Io,
display_new: bool,
) -> io::Result<String> {
if to_posix_eol {
// this may lead to incorrect progress bar because, a size of a file with replaced CRLF
// is smaller than `len`. Proposed solution:
// a Read implementation which can also report a progress.
read_to_tree(s, ToPosixEol::new(f), io, display_new)?
read_to_tree(s, ToPosixEol::new(f), io, display_new)
} else {
read_to_tree(s, f, io, display_new)?
};
println(stdout, &k)?;
Ok(())
read_to_tree(s, f, io, display_new)
}
}

fn add<'a, T: Io, S: 'a + TreeAdd>(
io: &'a T,
a: &mut T::Args,
storage: impl Fn(&'a T) -> S,
display_new: bool,
) -> io::Result<()> {
let stdout = &mut io.stdout();
let path = a.next().ok_or(invalid_input("missing file name"))?;
let to_posix_eol = is_to_posix_eol(a)?;
// let len = io.metadata(&path)?.len();
let f = io.open(&path)?;
let k = read_to_tree_file(to_posix_eol, storage(io), f, io, display_new)?;
println(stdout, &k)
}

fn get_hash(a: &mut impl Iterator<Item = String>) -> io::Result<U224> {
let b32 = a.next().ok_or(invalid_input("missing hash"))?;
b32.from_base32::<U224>()
.ok_or(invalid_input("invalid hash"))
}

fn validate(a: &mut impl Iterator<Item = String>, stdout: &mut impl Write) -> io::Result<()> {
let d = get_hash(a)?.to_base32();
println(stdout, &("valid: ".to_owned() + &d))
}

pub fn run(io: &impl Io) -> io::Result<()> {
Expand All @@ -102,34 +144,19 @@ pub fn run(io: &impl Io) -> io::Result<()> {
a.next().unwrap();
let command = a.next().ok_or(invalid_input("missing command"))?;
match command.as_str() {
"validate" => {
let b32 = a.next().ok_or(invalid_input("missing hash"))?;
let d = b32
.from_base32::<U224>()
.ok_or(invalid_input("invalid hash"))?;
print(stdout, "valid: ")?;
println(stdout, &d.to_base32())?;
Ok(())
}
"validate" => validate(&mut a, stdout),
"hash" => add(io, &mut a, |_| (), false),
"add" => add(io, &mut a, |io| ForestTreeAdd::new(FileForest(io)), true),
"get" => {
let b32 = a.next().ok_or(invalid_input("missing hash"))?;
let d = b32
.from_base32::<U224>()
.ok_or(invalid_input("invalid hash"))?;
let d = get_hash(&mut a)?;
let path = a.next().ok_or(invalid_input("missing file name"))?;
let mut f = io.create(&path)?;
let table = FileForest(io);
table.restore(&ForestNodeId::new(NodeType::Root, &d), &mut f, io)?;
Ok(())
}
"info" => {
let total = calculate_total(io)?;
let s = "size: ".to_owned() + &total.to_string() + " B.";
println(stdout, &s)?;
Ok(())
let w = &mut io.create(&path)?;
FileForest(io).restore(&ForestNodeId::new(NodeType::Root, &d), w, io)
}
"info" => println(
stdout,
&("size: ".to_owned() + &calculate_total(io)?.to_string() + " B."),
),
_ => Err(invalid_input("unknown command")),
}
}
Expand Down
4 changes: 3 additions & 1 deletion blockset-lib/src/cdt/main_tree.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::io;

use nanvm_lib::common::default::default;

use crate::uint::u224::U224;

use super::{
Expand All @@ -17,7 +19,7 @@ impl<T: TreeAdd> MainTreeAdd<T> {
pub fn new(tree_add: T) -> Self {
Self {
tree_add,
state: Vec::default(),
state: default(),
}
}
pub fn push(&mut self, c: u8) -> io::Result<u64> {
Expand Down
5 changes: 3 additions & 2 deletions blockset-lib/src/cdt/node_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub const fn root(hash: &U256) -> U224 {

#[cfg(test)]
mod test {
use nanvm_lib::common::default::default;
use wasm_bindgen_test::wasm_bindgen_test;

use crate::{
Expand Down Expand Up @@ -87,8 +88,8 @@ mod test {
#[wasm_bindgen_test]
#[test]
fn merge_empty_test() {
assert_eq!(merge(&to_node_id(0x12), &U256::default()), to_node_id(0x12));
assert_eq!(merge(&U256::default(), &to_node_id(0x34)), to_node_id(0x34));
assert_eq!(merge(&to_node_id(0x12), &default()), to_node_id(0x12));
assert_eq!(merge(&default(), &to_node_id(0x34)), to_node_id(0x34));
}

#[wasm_bindgen_test]
Expand Down
2 changes: 1 addition & 1 deletion blockset-lib/src/cdt/node_type.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[repr(u8)]
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NodeType {
Root = 0,
Child = 1,
Expand Down
14 changes: 8 additions & 6 deletions blockset-lib/src/cdt/subtree.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use nanvm_lib::common::cast::Cast;

use crate::{
cdt::node_id::merge,
common::array::ArrayEx,
uint::u256::{great, U256},
};

Expand Down Expand Up @@ -42,7 +43,7 @@ pub struct SubTree(Vec<Node>);

impl SubTree {
pub fn new(last: &U256) -> Self {
Self([Node::new2(last, 0)].move_to_vec())
Self([Node::new2(last, 0)].cast())
}
pub fn push(&mut self, last0: &U256) -> Option<U256> {
let mut height10 = 0;
Expand Down Expand Up @@ -79,6 +80,7 @@ impl SubTree {

#[cfg(test)]
mod test {
use nanvm_lib::common::default::default;
use wasm_bindgen_test::wasm_bindgen_test;

use crate::{
Expand Down Expand Up @@ -122,7 +124,7 @@ mod test {
assert!(t.0.is_empty());
}
{
let mut t = SubTree(Vec::default());
let mut t = SubTree(default());
assert_eq!(t.push(&c), None);
assert_eq!(
t.0,
Expand Down Expand Up @@ -244,7 +246,7 @@ mod test {
let a = to_node_id(b'a');
let b = to_node_id(b'b');
let ab = {
let mut t = SubTree(Vec::default());
let mut t = SubTree(default());
assert_eq!(t.push(&a), None);
assert_eq!(t.0, [Node::new2(&a, 0)]);
let ab = t.push(&b);
Expand All @@ -254,7 +256,7 @@ mod test {
}
.unwrap();
let baa = {
let mut t = SubTree(Vec::default());
let mut t = SubTree(default());
assert_eq!(t.push(&b), None);
assert_eq!(t.0, [Node::new2(&b, 0)]);
assert_eq!(t.push(&a), None);
Expand All @@ -266,7 +268,7 @@ mod test {
}
.unwrap();
{
let mut t = SubTree(Vec::default());
let mut t = SubTree(default());
assert_eq!(t.push(&ab), None);
assert_eq!(t.0, [Node::new2(&ab, 0)]);
let r = t.push(&baa);
Expand Down
17 changes: 0 additions & 17 deletions blockset-lib/src/common/array.rs

This file was deleted.

24 changes: 24 additions & 0 deletions blockset-lib/src/common/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,27 @@ pub const fn to_ascii(x: char) -> Option<u8> {
None
}
}

#[cfg(test)]
mod test {
use wasm_bindgen_test::wasm_bindgen_test;

use super::to_ascii;

#[inline(never)]
fn x(x: char, y: Option<u8>) {
assert_eq!(to_ascii(x), y);
if let Some(y) = y {
assert_eq!(to_ascii(char::from_u32(x as u32 / 2).unwrap()), Some(y / 2));
} else {
assert_eq!(to_ascii(char::from_u32(x as u32 / 2).unwrap()), None);
}
}

#[test]
#[wasm_bindgen_test]
fn test() {
x('a', Some(97));
x('🦀', None);
}
}
15 changes: 15 additions & 0 deletions blockset-lib/src/common/bit_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,18 @@ impl BitVec {
}
}
}

#[cfg(test)]
mod test {
use wasm_bindgen_test::wasm_bindgen_test;

use super::BitVec;

#[test]
#[wasm_bindgen_test]
fn test() {
let x = BitVec::new(0b1010, 4);
assert_eq!(x.value, 0b1010);
assert_eq!(x.len, 4);
}
}
Loading

0 comments on commit 822570d

Please sign in to comment.