Skip to content

Commit

Permalink
improved texts, clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
wasm-forge committed Aug 12, 2024
1 parent 6b714b5 commit adb2995
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 62 deletions.
10 changes: 4 additions & 6 deletions src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,15 @@ fn create_1000_files() {

let result = fns::list_files(&pic, "");

let filenames = vec!["stable_file.txt", "files1", "files2", "files3", "files4"];
let filenames = vec!["mount_file.txt", "files1", "files2", "files3", "files4"];

assert_eq!(result, filenames);
}

fn no_virtual_names(vec: Vec<String>) -> Vec<String> {
let mut v = vec;

v.retain(|v| !(*v).eq("stable_file.txt"));
v.retain(|v| !(*v).eq("mount_file.txt"));

v
}
Expand Down Expand Up @@ -445,7 +445,7 @@ fn large_file_read() {
fn large_file_read_after_upgrade() {
let pic = setup_initial_canister();

let filename = "stable_file.txt";
let filename = "mount_file.txt";

// create large file
fns::append_text(&pic, "t1.txt", "abcdef7890", 10_000_000);
Expand Down Expand Up @@ -473,7 +473,7 @@ fn large_file_read_after_upgrade() {
fn large_mounted_file_write() {
let pic = setup_initial_canister();

let filename = "stable_file.txt";
let filename = "mount_file.txt";

// create large buffer
fns::append_buffer(&pic, "abcdef7890", 10_000_000);
Expand Down Expand Up @@ -510,5 +510,3 @@ fn large_file_write() {

assert_eq!(size, 100_000_000);
}


2 changes: 1 addition & 1 deletion src/runtime/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl File {
mod tests {
use crate::{
fs::OpenFlags,
test_utils::{test_fs, test_fs_setups, test_fs_transient},
test_utils::{test_fs, test_fs_setups},
};

use super::*;
Expand Down
64 changes: 37 additions & 27 deletions src/storage/stable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,25 @@ impl<M: Memory> ChunkPtrAllocator<M> {
#[cfg(test)]
self.check_free(ptr);

println!("release: {}", ptr / FILE_CHUNK_SIZE as u64);

self.push_ptr(ptr);
}
}


struct StorageMemories<M: Memory> {
header_memory: VirtualMemory<M>,
metadata_memory: VirtualMemory<M>,
direntry_memory: VirtualMemory<M>,
filechunk_memory: VirtualMemory<M>,
mounted_meta_memory: VirtualMemory<M>,

v2_chunk_ptr_memory: VirtualMemory<M>,
v2_chunks_memory: VirtualMemory<M>,
v2_allocator_memory: VirtualMemory<M>,

}


#[repr(C)]
pub struct StableStorage<M: Memory> {
header: Cell<Header, VirtualMemory<M>>,
Expand Down Expand Up @@ -227,51 +240,48 @@ impl<M: Memory> StableStorage<M> {
let metadata_memory = memory_manager.get(MemoryId::new(memory_indices.start + 1u8));
let direntry_memory = memory_manager.get(MemoryId::new(memory_indices.start + 2u8));
let filechunk_memory = memory_manager.get(MemoryId::new(memory_indices.start + 3u8));
let mounted_meta = memory_manager.get(MemoryId::new(memory_indices.start + 4u8));
let mounted_meta_memory = memory_manager.get(MemoryId::new(memory_indices.start + 4u8));

let v2_chunk_ptr = memory_manager.get(MemoryId::new(memory_indices.start + 5u8));
let v2_chunks = memory_manager.get(MemoryId::new(memory_indices.start + 7u8));
let v2_chunk_ptr_memory = memory_manager.get(MemoryId::new(memory_indices.start + 5u8));
let v2_chunks_memory = memory_manager.get(MemoryId::new(memory_indices.start + 7u8));
let v2_allocator_memory = memory_manager.get(MemoryId::new(memory_indices.start + 6u8));

Self::new_with_custom_memories(
let memories = StorageMemories {
header_memory,
metadata_memory,
direntry_memory,
filechunk_memory,
mounted_meta,
v2_chunk_ptr,
v2_chunks,
mounted_meta_memory,
v2_chunk_ptr_memory,
v2_chunks_memory,
v2_allocator_memory,
};

Self::new_with_custom_memories(
memories
)
}

fn new_with_custom_memories(
header: VirtualMemory<M>,
metadata: VirtualMemory<M>,
direntry: VirtualMemory<M>,
filechunk: VirtualMemory<M>,
mounted_meta: VirtualMemory<M>,

v2_chunk_ptr: VirtualMemory<M>,
v2_chunks: VirtualMemory<M>,
v2_allocator_memory: VirtualMemory<M>,
memories: StorageMemories<M>

) -> Self {
let default_header_value = Header {
version: FS_VERSION,
next_node: ROOT_NODE + 1,
};

let v2_allocator = ChunkPtrAllocator::new(v2_allocator_memory);
let v2_allocator = ChunkPtrAllocator::new(memories.v2_allocator_memory);

let mut result = Self {
header: Cell::init(header, default_header_value).unwrap(),
metadata: BTreeMap::init(metadata),
direntry: BTreeMap::init(direntry),
filechunk: BTreeMap::init(filechunk),
mounted_meta: BTreeMap::init(mounted_meta),

v2_chunk_ptr: BTreeMap::init(v2_chunk_ptr),
v2_chunks,
header: Cell::init(memories.header_memory, default_header_value).unwrap(),
metadata: BTreeMap::init(memories.metadata_memory),
direntry: BTreeMap::init(memories.direntry_memory),
filechunk: BTreeMap::init(memories.filechunk_memory),
mounted_meta: BTreeMap::init(memories.mounted_meta_memory),

v2_chunk_ptr: BTreeMap::init(memories.v2_chunk_ptr_memory),
v2_chunks: memories.v2_chunks_memory,
v2_allocator,

// runtime data
Expand Down
25 changes: 2 additions & 23 deletions src/storage/transient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct TransientStorage {
filechunk: BTreeMap<(Node, FileChunkIndex), FileChunk>,
// Mounted memory Node metadata information.
mounted_meta: BTreeMap<Node, Metadata>,
// active mounts
// Active mounts.
active_mounts: HashMap<Node, Box<dyn Memory>>,
}

Expand All @@ -53,7 +53,7 @@ impl TransientStorage {
let mut result = Self {
header: Header {
version: 1,
next_node: ROOT_NODE + 1,
next_node: ROOT_NODE + 1
},
metadata: Default::default(),
direntry: Default::default(),
Expand Down Expand Up @@ -84,27 +84,6 @@ impl TransientStorage {
entry.bytes[offset as usize..offset as usize + buf.len()].copy_from_slice(buf)
}
}

// Fill the buffer contents with data of a chosen file chunk.
#[cfg(test)]
fn read_filechunk(
&self,
node: Node,
index: FileChunkIndex,
offset: FileSize,
buf: &mut [u8],
) -> Result<(), Error> {
if let Some(memory) = self.get_mounted_memory(node) {
// work with memory
let address = index as FileSize * FILE_CHUNK_SIZE as FileSize + offset as FileSize;
memory.read(address, buf);
} else {
let value = self.filechunk.get(&(node, index)).ok_or(Error::NotFound)?;
buf.copy_from_slice(&value.bytes[offset as usize..offset as usize + buf.len()]);
}

Ok(())
}
}

impl Storage for TransientStorage {
Expand Down
9 changes: 7 additions & 2 deletions src/storage/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ use crate::error::Error;
use ic_stable_structures::storable::Bound;
use serde::{Deserialize, Serialize};

//pub const FILE_CHUNK_SIZE: usize = 4096;
pub const FILE_CHUNK_SIZE: usize = 8192;
//pub const FILE_CHUNK_SIZE: usize = 1024;
//pub const FILE_CHUNK_SIZE: usize = 2048;
pub const FILE_CHUNK_SIZE: usize = 4096;
//pub const FILE_CHUNK_SIZE: usize = 8192;
//pub const FILE_CHUNK_SIZE: usize = 16384;
//pub const FILE_CHUNK_SIZE: usize = 32768;

pub const MAX_FILE_NAME: usize = 255;

// The unique identifier of a node, which can be a file or a directory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ thread_local! {
FileSystem::new(Box::new(storage)).unwrap()
);

fs.borrow_mut().mount_memory_file("stable_file.txt", Box::new(memory_manager.get(MemoryId::new(155)))).unwrap();
fs.borrow_mut().mount_memory_file("mount_file.txt", Box::new(memory_manager.get(MemoryId::new(155)))).unwrap();

fs
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ thread_local! {
FileSystem::new(Box::new(storage)).unwrap()
);

fs.borrow_mut().mount_memory_file("stable_file.txt", Box::new(memory_manager.get(MemoryId::new(155)))).unwrap();
fs.borrow_mut().mount_memory_file("mount_file.txt", Box::new(memory_manager.get(MemoryId::new(155)))).unwrap();

fs
})
Expand Down
2 changes: 1 addition & 1 deletion tests/fs_benchmark_test/store_stable.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

dfx canister call fs_benchmark_test_backend store_buffer '("stable_file.txt")'
dfx canister call fs_benchmark_test_backend store_buffer '("mount_file.txt")'

0 comments on commit adb2995

Please sign in to comment.