-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2204 from input-output-hk/jpraynaud/2174-digests-…
…route-cardano-database-aggregator Feat: immutable file digests route for `CardanoDatabase` artifacts aggregator
- Loading branch information
Showing
29 changed files
with
1,234 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
mithril-aggregator/src/database/query/immutable_file_digest/delete_immutable_file_digest.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use mithril_persistence::sqlite::{Query, WhereCondition}; | ||
|
||
use crate::database::record::ImmutableFileDigestRecord; | ||
|
||
/// Query to delete [ImmutableFileDigestRecord] from the sqlite database | ||
pub struct DeleteImmutableFileDigestQuery { | ||
condition: WhereCondition, | ||
} | ||
|
||
impl DeleteImmutableFileDigestQuery { | ||
pub fn all() -> Self { | ||
Self { | ||
condition: WhereCondition::default(), | ||
} | ||
} | ||
} | ||
|
||
impl Query for DeleteImmutableFileDigestQuery { | ||
type Entity = ImmutableFileDigestRecord; | ||
|
||
fn filters(&self) -> WhereCondition { | ||
self.condition.clone() | ||
} | ||
|
||
fn get_definition(&self, condition: &str) -> String { | ||
let projection = Self::Entity::expand_projection("immutable_file_digest"); | ||
|
||
format!("delete from immutable_file_digest where {condition} returning {projection}") | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
mithril-aggregator/src/database/query/immutable_file_digest/get_immutable_file_digest.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use sqlite::Value; | ||
|
||
use mithril_common::{entities::ImmutableFileName, StdResult}; | ||
use mithril_persistence::sqlite::{Query, WhereCondition}; | ||
|
||
use crate::database::record::ImmutableFileDigestRecord; | ||
|
||
/// Simple queries to retrieve [ImmutableFileDigestRecord] from the sqlite database. | ||
pub struct GetImmutableFileDigestQuery { | ||
condition: WhereCondition, | ||
} | ||
|
||
impl GetImmutableFileDigestQuery { | ||
pub fn by_immutable_file_name(immutable_file_name: &ImmutableFileName) -> StdResult<Self> { | ||
let condition = WhereCondition::new( | ||
"immutable_file_name = ?*", | ||
vec![Value::String(immutable_file_name.to_string())], | ||
); | ||
|
||
Ok(Self { condition }) | ||
} | ||
|
||
pub fn all() -> Self { | ||
Self { | ||
condition: WhereCondition::default(), | ||
} | ||
} | ||
} | ||
|
||
impl Query for GetImmutableFileDigestQuery { | ||
type Entity = ImmutableFileDigestRecord; | ||
|
||
fn filters(&self) -> WhereCondition { | ||
self.condition.clone() | ||
} | ||
|
||
fn get_definition(&self, condition: &str) -> String { | ||
let projection = Self::Entity::expand_projection("immutable_file_digest"); | ||
|
||
format!( | ||
"select {projection} from immutable_file_digest where {condition} order by rowid desc" | ||
) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
mithril-aggregator/src/database/query/immutable_file_digest/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
mod delete_immutable_file_digest; | ||
mod get_immutable_file_digest; | ||
mod upsert_immutable_file_digest; | ||
|
||
pub use delete_immutable_file_digest::*; | ||
pub use get_immutable_file_digest::*; | ||
pub use upsert_immutable_file_digest::*; |
46 changes: 46 additions & 0 deletions
46
mithril-aggregator/src/database/query/immutable_file_digest/upsert_immutable_file_digest.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use sqlite::Value; | ||
|
||
use mithril_common::entities::ImmutableFileName; | ||
use mithril_common::StdResult; | ||
use mithril_persistence::sqlite::{Query, WhereCondition}; | ||
|
||
use crate::database::record::ImmutableFileDigestRecord; | ||
|
||
/// Query to upsert [ImmutableFileDigestRecord] in the sqlite database | ||
pub struct UpsertImmutableFileDigestQuery { | ||
condition: WhereCondition, | ||
} | ||
|
||
impl UpsertImmutableFileDigestQuery { | ||
pub fn one(immutable_file_name: &ImmutableFileName, digest: &str) -> StdResult<Self> { | ||
let expression = "(immutable_file_name, digest) values (?*, ?*)"; | ||
let parameters = vec![ | ||
Value::String(immutable_file_name.to_string()), | ||
Value::String(digest.to_string()), | ||
]; | ||
|
||
Ok(Self { | ||
condition: WhereCondition::new(expression, parameters), | ||
}) | ||
} | ||
} | ||
|
||
impl Query for UpsertImmutableFileDigestQuery { | ||
type Entity = ImmutableFileDigestRecord; | ||
|
||
fn filters(&self) -> WhereCondition { | ||
self.condition.clone() | ||
} | ||
|
||
fn get_definition(&self, condition: &str) -> String { | ||
let projection = Self::Entity::expand_projection("immutable_file_digest"); | ||
|
||
format!( | ||
r#" | ||
insert into immutable_file_digest {condition} | ||
on conflict (immutable_file_name) do update set digest = excluded.digest | ||
returning {projection} | ||
"# | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
mithril-aggregator/src/database/record/immutable_file_digest.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use sqlite::Row; | ||
|
||
use mithril_common::entities::{HexEncodedDigest, ImmutableFileName}; | ||
use mithril_persistence::sqlite::{HydrationError, Projection, SourceAlias, SqLiteEntity}; | ||
|
||
/// ImmutableFileDigestRecord is the record that stores the digest of an immutable file. | ||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] | ||
pub struct ImmutableFileDigestRecord { | ||
/// Immutable file name | ||
pub immutable_file_name: ImmutableFileName, | ||
|
||
/// Digest of an immutable file | ||
pub digest: HexEncodedDigest, | ||
} | ||
|
||
impl ImmutableFileDigestRecord { | ||
/// Construct a [Projection] that will allow to hydrate this `CertificatePendingRecord` and expend table alias. | ||
pub fn expand_projection(table: &str) -> String { | ||
let aliases = SourceAlias::new(&[("{:immutable_file_digest:}", table)]); | ||
Self::get_projection().expand(aliases) | ||
} | ||
|
||
#[cfg(test)] | ||
/// Create a dumb ImmutableFileDigestRecord instance mainly for test purposes | ||
pub fn dummy() -> Self { | ||
Self { | ||
immutable_file_name: "123.chunk".to_string(), | ||
digest: "dummy_digest".to_string(), | ||
} | ||
} | ||
} | ||
|
||
impl SqLiteEntity for ImmutableFileDigestRecord { | ||
fn hydrate(row: Row) -> Result<Self, HydrationError> | ||
where | ||
Self: Sized, | ||
{ | ||
let immutable_file_name = row.read::<&str, _>(0).to_string(); | ||
let digest = row.read::<&str, _>(1).to_string(); | ||
|
||
Ok(Self { | ||
immutable_file_name, | ||
digest, | ||
}) | ||
} | ||
|
||
fn get_projection() -> Projection { | ||
Projection::from(&[ | ||
( | ||
"immutable_file_name", | ||
"{:immutable_file_digest:}.immutable_file_name", | ||
"text", | ||
), | ||
("digest", "{:immutable_file_digest:}.digest", "text"), | ||
]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.