Skip to content

Commit

Permalink
[blob-service] Move database module to database/old
Browse files Browse the repository at this point in the history
Summary:
Restructure modules a bit to make it easier to add new database interface.

Moved `database` mod into `database/old` and extracted `database/error` module
because it will be used in the new model too.

Test Plan: cargo build

Reviewers: michal, jon, varun, patryk

Reviewed By: varun

Subscribers: ashoat, tomek

Differential Revision: https://phab.comm.dev/D8440
  • Loading branch information
barthap committed Jul 21, 2023
1 parent 9ab2f6e commit 168a79a
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 45 deletions.
39 changes: 39 additions & 0 deletions services/blob/src/database/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::fmt::{Display, Formatter};

use aws_sdk_dynamodb::Error as DynamoDBError;
use comm_services_lib::database::DBItemError;

use crate::s3::S3PathError;

#[derive(
Debug, derive_more::Display, derive_more::From, derive_more::Error,
)]
pub enum Error {
#[display(...)]
AwsSdk(DynamoDBError),
#[display(...)]
Attribute(DBItemError),
#[display(...)]
Blob(BlobDBError),
#[display(...)]
ItemAlreadyExists,
}

#[derive(Debug)]
pub enum BlobDBError {
HolderAlreadyExists(String),
InvalidS3Path(S3PathError),
}

impl Display for BlobDBError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
match self {
BlobDBError::HolderAlreadyExists(holder) => {
write!(f, "Item for given holder [{}] already exists", holder)
}
BlobDBError::InvalidS3Path(err) => err.fmt(f),
}
}
}

impl std::error::Error for BlobDBError {}
2 changes: 2 additions & 0 deletions services/blob/src/database/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod errors;
pub mod old;
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use aws_sdk_dynamodb::{
operation::get_item::GetItemOutput, types::AttributeValue,
Error as DynamoDBError,
};
use chrono::{DateTime, Utc};
use comm_services_lib::database::{self, DBItemError};
use std::{
collections::HashMap,
fmt::{Display, Formatter},
sync::Arc,
};
use comm_services_lib::database;
use std::{collections::HashMap, sync::Arc};
use tracing::error;

use crate::{
Expand All @@ -19,9 +14,11 @@ use crate::{
BLOB_S3_BUCKET_NAME, BLOB_TABLE_BLOB_HASH_FIELD, BLOB_TABLE_CREATED_FIELD,
BLOB_TABLE_NAME, BLOB_TABLE_S3_PATH_FIELD,
},
s3::{S3Path, S3PathError},
s3::S3Path,
};

use super::errors::{BlobDBError, Error};

#[derive(Clone, Debug)]
pub struct BlobItem {
pub blob_hash: String,
Expand Down Expand Up @@ -306,34 +303,3 @@ impl DatabaseClient {
Ok(())
}
}

#[derive(
Debug, derive_more::Display, derive_more::From, derive_more::Error,
)]
pub enum Error {
#[display(...)]
AwsSdk(DynamoDBError),
#[display(...)]
Attribute(DBItemError),
#[display(...)]
Blob(BlobDBError),
}

#[derive(Debug)]
pub enum BlobDBError {
HolderAlreadyExists(String),
InvalidS3Path(S3PathError),
}

impl Display for BlobDBError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
match self {
BlobDBError::HolderAlreadyExists(holder) => {
write!(f, "Item for given holder [{}] already exists", holder)
}
BlobDBError::InvalidS3Path(err) => err.fmt(f),
}
}
}

impl std::error::Error for BlobDBError {}
3 changes: 2 additions & 1 deletion services/blob/src/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use crate::{
GRPC_CHUNK_SIZE_LIMIT, GRPC_METADATA_SIZE_PER_MESSAGE,
MPSC_CHANNEL_BUFFER_CAPACITY, S3_MULTIPART_UPLOAD_MINIMUM_CHUNK_SIZE,
},
database::{BlobItem, DatabaseClient, Error as DBError, ReverseIndexItem},
database::errors::Error as DBError,
database::old::{BlobItem, DatabaseClient, ReverseIndexItem},
s3::{MultiPartUploadSession, S3Client, S3Path},
tools::MemOps,
};
Expand Down
4 changes: 2 additions & 2 deletions services/blob/src/http/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::database::ReverseIndexItem;
use crate::database::{BlobItem, DatabaseClient, Error as DBError};
use crate::database::errors::Error as DBError;
use crate::database::old::{BlobItem, DatabaseClient, ReverseIndexItem};
use crate::s3::{Error as S3Error, S3Client, S3Path};
use actix_web::error::{
ErrorBadRequest, ErrorInternalServerError, ErrorNotFound,
Expand Down
2 changes: 1 addition & 1 deletion services/blob/src/http/handlers/blob.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::constants::{
BLOB_DOWNLOAD_CHUNK_SIZE, S3_MULTIPART_UPLOAD_MINIMUM_CHUNK_SIZE,
};
use crate::database::{BlobItem, ReverseIndexItem};
use crate::database::old::{BlobItem, ReverseIndexItem};
use crate::http::context::handle_s3_error;
use crate::tools::MemOps;
use crate::validate_identifier;
Expand Down
2 changes: 1 addition & 1 deletion services/blob/src/http/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::config::CONFIG;
use crate::database::DatabaseClient;
use crate::database::old::DatabaseClient;
use crate::s3::S3Client;

use actix_cors::Cors;
Expand Down
2 changes: 1 addition & 1 deletion services/blob/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn main() -> Result<()> {
config::parse_cmdline_args()?;

let aws_config = config::load_aws_config().await;
let db = database::DatabaseClient::new(&aws_config);
let db = database::old::DatabaseClient::new(&aws_config);
let s3 = s3::S3Client::new(&aws_config);

tokio::select! {
Expand Down

0 comments on commit 168a79a

Please sign in to comment.