1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use crate::LockResult;
use crate::Storage;
use crate::StorageItem;
use crate::StorageLock;
use async_trait::async_trait;
use aws_sdk_dynamodb::error::SdkError;
use aws_sdk_dynamodb::operation::describe_table::DescribeTableError::ResourceNotFoundException;
use aws_sdk_dynamodb::types::AttributeDefinition;
use aws_sdk_dynamodb::types::AttributeValue;
use aws_sdk_dynamodb::types::KeySchemaElement;
use aws_sdk_dynamodb::types::KeyType;
use aws_sdk_dynamodb::types::ProvisionedThroughput;
use aws_sdk_dynamodb::types::ScalarAttributeType;
use color_eyre::eyre::eyre;
use color_eyre::eyre::Result;
use tokio::sync::Semaphore;
use core::marker::PhantomData;
#[derive(Debug)]
pub struct StorageDynamoDb<ITEM: StorageItem> {
table_name: String,
endpoint_url: Option<String>,
item_type: PhantomData<ITEM>,
lock_semaphore: Semaphore,
}
impl<ITEM: StorageItem> StorageDynamoDb<ITEM> {
pub async fn new(table_name: &str) -> Self {
Self {
table_name: String::from(table_name),
endpoint_url: None,
item_type: PhantomData,
lock_semaphore: Semaphore::new(1),
}
}
pub fn set_endpoint_url(&mut self, url: &str) -> Result<()> {
self.endpoint_url = Some(String::from(url));
Ok(())
}
pub async fn ensure_table_exists(&mut self) -> Result<()> {
// let config = aws_config::load_from_env().await;
let config = aws_config::defaults(aws_config::BehaviorVersion::latest());
let config = if let Some(endpoint_url) = &self.endpoint_url {
config.endpoint_url(endpoint_url)
} else {
config
};
let config = config.load().await;
let client = aws_sdk_dynamodb::Client::new(&config);
match client
.describe_table()
.table_name(&self.table_name)
.send()
.await
{
Ok(_o) => {
// :TODO: verify table format?
}
Err(e) => {
// tracing::debug!("Err {e:?}");
match e {
SdkError::ServiceError(se) => {
match se.err() {
ResourceNotFoundException(nf) => {
// tracing::debug!("{nf:?}");
tracing::info!("Table {} not found. Creating...", &self.table_name);
// :TODO:
let ad_id = AttributeDefinition::builder()
.attribute_name("id")
.attribute_type(ScalarAttributeType::S)
.build()?;
let ad_lock = AttributeDefinition::builder()
.attribute_name("lock")
.attribute_type(ScalarAttributeType::S)
.build()?;
let ad_data = AttributeDefinition::builder()
.attribute_name("data")
.attribute_type(ScalarAttributeType::S)
.build()?;
let key_id = KeySchemaElement::builder()
.attribute_name("id")
.key_type(KeyType::Hash)
.build()?;
let key_lock = KeySchemaElement::builder()
.attribute_name("lock")
.key_type(KeyType::Range)
.build()?;
let key_data = KeySchemaElement::builder()
.attribute_name("data")
.key_type(KeyType::Range)
.build()?;
let pt = ProvisionedThroughput::builder()
.read_capacity_units(1)
.write_capacity_units(1)
.build()?;
let r = client
.create_table()
.table_name(&self.table_name)
.attribute_definitions(ad_id)
//.attribute_definitions(ad_lock)
//.attribute_definitions(ad_data)
.key_schema(key_id)
//.key_schema(key_lock)
//.key_schema(key_data)
.provisioned_throughput(pt);
// add schema
// id | lock | data
// string | string | string
/*
let ad = AttributeDefinition::builder()
.attribute_name(&a_name)
.attribute_type(ScalarAttributeType::S)
.build()
.map_err(Error::BuildError)?;
let ks = KeySchemaElement::builder()
.attribute_name(&a_name)
.key_type(KeyType::Hash)
.build()
.map_err(Error::BuildError)?;
*/
r.send().await?;
}
oe => return Err(eyre!("Error describing table {oe:?}")),
}
}
_o => {
todo!();
}
}
}
};
// tracing::debug!("{client:?}");
// insert test data
let request = client
.put_item()
.table_name(&self.table_name)
.item("id", AttributeValue::S(nanoid::nanoid!()))
.item("lock", AttributeValue::S(String::from("")))
.item("data", AttributeValue::S(String::from("{}")))
.send()
.await?;
Ok(())
}
}
#[async_trait]
impl<ITEM: StorageItem + std::marker::Send> Storage<ITEM> for StorageDynamoDb<ITEM> {
async fn create(&self) -> Result<String> {
let mut tries = 10;
loop {
let id = nanoid::nanoid!();
if !self.exists(&id).await? {
return Ok(id);
}
tries -= 1;
if tries <= 0 {
todo!();
}
}
}
async fn exists(&self, id: &str) -> Result<bool> {
todo!();
}
async fn load(&self, id: &str) -> Result<ITEM> {
todo!();
}
async fn save(&self, id: &str, item: &ITEM, lock: &StorageLock) -> Result<()> {
todo!();
}
async fn lock(&self, id: &str, who: &str) -> Result<LockResult<ITEM>> {
todo!();
}
async fn unlock(&self, id: &str, lock: StorageLock) -> Result<()> {
todo!();
}
async fn force_unlock(&self, id: &str) -> Result<()> {
todo!();
}
async fn verify_lock(&self, id: &str, lock: &StorageLock) -> Result<bool> {
todo!();
}
}