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
|
use std::time::Duration;
use anyhow::Result;
use rand::prelude::*;
use serde::{Deserialize, Serialize};
use k2v_client::K2vClient;
use rusoto_core::HttpClient;
use rusoto_credential::{AwsCredentials, StaticProvider};
use rusoto_s3::S3Client;
use rusoto_signature::Region;
use crate::cryptoblob::Key;
use crate::time::now_msec;
pub trait BayouState:
Default + Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static
{
type Op: Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static;
fn apply(&self, op: &Self::Op) -> Self;
}
pub struct Bayou<S: BayouState> {
bucket: String,
path: String,
key: Key,
k2v: K2vClient,
s3: S3Client,
checkpoint: (Timestamp, S),
history: Vec<(Timestamp, S::Op, Option<S>)>,
}
impl<S: BayouState> Bayou<S> {
pub fn new(
creds: AwsCredentials,
k2v_region: Region,
s3_region: Region,
bucket: String,
path: String,
key: Key,
) -> Result<Self> {
let k2v_client = K2vClient::new(k2v_region, bucket.clone(), creds.clone(), None)?;
let static_creds = StaticProvider::new(
creds.aws_access_key_id().to_string(),
creds.aws_secret_access_key().to_string(),
creds.token().clone(),
None,
);
let s3_client = S3Client::new_with(HttpClient::new()?, static_creds, s3_region);
Ok(Self {
bucket,
path,
key,
k2v: k2v_client,
s3: s3_client,
checkpoint: (Timestamp::zero(), S::default()),
history: vec![],
})
}
pub async fn sync(&mut self) -> Result<()> {
// 1. List checkpoints
// 2. Load last checkpoint
// 3. List all operations starting from checkpoint
// 4. Check that first operation has same timestamp as checkpoint (if not zero)
// 5. Apply all operations in order
unimplemented!()
}
pub fn state(&self) -> &S {
unimplemented!()
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Timestamp {
pub msec: u64,
pub rand: u64,
}
impl Timestamp {
pub fn now() -> Self {
let mut rng = thread_rng();
Self {
msec: now_msec(),
rand: rng.gen::<u64>(),
}
}
pub fn after(other: &Self) -> Self {
let mut rng = thread_rng();
Self {
msec: std::cmp::max(now_msec(), other.msec + 1),
rand: rng.gen::<u64>(),
}
}
pub fn zero() -> Self {
Self { msec: 0, rand: 0 }
}
pub fn serialize(&self) -> String {
let mut bytes = [0u8; 16];
bytes[0..8].copy_from_slice(&u64::to_be_bytes(self.msec));
bytes[8..16].copy_from_slice(&u64::to_be_bytes(self.rand));
hex::encode(&bytes)
}
pub fn parse(v: &str) -> Option<Self> {
let bytes = hex::decode(v).ok()?;
if bytes.len() != 16 {
return None;
}
Some(Self {
msec: u64::from_be_bytes(bytes[0..8].try_into().unwrap()),
rand: u64::from_be_bytes(bytes[8..16].try_into().unwrap()),
})
}
}
|