bytecon_data_store 0.1.0

A library for storing ByteConverter implementations conveniently.
Documentation
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
use std::{error::Error, sync::Arc};
use bytecon::ByteConverter;
use cloneless_cow::ClonelessCow;
use bytecon_tls::{ByteConClient, ByteConPrivateKey, ByteConPublicKey, ByteConServer, MessageProcessor};
use tokio::sync::Mutex;
use crate::DataStore;

pub struct RemoteDataStoreClient {
    client: ByteConClient<ServerRequest<'static>, ServerResponse>,
}

impl RemoteDataStoreClient {
    pub fn new(
        server_public_key: ByteConPublicKey,
        server_domain: String,
        server_address: String,
        server_port: u16,
    ) -> Self {
        Self {
            client: ByteConClient::new(
                server_address,
                server_port,
                server_public_key,
                server_domain,
            ),
        }
    }
    async fn send_request<'a>(&self, server_request: &ServerRequest<'a>) -> Result<ServerResponse, Box<dyn Error>> {
        return Ok(self.client.send_message(server_request).await?);
    }
}

impl DataStore for RemoteDataStoreClient {
    type Item = Vec<u8>;
    type Key = i64;

    async fn initialize(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        let health_check_request = ServerRequest::HealthCheck;
        let health_check_response = self.send_request(&health_check_request)
            .await
            .map_err(|error| {
                format!("Error trying to send request: {:?}", error)
            })?;
        match health_check_response {
            ServerResponse::HealthCheck => {
                Ok(())
            },
            _ => {
                Err(RemoteDataStoreError::UnexpectedResponseForRequest {
                    request: String::from(format!("{:?}", health_check_request)),
                    response: String::from(format!("{:?}", health_check_response)),
                }.into())
            }
        }
    }
    async fn insert(&mut self, item: Self::Item) -> Result<Self::Key, Box<dyn std::error::Error>> {
        let server_request = ServerRequest::SendBytes {
            bytes: item,
        };
        let server_response = self.send_request(&server_request)
            .await?;
        if let ServerResponse::SentBytes { id } = server_response {
            Ok(id)
        }
        else {
            Err(RemoteDataStoreError::UnexpectedResponseForRequest {
                response: String::from(format!("{:?}", server_response)),
                request: String::from(format!("{:?}", server_request)),
            }.into())
        }
    }
    async fn get(&self, id: &Self::Key) -> Result<Self::Item, Box<dyn std::error::Error>> {
        let server_request = ServerRequest::GetBytes {
            id: *id,
        };
        let server_response = self.send_request(&server_request)
            .await?;
        if let ServerResponse::ReceivedBytes { bytes } = server_response {
            Ok(bytes)
        }
        else {
            Err(RemoteDataStoreError::UnexpectedResponseForRequest {
                response: String::from(format!("{:?}", server_response)),
                request: String::from(format!("{:?}", server_request)),
            }.into())
        }
    }
    async fn delete(&self, id: &Self::Key) -> Result<(), Box<dyn std::error::Error>> {
        
        // copy key into owned object
        let id = *id;

        let server_request = ServerRequest::Delete {
            id: id,
        };
        let server_response = self.send_request(&server_request)
            .await?;
        if let ServerResponse::Deleted { id: response_file_record_id } = server_response {
            if id != response_file_record_id {
                Err(RemoteDataStoreError::DeleteMismatch {
                    request_file_record_id: id,
                    response_file_record_id,
                }.into())
            }
            else {
                Ok(())
            }
        }
        else {
            Err(RemoteDataStoreError::UnexpectedResponseForRequest {
                response: String::from(format!("{:?}", server_response)),
                request: String::from(format!("{:?}", server_request)),
            }.into())
        }
    }
    async fn list(&self, page_index: u64, page_size: u64, row_offset: u64) -> Result<Vec<Self::Key>, Box<dyn std::error::Error>> {
        let server_request = ServerRequest::ListIds {
            page_index,
            page_size,
            row_offset,
        };
        let server_response = self.send_request(&server_request)
            .await?;
        if let ServerResponse::ReceivedIdList { ids } = server_response {
            Ok(ids)
        }
        else {
            Err(RemoteDataStoreError::UnexpectedResponseForRequest {
                response: String::from(format!("{:?}", server_response)),
                request: String::from(format!("{:?}", server_request)),
            }.into())
        }
    }
    async fn bulk_insert(&mut self, items: Vec<Self::Item>) -> Result<Vec<Self::Key>, Box<dyn Error>> {
        let server_request = ServerRequest::BulkSendBytes {
            bytes_collection: items,
        };
        let server_response = self.send_request(&server_request)
            .await?;
        if let ServerResponse::BulkSentBytes { ids } = server_response {
            Ok(ids)
        }
        else {
            Err(RemoteDataStoreError::UnexpectedResponseForRequest {
                response: String::from(format!("{:?}", server_response)),
                request: String::from(format!("{:?}", server_request)),
            }.into())
        }
    }
    async fn bulk_get(&self, ids: &Vec<Self::Key>) -> Result<Vec<Self::Item>, Box<dyn Error>> {
        let server_request = ServerRequest::BulkGetBytes {
            ids: ClonelessCow::Borrowed(ids),
        };
        let server_response = self.send_request(&server_request)
            .await?;
        if let ServerResponse::BulkReceivedBytes { bytes_collection } = server_response {
            Ok(bytes_collection)
        }
        else {
            Err(RemoteDataStoreError::UnexpectedResponseForRequest {
                response: String::from(format!("{:?}", server_response)),
                request: String::from(format!("{:?}", server_request)),
            }.into())
        }
    }
}

impl ByteConverter for RemoteDataStoreClient {
    fn append_to_bytes(&self, bytes: &mut Vec<u8>) -> Result<(), Box<dyn Error>> {
        self.client.append_to_bytes(bytes)?;
        Ok(())
    }
    fn extract_from_bytes(bytes: &Vec<u8>, index: &mut usize) -> Result<Self, Box<dyn Error>> where Self: Sized {
        Ok(Self {
            client: ByteConClient::<ServerRequest, ServerResponse>::extract_from_bytes(bytes, index)?,
        })
    }
}

struct RemoteDataStoreMessageProcessor<TDataStore>
where
    TDataStore: DataStore<Item = Vec<u8>, Key = i64> + Send + Sync + 'static,
{
    data_store: Arc<Mutex<TDataStore>>
}

impl<TDataStore> MessageProcessor for RemoteDataStoreMessageProcessor<TDataStore>
where
    TDataStore: DataStore<Item = Vec<u8>, Key = i64> + Send + Sync + 'static,
{
    type TInput = ServerRequest<'static>;
    type TOutput = ServerResponse;

    async fn process_message(&self, message: &Self::TInput) -> Result<Self::TOutput, Box<dyn Error>> {
        let server_response = match message {
            ServerRequest::HealthCheck => {
                ServerResponse::HealthCheck
            },
            ServerRequest::SendBytes { bytes } => {
                let key = self.data_store
                    .lock()
                    .await
                    .insert(bytes.to_vec())
                    .await?;

                ServerResponse::SentBytes {
                    id: key,
                }
            },
            ServerRequest::GetBytes { id } => {
                let bytes = self.data_store
                    .lock()
                    .await
                    .get(&id)
                    .await?;

                ServerResponse::ReceivedBytes {
                    bytes,
                }
            },
            ServerRequest::Delete { id } => {
                self.data_store
                    .lock()
                    .await
                    .delete(&id)
                    .await?;

                ServerResponse::Deleted {
                    id: *id,
                }
            },
            ServerRequest::ListIds { page_index, page_size, row_offset } => {
                let ids = self.data_store
                    .lock()
                    .await
                    .list(*page_index, *page_size, *row_offset)
                    .await?;

                ServerResponse::ReceivedIdList {
                    ids,
                }
            },
            ServerRequest::BulkSendBytes { bytes_collection } => {
                let ids = self.data_store
                    .lock()
                    .await
                    .bulk_insert(bytes_collection.to_vec())
                    .await?;

                ServerResponse::BulkSentBytes {
                    ids,
                }
            },
            ServerRequest::BulkGetBytes { ids } => {
                let bytes_collection = self.data_store
                    .lock()
                    .await
                    .bulk_get(ids.as_ref())
                    .await?;

                ServerResponse::BulkReceivedBytes {
                    bytes_collection,
                }
            },
        };
        Ok(server_response)
    }
}

impl<TDataStore> ByteConverter for RemoteDataStoreMessageProcessor<TDataStore>
where
    TDataStore: DataStore<Item = Vec<u8>, Key = i64> + Send + Sync + 'static + ByteConverter,
{
    fn append_to_bytes(&self, bytes: &mut Vec<u8>) -> Result<(), Box<dyn Error>> {
        self.data_store
            .blocking_lock()
            .append_to_bytes(bytes)?;
        Ok(())
    }
    fn extract_from_bytes(bytes: &Vec<u8>, index: &mut usize) -> Result<Self, Box<dyn Error>> where Self: Sized {
        Ok(Self {
            data_store: Arc::new(Mutex::new(TDataStore::extract_from_bytes(bytes, index)?)),
        })
    }
}


pub struct RemoteDataStoreServer<TDataStore>
where
    TDataStore: DataStore<Item = Vec<u8>, Key = i64> + Send + Sync + 'static,
{
    server: ByteConServer<RemoteDataStoreMessageProcessor<TDataStore>>,
}

impl<TDataStore> RemoteDataStoreServer<TDataStore>
where
    TDataStore: DataStore<Item = Vec<u8>, Key = i64> + Send + Sync + 'static,
{
    pub fn new(
        data_store: Arc<Mutex<TDataStore>>,
        public_key: ByteConPublicKey,
        private_key: ByteConPrivateKey,
        bind_address: String,
        bind_port: u16,
    ) -> Self {
        Self {
            server: ByteConServer::<RemoteDataStoreMessageProcessor<TDataStore>>::new(
                bind_address,
                bind_port,
                public_key,
                private_key,
                Arc::new(RemoteDataStoreMessageProcessor {
                    data_store,
                }),
            )
        }
    }
    pub async fn start(&mut self) -> Result<(), Box<dyn Error>> {
        self.server.start()
            .await
    }
}

impl<TDataStore> ByteConverter for RemoteDataStoreServer<TDataStore>
where
    TDataStore: DataStore<Item = Vec<u8>, Key = i64> + Send + Sync + 'static + ByteConverter,
{
    fn append_to_bytes(&self, bytes: &mut Vec<u8>) -> Result<(), Box<dyn Error>> {
        self.server.append_to_bytes(bytes)?;
        Ok(())
    }
    fn extract_from_bytes(bytes: &Vec<u8>, index: &mut usize) -> Result<Self, Box<dyn Error>> where Self: Sized {
        Ok(Self {
            server: ByteConServer::<RemoteDataStoreMessageProcessor<TDataStore>>::extract_from_bytes(bytes, index)?,
        })
    }
}

#[derive(Debug)]
enum ServerRequest<'a> {
    HealthCheck,
    SendBytes {
        bytes: Vec<u8>,
    },
    GetBytes {
        id: i64,
    },
    Delete {
        id: i64,
    },
    ListIds {
        page_index: u64,
        page_size: u64,
        row_offset: u64,
    },
    BulkSendBytes {
        bytes_collection: Vec<Vec<u8>>,
    },
    BulkGetBytes {
        ids: ClonelessCow<'a, Vec<i64>>,
    },
}

impl ByteConverter for ServerRequest<'_> {
    fn append_to_bytes(&self, bytes: &mut Vec<u8>) -> Result<(), Box<dyn Error>> {
        match self {
            ServerRequest::HealthCheck => {
                // byte
                0u8.append_to_bytes(bytes)?;
            },
            ServerRequest::SendBytes { bytes: send_bytes } => {
                // byte
                1u8.append_to_bytes(bytes)?;
                // vec<u8>
                send_bytes.append_to_bytes(bytes)?;
            },
            ServerRequest::GetBytes { id } => {
                // byte
                2u8.append_to_bytes(bytes)?;
                // i64
                id.append_to_bytes(bytes)?;
            },
            ServerRequest::Delete { id } => {
                // byte
                3u8.append_to_bytes(bytes)?;
                // i64
                id.append_to_bytes(bytes)?;
            },
            ServerRequest::ListIds { page_index, page_size, row_offset } => {
                // byte
                4u8.append_to_bytes(bytes)?;
                // u64
                page_index.append_to_bytes(bytes)?;
                // u64
                page_size.append_to_bytes(bytes)?;
                // u64
                row_offset.append_to_bytes(bytes)?;
            },
            ServerRequest::BulkSendBytes { bytes_collection } => {
                // byte
                5u8.append_to_bytes(bytes)?;
                // Vec<Vec<u8>>
                bytes_collection.append_to_bytes(bytes)?;
            },
            ServerRequest::BulkGetBytes { ids } => {
                // byte
                6u8.append_to_bytes(bytes)?;
                // Vec<i64>
                ids.as_ref().append_to_bytes(bytes)?;
            },
        }
        Ok(())
    }
    fn extract_from_bytes(bytes: &Vec<u8>, index: &mut usize) -> Result<Self, Box<dyn Error>> where Self: Sized {
        // byte
        let enum_variant_byte = u8::extract_from_bytes(bytes, index)?;

        match enum_variant_byte {
            0 => {
                Ok(Self::HealthCheck)
            },
            1 => {
                Ok(Self::SendBytes {
                    bytes: Vec::<u8>::extract_from_bytes(bytes, index)?,
                })
            },
            2 => {
                Ok(Self::GetBytes {
                    id: i64::extract_from_bytes(bytes, index)?,
                })
            },
            3 => {
                Ok(Self::Delete {
                    id: i64::extract_from_bytes(bytes, index)?,
                })
            },
            4 => {
                Ok(Self::ListIds {
                    page_index: u64::extract_from_bytes(bytes, index)?,
                    page_size: u64::extract_from_bytes(bytes, index)?,
                    row_offset: u64::extract_from_bytes(bytes, index)?,
                })
            },
            5 => {
                Ok(Self::BulkSendBytes {
                    bytes_collection: Vec::<Vec<u8>>::extract_from_bytes(bytes, index)?,
                })
            },
            6 => {
                Ok(Self::BulkGetBytes {
                    ids: ClonelessCow::Owned(Vec::<i64>::extract_from_bytes(bytes, index)?),
                })
            },
            _ => {
                Err(RemoteDataStoreError::UnexpectedEnumVariantByte {
                    enum_variant_byte,
                    enum_variant_name: String::from(std::any::type_name::<Self>()),
                }.into())
            }
        }
    }
}

#[derive(Debug)]
enum ServerResponse {
    HealthCheck,
    SentBytes {
        id: i64,
    },
    ReceivedBytes {
        bytes: Vec<u8>,
    },
    Deleted {
        id: i64,
    },
    ReceivedIdList {
        ids: Vec<i64>,
    },
    BulkSentBytes {
        ids: Vec<i64>,
    },
    BulkReceivedBytes {
        bytes_collection: Vec<Vec<u8>>,
    },
}

impl ByteConverter for ServerResponse {
    fn append_to_bytes(&self, bytes: &mut Vec<u8>) -> Result<(), Box<dyn Error>> {
        match self {
            ServerResponse::HealthCheck => {
                // byte
                0u8.append_to_bytes(bytes)?;
            },
            ServerResponse::ReceivedBytes { bytes: received_bytes } => {
                // byte
                1u8.append_to_bytes(bytes)?;
                // Vec<u8>
                received_bytes.append_to_bytes(bytes)?;
            },
            ServerResponse::SentBytes { id } => {
                // byte
                2u8.append_to_bytes(bytes)?;
                // i64
                id.append_to_bytes(bytes)?;
            },
            ServerResponse::Deleted { id } => {
                // byte
                3u8.append_to_bytes(bytes)?;
                // i64
                id.append_to_bytes(bytes)?;
            },
            ServerResponse::ReceivedIdList { ids } => {
                // byte
                4u8.append_to_bytes(bytes)?;
                // Vec<i64>
                ids.append_to_bytes(bytes)?;
            },
            ServerResponse::BulkSentBytes { ids } => {
                // byte
                5u8.append_to_bytes(bytes)?;
                // Vec<i64>
                ids.append_to_bytes(bytes)?;
            },
            ServerResponse::BulkReceivedBytes { bytes_collection } => {
                // byte
                6u8.append_to_bytes(bytes)?;
                // Vec<Vec<u8>>
                bytes_collection.append_to_bytes(bytes)?;
            },
        }

        Ok(())
    }
    fn extract_from_bytes(bytes: &Vec<u8>, index: &mut usize) -> Result<Self, Box<dyn Error>> where Self: Sized {
        // enum variant byte
        let enum_variant_byte = u8::extract_from_bytes(bytes, index)?;

        match enum_variant_byte {
            0 => {
                Ok(Self::HealthCheck)
            },
            1 => {
                Ok(Self::ReceivedBytes {
                    bytes: Vec::<u8>::extract_from_bytes(bytes, index)?,
                })
            },
            2 => {
                Ok(Self::SentBytes {
                    id: i64::extract_from_bytes(bytes, index)?,
                })
            },
            3 => {
                Ok(Self::Deleted {
                    id: i64::extract_from_bytes(bytes, index)?,
                })
            },
            4 => {
                Ok(Self::ReceivedIdList {
                    ids: Vec::<i64>::extract_from_bytes(bytes, index)?,
                })
            },
            5 => {
                Ok(Self::BulkSentBytes {
                    ids: Vec::<i64>::extract_from_bytes(bytes, index)?,
                })
            },
            6 => {
                Ok(Self::BulkReceivedBytes {
                    bytes_collection: Vec::<Vec<u8>>::extract_from_bytes(bytes, index)?,
                })
            },
            _ => {
                Err(RemoteDataStoreError::UnexpectedEnumVariantByte {
                    enum_variant_byte,
                    enum_variant_name: String::from(std::any::type_name::<Self>()),
                }.into())
            }
        }
    }
}

#[derive(thiserror::Error, Debug)]
enum RemoteDataStoreError {
    #[error("Unexpected response {response} based on request {request}.")]
    UnexpectedResponseForRequest {
        response: String,
        request: String,
    },
    #[error("Unexpected deleted ID mismatch from request {request_file_record_id} and response {response_file_record_id}.")]
    DeleteMismatch {
        request_file_record_id: i64,
        response_file_record_id: i64,
    },
    #[error("Unexpected enum variant byte {enum_variant_byte} when trying to construct {enum_variant_name}.")]
    UnexpectedEnumVariantByte {
        enum_variant_byte: u8,
        enum_variant_name: String,
    },
}