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
use crate::{Cnn, CnnPool, Error};
use bb8_postgres::{bb8::Pool, PostgresConnectionManager};
use bytes::Bytes;
use eventsourced::{Metadata, Snapshot, SnapshotStore};
use serde::{Deserialize, Serialize};
use std::{
error::Error as StdError,
fmt::{self, Debug, Formatter},
};
use tokio_postgres::NoTls;
use tracing::debug;
use uuid::Uuid;
#[derive(Clone)]
pub struct PostgresSnapshotStore {
cnn_pool: CnnPool<NoTls>,
}
impl PostgresSnapshotStore {
#[allow(missing_docs)]
pub async fn new(config: Config) -> Result<Self, Error> {
debug!(?config, "Creating PostgresSnapshotStore");
let tls = NoTls;
let cnn_manager = PostgresConnectionManager::new_from_stringlike(config.cnn_config(), tls)
.map_err(Error::ConnectionManager)?;
let cnn_pool = Pool::builder()
.build(cnn_manager)
.await
.map_err(Error::ConnectionPool)?;
Ok(Self { cnn_pool })
}
pub async fn setup(&self) -> Result<(), Box<dyn StdError + Send + Sync>> {
self.cnn()
.await?
.execute(include_str!("create_snapshot_store.sql"), &[])
.await?;
Ok(())
}
async fn cnn(&self) -> Result<Cnn<NoTls>, Error> {
self.cnn_pool.get().await.map_err(Error::GetConnection)
}
}
impl Debug for PostgresSnapshotStore {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("PostgresSnapshotStore").finish()
}
}
impl SnapshotStore for PostgresSnapshotStore {
type Error = Error;
async fn save<'a, 'b, 'c, S, StateToBytes, StateToBytesError>(
&'a mut self,
id: Uuid,
seq_no: u64,
state: &'b S,
_metadata: Metadata,
state_to_bytes: &'c StateToBytes,
) -> Result<(), Self::Error>
where
'b: 'a,
'c: 'a,
S: Send + Sync + 'a,
StateToBytes: Fn(&S) -> Result<Bytes, StateToBytesError> + Send + Sync + 'static,
StateToBytesError: StdError + Send + Sync + 'static,
{
let cnn = self.cnn().await?;
let bytes = state_to_bytes(state).map_err(|source| Error::ToBytes(Box::new(source)))?;
cnn.execute(
"INSERT INTO snapshots VALUES ($1, $2, $3)",
&[&id, &(seq_no as i64), &bytes.as_ref()],
)
.await
.map_err(Error::ExecuteStmt)?;
Ok(())
}
async fn load<'a, S, StateFromBytes, StateFromBytesError>(
&'a self,
id: Uuid,
state_from_bytes: StateFromBytes,
) -> Result<Option<Snapshot<S>>, Self::Error>
where
S: 'a,
StateFromBytes: Fn(Bytes) -> Result<S, StateFromBytesError> + Copy + Send + Sync + 'static,
StateFromBytesError: StdError + Send + Sync + 'static,
{
let cnn = self.cnn().await?;
cnn.query_opt("SELECT seq_no, state FROM snapshots WHERE id = $1", &[&id])
.await
.map_err(Error::ExecuteStmt)?
.map(move |row| {
let seq_no = row.get::<_, i64>(0) as u64;
let bytes = row.get::<_, &[u8]>(1);
let bytes = Bytes::copy_from_slice(bytes);
state_from_bytes(bytes)
.map_err(|source| Error::FromBytes(Box::new(source)))
.map(|state| Snapshot::new(seq_no, state, None))
})
.transpose()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Config {
host: String,
port: u16,
user: String,
password: String,
dbname: String,
sslmode: String,
}
impl Config {
pub fn with_host<T>(self, host: T) -> Self
where
T: Into<String>,
{
let host = host.into();
Self { host, ..self }
}
pub fn with_port(self, port: u16) -> Self {
Self { port, ..self }
}
pub fn with_user<T>(self, user: T) -> Self
where
T: Into<String>,
{
let user = user.into();
Self { user, ..self }
}
pub fn with_password<T>(self, password: T) -> Self
where
T: Into<String>,
{
let password = password.into();
Self { password, ..self }
}
pub fn with_dbname<T>(self, dbname: T) -> Self
where
T: Into<String>,
{
let dbname = dbname.into();
Self { dbname, ..self }
}
pub fn with_sslmode<T>(self, sslmode: T) -> Self
where
T: Into<String>,
{
let sslmode = sslmode.into();
Self { sslmode, ..self }
}
fn cnn_config(&self) -> String {
format!(
"host={} port={} user={} password={} dbname={} sslmode={}",
self.host, self.port, self.user, self.password, self.dbname, self.sslmode
)
}
}
impl Default for Config {
fn default() -> Self {
Self {
host: "localhost".to_string(),
port: 5432,
user: "postgres".to_string(),
password: "".to_string(),
dbname: "postgres".to_string(),
sslmode: "prefer".to_string(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use eventsourced::convert;
use testcontainers::{clients::Cli, images::postgres::Postgres};
#[tokio::test]
async fn test_snapshot_store() -> Result<(), Box<dyn StdError + Send + Sync>> {
let client = Cli::default();
let container = client.run(Postgres::default());
let port = container.get_host_port_ipv4(5432);
let config = Config::default().with_port(port);
let mut snapshot_store = PostgresSnapshotStore::new(config).await?;
snapshot_store.setup().await?;
let id = Uuid::now_v7();
let snapshot = snapshot_store
.load::<i32, _, _>(id, &convert::prost::from_bytes)
.await?;
assert!(snapshot.is_none());
let seq_no = 42;
let state = 666;
snapshot_store
.save(id, seq_no, &state, None, &convert::prost::to_bytes)
.await?;
let snapshot = snapshot_store
.load::<i32, _, _>(id, &convert::prost::from_bytes)
.await?;
assert!(snapshot.is_some());
let snapshot = snapshot.unwrap();
assert_eq!(snapshot.seq_no, seq_no);
assert_eq!(snapshot.state, state);
assert!(snapshot.metadata.is_none());
Ok(())
}
}