Skip to content

Commit

Permalink
Merge pull request #6 from tompro/develop
Browse files Browse the repository at this point in the history
All commands and redis 0.20.0
  • Loading branch information
tompro authored Feb 20, 2021
2 parents 5cc7bae + 9d7e6bd commit 4561fc1
Show file tree
Hide file tree
Showing 12 changed files with 631 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "redis-graph"
version = "0.3.0"
version = "0.4.0"
authors = ["tompro <office@protom.eu>"]
keywords = ["redis", "database", "graph"]
description = "API for Redis graph database types."
Expand All @@ -13,7 +13,7 @@ edition = "2018"
exclude = ["docker"]

[dependencies]
redis = { version = "0.19.0", optional = true }
redis = { version = "^0.20.0", optional = true }

[features]
default = ['redis']
Expand Down
41 changes: 41 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[tasks.publish]
description = "Publish to crates.io"
dependencies = ["gh_checks"]
command = "cargo"
args = ["publish", "--all-features"]

[tasks.gh_checks]
dependencies = [
"cargo_check",
"test",
"check_fmt",
"clippy"
]

[tasks.cargo_check]
description = "Runs cargo check"
command = "cargo"
args = ["check"]

[tasks.check_fmt]
description = "Runs fmt in check mode"
install_crate = "rustfmt"
command = "cargo"
args = ["fmt", "--all", "--", "--check"]

[tasks.test]
description = "Runs tests with all features"
command = "cargo"
args = ["test", "--all-features"]

[tasks.doc]
description = "Generates docs with all features"
command = "cargo"
args = ["doc", "--all-features"]

[tasks.clippy]
description = "Runs clippy"
install_crate = "clippy"
command = "cargo"
args = ["clippy", "--", "-D warnings"]

20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
# redis_graph

[![crates.io](https://img.shields.io/badge/crates.io-v0.3.0-orange)](https://crates.io/crates/redis_graph)
[![crates.io](https://img.shields.io/badge/crates.io-v0.4.0-orange)](https://crates.io/crates/redis_graph)
![Continuous integration](https://github.com/tompro/redis_graph/workflows/Continuous%20integration/badge.svg)

redis-graph provides a small trait with an extension function for the
[redis](https://docs.rs/redis/) crate to allow working with redis graph
data types that can be installed as a [redis module](https://oss.redislabs.com/redisgraph).
Redis graph operation are only using two top level Redis commands
(one for read/write operations and one for read-only operations), so
this crate only adds two functions to the redis commands.
Redis graph operations are mostly using two top level Redis commands
(one for read/write operations and one for read-only operations). In addition
to those there are some more maintenance oriented commands for perfomance,
configuration and clean-up which starting from v0.4.0 are also supported.
The Graph commands are available in synchronous and asynchronous versions.

The crate is called `redis-graph` and you can depend on it via cargo. You will
also need redis in your dependencies.
also need redis in your dependencies. This version was tested against redis 0.20.0
but should run with versions higher than that.

```ini
[dependencies]
redis = "0.19.0"
redis-graph = "*"
redis = "0.20.0"
redis-graph = "0.4.0"
```

Or via git:
Expand All @@ -32,8 +34,8 @@ With async feature inherited from the [redis](https://docs.rs/redis) crate (eith

```ini
[dependencies]
redis = "0.19.0"
redis-graph = { version = "0.3.0", features = ['tokio-comp'] }
redis = "0.20.0"
redis-graph = { version = "0.4.0", features = ['tokio-comp'] }
```

## Synchronous usage
Expand Down
99 changes: 98 additions & 1 deletion src/async_commands.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::types::*;
use redis::aio::ConnectionLike;
use redis::{cmd, RedisFuture, ToRedisArgs};
use redis::{cmd, FromRedisValue, RedisFuture, ToRedisArgs};

/// Provides a high level asynchronous API to work with Redis graph data types.
/// The graph command becomes directly available on ConnectionLike types from
Expand Down Expand Up @@ -39,6 +39,7 @@ pub trait AsyncGraphCommands: ConnectionLike + Send + Sized {
.await
})
}

fn graph_ro_query<'a, K: ToRedisArgs + Send + Sync + 'a, Q: ToRedisArgs + Send + Sync + 'a>(
&'a mut self,
key: K,
Expand All @@ -52,6 +53,102 @@ pub trait AsyncGraphCommands: ConnectionLike + Send + Sized {
.await
})
}

fn graph_profile<
'a,
K: ToRedisArgs + Send + Sync + 'a,
Q: ToRedisArgs + Send + Sync + 'a,
RV: FromRedisValue,
>(
&'a mut self,
key: K,
query: Q,
) -> RedisFuture<RV> {
Box::pin(async move {
cmd("GRAPH.PROFILE")
.arg(key)
.arg(query)
.query_async(self)
.await
})
}

fn graph_delete<'a, K: ToRedisArgs + Send + Sync + 'a>(
&'a mut self,
key: K,
) -> RedisFuture<String> {
Box::pin(async move { cmd("GRAPH.DELETE").arg(key).query_async(self).await })
}

fn graph_explain<
'a,
K: ToRedisArgs + Send + Sync + 'a,
Q: ToRedisArgs + Send + Sync + 'a,
RV: FromRedisValue,
>(
&'a mut self,
key: K,
query: Q,
) -> RedisFuture<RV> {
Box::pin(async move {
cmd("GRAPH.EXPLAIN")
.arg(key)
.arg(query)
.query_async(self)
.await
})
}

fn graph_slowlog<'a, K: ToRedisArgs + Send + Sync + 'a>(
&'a mut self,
key: K,
) -> RedisFuture<Vec<SlowLogEntry>> {
Box::pin(async move { cmd("GRAPH.SLOWLOG").arg(key).query_async(self).await })
}

fn graph_config_set<
'a,
K: ToRedisArgs + Send + Sync + 'a,
V: ToRedisArgs + Send + Sync + 'a,
>(
&'a mut self,
name: K,
value: V,
) -> RedisFuture<bool> {
Box::pin(async move {
cmd("GRAPH.CONFIG")
.arg("SET")
.arg(name)
.arg(value)
.query_async(self)
.await
})
}

fn graph_config_get<'a, K: ToRedisArgs + Send + Sync + 'a, RV: FromRedisValue>(
&'a mut self,
name: K,
) -> RedisFuture<RV> {
Box::pin(async move {
value_from_pair(
&cmd("GRAPH.CONFIG")
.arg("GET")
.arg(name)
.query_async(self)
.await?,
)
})
}

fn graph_config_get_all<'a>(&'a mut self) -> RedisFuture<GraphConfig> {
Box::pin(async move {
cmd("GRAPH.CONFIG")
.arg("GET")
.arg("*")
.query_async(self)
.await
})
}
}

impl<T> AsyncGraphCommands for T where T: Send + ConnectionLike {}
47 changes: 46 additions & 1 deletion src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::types::*;
use redis::{cmd, ConnectionLike, RedisResult, ToRedisArgs};
use redis::{cmd, ConnectionLike, FromRedisValue, RedisResult, ToRedisArgs};

/// Provides a high level synchronous API to work with Redis graph data types.
/// The graph command becomes directly available on ConnectionLike types from
Expand Down Expand Up @@ -32,13 +32,58 @@ pub trait GraphCommands: ConnectionLike + Sized {
) -> RedisResult<GraphResultSet> {
cmd("GRAPH.QUERY").arg(key).arg(query).query(self)
}

fn graph_ro_query<K: ToRedisArgs, Q: ToRedisArgs>(
&mut self,
key: K,
query: Q,
) -> RedisResult<GraphResultSet> {
cmd("GRAPH.RO_QUERY").arg(key).arg(query).query(self)
}

fn graph_profile<K: ToRedisArgs, Q: ToRedisArgs, RV: FromRedisValue>(
&mut self,
key: K,
query: Q,
) -> RedisResult<RV> {
cmd("GRAPH.PROFILE").arg(key).arg(query).query(self)
}

fn graph_delete<K: ToRedisArgs>(&mut self, key: K) -> RedisResult<String> {
cmd("GRAPH.DELETE").arg(key).query(self)
}

fn graph_explain<K: ToRedisArgs, Q: ToRedisArgs, RV: FromRedisValue>(
&mut self,
key: K,
query: Q,
) -> RedisResult<RV> {
cmd("GRAPH.EXPLAIN").arg(key).arg(query).query(self)
}

fn graph_slowlog<K: ToRedisArgs>(&mut self, key: K) -> RedisResult<Vec<SlowLogEntry>> {
cmd("GRAPH.SLOWLOG").arg(key).query(self)
}

fn graph_config_set<K: ToRedisArgs, V: ToRedisArgs>(
&mut self,
name: K,
value: V,
) -> RedisResult<bool> {
cmd("GRAPH.CONFIG")
.arg("SET")
.arg(name)
.arg(value)
.query(self)
}

fn graph_config_get<K: ToRedisArgs, RV: FromRedisValue>(&mut self, name: K) -> RedisResult<RV> {
value_from_pair(&cmd("GRAPH.CONFIG").arg("GET").arg(name).query(self)?)
}

fn graph_config_get_all(&mut self) -> RedisResult<GraphConfig> {
cmd("GRAPH.CONFIG").arg("GET").arg("*").query(self)
}
}

impl<T> GraphCommands for T where T: ConnectionLike {}
Loading

0 comments on commit 4561fc1

Please sign in to comment.