Skip to content

Commit

Permalink
chore: improve FS store
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Jul 14, 2024
1 parent 7985015 commit a4847f4
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 31 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ strip = true
opt-level = 's'

[workspace.package]
version = "0.6.0"
version = "0.6.1"
edition = "2021"
repository = "https://github.com/ldclabs/ic-oss"
keywords = ["file", "storage", "oss", "s3", "icp"]
Expand Down
2 changes: 0 additions & 2 deletions examples/ai_canister/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ fn admin_load_model(args: LoadModelInput) -> Result<u64, String> {

#[ic_cdk::init]
fn init() {
state::save();
fs::save();
// state::init_rand();
}

Expand Down
5 changes: 3 additions & 2 deletions src/ic_oss_bucket/ic_oss_bucket.did
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ type BTreeMap = vec record {
};
type BucketInfo = record {
status : int8;
total_chunks : nat64;
trusted_eddsa_pub_keys : vec blob;
managers : vec principal;
name : text;
max_custom_data_size : nat16;
auditors : vec principal;
total_files : nat64;
max_children : nat16;
enable_hash_index : bool;
max_file_size : nat64;
folder_id : nat32;
visibility : nat8;
max_folder_depth : nat8;
trusted_ecdsa_pub_keys : vec blob;
file_count : nat64;
folder_count : nat64;
total_folders : nat64;
file_id : nat32;
};
type CanisterArgs = variant { Upgrade : UpgradeArgs; Init : InitArgs };
Expand Down
1 change: 0 additions & 1 deletion src/ic_oss_bucket/src/api_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ fn init(args: Option<CanisterArgs>) {
None => {}
}

store::state::save();
store::state::init_http_certified_data();
}

Expand Down
5 changes: 3 additions & 2 deletions src/ic_oss_bucket/src/api_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ fn get_bucket_info(access_token: Option<ByteBuf>) -> Result<BucketInfo, String>

Ok(store::state::with(|r| BucketInfo {
name: r.name.clone(),
file_count: r.file_count,
file_id: r.file_id,
folder_count: r.folder_count,
folder_id: r.folder_id,
max_file_size: r.max_file_size,
max_folder_depth: r.max_folder_depth,
Expand All @@ -47,6 +45,9 @@ fn get_bucket_info(access_token: Option<ByteBuf>) -> Result<BucketInfo, String>
enable_hash_index: r.enable_hash_index,
status: r.status,
visibility: r.visibility,
total_files: store::fs::total_files(),
total_chunks: store::fs::total_chunks(),
total_folders: store::fs::total_folders(),
managers: r.managers.clone(),
auditors: r.auditors.clone(),
trusted_ecdsa_pub_keys: r.trusted_ecdsa_pub_keys.clone(),
Expand Down
20 changes: 13 additions & 7 deletions src/ic_oss_bucket/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ type Memory = VirtualMemory<DefaultMemoryImpl>;
#[derive(Clone, Deserialize, Serialize)]
pub struct Bucket {
pub name: String,
pub file_count: u64,
pub file_id: u32,
pub folder_count: u64,
pub folder_id: u32,
pub max_file_size: u64,
pub max_folder_depth: u8,
Expand All @@ -58,10 +56,8 @@ impl Default for Bucket {
fn default() -> Self {
Self {
name: "default".to_string(),
file_count: 0,
file_id: 0,
folder_count: 1, // The root folder 0 is created by default
folder_id: 1,
folder_id: 1, // The root folder 0 is created by default
max_file_size: MAX_FILE_SIZE,
max_folder_depth: 10,
max_children: 100,
Expand Down Expand Up @@ -789,6 +785,18 @@ pub mod state {
pub mod fs {
use super::*;

pub fn total_files() -> u64 {
FS_METADATA_STORE.with(|r| r.borrow().len())
}

pub fn total_chunks() -> u64 {
FS_CHUNKS_STORE.with(|r| r.borrow().len())
}

pub fn total_folders() -> u64 {
FOLDERS.with(|r| r.borrow().len() as u64)
}

pub fn get_file_id(hash: &[u8; 32]) -> Option<u32> {
HASHS.with(|r| r.borrow().get(hash).copied())
}
Expand Down Expand Up @@ -852,7 +860,6 @@ pub mod fs {
)?;

s.folder_id = s.folder_id.saturating_add(1);
s.folder_count += 1;
Ok(id)
})
})
Expand Down Expand Up @@ -884,7 +891,6 @@ pub mod fs {
}

s.file_id = s.file_id.saturating_add(1);
s.file_count += 1;
parent.files.insert(id);
FS_METADATA_STORE.with(|r| r.borrow_mut().insert(id, metadata));
Ok(id)
Expand Down
2 changes: 0 additions & 2 deletions src/ic_oss_can/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ mod test {

#[test]
fn test_ic_oss_fs() {
fs::set_max_file_size(1024 * 1024);

let files = fs::list_files(u32::MAX, 2);
assert!(files.is_empty());

Expand Down
10 changes: 7 additions & 3 deletions src/ic_oss_can/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#[macro_export]
macro_rules! ic_oss_fs {
() => {
#[allow(dead_code)]
pub mod fs {
use candid::Principal;
use ciborium::{from_reader, into_writer};
Expand Down Expand Up @@ -65,6 +66,10 @@ macro_rules! ic_oss_fs {
});
}

pub fn total_chunks() -> u64 {
FS_CHUNKS_STORE.with(|r| r.borrow().len())
}

pub fn get_file(id: u32) -> Option<FileMetadata> {
if id == 0 {
return None;
Expand All @@ -82,13 +87,12 @@ macro_rules! ic_oss_fs {
Err(format!("file size exceeds limit: {}", r.max_file_size))?;
}

let id = r.file_id.saturating_add(1);
let id = r.file_id;
if id == u32::MAX {
Err("file id overflow".to_string())?;
}

r.file_id = id;
r.file_count += 1;
r.file_id = id.saturating_add(1);
r.files.insert(id, file);
Ok(id)
})
Expand Down
15 changes: 13 additions & 2 deletions src/ic_oss_can/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ use std::{

pub const MILLISECONDS: u64 = 1_000_000_000;

#[derive(Clone, Default, Deserialize, Serialize)]
#[derive(Clone, Deserialize, Serialize)]
pub struct Files {
pub file_id: u32,
pub file_count: u64,
pub max_file_size: u64,
pub visibility: u8, // 0: private; 1: public
pub managers: BTreeSet<Principal>, // managers can read and write
Expand All @@ -41,6 +40,18 @@ impl Files {
}
}

impl Default for Files {
fn default() -> Self {
Self {
file_id: 1, // 0 is reserved for the Files data itself
max_file_size: MAX_FILE_SIZE,
visibility: 0,
managers: BTreeSet::new(),
files: BTreeMap::new(),
}
}
}

impl Storable for Files {
const BOUND: Bound = Bound::Unbounded;

Expand Down
5 changes: 3 additions & 2 deletions src/ic_oss_types/src/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use crate::{file::MAX_FILE_SIZE, ByteN};
#[derive(CandidType, Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
pub struct BucketInfo {
pub name: String,
pub file_count: u64,
pub file_id: u32,
pub folder_count: u64,
pub folder_id: u32,
pub max_file_size: u64,
pub max_folder_depth: u8,
Expand All @@ -19,6 +17,9 @@ pub struct BucketInfo {
pub enable_hash_index: bool,
pub status: i8, // -1: archived; 0: readable and writable; 1: readonly
pub visibility: u8, // 0: private; 1: public
pub total_files: u64,
pub total_chunks: u64,
pub total_folders: u64,
pub managers: BTreeSet<Principal>, // managers can read and write
// auditors can read and list even if the bucket is private
pub auditors: BTreeSet<Principal>,
Expand Down

0 comments on commit a4847f4

Please sign in to comment.