Skip to content

Commit

Permalink
fix read only query builder (#77)
Browse files Browse the repository at this point in the history
* Update version on readme
Fix clippy warnings
fix read only query builder

* remove extra line in the comment
Upgrade thiserror

* Add test

* Add some comment and vectical spaces
  • Loading branch information
barakb authored Dec 10, 2024
1 parent 403e899 commit db619be
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 27 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ parking_lot = { version = "0.12.3", default-features = false, features = ["deadl
redis = { version = "0.27.6", default-features = false, features = ["sentinel"] }
regex = { version = "1.11.1", default-features = false, features = ["std", "perf", "unicode-bool", "unicode-perl"] }
strum = { version = "0.26.3", default-features = false, features = ["std", "derive"] }
thiserror = "2.0.4"
thiserror = "2.0.6"
tokio = { version = "1.42.0", default-features = false, features = ["macros", "sync", "rt-multi-thread"], optional = true }
tracing = { version = "0.1.41", default-features = false, features = ["std", "attributes"], optional = true }

Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Just add it to your `Cargo.toml`, like so:

```toml
falkordb = { version = "0.1.9" }
falkordb = { version = "0.1.10" }
```

### Run FalkorDB instance
Expand Down Expand Up @@ -69,7 +69,7 @@ This client supports nonblocking API using the [`tokio`](https://tokio.rs/) runt
It can be enabled like so:

```toml
falkordb = { version = "0.1.9", features = ["tokio"] }
falkordb = { version = "0.1.10", features = ["tokio"] }
```

Currently, this API requires running within a [
Expand Down Expand Up @@ -123,21 +123,21 @@ when using tokio: `"tokio-rustls"`/`"tokio-native-tls"`).
For Rustls:

```toml
falkordb = { version = "0.1.9", features = ["rustls"] }
falkordb = { version = "0.1.10", features = ["rustls"] }
```

```toml
falkordb = { version = "0.1.9", features = ["tokio-rustls"] }
falkordb = { version = "0.1.10", features = ["tokio-rustls"] }
```

For Native TLS:

```toml
falkordb = { version = "0.1.9", features = ["native-tls"] }
falkordb = { version = "0.1.10", features = ["native-tls"] }
```

```toml
falkordb = { version = "0.1.9", features = ["tokio-native-tls"] }
falkordb = { version = "0.1.10", features = ["tokio-native-tls"] }
```

### Tracing
Expand All @@ -146,7 +146,7 @@ This crate fully supports instrumentation using the [`tracing`](https://docs.rs/
it, simply, enable the `tracing` feature:

```toml
falkordb = { version = "0.1.9", features = ["tracing"] }
falkordb = { version = "0.1.10", features = ["tracing"] }
```

Note that different functions use different filtration levels, to avoid spamming your tests, be sure to enable the
Expand Down
42 changes: 39 additions & 3 deletions src/client/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl FalkorSyncClient {
})
}

/// Get the max number of connections in the client's connection pool
/// Get the max number of connections in the client's connection pool
pub fn connection_pool_size(&self) -> u8 {
self.inner.connection_pool_size
}
Expand All @@ -138,7 +138,7 @@ impl FalkorSyncClient {
///
/// # Arguments
/// * `config_Key`: A [`String`] representation of a configuration's key.
/// The config key can also be "*", which will return ALL the configuration options.
/// The config key can also be "*", which will return ALL the configuration options.
///
/// # Returns
/// A [`HashMap`] comprised of [`String`] keys, and [`ConfigValue`] values.
Expand All @@ -161,7 +161,7 @@ impl FalkorSyncClient {
///
/// # Arguments
/// * `config_Key`: A [`String`] representation of a configuration's key.
/// The config key can also be "*", which will return ALL the configuration options.
/// The config key can also be "*", which will return ALL the configuration options.
/// * `value`: The new value to set, which is anything that can be converted into a [`ConfigValue`], namely string types and i64.
#[cfg_attr(
feature = "tracing",
Expand Down Expand Up @@ -293,6 +293,42 @@ mod tests {
let graphs = res.unwrap();
assert!(graphs.contains(&"imdb".to_string()));
}

#[test]
fn test_read_only_query() {
let client = create_test_client();
let mut graph = client.select_graph("test_read_only_query");
graph
.query("CREATE (n:Person {name: 'John Doe', age: 30})")
.execute()
.expect("Could not create John");

// test ro_query with a read query
graph
.ro_query("MATCH (n:Person {name: 'John Doe', age: 30}) RETURN n")
.execute()
.expect("Could not read John");

// test ro_query with a write query
let result = graph
.ro_query("CREATE (n:Person {name: 'John Doe', age: 30})")
.execute();
assert!(
result.is_err(),
"Expected an error for write operation in read-only query"
);
if let Err(e) = result {
assert!(
e.to_string()
.contains("is to be executed only on read-only queries"),
"Unexpected error message: {}",
e
);
}

graph.delete().unwrap();
}

#[test]
fn test_read_vec32() {
let client = create_test_client();
Expand Down
2 changes: 1 addition & 1 deletion src/graph/asynchronous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl AsyncGraph {
&'a mut self,
query_string: &'a str,
) -> QueryBuilder<'a, QueryResult<LazyResultSet<'a>>, &'a str, Self> {
QueryBuilder::new(self, "GRAPH.QUERY_RO", query_string)
QueryBuilder::new(self, "GRAPH.RO_QUERY", query_string)
}

/// Creates a [`ProcedureQueryBuilder`] for this graph
Expand Down
2 changes: 1 addition & 1 deletion src/graph/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl SyncGraph {
&'a mut self,
query_string: &'a str,
) -> QueryBuilder<'a, QueryResult<LazyResultSet<'a>>, &'a str, Self> {
QueryBuilder::new(self, "GRAPH.QUERY_RO", query_string)
QueryBuilder::new(self, "GRAPH.RO_QUERY", query_string)
}

/// Creates a [`ProcedureQueryBuilder`] for this graph
Expand Down
17 changes: 9 additions & 8 deletions src/graph/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ impl<'a, Output, T: Display, G: HasGraphSchema> QueryBuilder<'a, Output, T, G> {
/// Specify a timeout after which to abort the query
///
/// # Arguments
/// * `timeout`: the timeout after which to abort, in ms
/// * `timeout`: the timeout after which the server is allowed to abort or throw this request,
/// in milliseconds, when that happens the server will return a timeout error
pub fn with_timeout(
self,
timeout: i64,
Expand Down Expand Up @@ -155,7 +156,7 @@ impl<'a, Output, T: Display, G: HasGraphSchema> QueryBuilder<'a, Output, T, G> {
}
}

impl<'a, Out, T: Display> QueryBuilder<'a, Out, T, SyncGraph> {
impl<Out, T: Display> QueryBuilder<'_, Out, T, SyncGraph> {
#[cfg_attr(
feature = "tracing",
tracing::instrument(name = "Common Query Execution Steps", skip_all, level = "trace")
Expand Down Expand Up @@ -234,7 +235,7 @@ impl<'a, T: Display> QueryBuilder<'a, QueryResult<LazyResultSet<'a>>, T, AsyncGr
}
}

impl<'a, T: Display> QueryBuilder<'a, ExecutionPlan, T, SyncGraph> {
impl<T: Display> QueryBuilder<'_, ExecutionPlan, T, SyncGraph> {
/// Executes the query, returning an [`ExecutionPlan`] from the data returned
pub fn execute(mut self) -> FalkorResult<ExecutionPlan> {
self.common_execute_steps().and_then(ExecutionPlan::parse)
Expand Down Expand Up @@ -386,7 +387,7 @@ impl<'a, Out, G: HasGraphSchema> ProcedureQueryBuilder<'a, Out, G> {
}
}

impl<'a, Out> ProcedureQueryBuilder<'a, Out, SyncGraph> {
impl<Out> ProcedureQueryBuilder<'_, Out, SyncGraph> {
#[cfg_attr(
feature = "tracing",
tracing::instrument(
Expand All @@ -397,7 +398,7 @@ impl<'a, Out> ProcedureQueryBuilder<'a, Out, SyncGraph> {
)]
fn common_execute_steps(&mut self) -> FalkorResult<redis::Value> {
let command = match self.readonly {
true => "GRAPH.QUERY_RO",
true => "GRAPH.RO_QUERY",
false => "GRAPH.QUERY",
};

Expand Down Expand Up @@ -431,7 +432,7 @@ impl<'a, Out> ProcedureQueryBuilder<'a, Out, AsyncGraph> {
)]
async fn common_execute_steps(&mut self) -> FalkorResult<redis::Value> {
let command = match self.readonly {
true => "GRAPH.QUERY_RO",
true => "GRAPH.RO_QUERY",
false => "GRAPH.QUERY",
};

Expand All @@ -453,7 +454,7 @@ impl<'a, Out> ProcedureQueryBuilder<'a, Out, AsyncGraph> {
}
}

impl<'a> ProcedureQueryBuilder<'a, QueryResult<Vec<FalkorIndex>>, SyncGraph> {
impl ProcedureQueryBuilder<'_, QueryResult<Vec<FalkorIndex>>, SyncGraph> {
/// Executes the procedure call and return a [`QueryResult`] type containing a result set of [`FalkorIndex`]s
/// This functions consumes self
#[cfg_attr(
Expand Down Expand Up @@ -481,7 +482,7 @@ impl<'a> ProcedureQueryBuilder<'a, QueryResult<Vec<FalkorIndex>>, AsyncGraph> {
}
}

impl<'a> ProcedureQueryBuilder<'a, QueryResult<Vec<Constraint>>, SyncGraph> {
impl ProcedureQueryBuilder<'_, QueryResult<Vec<Constraint>>, SyncGraph> {
/// Executes the procedure call and return a [`QueryResult`] type containing a result set of [`Constraint`]s
/// This functions consumes self
#[cfg_attr(
Expand Down
1 change: 0 additions & 1 deletion src/response/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ fn parse_string_array(
vector
.into_iter()
.map(FalkorValue::into_string)
.into_iter()
.collect::<Result<Vec<String>, FalkorDBError>>()
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/response/lazy_result_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<'a> LazyResultSet<'a> {
}
}

impl<'a> Iterator for LazyResultSet<'a> {
impl Iterator for LazyResultSet<'_> {
type Item = Vec<FalkorValue>;

#[cfg_attr(
Expand Down

0 comments on commit db619be

Please sign in to comment.