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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
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::operation::update_item::UpdateItemOutput;
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::ReturnValue;
use aws_sdk_dynamodb::types::ScalarAttributeType;
use color_eyre::eyre::eyre;
use color_eyre::eyre::Result;
use core::marker::PhantomData;
#[derive(Debug)]
pub struct StorageDynamoDb<ITEM: StorageItem> {
table_name: String,
endpoint_url: Option<String>,
item_type: PhantomData<ITEM>,
}
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,
}
}
pub fn set_endpoint_url(&mut self, url: &str) -> Result<()> {
self.endpoint_url = Some(String::from(url));
Ok(())
}
async fn client(&self) -> Result<aws_sdk_dynamodb::Client> {
// 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);
Ok(client)
}
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);
*/
let client = self.client().await?;
match client
.describe_table()
.table_name(&self.table_name)
.send()
.await
{
Ok(_o) => {
// :TODO: verify table format?
// tracing::info!("Table {} exists -> {o:#?}", &self.table_name);
tracing::info!("Table {} exists", &self.table_name);
}
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 key_id = KeySchemaElement::builder()
.attribute_name("id")
.key_type(KeyType::Hash)
.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);
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 ensure_storage_exists(&mut self) -> Result<()> {
Ok(())
}
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> {
Ok(false) // :TODO:
}
async fn load(&self, _id: &str) -> Result<ITEM> {
todo!();
}
async fn save(&self, id: &str, item: &ITEM, lock: &StorageLock) -> Result<()> {
tracing::info!("Saving: {id} -> {item:?} with lock {lock:?}");
let client = self.client().await?;
let data = item.serialize()?;
let data = String::from_utf8_lossy(&data);
match client
.update_item()
.table_name(&self.table_name)
.key("id", AttributeValue::S(String::from(id)))
//.expression_attribute_names()
//.update_expression("SET #Count = if_not_exists(#Count, :zero) + :one, Images = list_append(if_not_exists(Images, :empty), :image)")
.update_expression("SET #Data = :data")
.expression_attribute_names("#Data", "data")
.expression_attribute_values(
":data",
aws_sdk_dynamodb::types::AttributeValue::S(data.to_string()),
)
.return_values(ReturnValue::AllOld)
.send()
.await
{
Ok(o) => {
tracing::info!("Save - UpdateItem {id} success {o:?}");
Ok(())
}
Err(e) => {
tracing::warn!("Save - UpdateItem {id} failure {e:?}");
todo!();
}
}
}
async fn lock(&self, id: &str, who: &str) -> Result<LockResult<ITEM>> {
let lock = StorageLock::new(who);
let lock_json = serde_json::to_string_pretty(&lock)?;
// write lock
let client = self.client().await?;
match client
.update_item()
.table_name(&self.table_name)
.key("id", AttributeValue::S(String::from(id)))
//.expression_attribute_names()
//.update_expression("SET #Count = if_not_exists(#Count, :zero) + :one, Images = list_append(if_not_exists(Images, :empty), :image)")
.update_expression("SET #Lock = :lock")
.expression_attribute_names("#Lock", "lock")
.expression_attribute_values(
":lock",
aws_sdk_dynamodb::types::AttributeValue::S(lock_json),
)
.return_values(ReturnValue::AllOld)
.send()
.await
{
Ok(o) => {
tracing::info!("Lock - UpdateItem {id} success {o:?}");
let item = match o {
UpdateItemOutput { ref attributes, .. } => {
if let Some(attributes) = &attributes {
if let Some(data) = attributes.get("data") {
match data {
AttributeValue::S(data) => {
let item = ITEM::deserialize(data.as_bytes())?;
tracing::info!("Lock - Got item {item:?}");
item
}
o => {
tracing::warn!(
"No data attribute for item is not a string {o:?}"
);
ITEM::default()
}
}
} else {
tracing::warn!("No data attribute for item");
ITEM::default()
}
} else {
tracing::warn!("No attributes for item");
ITEM::default()
}
}
};
//let item = ITEM::default();
Ok(LockResult::Success { lock, item })
}
Err(e) => {
tracing::warn!("Lock - UpdateItem {id} failure {e:?}");
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!();
}
}
#[cfg(test)]
mod tests {
use crate::Storage;
use crate::StorageDynamoDb;
use crate::StorageItem;
use color_eyre::Result;
use serde::Deserialize;
use serde::Serialize;
#[derive(Default, Debug, Serialize, Deserialize)]
struct TestItem {}
impl StorageItem for TestItem {
fn serialize(&self) -> Result<Vec<u8>> {
todo!()
}
fn deserialize(_: &[u8]) -> Result<Self> {
todo!()
}
}
#[tokio::test]
async fn it_debugs() -> Result<()> {
let table_name = "test_items";
let storage = StorageDynamoDb::<TestItem>::new(&table_name).await;
println!("{storage:?}");
let storage: Box<dyn Storage<TestItem>> = Box::new(storage);
println!("{storage:?}");
Ok(())
}
}