Skip to content

Commit

Permalink
feat: added put methods (#121)
Browse files Browse the repository at this point in the history
* feat: added put methods

* fix: removed an unused variable

* fix: removed an unused module
  • Loading branch information
hitohata authored Nov 19, 2024
1 parent 4f90fee commit 2fd9815
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 26 deletions.
1 change: 1 addition & 0 deletions crates/aws_clients/Cargo.lock

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

1 change: 1 addition & 0 deletions crates/aws_clients/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ aws-config = { version = "1", features = ["behavior-version-latest"] }
time_file_name = { path = "../time_file_name" }
shared = { path = "../shared" }
aws-sdk-dynamodb = "1.54.0"
log = "0.4.22"
31 changes: 31 additions & 0 deletions crates/aws_clients/src/dynamodb/entities/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//! Entities
/// Collection
/// <https://hitohata.github.io/ExogenesisEnsemble-Part3-Redemption/project/docs/technical-information/DynamoDB-Definition/#collection>
pub mod collection {
use time_file_name::file_datetime::PathDateTime;

pub struct CollectionItem {
pub year: String,
pub unix_time: i64,
pub is_unzipped: bool,
pub vault: String,
/// This is a S3 bucket name
pub key_name: String,
}

impl CollectionItem {
/// create a new item
pub fn new_object(key_name: &str, vault: &str) -> Result<Self, String> {
let path_date_time = PathDateTime::parse(key_name)?;

Ok(CollectionItem {
year: path_date_time.year.to_string(),
unix_time: path_date_time.unix_time,
is_unzipped: false,
vault: vault.to_string(),
key_name: key_name.to_string(),
})
}
}
}
99 changes: 96 additions & 3 deletions crates/aws_clients/src/dynamodb/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
pub mod entities;
pub mod client {
use crate::dynamodb::entities::collection::CollectionItem;
use crate::environment_values::clients::dynamodb_client;
use crate::environment_values::lambda_environment_values::table_name;
use aws_sdk_dynamodb::types::AttributeValue;
use shared::traits::GetFileListTrait;
use std::future::Future;
use std::time::{SystemTime, UNIX_EPOCH};

pub struct DynamoDbClient {}

pub trait DynamoClientTrait: GetFileListTrait {}
pub trait DynamoClientTrait: GetFileListTrait {
/// put a new collection item
fn put_collection_item(
collection: &CollectionItem,
) -> impl Future<Output = Result<bool, String>> + Send;
// put zipping time
/// time is mill sec
fn put_unzipping_item(
key_name: &str,
time: Option<u128>,
) -> impl Future<Output = Result<bool, String>> + Send;
/// put unzipped item
/// time is mill sec
fn put_unzipped_item(
key_name: &str,
time: Option<u128>,
) -> impl Future<Output = Result<bool, String>> + Send;
}

impl GetFileListTrait for DynamoDbClient {
async fn get_years() -> Result<Vec<String>, String> {
Expand All @@ -27,9 +51,78 @@ pub mod client {
}
}

impl DynamoClientTrait for DynamoDbClient {}
impl DynamoClientTrait for DynamoDbClient {
async fn put_collection_item(collection: &CollectionItem) -> Result<bool, String> {
let request = dynamodb_client()
.await
.put_item()
.table_name(table_name())
.item("PK", AttributeValue::S(collection.year.to_string()))
.item("SK", AttributeValue::N(collection.unix_time.to_string()))
.item("IsUnzipped", AttributeValue::Bool(collection.is_unzipped))
.item("Vault", AttributeValue::S(collection.vault.to_string()))
.item(
"KeyName",
AttributeValue::S(collection.key_name.to_string()),
);

if let Err(e) = request.send().await {
return Err(e.to_string());
}

Ok(true)
}

async fn put_unzipping_item(key_name: &str, time: Option<u128>) -> Result<bool, String> {
let now = get_now(time)?;

let request = dynamodb_client()
.await
.put_item()
.table_name(table_name())
.item("PK", AttributeValue::S("Unzipping".to_string()))
.item("SK", AttributeValue::N(now.to_string()))
.item("KeyName", AttributeValue::S(key_name.to_string()));

if let Err(e) = request.send().await {
return Err(e.to_string());
}

Ok(true)
}

async fn put_unzipped_item(key_name: &str, time: Option<u128>) -> Result<bool, String> {
let now = get_now(time)?;

let request = dynamodb_client()
.await
.put_item()
.table_name(table_name())
.item("PK", AttributeValue::S("Unzipped".to_string()))
.item("SK", AttributeValue::N(now.to_string()))
.item("KeyName", AttributeValue::S(key_name.to_string()));

if let Err(e) = request.send().await {
return Err(e.to_string());
}

Ok(true)
}
}

/// this is a helper function.
/// if there is argument, this function returns it.
/// If not, this function gets system time.
fn get_now(time: Option<u128>) -> Result<u128, String> {
match time {
Some(now) => Ok(now),
None => match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(now) => Ok(now.as_millis()),
Err(_) => Err("Failed to get current time".to_string()),
},
}
}
}

#[cfg(test)]
mod test_util;

9 changes: 4 additions & 5 deletions crates/aws_clients/src/dynamodb/test_util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use crate::environment_values::clients::test_dynamo_client;
use aws_sdk_dynamodb::types::{
AttributeDefinition, KeySchemaElement, KeyType, ProvisionedThroughput, ScalarAttributeType,
Expand Down Expand Up @@ -96,12 +95,12 @@ mod test {
use super::*;
use crate::dynamodb::test_util::create_table;
use tokio;

#[tokio::test]
async fn test_table_existence() {
// Act
let result = table_existence("non-existing-table").await;

// Assert
assert_eq!(result, false);
}
Expand All @@ -111,10 +110,10 @@ mod test {
// Arrange
let table_name = "new-table";
create_table(table_name).await;

// Act
let result = table_existence(table_name).await;

// Assert
assert!(result);
}
Expand Down
1 change: 0 additions & 1 deletion crates/aws_clients/src/environment_values/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub mod lambda_environment_values {
}

/// DynamoDB table name
#[allow(dead_code)] // TODO: Delete
pub fn table_name() -> &'static str {
static TABLE_NAME: OnceLock<String> = OnceLock::new();
TABLE_NAME.get_or_init(|| var("TABLE_NAME").unwrap())
Expand Down
34 changes: 17 additions & 17 deletions docs/project/docs/technical-information/DynamoDB-Definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ sidebar_position: 2

### Manage Files

| Key | Detail | Note |
|:-----------|:-----------|:---------------------------------------------------------|
| PK | datetime | The key is `{year}-{month}-{day}-{hour}-{min}-{sec}`[^1] |
| SK | Epoch time | |
| IsUnzipped | boolean | If the file is unzipped or not |
| Vault | String | Glacier vault |
| KeyName | String | S3 prefix |
| Key | Detail | Note |
|:-----------|:-----------|:-------------------------------|
| PK | datetime | The key is `{year}` |
| SK | Epoch time | |
| IsUnzipped | boolean | If the file is unzipped or not |
| Vault | String | Glacier vault |
| KeyName | String | S3 prefix |

### Date Lookup

Expand All @@ -45,22 +45,22 @@ If the `{year}-{month}` has days in the month of the year.

This is for the unzipped files

| Key | Detail | Note |
|:------------|:-------------------|:---------------------------------------------------|
| PK | Unzipped | Fixed string "Unzipped" |
| CreatedDate | Epoch time | The datetime that the file is retrieved or created |
| KeyName | name of object key | The object key name |
| Key | Detail | Note |
|:--------|:-------------------|:---------------------------------------------------|
| PK | Unzipped | Fixed string "Unzipped" |
| SK | Epoch time | The datetime that the file is retrieved or created |
| KeyName | name of object key | The object key name |


### Manage Unzipping Files

This is for the unzipping files

| Key | Detail | Note |
|:------------|:-------------------|:------------------------------------------------|
| PK | Unzipped | Fixed string "Unzipping" |
| CreatedDate | Epoch time | The datetime that the retrieval request is made |
| KeyName | name of object key | The object key name |
| Key | Detail | Note |
|:--------|:-------------------|:------------------------------------------------|
| PK | Unzipped | Fixed string "Unzipping" |
| SK | Epoch time | The datetime that the retrieval request is made |
| KeyName | name of object key | The object key name |


## Access Pattern
Expand Down

0 comments on commit 2fd9815

Please sign in to comment.