Skip to content

Commit 1739815

Browse files
committed
Enhance Store struct with session management capabilities
- Added `session_defaults` and `session_advisory_locks` fields to the `Store` struct for improved session configuration. - Updated the `connect` method to initialize these new fields. - Refactored `session_builder` to utilize the new session defaults. - Introduced `set_session_defaults` and `with_session_defaults` methods for flexible session configuration. - Updated tests to validate the new session management features, ensuring correct behavior during session operations.
1 parent 93a4918 commit 1739815

2 files changed

Lines changed: 49 additions & 16 deletions

File tree

src/store.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ use std::time::Duration;
1111
#[derive(Clone)]
1212
pub struct Store {
1313
pool: PgPool,
14+
session_defaults: AppendOptions,
15+
session_advisory_locks: bool,
1416
}
1517

1618
impl Store {
1719
pub async fn connect(url: &str) -> Result<Self> {
1820
let pool = PgPool::connect(url).await?;
19-
Ok(Self { pool })
21+
Ok(Self {
22+
pool,
23+
session_defaults: AppendOptions::default(),
24+
session_advisory_locks: false,
25+
})
2026
}
2127

2228
pub fn builder(url: impl Into<String>) -> StoreBuilder {
@@ -30,17 +36,31 @@ impl Store {
3036
}
3137

3238
pub fn document_session(&self) -> DocumentSession {
33-
self.docs().session()
39+
self.session_builder().build()
3440
}
3541

3642
pub fn session_builder(&self) -> SessionBuilder {
3743
SessionBuilder {
3844
store: self.clone(),
39-
defaults: AppendOptions::default(),
40-
use_advisory_lock: false,
45+
defaults: self.session_defaults.clone(),
46+
use_advisory_lock: self.session_advisory_locks,
4147
}
4248
}
4349

50+
pub fn session_defaults(&self) -> (&AppendOptions, bool) {
51+
(&self.session_defaults, self.session_advisory_locks)
52+
}
53+
54+
pub fn set_session_defaults(&mut self, defaults: AppendOptions, advisory_locks: bool) {
55+
self.session_defaults = defaults;
56+
self.session_advisory_locks = advisory_locks;
57+
}
58+
59+
pub fn with_session_defaults(mut self, defaults: AppendOptions, advisory_locks: bool) -> Self {
60+
self.set_session_defaults(defaults, advisory_locks);
61+
self
62+
}
63+
4464
pub fn events(&self) -> Events {
4565
Events {
4666
pool: self.pool.clone(),
@@ -132,7 +152,11 @@ impl StoreBuilder {
132152
opts = opts.acquire_timeout(t);
133153
}
134154
let pool = opts.connect(&self.url).await?;
135-
Ok(Store { pool })
155+
Ok(Store {
156+
pool,
157+
session_defaults: AppendOptions::default(),
158+
session_advisory_locks: false,
159+
})
136160
}
137161
}
138162

@@ -194,14 +218,18 @@ impl SessionBuilder {
194218

195219
let mut session = DocumentSession::new(store.pool.clone(), events);
196220
if let Some(headers) = defaults.headers.clone() {
197-
let headers_clone = headers.clone();
198-
session.merge_event_headers(headers_clone);
199-
if let Some(key) = headers.get("idempotency_key").and_then(|v| v.as_str()) {
200-
session.set_event_idempotency_key(key);
201-
}
221+
session.merge_event_headers(headers);
202222
}
203223
session.set_event_causation_id(defaults.causation_id);
204224
session.set_event_correlation_id(defaults.correlation_id);
225+
if let Some(key) = defaults
226+
.headers
227+
.as_ref()
228+
.and_then(|h| h.get("idempotency_key"))
229+
.and_then(|v| v.as_str())
230+
{
231+
session.set_event_idempotency_key(key);
232+
}
205233
session
206234
}
207235
}

tests/documents_repo.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use anyhow::Result;
2+
use rillflow::events::AppendOptions;
23
use rillflow::{Error, Event, Expected, Store};
34
use serde::{Deserialize, Serialize};
45
use serde_json::json;
@@ -109,7 +110,7 @@ async fn session_load_store_delete_roundtrip() -> Result<()> {
109110
let port = container.get_host_port_ipv4(5432).await?;
110111
let url = format!("postgres://postgres:postgres@{host}:{port}/postgres?sslmode=disable");
111112

112-
let store = Store::connect(&url).await?;
113+
let mut store = Store::connect(&url).await?;
113114
rillflow::testing::migrate_core_schema(store.pool()).await?;
114115

115116
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
@@ -119,11 +120,15 @@ async fn session_load_store_delete_roundtrip() -> Result<()> {
119120
}
120121

121122
let id = Uuid::new_v4();
122-
let mut session = store
123-
.session_builder()
124-
.merge_headers(json!({"source": "test"}))
125-
.advisory_locks(true)
126-
.build();
123+
store.set_session_defaults(
124+
AppendOptions {
125+
headers: Some(json!({"source": "test"})),
126+
causation_id: None,
127+
correlation_id: None,
128+
},
129+
true,
130+
);
131+
let mut session = store.document_session();
127132

128133
// create new doc via session store + save
129134
session.store(

0 commit comments

Comments
 (0)