redisgraph 0.5.0

A Rust client for RedisGraph.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
use std::collections::HashMap;
use std::mem;
use std::str;

use num::FromPrimitive;
use redis::{FromRedisValue, Value};
use serde::{Serialize, Deserialize};

use crate::{server_type_error, Graph, RedisGraphError, RedisGraphResult};

/// Implemented by types that can be contructed from a
/// Redis [`Value`](https://docs.rs/redis/0.15.1/redis/enum.Value.html) and a [`Graph`](../graph/struct.Graph.html)
pub trait FromRedisValueWithGraph: Sized {
    fn from_redis_value_with_graph(value: Value, graph: &Graph) -> RedisGraphResult<Self>;
}

impl<T: FromRedisValue> FromRedisValueWithGraph for T {
    fn from_redis_value_with_graph(value: Value, _graph: &Graph) -> RedisGraphResult<T> {
        T::from_redis_value(&value).map_err(RedisGraphError::from)
    }
}

/// A result set returned by RedisGraph in response to a query.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ResultSet {
    /// The columns of this result set.
    ///     
    /// Empty if the response did not contain any return values.
    pub columns: Vec<Column>,
    /// Contains statistics messages from the response.
    pub statistics: Statistics,
}

/// Statistics returned by RedisGraph about a query as a list of messages.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Statistics(pub Vec<String>);

impl ResultSet {
    /// Returns the number of rows in the result set.
    pub fn num_columns(&self) -> usize {
        self.columns.len()
    }

    /// Returns the number of columns in the result set.
    pub fn num_rows(&self) -> usize {
        match self.columns.get(0) {
            Some(first_column) => first_column.len(),
            None => 0,
        }
    }

    /// Returns the scalar at the given position.
    ///
    /// Returns an error if the value at the given position is not a scalar
    /// or if the position is out of bounds.
    pub fn get_scalar(&self, row_idx: usize, column_idx: usize) -> RedisGraphResult<&Scalar> {
        match self.columns.get(column_idx) {
            Some(column) => match column {
                Column::Scalars(cells) => match cells.get(row_idx) {
                    Some(cell) => Ok(cell),
                    None => client_type_error!(
                        "failed to get scalar: row index out of bounds: the len is {:?} but the index is {:?}", self.columns.len(), column_idx,
                    ),
                },
                any => client_type_error!(
                    "failed to get scalar: expected column of scalars, found {:?}",
                    any
                ),
            }
            None => client_type_error!(
                "failed to get scalar: column index out of bounds: the len is {:?} but the index is {:?}", self.columns.len(), column_idx,
            ),
        }
    }

    /// Returns the node at the given position.
    ///
    /// Returns an error if the value at the given position is not a node
    /// or if the position is out of bounds.
    pub fn get_node(&self, row_idx: usize, column_idx: usize) -> RedisGraphResult<&Node> {
        match self.columns.get(column_idx) {
            Some(column) => match column {
                Column::Nodes(cells) => match cells.get(row_idx) {
                    Some(cell) => Ok(cell),
                    None => client_type_error!(
                        "failed to get node: row index out of bounds: the len is {:?} but the index is {:?}", self.columns.len(), column_idx,
                    ),
                },
                any => client_type_error!(
                    "failed to get node: expected column of nodes, found {:?}",
                    any
                ),
            }
            None => client_type_error!(
                "failed to get node: column index out of bounds: the len is {:?} but the index is {:?}", self.columns.len(), column_idx,
            ),
        }
    }

    /// Returns the relation at the given position.
    ///
    /// Returns an error if the value at the given position is not a relation
    /// or if the position is out of bounds.
    pub fn get_relation(&self, row_idx: usize, column_idx: usize) -> RedisGraphResult<&Relation> {
        match self.columns.get(column_idx) {
            Some(column) => match column {
                Column::Relations(cells) => match cells.get(row_idx) {
                    Some(cell) => Ok(cell),
                    None => client_type_error!(
                        "failed to get relation: row index out of bounds: the len is {:?} but the index is {:?}", self.columns.len(), column_idx,
                    ),
                },
                any => client_type_error!(
                    "failed to get relation: expected column of relations, found {:?}",
                    any
                ),
            }
            None => client_type_error!(
                "failed to get relation: column index out of bounds: the len is {:?} but the index is {:?}", self.columns.len(), column_idx,
            ),
        }
    }
}

/// A single column of the result set.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Column {
    Scalars(Vec<Scalar>),
    Nodes(Vec<Node>),
    Relations(Vec<Relation>),
}

impl Column {
    /// Returns the lenghth of this column.
    pub fn len(&self) -> usize {
        match self {
            Self::Scalars(cells) => cells.len(),
            Self::Nodes(cells) => cells.len(),
            Self::Relations(cells) => cells.len(),
        }
    }

    /// Returns `true` if this column is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

#[derive(num_derive::FromPrimitive)]
enum ColumnType {
    Unknown = 0,
    Scalar = 1,
    Node = 2,
    Relation = 3,
}

impl FromRedisValueWithGraph for ResultSet {
    fn from_redis_value_with_graph(value: Value, graph: &Graph) -> RedisGraphResult<Self> {
        match value {
            Value::Bulk(mut values) => {
                match values.len() {
                    3 => {
                        let header_row = values[0].take();
                        let result_rows = values[1].take();
                        let statistics = values[2].take();

                        match header_row {
                            Value::Bulk(header_row) => {
                                let column_count = header_row.len();
                                let mut columns = Vec::<Column>::with_capacity(column_count);

                                // `result_table[0][1]` is row 0, column 1
                                let mut result_table: Vec<Vec<Value>> = match result_rows {
                                    Value::Bulk(rows) => rows
                                        .into_iter()
                                        .map(|row| match row {
                                            Value::Bulk(row) => Ok(row),
                                            _ => server_type_error!(
                                                "expected array as result row representation",
                                            ),
                                        })
                                        .collect::<RedisGraphResult<Vec<Vec<Value>>>>(),
                                    _ => server_type_error!(
                                        "expected array as result table representation",
                                    ),
                                }?;

                                for i in 0..column_count {
                                    match &header_row[i] {
                                        Value::Bulk(header_cell) => {
                                            let column_type_i64 = match header_cell[0] {
                                                Value::Int(column_type_i64) => column_type_i64,
                                                _ => {
                                                    return server_type_error!(
                                                        "expected integer as column type",
                                                    )
                                                }
                                            };

                                            let column = match ColumnType::from_i64(column_type_i64) {
                                                Some(ColumnType::Unknown) => server_type_error!("column type is unknown"),
                                                Some(ColumnType::Scalar) => Ok(Column::Scalars(
                                                    result_table
                                                        .iter_mut()
                                                        .map(|row| {
                                                            Scalar::from_redis_value_with_graph(row[i].take(), graph)
                                                                .map_err(RedisGraphError::from)
                                                        })
                                                        .collect::<RedisGraphResult<Vec<Scalar>>>()?,
                                                )),
                                                Some(ColumnType::Node) => Ok(Column::Nodes(
                                                    result_table
                                                        .iter_mut()
                                                        .map(|row| {
                                                            Node::from_redis_value_with_graph(row[i].take(), graph)
                                                                .map_err(RedisGraphError::from)
                                                        })
                                                        .collect::<RedisGraphResult<Vec<Node>>>()?,
                                                )),
                                                Some(ColumnType::Relation) => Ok(Column::Relations(
                                                    result_table
                                                        .iter_mut()
                                                        .map(|row| {
                                                            Relation::from_redis_value_with_graph(row[i].take(), graph)
                                                                .map_err(RedisGraphError::from)
                                                        })
                                                        .collect::<RedisGraphResult<Vec<Relation>>>()?,
                                                )),
                                                None => server_type_error!("expected integer between 0 and 3 as column type")
                                            }?;

                                            columns.push(column);
                                        }
                                        _ => {
                                            return server_type_error!(
                                                "expected array as header cell representation",
                                            )
                                        }
                                    }
                                }

                                if let Some(first_column) = columns.get(0) {
                                    if !columns
                                        .iter()
                                        .all(|column| column.len() == first_column.len())
                                    {
                                        return server_type_error!(
                                            "result columns have unequal lengths",
                                        );
                                    }
                                }

                                let statistics = parse_statistics(statistics)?;

                                Ok(Self {
                                    columns,
                                    statistics,
                                })
                            }
                            _ => server_type_error!("expected array as header row representation",),
                        }
                    }
                    1 => {
                        let statistics = parse_statistics(values[0].take())?;

                        Ok(Self {
                            columns: Vec::new(),
                            statistics,
                        })
                    }
                    _ => server_type_error!(
                        "expected array of size 3 or 1 as result set representation",
                    ),
                }
            }
            _ => server_type_error!("expected array as result set representation"),
        }
    }
}

fn parse_statistics(value: Value) -> RedisGraphResult<Statistics> {
    match value {
        Value::Bulk(statistics) => statistics
            .into_iter()
            .map(|entry| match entry {
                Value::Data(utf8) => {
                    String::from_utf8(utf8).map_err(|_| RedisGraphError::InvalidUtf8)
                }
                _ => server_type_error!("expected string as statistics entry"),
            })
            .collect::<RedisGraphResult<Vec<String>>>()
            .map(Statistics),
        _ => server_type_error!("expected array as statistics list"),
    }
}

/// A scalar value returned by RedisGraph.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Scalar {
    Nil,
    Boolean(bool),
    Integer(i64),
    Double(f64),
    String(RedisString),
}

/// Implemented for Redis types with a nil-like variant.
pub trait Take {
    /// Takes the value, leaving the "nil" variant in its place.
    fn take(&mut self) -> Self;
}

impl Take for Value {
    fn take(&mut self) -> Self {
        mem::replace(self, Self::Nil)
    }
}

impl Take for Scalar {
    fn take(&mut self) -> Self {
        mem::replace(self, Self::Nil)
    }
}

/// A string returned by Redis.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RedisString(pub Vec<u8>);

impl From<String> for RedisString {
    fn from(string: String) -> Self {
        Self(string.into_bytes())
    }
}

impl From<Vec<u8>> for RedisString {
    fn from(bytes: Vec<u8>) -> Self {
        Self(bytes)
    }
}

impl From<RedisString> for Vec<u8> {
    fn from(redis_string: RedisString) -> Self {
        redis_string.0
    }
}

#[derive(num_derive::FromPrimitive)]
enum ScalarType {
    Unknown = 0,
    Nil = 1,
    String = 2,
    Integer = 3,
    Boolean = 4,
    Double = 5,
}

impl FromRedisValueWithGraph for Scalar {
    fn from_redis_value_with_graph(value: Value, _graph: &Graph) -> RedisGraphResult<Self> {
        match value {
            Value::Bulk(mut values) => {
                if values.len() == 2 {
                    let scalar_type = values[0].take();
                    let scalar_value = values[1].take();
                    match scalar_type {
                        Value::Int(scalar_type_int) => match ScalarType::from_i64(scalar_type_int) {
                            Some(ScalarType::Unknown) => server_type_error!("scalar type is unknown"),
                            Some(ScalarType::Nil) => Ok(Scalar::Nil),
                            Some(ScalarType::String) => match scalar_value {
                                Value::Data(string_data) => Ok(Scalar::String(RedisString(string_data))),
                                _ => server_type_error!("expected binary data as scalar value (scalar type is string)")
                            },
                            Some(ScalarType::Integer) => match scalar_value {
                                Value::Int(integer) => Ok(Scalar::Integer(integer)),
                                _ => server_type_error!("expected integer as scalar value (scalar type is integer)")
                            },
                            Some(ScalarType::Boolean) => match scalar_value {
                                Value::Data(bool_data) => match &bool_data[..] {
                                    b"true" => Ok(Scalar::Boolean(true)),
                                    b"false" => Ok(Scalar::Boolean(false)),
                                    _ => server_type_error!("expected either \"true\" or \"false\" as scalar value (scalar type is boolean)")
                                }
                                _ => server_type_error!("expected binary data as scalar value (scalar type is boolean)")
                            },
                            Some(ScalarType::Double) => match scalar_value {
                                Value::Data(double_data) => match str::from_utf8(&double_data[..]) {
                                    Ok(double_string) => match double_string.parse::<f64>() {
                                        Ok(double) => Ok(Scalar::Double(double)),
                                        Err(_) => server_type_error!("expected string representation of double as scalar value (scalar type is double)")
                                    },
                                    Err(_) => Err(RedisGraphError::InvalidUtf8),
                                }
                                _ => server_type_error!("expected string representating a double as scalar value (scalar type is double)")
                            }
                            None => server_type_error!("expected integer between 0 and 5 (scalar type) as first element of scalar array")
                        },
                        _ => server_type_error!("expected integer representing scalar type as first element of scalar array")
                    }
                } else {
                    server_type_error!("expected array of size 2 as scalar representation")
                }
            }
            _ => server_type_error!("expected array as scalar representation"),
        }
    }
}

/// A node returned by RedisGraph.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Node {
    /// The labels attached to this node.
    pub labels: Vec<RedisString>,
    /// The properties of this node.
    pub properties: HashMap<RedisString, Scalar>,
}

impl FromRedisValueWithGraph for Node {
    fn from_redis_value_with_graph(value: Value, graph: &Graph) -> RedisGraphResult<Self> {
        match value {
            Value::Bulk(mut values) => {
                if values.len() == 3 {
                    let label_ids = values[1].take();
                    let properties = values[2].take();

                    let graph_labels = graph.labels();
                    let labels = match label_ids {
                        Value::Bulk(label_ids) => label_ids
                            .iter()
                            .map(|label_id| {
                                let label_id = match label_id {
                                    Value::Int(id) => id,
                                    _ => return server_type_error!("expected integer as label ID",),
                                };

                                graph_labels
                                    .get(*label_id as usize)
                                    .cloned()
                                    .ok_or(RedisGraphError::LabelNotFound)
                            })
                            .collect::<RedisGraphResult<Vec<RedisString>>>()?,
                        _ => return server_type_error!("expected array as label IDs"),
                    };

                    let properties = parse_properties(graph, properties)?;

                    Ok(Self { labels, properties })
                } else {
                    server_type_error!("expected array of size 3 as node representation")
                }
            }
            _ => server_type_error!("expected array as node representation"),
        }
    }
}

/// A relation returned by RedisGraph.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Relation {
    /// The type name of this relation. 
    pub type_name: RedisString,
    /// The properties of this relation.
    pub properties: HashMap<RedisString, Scalar>,
}

impl FromRedisValueWithGraph for Relation {
    fn from_redis_value_with_graph(value: Value, graph: &Graph) -> RedisGraphResult<Self> {
        match value {
            Value::Bulk(mut values) => {
                if values.len() == 5 {
                    let type_id = values[1].take();
                    let properties = values[4].take();

                    let type_name = match type_id {
                        Value::Int(id) => graph
                            .relationship_types()
                            .get(id as usize)
                            .cloned()
                            .ok_or(RedisGraphError::RelationshipTypeNotFound)?,
                        _ => return server_type_error!("expected integer as relationship type ID",),
                    };

                    let properties = parse_properties(graph, properties)?;

                    Ok(Self {
                        type_name,
                        properties,
                    })
                } else {
                    server_type_error!("expected array of size 5 as relationship representation",)
                }
            }
            _ => server_type_error!("expected array as relationship representation"),
        }
    }
}

fn parse_properties(
    graph: &Graph,
    properties: Value,
) -> RedisGraphResult<HashMap<RedisString, Scalar>> {
    let graph_property_keys = graph.property_keys();
    match properties {
        Value::Bulk(properties) => properties
            .into_iter()
            .map(|property| match property {
                Value::Bulk(mut property) => {
                    if property.len() == 3 {
                        let property_key_id = property[0].take();
                        let property_type = property[1].take();
                        let property_value = property[2].take();

                        let property_key = match property_key_id {
                            Value::Int(id) => graph_property_keys
                                .get(id as usize)
                                .cloned()
                                .ok_or(RedisGraphError::PropertyKeyNotFound)?,
                            _ => return server_type_error!("expected integer as property key ID",),
                        };

                        let property_value = Scalar::from_redis_value_with_graph(
                            Value::Bulk(vec![property_type, property_value]),
                            graph,
                        )?;

                        Ok((property_key, property_value))
                    } else {
                        server_type_error!("expected array of size 3 as properties representation",)
                    }
                }
                _ => server_type_error!("expected array as properties representation"),
            })
            .collect::<RedisGraphResult<HashMap<RedisString, Scalar>>>(),
        _ => server_type_error!("expected array as properties representation"),
    }
}