Skip to content

Commit

Permalink
refacto: Create a file_uri module containing FileUri, `TemplateUr…
Browse files Browse the repository at this point in the history
…i` and `MultiFilesUri`
  • Loading branch information
sfauvel committed Jan 16, 2025
1 parent bdcc8de commit fc43569
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use anyhow::Context;
use async_trait::async_trait;
use mithril_common::entities::FileUri;
use semver::Version;
use slog::{debug, warn, Logger};
use std::path::Path;
use std::sync::Arc;
use thiserror::Error;

use crate::{file_uploaders::FileUri, snapshotter::OngoingSnapshot, FileUploader, Snapshotter};
use crate::{snapshotter::OngoingSnapshot, FileUploader, Snapshotter};

use super::ArtifactBuilder;
use mithril_common::logging::LoggerExtensions;
Expand Down
4 changes: 2 additions & 2 deletions mithril-aggregator/src/file_uploaders/dumb_uploader.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::anyhow;
use async_trait::async_trait;
use mithril_common::StdResult;
use mithril_common::{entities::FileUri, StdResult};
use std::{path::Path, sync::RwLock};

use crate::file_uploaders::{FileUploader, FileUri};
use crate::file_uploaders::FileUploader;

/// Dummy uploader for test purposes.
///
Expand Down
4 changes: 2 additions & 2 deletions mithril-aggregator/src/file_uploaders/gcp_uploader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use slog::{info, Logger};
use std::{env, path::Path};
use tokio_util::codec::{BytesCodec, FramedRead};

use mithril_common::{logging::LoggerExtensions, StdResult};
use mithril_common::{entities::FileUri, logging::LoggerExtensions, StdResult};

use crate::{file_uploaders::FileUri, FileUploader};
use crate::FileUploader;

/// GcpUploader represents a Google Cloud Platform file uploader interactor
pub struct GcpUploader {
Expand Down
12 changes: 1 addition & 11 deletions mithril-aggregator/src/file_uploaders/interface.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
use async_trait::async_trait;
use mithril_common::StdResult;
use mithril_common::{entities::FileUri, StdResult};
use std::path::Path;

/// FileUri represents a file URI used to identify the file's location
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct FileUri(pub String);

impl From<FileUri> for String {
fn from(file_uri: FileUri) -> Self {
file_uri.0
}
}

/// FileUploader represents a file uploader interactor
#[cfg_attr(test, mockall::automock)]
#[async_trait]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use anyhow::Context;
use async_trait::async_trait;
use mithril_common::entities::FileUri;
use reqwest::Url;
use slog::{debug, Logger};
use std::path::{Path, PathBuf};

use mithril_common::logging::LoggerExtensions;
use mithril_common::StdResult;

use crate::file_uploaders::{url_sanitizer::sanitize_url_path, FileUploader, FileUri};
use crate::file_uploaders::{url_sanitizer::sanitize_url_path, FileUploader};
use crate::tools;

// It's only used by the legacy snapshot that uploads the entire Cardano database.
Expand Down Expand Up @@ -70,7 +71,7 @@ mod tests {
use std::path::{Path, PathBuf};
use tempfile::tempdir;

use crate::file_uploaders::{FileUploader, FileUri};
use crate::file_uploaders::FileUploader;
use crate::test_tools::TestLogger;

use super::*;
Expand Down
4 changes: 2 additions & 2 deletions mithril-aggregator/src/file_uploaders/local_uploader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use reqwest::Url;
use slog::{debug, Logger};
use std::path::{Path, PathBuf};

use mithril_common::logging::LoggerExtensions;
use mithril_common::StdResult;
use mithril_common::{entities::FileUri, logging::LoggerExtensions};

use crate::file_uploaders::{url_sanitizer::sanitize_url_path, FileUploader, FileUri};
use crate::file_uploaders::{url_sanitizer::sanitize_url_path, FileUploader};

/// LocalUploader is a file uploader working using local files
pub struct LocalUploader {
Expand Down
2 changes: 1 addition & 1 deletion mithril-aggregator/src/file_uploaders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod url_sanitizer;

pub use dumb_uploader::*;
pub use gcp_uploader::GcpUploader;
pub use interface::{FileUploader, FileUri /*FilesUploader, MultiFilesUri, TemplateUri*/};
pub use interface::FileUploader;
pub use local_snapshot_uploader::LocalSnapshotUploader;
pub use local_uploader::LocalUploader;

Expand Down
81 changes: 2 additions & 79 deletions mithril-common/src/entities/cardano_database.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use std::collections::HashSet;

use anyhow::anyhow;
use semver::Version;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
Expand All @@ -9,9 +6,10 @@ use strum::EnumDiscriminants;
use crate::{
entities::{CardanoDbBeacon, CompressionAlgorithm},
signable_builder::Artifact,
StdResult,
};

use super::MultiFilesUri;

/// Cardano database snapshot.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CardanoDatabaseSnapshot {
Expand Down Expand Up @@ -72,37 +70,6 @@ impl CardanoDatabaseSnapshot {
}
}

/// [TemplateUri] represents an URI pattern used to build a file's location
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize)]
pub struct TemplateUri(pub String);

/// [MultiFilesUri] represents a unique location uri for multiple files
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize)]
pub enum MultiFilesUri {
/// URI template representing several URI
Template(TemplateUri),
}

impl MultiFilesUri {
/// Extract a template from a list of URIs
pub fn extract_template_from_uris(
file_uris: Vec<String>,
extractor: impl Fn(&str) -> StdResult<Option<String>>,
) -> StdResult<Option<TemplateUri>> {
let mut templates = HashSet::new();
for file_uri in file_uris {
let template_uri = extractor(&file_uri)?;
template_uri.map(|template| templates.insert(template));
}

if templates.len() > 1 {
return Err(anyhow!("Multiple templates found in the file URIs"));
}

Ok(templates.into_iter().next().map(TemplateUri))
}
}

/// Locations of the immutable file digests.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "type")]
Expand Down Expand Up @@ -271,48 +238,4 @@ mod tests {
);
}
}

mod extract_template_from_uris {
use super::*;

#[test]
fn returns_template() {
let file_uris = vec![
"http://whatever/00001.tar.gz".to_string(),
"http://whatever/00002.tar.gz".to_string(),
];
fn extractor_returning_same_uri(_file_uri: &str) -> StdResult<Option<String>> {
Ok(Some(
"http://whatever/{immutable_file_number}.tar.gz".to_string(),
))
}

let template =
MultiFilesUri::extract_template_from_uris(file_uris, extractor_returning_same_uri)
.unwrap();

assert_eq!(
template,
Some(TemplateUri(
"http://whatever/{immutable_file_number}.tar.gz".to_string()
))
);
}

#[test]
fn returns_error_with_multiple_templates() {
let file_uris = vec![
"http://whatever/00001.tar.gz".to_string(),
"http://00002.tar.gz/whatever".to_string(),
];
fn extractor_returning_different_uri(file_uri: &str) -> StdResult<Option<String>> {
Ok(Some(file_uri.to_string()))
}

MultiFilesUri::extract_template_from_uris(file_uris, extractor_returning_different_uri)
.expect_err(
"Should return an error when multiple templates are found in the file URIs",
);
}
}
}
92 changes: 92 additions & 0 deletions mithril-common/src/entities/file_uri.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use std::collections::HashSet;

use anyhow::anyhow;
use serde::{Deserialize, Serialize};

use crate::StdResult;

/// FileUri represents a file URI used to identify the file's location
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct FileUri(pub String);

impl From<FileUri> for String {
fn from(file_uri: FileUri) -> Self {
file_uri.0
}
}

/// [TemplateUri] represents an URI pattern used to build a file's location
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize)]
pub struct TemplateUri(pub String);

/// [MultiFilesUri] represents a unique location uri for multiple files
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize)]
pub enum MultiFilesUri {
/// URI template representing several URI
Template(TemplateUri),
}

impl MultiFilesUri {
/// Extract a template from a list of URIs
pub fn extract_template_from_uris(
file_uris: Vec<String>,
extractor: impl Fn(&str) -> StdResult<Option<String>>,
) -> StdResult<Option<TemplateUri>> {
let mut templates = HashSet::new();
for file_uri in file_uris {
let template_uri = extractor(&file_uri)?;
template_uri.map(|template| templates.insert(template));
}

if templates.len() > 1 {
return Err(anyhow!("Multiple templates found in the file URIs"));
}

Ok(templates.into_iter().next().map(TemplateUri))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn returns_template() {
let file_uris = vec![
"http://whatever/00001.tar.gz".to_string(),
"http://whatever/00002.tar.gz".to_string(),
];
fn extractor_returning_same_uri(_file_uri: &str) -> StdResult<Option<String>> {
Ok(Some(
"http://whatever/{immutable_file_number}.tar.gz".to_string(),
))
}

let template =
MultiFilesUri::extract_template_from_uris(file_uris, extractor_returning_same_uri)
.unwrap();

assert_eq!(
template,
Some(TemplateUri(
"http://whatever/{immutable_file_number}.tar.gz".to_string()
))
);
}

#[test]
fn returns_error_with_multiple_templates() {
let file_uris = vec![
"http://whatever/00001.tar.gz".to_string(),
"http://00002.tar.gz/whatever".to_string(),
];
fn extractor_returning_different_uri(file_uri: &str) -> StdResult<Option<String>> {
Ok(Some(file_uri.to_string()))
}

MultiFilesUri::extract_template_from_uris(file_uris, extractor_returning_different_uri)
.expect_err(
"Should return an error when multiple templates are found in the file URIs",
);
}
}
4 changes: 3 additions & 1 deletion mithril-common/src/entities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod certificate_metadata;
mod certificate_pending;
mod compression_algorithm;
mod epoch;
mod file_uri;
mod http_server_error;
mod mithril_stake_distribution;
mod protocol_message;
Expand All @@ -35,7 +36,7 @@ pub use block_range::{BlockRange, BlockRangeLength, BlockRangesSequence};
pub use cardano_chain_point::{BlockHash, ChainPoint};
pub use cardano_database::{
AncillaryLocation, AncillaryLocationDiscriminants, ArtifactsLocations, CardanoDatabaseSnapshot,
DigestLocation, ImmutablesLocation, MultiFilesUri, TemplateUri,
DigestLocation, ImmutablesLocation,
};
pub use cardano_db_beacon::CardanoDbBeacon;
pub use cardano_network::CardanoNetwork;
Expand All @@ -48,6 +49,7 @@ pub use certificate_metadata::{CertificateMetadata, StakeDistributionParty};
pub use certificate_pending::CertificatePending;
pub use compression_algorithm::*;
pub use epoch::{Epoch, EpochError};
pub use file_uri::{FileUri, MultiFilesUri, TemplateUri};
pub use http_server_error::{ClientError, ServerError};
pub use mithril_stake_distribution::MithrilStakeDistribution;
pub use protocol_message::{ProtocolMessage, ProtocolMessagePartKey, ProtocolMessagePartValue};
Expand Down

0 comments on commit fc43569

Please sign in to comment.