Skip to content

Commit 5eacd78

Browse files
committed
Modify the graph interface to make possible select the type of operation to perform (read or write)
1 parent 79722b5 commit 5eacd78

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
lines changed

lib/src/graph.rs

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,19 @@ impl Graph {
134134
///
135135
/// Transactions will not be automatically retried on any failure.
136136
pub async fn start_txn(&self) -> Result<Txn> {
137-
self.impl_start_txn_on(self.config.db.clone()).await
137+
self.impl_start_txn_on(self.config.db.clone(), Operation::Write)
138+
.await
139+
}
140+
141+
/// Starts a READ ONLY new transaction on the configured database.
142+
/// All queries that needs to be run/executed within the transaction
143+
/// should be executed using either [`Txn::run`] or [`Txn::execute`]
144+
///
145+
/// Transactions will not be automatically retried on any failure.
146+
#[cfg(feature = "unstable-bolt-protocol-impl-v2")]
147+
pub async fn start_read_txn(&self) -> Result<Txn> {
148+
self.impl_start_txn_on(self.config.db.clone(), Operation::Read)
149+
.await
138150
}
139151

140152
/// Starts a new transaction on the provided database.
@@ -143,11 +155,12 @@ impl Graph {
143155
///
144156
/// Transactions will not be automatically retried on any failure.
145157
pub async fn start_txn_on(&self, db: impl Into<Database>) -> Result<Txn> {
146-
self.impl_start_txn_on(Some(db.into())).await
158+
self.impl_start_txn_on(Some(db.into()), Operation::Write)
159+
.await
147160
}
148161

149-
async fn impl_start_txn_on(&self, db: Option<Database>) -> Result<Txn> {
150-
let connection = self.pool.get(Some(Operation::Write)).await?;
162+
async fn impl_start_txn_on(&self, db: Option<Database>, operation: Operation) -> Result<Txn> {
163+
let connection = self.pool.get(Some(operation)).await?;
151164
Txn::new(db, self.config.fetch_size, connection).await
152165
}
153166

@@ -163,6 +176,23 @@ impl Graph {
163176
///
164177
/// use [`Graph::execute`] when you are interested in the result stream
165178
pub async fn run(&self, q: Query) -> Result<()> {
179+
self.impl_run_on(self.config.db.clone(), q, Operation::Write)
180+
.await
181+
}
182+
183+
/// Runs a READ ONLY query on the configured database using a connection from the connection pool,
184+
/// It doesn't return any [`DetachedRowStream`] as the `run` abstraction discards any stream.
185+
///
186+
/// This operation retires the query on certain failures.
187+
/// All errors with the `Transient` error class as well as a few other error classes are considered retryable.
188+
/// This includes errors during a leader election or when the transaction resources on the server (memory, handles, ...) are exhausted.
189+
/// Retries happen with an exponential backoff until a retry delay exceeds 60s, at which point the query fails with the last error as it would without any retry.
190+
///
191+
/// Use [`Graph::run`] for cases where you just want a write operation
192+
///
193+
/// use [`Graph::execute`] when you are interested in the result stream
194+
#[cfg(feature = "unstable-bolt-protocol-impl-v2")]
195+
pub async fn run_read(&self, q: Query) -> Result<()> {
166196
self.impl_run_on(self.config.db.clone(), q, Operation::Read)
167197
.await
168198
}
@@ -178,8 +208,13 @@ impl Graph {
178208
/// Use [`Graph::run`] for cases where you just want a write operation
179209
///
180210
/// use [`Graph::execute`] when you are interested in the result stream
181-
pub async fn run_on(&self, db: impl Into<Database>, q: Query) -> Result<()> {
182-
self.impl_run_on(Some(db.into()), q, Operation::Read).await
211+
pub async fn run_on(
212+
&self,
213+
db: impl Into<Database>,
214+
q: Query,
215+
operation: Operation,
216+
) -> Result<()> {
217+
self.impl_run_on(Some(db.into()), q, operation).await
183218
}
184219

185220
async fn impl_run_on(
@@ -205,7 +240,7 @@ impl Graph {
205240
.await
206241
}
207242

208-
/// Executes a query on the configured database and returns a [`DetachedRowStream`]
243+
/// Executes a READ/WRITE query on the configured database and returns a [`DetachedRowStream`]
209244
///
210245
/// This operation retires the query on certain failures.
211246
/// All errors with the `Transient` error class as well as a few other error classes are considered retryable.
@@ -216,17 +251,32 @@ impl Graph {
216251
.await
217252
}
218253

219-
/// Executes a query on the provided database and returns a [`DetachedRowStream`]
254+
/// Executes a query READ on the configured database and returns a [`DetachedRowStream`]
220255
///
221256
/// This operation retires the query on certain failures.
222257
/// All errors with the `Transient` error class as well as a few other error classes are considered retryable.
223258
/// This includes errors during a leader election or when the transaction resources on the server (memory, handles, ...) are exhausted.
224259
/// Retries happen with an exponential backoff until a retry delay exceeds 60s, at which point the query fails with the last error as it would without any retry.
225-
pub async fn execute_on(&self, db: impl Into<Database>, q: Query) -> Result<DetachedRowStream> {
226-
self.impl_execute_on(Some(db.into()), q, Operation::Write)
260+
pub async fn execute_read(&self, q: Query) -> Result<DetachedRowStream> {
261+
self.impl_execute_on(self.config.db.clone(), q, Operation::Read)
227262
.await
228263
}
229264

265+
/// Executes a query on the provided database and returns a [`DetachedRowStream`]
266+
///
267+
/// This operation retires the query on certain failures.
268+
/// All errors with the `Transient` error class as well as a few other error classes are considered retryable.
269+
/// This includes errors during a leader election or when the transaction resources on the server (memory, handles, ...) are exhausted.
270+
/// Retries happen with an exponential backoff until a retry delay exceeds 60s, at which point the query fails with the last error as it would without any retry.
271+
pub async fn execute_on(
272+
&self,
273+
db: impl Into<Database>,
274+
q: Query,
275+
operation: Operation,
276+
) -> Result<DetachedRowStream> {
277+
self.impl_execute_on(Some(db.into()), q, operation).await
278+
}
279+
230280
async fn impl_execute_on(
231281
&self,
232282
db: Option<Database>,

lib/src/routing/routed_connection_manager.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ impl RoutedConnectionManager {
117117
.load_balancing_strategy
118118
.select_reader(available_servers.as_slice()),
119119
} {
120+
debug!("requesting connection for server: {:?}", server);
120121
if let Some(pool) = self.registry.get_pool(&server) {
121122
match pool.get().await {
122123
Ok(connection) => return Ok(connection),

0 commit comments

Comments
 (0)