Skip to content

Commit

Permalink
refactor(autonomi): move structs to top-level
Browse files Browse the repository at this point in the history
  • Loading branch information
b-zee committed Dec 6, 2024
1 parent 3781406 commit c8cfafc
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 115 deletions.
3 changes: 1 addition & 2 deletions ant-cli/src/access/user_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use std::collections::HashMap;

use autonomi::client::{
address::{addr_to_str, str_to_addr},
archive::ArchiveAddr,
archive_private::PrivateArchiveAccess,
files::{archive::PrivateArchiveAccess, archive_public::ArchiveAddr},
registers::{RegisterAddress, RegisterSecretKey},
vault::UserData,
};
Expand Down
5 changes: 4 additions & 1 deletion ant-cli/src/actions/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

use super::get_progress_bar;
use autonomi::{
client::{address::str_to_addr, archive::ArchiveAddr, archive_private::PrivateArchiveAccess},
client::{
address::str_to_addr,
files::{archive::PrivateArchiveAccess, archive_public::ArchiveAddr},
},
Client,
};
use color_eyre::{
Expand Down
10 changes: 6 additions & 4 deletions autonomi/src/client/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::{self_encryption::encrypt, Client};
pub mod public;

/// Number of chunks to upload in parallel.
///
/// Can be overridden by the `CHUNK_UPLOAD_BATCH_SIZE` environment variable.
pub(crate) static CHUNK_UPLOAD_BATCH_SIZE: LazyLock<usize> = LazyLock::new(|| {
let batch_size = std::env::var("CHUNK_UPLOAD_BATCH_SIZE")
Expand All @@ -39,12 +40,10 @@ pub(crate) static CHUNK_UPLOAD_BATCH_SIZE: LazyLock<usize> = LazyLock::new(|| {
batch_size
});

/// Number of retries to upload chunks.
pub(crate) const RETRY_ATTEMPTS: usize = 3;

/// Number of chunks to download in parallel.
///
/// Can be overridden by the `CHUNK_DOWNLOAD_BATCH_SIZE` environment variable.
pub(crate) static CHUNK_DOWNLOAD_BATCH_SIZE: LazyLock<usize> = LazyLock::new(|| {
pub static CHUNK_DOWNLOAD_BATCH_SIZE: LazyLock<usize> = LazyLock::new(|| {
let batch_size = std::env::var("CHUNK_DOWNLOAD_BATCH_SIZE")
.ok()
.and_then(|s| s.parse().ok())
Expand All @@ -58,6 +57,9 @@ pub(crate) static CHUNK_DOWNLOAD_BATCH_SIZE: LazyLock<usize> = LazyLock::new(||
batch_size
});

/// Number of retries to upload chunks.
pub(crate) const RETRY_ATTEMPTS: usize = 3;

/// Raw Data Address (points to a DataMap)
pub type DataAddr = XorName;
/// Raw Chunk Address (points to a [`Chunk`])
Expand Down
38 changes: 37 additions & 1 deletion autonomi/src/client/files/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use std::{

use ant_networking::target_arch::{Duration, SystemTime, UNIX_EPOCH};

use super::archive_public::{Metadata, RenameError};
use crate::{
client::{
data::{GetError, PrivateDataAccess, PutError},
Expand All @@ -23,11 +22,48 @@ use crate::{
};
use bytes::Bytes;
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// The address of a private archive
/// Contains the [`PrivateDataAccess`] leading to the [`PrivateArchive`] data
pub type PrivateArchiveAccess = PrivateDataAccess;

#[derive(Error, Debug, PartialEq, Eq)]
pub enum RenameError {
#[error("File not found in archive: {0}")]
FileNotFound(PathBuf),
}

/// Metadata for a file in an archive. Time values are UNIX timestamps.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Metadata {
/// When the file was (last) uploaded to the network.
pub uploaded: u64,
/// File creation time on local file system. See [`std::fs::Metadata::created`] for details per OS.
pub created: u64,
/// Last file modification time taken from local file system. See [`std::fs::Metadata::modified`] for details per OS.
pub modified: u64,
/// File size in bytes
pub size: u64,
}

impl Metadata {
/// Create a new metadata struct with the current time as uploaded, created and modified.
pub fn new_with_size(size: u64) -> Self {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or(Duration::from_secs(0))
.as_secs();

Self {
uploaded: now,
created: now,
modified: now,
size,
}
}
}

/// A private archive of files that containing file paths, their metadata and the files data maps
/// Using archives is useful for uploading entire directories to the network, only needing to keep track of a single address.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
Expand Down
48 changes: 7 additions & 41 deletions autonomi/src/client/files/archive_public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,17 @@ use bytes::Bytes;
use serde::{Deserialize, Serialize};
use xor_name::XorName;

/// The address of an archive on the network. Points to an [`Archive`].
pub type ArchiveAddr = XorName;

use thiserror::Error;

use super::archive::Metadata;
use crate::{
client::data::{CostError, DataAddr, GetError, PutError},
client::{
data::{CostError, DataAddr, GetError, PutError},
files::archive::RenameError,
},
Client,
};

#[derive(Error, Debug, PartialEq, Eq)]
pub enum RenameError {
#[error("File not found in archive: {0}")]
FileNotFound(PathBuf),
}
/// The address of an archive on the network. Points to an [`Archive`].
pub type ArchiveAddr = XorName;

/// An archive of files that containing file paths, their metadata and the files data addresses
/// Using archives is useful for uploading entire directories to the network, only needing to keep track of a single address.
Expand All @@ -42,36 +38,6 @@ pub struct Archive {
map: HashMap<PathBuf, (DataAddr, Metadata)>,
}

/// Metadata for a file in an archive. Time values are UNIX timestamps.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Metadata {
/// When the file was (last) uploaded to the network.
pub uploaded: u64,
/// File creation time on local file system. See [`std::fs::Metadata::created`] for details per OS.
pub created: u64,
/// Last file modification time taken from local file system. See [`std::fs::Metadata::modified`] for details per OS.
pub modified: u64,
/// File size in bytes
pub size: u64,
}

impl Metadata {
/// Create a new metadata struct with the current time as uploaded, created and modified.
pub fn new_with_size(size: u64) -> Self {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or(Duration::from_secs(0))
.as_secs();

Self {
uploaded: now,
created: now,
modified: now,
size,
}
}
}

impl Archive {
/// Create a new emtpy local archive
/// Note that this does not upload the archive to the network
Expand Down
63 changes: 59 additions & 4 deletions autonomi/src/client/files/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,72 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::client::data::PrivateDataAccess;
use crate::client::data::{CostError, GetError, PrivateDataAccess, PutError};
use crate::client::utils::process_tasks_with_max_concurrency;
use crate::client::Client;
use ant_evm::EvmWallet;
use bytes::Bytes;
use std::path::PathBuf;
use std::{path::PathBuf, sync::LazyLock};

use super::archive::{PrivateArchive, PrivateArchiveAccess};
use super::fs_public::{DownloadError, UploadError};

use super::fs_public::FILE_UPLOAD_BATCH_SIZE;
/// Number of files to upload in parallel.
///
/// Can be overridden by the `FILE_UPLOAD_BATCH_SIZE` environment variable.
pub static FILE_UPLOAD_BATCH_SIZE: LazyLock<usize> = LazyLock::new(|| {
let batch_size = std::env::var("FILE_UPLOAD_BATCH_SIZE")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
* 8,
);
info!("File upload batch size: {}", batch_size);
batch_size
});

/// Errors that can occur during the file upload operation.
#[derive(Debug, thiserror::Error)]
pub enum UploadError {
#[error("Failed to recursively traverse directory")]
WalkDir(#[from] walkdir::Error),
#[error("Input/output failure")]
IoError(#[from] std::io::Error),
#[error("Failed to upload file")]
PutError(#[from] PutError),
#[error("Failed to fetch file")]
GetError(#[from] GetError),
#[error("Failed to serialize")]
Serialization(#[from] rmp_serde::encode::Error),
#[error("Failed to deserialize")]
Deserialization(#[from] rmp_serde::decode::Error),
}

/// Errors that can occur during the download operation.
#[derive(Debug, thiserror::Error)]
pub enum DownloadError {
#[error("Failed to download file")]
GetError(#[from] GetError),
#[error("IO failure")]
IoError(#[from] std::io::Error),
}

/// Errors that can occur during the file cost calculation.
#[derive(Debug, thiserror::Error)]
pub enum FileCostError {
#[error("Cost error: {0}")]
Cost(#[from] CostError),
#[error("IO failure")]
IoError(#[from] std::io::Error),
#[error("Serialization error")]
Serialization(#[from] rmp_serde::encode::Error),
#[error("Self encryption error")]
SelfEncryption(#[from] crate::self_encryption::Error),
#[error("Walkdir error")]
WalkDir(#[from] walkdir::Error),
}

impl Client {
/// Download a private file from network to local file system
Expand Down
64 changes: 4 additions & 60 deletions autonomi/src/client/files/fs_public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,17 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::client::data::{CostError, DataAddr, GetError, PutError};
use crate::client::data::DataAddr;
use crate::client::files::archive::Metadata;
use crate::client::utils::process_tasks_with_max_concurrency;
use crate::client::Client;
use ant_evm::EvmWallet;
use ant_networking::target_arch::{Duration, SystemTime};
use bytes::Bytes;
use std::path::PathBuf;
use std::sync::LazyLock;

use super::archive_public::{Archive, ArchiveAddr, Metadata};

/// Number of files to upload in parallel.
/// Can be overridden by the `FILE_UPLOAD_BATCH_SIZE` environment variable.
pub static FILE_UPLOAD_BATCH_SIZE: LazyLock<usize> = LazyLock::new(|| {
let batch_size = std::env::var("FILE_UPLOAD_BATCH_SIZE")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
* 8,
);
info!("File upload batch size: {}", batch_size);
batch_size
});

/// Errors that can occur during the file upload operation.
#[derive(Debug, thiserror::Error)]
pub enum UploadError {
#[error("Failed to recursively traverse directory")]
WalkDir(#[from] walkdir::Error),
#[error("Input/output failure")]
IoError(#[from] std::io::Error),
#[error("Failed to upload file")]
PutError(#[from] PutError),
#[error("Failed to fetch file")]
GetError(#[from] GetError),
#[error("Failed to serialize")]
Serialization(#[from] rmp_serde::encode::Error),
#[error("Failed to deserialize")]
Deserialization(#[from] rmp_serde::decode::Error),
}

/// Errors that can occur during the download operation.
#[derive(Debug, thiserror::Error)]
pub enum DownloadError {
#[error("Failed to download file")]
GetError(#[from] GetError),
#[error("IO failure")]
IoError(#[from] std::io::Error),
}

/// Errors that can occur during the file cost calculation.
#[derive(Debug, thiserror::Error)]
pub enum FileCostError {
#[error("Cost error: {0}")]
Cost(#[from] CostError),
#[error("IO failure")]
IoError(#[from] std::io::Error),
#[error("Serialization error")]
Serialization(#[from] rmp_serde::encode::Error),
#[error("Self encryption error")]
SelfEncryption(#[from] crate::self_encryption::Error),
#[error("Walkdir error")]
WalkDir(#[from] walkdir::Error),
}
use super::archive_public::{Archive, ArchiveAddr};
use super::fs::*;

impl Client {
/// Download file from network to local file system
Expand Down
3 changes: 1 addition & 2 deletions autonomi/tests/external_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use alloy::providers::Provider;
use ant_evm::{QuoteHash, TxHash};
use ant_logging::LogBuilder;
use autonomi::client::external_signer::encrypt_data;
use autonomi::client::files::archive::PrivateArchive;
use autonomi::client::files::archive_public::Metadata;
use autonomi::client::files::archive::{Metadata, PrivateArchive};
use autonomi::client::payment::Receipt;
use autonomi::client::vault::user_data::USER_DATA_VAULT_CONTENT_IDENTIFIER;
use autonomi::client::vault::VaultSecretKey;
Expand Down

0 comments on commit c8cfafc

Please sign in to comment.