Skip to content

Commit

Permalink
Merge pull request #10 from wasm-forge/large_files
Browse files Browse the repository at this point in the history
Better reading large files
  • Loading branch information
wasm-forge authored Jul 27, 2024
2 parents 90a0e23 + ad38717 commit 9854a89
Show file tree
Hide file tree
Showing 35 changed files with 878 additions and 52 deletions.
57 changes: 51 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "stable-fs"
version = "0.5.0"
version = "0.5.1"
edition = "2021"
description = "A Simple File system implementing WASI endpoints and using the stable structures of the Internet Computer"
keywords = ["ic", "internet-computer", "file-system"]
Expand All @@ -9,7 +9,7 @@ repository = "https://github.com/wasm-forge/stable-fs"

[dependencies]
bitflags = "2.3.1"
ic-cdk = "0.13.1"
ic-cdk = "0.15"
ic-stable-structures = "0.6.5"
serde = "1.0.164"
serde_bytes = "0.11"
Expand Down
3 changes: 1 addition & 2 deletions build_tests.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

cd src/tests/fs_benchmark_test
cd tests/fs_benchmark_test

cargo build --release --target wasm32-unknown-unknown

Expand All @@ -12,4 +12,3 @@ cargo build --release --target wasm32-unknown-unknown

wasi2ic target/wasm32-unknown-unknown/release/demo_test_upgraded_backend.wasm target/wasm32-unknown-unknown/release/demo_test_upgraded_backend_small.wasm


2 changes: 2 additions & 0 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ impl FileSystem {
let mut written_size = 0;
for buf in src {
let buf = unsafe { std::slice::from_raw_parts(buf.buf, buf.len) };

let size = file.write_with_offset(written_size + offset, buf, self.storage.as_mut())?;

written_size += size;
}
self.put_file(fd, file);
Expand Down
46 changes: 43 additions & 3 deletions src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use fns::read_text;
use pocket_ic::PocketIc;
use std::{cell::RefCell, fs};

const BACKEND_WASM: &str = "src/tests/fs_benchmark_test/target/wasm32-unknown-unknown/release/fs_benchmark_test_backend_small.wasm";
const BACKEND_WASM_UPGRADED: &str = "src/tests/demo_test_upgraded/target/wasm32-unknown-unknown/release/demo_test_upgraded_backend_small.wasm";
const BACKEND_WASM: &str = "tests/fs_benchmark_test/target/wasm32-unknown-unknown/release/fs_benchmark_test_backend_small.wasm";
const BACKEND_WASM_UPGRADED: &str = "tests/demo_test_upgraded/target/wasm32-unknown-unknown/release/demo_test_upgraded_backend_small.wasm";

thread_local!(
static ACTIVE_CANISTER: RefCell<Option<Principal>> = RefCell::new(None);
Expand Down Expand Up @@ -63,7 +63,7 @@ fn upgrade_canister(pic: &PocketIc) {

mod fns {

use candid::{decode_one, encode_one, Principal};
use candid::{decode_args, decode_one, encode_one, Principal};
use pocket_ic::{PocketIc, WasmResult};

use super::active_canister;
Expand Down Expand Up @@ -111,6 +111,25 @@ mod fns {
}
}

pub(crate) fn read_bytes(pic: &PocketIc, filename: &str, offset: i64, size: u64) -> (u64, u64) {
let response = pic
.update_call(
active_canister(),
Principal::anonymous(),
"read_bytes",
candid::encode_args((filename, offset, size)).unwrap(),
)
.unwrap();

if let WasmResult::Reply(response) = response {
let result: (u64, u64) = decode_args(&response).unwrap();

return result;
} else {
panic!("unintended call failure!");
}
}

pub(crate) fn create_files(pic: &PocketIc, path: &str, count: u64) {
pic.update_call(
active_canister(),
Expand Down Expand Up @@ -349,3 +368,24 @@ fn long_paths_and_file_names() {
assert_eq!(expected_content, content);

}


#[test]
fn large_file_read() {
let pic = setup_initial_canister();

let filename = "test.txt";

// create large file
fns::append_text(&pic, filename, "abcdef7890", 10_000_000);

let (instructions, size) = fns::read_bytes(&pic, filename, 13, 100_000_000);

println!("instructions {instructions}, size {size}");

assert!(instructions < 4_000_000_000, "The call should take less than 8 billion instructions");

assert_eq!(size, 99_999_987);


}
Loading

0 comments on commit 9854a89

Please sign in to comment.