Skip to content

Commit

Permalink
Merge pull request #9 from malte-v/path-support
Browse files Browse the repository at this point in the history
Support RedisGraph 2.1.0
  • Loading branch information
malte-v committed Nov 1, 2020
2 parents c5b43d4 + 739b1a8 commit 9b3f66b
Show file tree
Hide file tree
Showing 7 changed files with 544 additions and 72 deletions.
14 changes: 14 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{ rustDate ? "2020-11-01" }:

let
mozillaOverlay = import (builtins.fetchTarball "https://github.com/mozilla/nixpkgs-mozilla/archive/8c007b60731c07dd7a052cce508de3bb1ae849b4.tar.gz");
pkgs = import <nixpkgs> {
overlays = [ mozillaOverlay ];
};
rustChannel = pkgs.rustChannelOf { date = rustDate; channel = "nightly"; };
in pkgs.mkShell {
nativeBuildInputs = with pkgs; [
rustChannel.rust
rustfmt
];
}
31 changes: 27 additions & 4 deletions src/conversions.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::{
assignments::FromCell,
client_type_error,
result_set::{Node, Relation, Scalar},
result_set::{Edge, Node, Path, RawPath, Scalar},
RedisGraphError, RedisGraphResult, RedisString, ResultSet,
};
use std::convert::TryInto;

impl FromCell for Scalar {
fn from_cell(
Expand Down Expand Up @@ -157,13 +158,35 @@ impl FromCell for Node {
}
}

impl FromCell for Relation {
impl FromCell for Edge {
fn from_cell(
result_set: &ResultSet,
row_idx: usize,
column_idx: usize,
) -> RedisGraphResult<Self> {
let relation = result_set.get_relation(row_idx, column_idx)?;
Ok(relation.clone())
let edge = result_set.get_edge(row_idx, column_idx)?;
Ok(edge.clone())
}
}

impl FromCell for RawPath {
fn from_cell(
result_set: &ResultSet,
row_idx: usize,
column_idx: usize,
) -> RedisGraphResult<Self> {
let path = result_set.get_path(row_idx, column_idx)?;
Ok(path.clone())
}
}

impl FromCell for Path {
fn from_cell(
result_set: &ResultSet,
row_idx: usize,
column_idx: usize,
) -> RedisGraphResult<Self> {
let path = result_set.get_path(row_idx, column_idx)?;
path.clone().try_into()
}
}
40 changes: 20 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
#![doc(html_logo_url = "https://oss.redislabs.com/redisgraph/images/logo_small.png")]

//! # redisgraph-rs
//!
//!
//! `redisgraph-rs` is an idiomatic Rust client for RedisGraph, the graph database by Redis.
//!
//!
//! This crate parses responses from RedisGraph and converts them into ordinary Rust values.
//! It exposes a very flexible API that allows you to retrieve a single value, a single record
//! or multiple records using only one function: [`Graph::query`](graph/struct.Graph.html#method.query).
//!
//!
//! If you want to use this crate, add this to your Cargo.toml:
//!
//!
//! ```ini
//! [dependencies]
//! redis = "0.15.1"
//! redisgraph = "0.1.0"
//! ```
//!
//!
//! **Warning**: This library has not been thoroughly tested yet and some features are still missing.
//! Expect bugs and breaking changes.
//!
//!
//! ## Resources
//!
//!
//! - RedisGraph documentation: [redisgraph.io][]
//! - API Reference: [docs.rs/redisgraph]
//!
//!
//! ## Example
//!
//!
//! First, run RedisGraph on your machine using
//!
//!
//! ```sh
//! $ docker run --name redisgraph-test -d --rm -p 6379:6379 redislabs/redisgraph
//! ```
//!
//!
//! Then, try out this code:
//!
//!
//! ```rust
//! use redis::Client;
//! use redisgraph::{Graph, RedisGraphResult};
//!
//!
//! fn main() -> RedisGraphResult<()> {
//! let client = Client::open("redis://127.0.0.1")?;
//! let client = Client::open(option_env!("TEST_REDIS_URI").unwrap_or("redis://127.0.0.1"))?;
//! let mut connection = client.get_connection()?;
//!
//!
//! let mut graph = Graph::open(connection, "MotoGP".to_string())?;
//!
//!
//! // Create six nodes (three riders, three teams) and three relationships between them.
//! graph.mutate("CREATE (:Rider {name: 'Valentino Rossi', birth_year: 1979})-[:rides]->(:Team {name: 'Yamaha'}), \
//! (:Rider {name:'Dani Pedrosa', birth_year: 1985, height: 1.58})-[:rides]->(:Team {name: 'Honda'}), \
//! (:Rider {name:'Andrea Dovizioso', birth_year: 1986, height: 1.67})-[:rides]->(:Team {name: 'Ducati'})")?;
//!
//!
//! // Get the names and birth years of all riders in team Yamaha.
//! let results: Vec<(String, u32)> = graph.query("MATCH (r:Rider)-[:rides]->(t:Team) WHERE t.name = 'Yamaha' RETURN r.name, r.birth_year")?;
//! // Since we know just one rider in our graph rides for team Yamaha,
Expand All @@ -57,14 +57,14 @@
//! // Let's now get all the data about the riders we have.
//! // Be aware of that we only know the height of some riders, and therefore we use an `Option`:
//! let results: Vec<(String, u32, Option<f32>)> = graph.query("MATCH (r:Rider) RETURN r.name, r.birth_year, r.height")?;
//!
//!
//! // That was just a demo; we don't need this graph anymore. Let's delete it from the database:
//! graph.delete()?;
//!
//!
//! Ok(())
//! }
//! ```
//!
//!
//! [redisgraph.io]:https://redisgraph.io
//! [docs.rs/redisgraph]:https://docs.rs/redisgraph

Expand Down
Loading

0 comments on commit 9b3f66b

Please sign in to comment.