-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgraph_commands.rs
802 lines (707 loc) · 27.1 KB
/
graph_commands.rs
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
use crate::{
client::{prepare_command, Client, PreparedCommand},
commands::{GraphCache, GraphValue, GraphValueArraySeed},
resp::{
cmd, CollectionResponse, Command, CommandArgs, KeyValueCollectionResponse,
PrimitiveResponse, RespBuf, RespDeserializer, SingleArg, ToArgs,
},
Error, Future, Result,
};
use serde::{
de::{self, DeserializeOwned, DeserializeSeed, Visitor},
Deserialize, Deserializer,
};
use smallvec::SmallVec;
use std::{collections::HashMap, fmt, future, str::FromStr};
/// A group of Redis commands related to [`RedisGraph`](https://redis.io/docs/stack/graph/)
///
/// # See Also
/// [RedisGraph Commands](https://redis.io/commands/?group=graph)
pub trait GraphCommands<'a> {
/// Retrieves the current value of a RedisGraph configuration parameter.
///
/// # Arguments
/// * `name` - name of the configuration parameter, or '*' for all.
///
/// # Return
/// Key/value collection holding names & values of the requested configs
///
/// # See Also
/// * [<https://redis.io/commands/graph.config-get/>](https://redis.io/commands/graph.config-get/)
/// * [`Configuration Parameters`](https://redis.io/docs/stack/graph/configuration/)
#[must_use]
fn graph_config_get<N, V, R>(self, name: impl SingleArg) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
N: PrimitiveResponse,
V: PrimitiveResponse,
R: KeyValueCollectionResponse<N, V>,
{
prepare_command(self, cmd("GRAPH.CONFIG").arg("GET").arg(name))
}
/// Set the value of a RedisGraph configuration parameter.
///
/// # Arguments
/// * `name` - name of the configuration option.
/// * `value` - value of the configuration option.
///
/// # See Also
/// * [<https://redis.io/commands/graph.config-set/>](https://redis.io/commands/graph.config-set/)
/// * [`Configuration Parameters`](https://redis.io/docs/stack/graph/configuration/)
///
/// # Note
/// As detailed in the link above, not all RedisGraph configuration parameters can be set at run-time.
#[must_use]
fn graph_config_set(
self,
name: impl SingleArg,
value: impl SingleArg,
) -> PreparedCommand<'a, Self, ()>
where
Self: Sized,
{
prepare_command(self, cmd("GRAPH.CONFIG").arg("SET").arg(name).arg(value))
}
/// Completely removes the graph and all of its entities.
///
/// # Arguments
/// * `graph` - name of the graph to delete.
///
/// # See Also
/// * [<https://redis.io/commands/graph.delete/>](https://redis.io/commands/graph.delete/)
#[must_use]
fn graph_delete(self, graph: impl SingleArg) -> PreparedCommand<'a, Self, String>
where
Self: Sized,
{
prepare_command(self, cmd("GRAPH.DELETE").arg(graph))
}
/// Constructs a query execution plan but does not run it.
///
/// Inspect this execution plan to better understand how your query will get executed.
///
/// # Arguments
/// * `graph` - graph name.
/// * `query` - query to explain.
///
/// # Return
/// String representation of a query execution plan
///
/// # See Also
/// * [<https://redis.io/commands/graph.explain/>](https://redis.io/commands/graph.explain/)
#[must_use]
fn graph_explain<R: PrimitiveResponse + DeserializeOwned, RR: CollectionResponse<R>>(
self,
graph: impl SingleArg,
query: impl SingleArg,
) -> PreparedCommand<'a, Self, RR>
where
Self: Sized,
{
prepare_command(self, cmd("GRAPH.EXPLAIN").arg(graph).arg(query))
}
/// Lists all graph keys in the keyspace.
///
/// # Return
/// String collection of graph names
///
/// # See Also
/// * [<https://redis.io/commands/graph.list/>](https://redis.io/commands/graph.list/)
#[must_use]
fn graph_list<R: PrimitiveResponse + DeserializeOwned, RR: CollectionResponse<R>>(
self,
) -> PreparedCommand<'a, Self, RR>
where
Self: Sized,
{
prepare_command(self, cmd("GRAPH.LIST"))
}
/// Executes a query and produces an execution plan augmented with metrics for each operation's execution.
///
/// # Arguments
/// * `graph` - graph name.
/// * `query`- query to profile
/// * `options` - See [`GraphQueryOptions`](GraphQueryOptions)
///
/// # Return
/// String representation of a query execution plan, with details on results produced by and time spent in each operation.
///
/// # See Also
/// * [<https://redis.io/commands/graph.list/>](https://redis.io/commands/graph.list/)
#[must_use]
fn graph_profile<R: PrimitiveResponse + DeserializeOwned, RR: CollectionResponse<R>>(
self,
graph: impl SingleArg,
query: impl SingleArg,
options: GraphQueryOptions,
) -> PreparedCommand<'a, Self, RR>
where
Self: Sized,
{
prepare_command(self, cmd("GRAPH.LIST").arg(graph).arg(query).arg(options))
}
/// Executes the given query against a specified graph.
///
/// # Arguments
/// * `graph` - graph name.
/// * `query`- query to execute
/// * `options` - See [`GraphQueryOptions`](GraphQueryOptions)
///
/// # Return
/// returns a [`result set`](GraphResultSet)
///
/// # See Also
/// * [<https://redis.io/commands/graph.query/>](https://redis.io/commands/graph.query/)
/// * [`openCypher query language`](https://opencypher.org/)
#[must_use]
fn graph_query(
self,
graph: impl SingleArg,
query: impl SingleArg,
options: GraphQueryOptions,
) -> PreparedCommand<'a, Self, GraphResultSet>
where
Self: Sized,
{
prepare_command(
self,
cmd("GRAPH.QUERY")
.arg(graph)
.arg(query)
.arg(options)
.arg("--compact"),
)
.custom_converter(Box::new(GraphResultSet::custom_conversion))
}
/// Executes a given read only query against a specified graph
///
/// # Arguments
/// * `graph` - graph name.
/// * `query`- query to execute
/// * `options` - See [`GraphQueryOptions`](GraphQueryOptions)
///
/// # Return
/// returns a [`result set`](GraphResultSet)
///
/// # See Also
/// * [<https://redis.io/commands/graph.ro_query/>](https://redis.io/commands/graph.ro_query/)
#[must_use]
fn graph_ro_query(
self,
graph: impl SingleArg,
query: impl SingleArg,
options: GraphQueryOptions,
) -> PreparedCommand<'a, Self, GraphResultSet>
where
Self: Sized,
{
prepare_command(
self,
cmd("GRAPH.RO_QUERY")
.arg(graph)
.arg(query)
.arg(options)
.arg("--compact"),
)
.custom_converter(Box::new(GraphResultSet::custom_conversion))
}
/// Returns a list containing up to 10 of the slowest queries issued against the given graph ID.
///
/// # Arguments
/// * `graph` - graph name.
///
/// # Return
/// A collection of GraphSlowlogResult
///
/// # See Also
/// * [<https://redis.io/commands/graph.slowlog/>](https://redis.io/commands/graph.slowlog/)
#[must_use]
fn graph_slowlog<R: CollectionResponse<GraphSlowlogResult>>(
self,
graph: impl SingleArg,
) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
{
prepare_command(self, cmd("GRAPH.SLOWLOG").arg(graph))
}
}
/// Options for the [`graph_query`](GraphCommands::graph_query) command
#[derive(Default)]
pub struct GraphQueryOptions {
command_args: CommandArgs,
}
impl GraphQueryOptions {
/// Timeout for the query in milliseconds
#[must_use]
pub fn timeout(timeout: u64) -> Self {
Self {
command_args: CommandArgs::default().arg("TIMEOUT").arg(timeout).build(),
}
}
}
impl ToArgs for GraphQueryOptions {
fn write_args(&self, args: &mut CommandArgs) {
args.arg(&self.command_args);
}
}
/// Result set for the [`graph_query`](GraphCommands::graph_query) command
#[derive(Debug, Deserialize)]
pub struct GraphResultSet {
pub header: GraphHeader,
pub rows: Vec<GraphResultRow>,
pub statistics: GraphQueryStatistics,
}
impl GraphResultSet {
pub(crate) fn custom_conversion(
resp_buffer: RespBuf,
command: Command,
client: &Client,
) -> Future<Self> {
let Some(graph_name) = command.args.iter().next() else {
return Box::pin(future::ready(Err(Error::Client(
"Cannot parse graph command".to_owned(),
))));
};
let Ok(graph_name) = std::str::from_utf8(graph_name) else {
return Box::pin(future::ready(Err(Error::Client(
"Cannot parse graph command".to_owned(),
))));
};
let graph_name = graph_name.to_owned();
Box::pin(async move {
let cache_key = format!("graph:{graph_name}");
let (cache_hit, num_node_labels, num_prop_keys, num_rel_types) = {
let client_state = client.get_client_state();
match client_state.get_state::<GraphCache>(&cache_key)? {
Some(cache) => {
let mut deserializer = RespDeserializer::new(&resp_buffer);
if cache.check_for_result(&mut deserializer)? {
(true, 0, 0, 0)
} else {
(
false,
cache.node_labels.len(),
cache.property_keys.len(),
cache.relationship_types.len(),
)
}
}
None => {
let cache = GraphCache::default();
let mut deserializer = RespDeserializer::new(&resp_buffer);
if cache.check_for_result(&mut deserializer)? {
(true, 0, 0, 0)
} else {
(false, 0, 0, 0)
}
}
}
};
if !cache_hit {
let (node_labels, prop_keys, rel_types) = Self::load_missing_ids(
&graph_name,
client,
num_node_labels,
num_prop_keys,
num_rel_types,
)
.await?;
let mut client_state = client.get_client_state_mut();
let cache = client_state.get_state_mut::<GraphCache>(&cache_key)?;
cache.update(
num_node_labels,
num_prop_keys,
num_rel_types,
node_labels,
prop_keys,
rel_types,
);
log::debug!("cache updated: {cache:?}");
} else if num_node_labels == 0 && num_prop_keys == 0 && num_rel_types == 0 {
// force cache creation
let mut client_state = client.get_client_state_mut();
client_state.get_state_mut::<GraphCache>(&cache_key)?;
log::debug!("graph cache created");
}
let mut deserializer = RespDeserializer::new(&resp_buffer);
Self::deserialize(&mut deserializer, client, &cache_key)
})
}
fn deserialize<'de, D>(
deserializer: D,
client: &Client,
cache_key: &str,
) -> std::result::Result<GraphResultSet, D::Error>
where
D: Deserializer<'de>,
{
struct GraphResultSetVisitor<'a, 'b> {
client: &'a Client,
cache_key: &'b str,
}
impl<'a, 'b, 'de> Visitor<'de> for GraphResultSetVisitor<'a, 'b> {
type Value = GraphResultSet;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("GraphResultSet")
}
fn visit_seq<A>(self, mut seq: A) -> std::result::Result<Self::Value, A::Error>
where
A: de::SeqAccess<'de>,
{
let Some(size) = seq.size_hint() else {
return Err(de::Error::custom(
"size hint is mandatory for GraphResultSet",
));
};
if size == 1 {
let Some(statistics) = seq.next_element::<GraphQueryStatistics>()? else {
return Err(de::Error::invalid_length(0, &"more elements in sequence"));
};
Ok(GraphResultSet {
header: Default::default(),
rows: Default::default(),
statistics,
})
} else {
let Some(header) = seq.next_element::<GraphHeader>()? else {
return Err(de::Error::invalid_length(0, &"more elements in sequence"));
};
let client_state = self.client.get_client_state();
let Ok(Some(cache)) = client_state.get_state::<GraphCache>(self.cache_key)
else {
return Err(de::Error::custom("Cannot find graph cache"));
};
let Some(rows) = seq.next_element_seed(GraphResultRowsSeed { cache })? else {
return Err(de::Error::invalid_length(1, &"more elements in sequence"));
};
let Some(statistics) = seq.next_element::<GraphQueryStatistics>()? else {
return Err(de::Error::invalid_length(2, &"more elements in sequence"));
};
Ok(GraphResultSet {
header,
rows,
statistics,
})
}
}
}
deserializer.deserialize_seq(GraphResultSetVisitor { client, cache_key })
}
async fn load_missing_ids(
graph_name: &str,
client: &Client,
num_node_labels: usize,
num_prop_keys: usize,
num_rel_types: usize,
) -> Result<(Vec<String>, Vec<String>, Vec<String>)> {
let mut pipeline = client.create_pipeline();
// node labels
pipeline.queue(cmd("GRAPH.QUERY").arg(graph_name.to_owned()).arg(format!(
"CALL db.labels() YIELD label RETURN label SKIP {}",
num_node_labels
)));
// property keys
pipeline.queue(cmd("GRAPH.QUERY").arg(graph_name.to_owned()).arg(format!(
"CALL db.propertyKeys() YIELD propertyKey RETURN propertyKey SKIP {}",
num_prop_keys
)));
// relationship types
pipeline.queue(cmd("GRAPH.QUERY").arg(graph_name.to_owned()).arg(format!(
"CALL db.relationshipTypes() YIELD relationshipType RETURN relationshipType SKIP {}",
num_rel_types
)));
let (MappingsResult(node_labels), MappingsResult(prop_keys), MappingsResult(rel_types)) =
pipeline
.execute::<(MappingsResult, MappingsResult, MappingsResult)>()
.await?;
Ok((node_labels, prop_keys, rel_types))
}
}
/// Result for Mappings
/// See: https://redis.io/docs/stack/graph/design/client_spec/#procedure-calls
struct MappingsResult(Vec<String>);
impl<'de> Deserialize<'de> for MappingsResult {
#[inline]
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct MappingsSeed;
impl<'de> DeserializeSeed<'de> for MappingsSeed {
type Value = Vec<String>;
#[inline]
fn deserialize<D>(self, deserializer: D) -> std::result::Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
struct MappingSeed;
impl<'de> DeserializeSeed<'de> for MappingSeed {
type Value = String;
#[inline]
fn deserialize<D>(
self,
deserializer: D,
) -> std::result::Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
struct MappingVisitor;
impl<'de> Visitor<'de> for MappingVisitor {
type Value = String;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("String")
}
fn visit_seq<A>(
self,
mut seq: A,
) -> std::result::Result<Self::Value, A::Error>
where
A: de::SeqAccess<'de>,
{
let Some(mapping) = seq.next_element::<String>()? else {
return Err(de::Error::invalid_length(
0,
&"more elements in sequence",
));
};
Ok(mapping)
}
}
deserializer.deserialize_seq(MappingVisitor)
}
}
struct MappingsVisitor;
impl<'de> Visitor<'de> for MappingsVisitor {
type Value = Vec<String>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("Vec<String>")
}
fn visit_seq<A>(self, mut seq: A) -> std::result::Result<Self::Value, A::Error>
where
A: de::SeqAccess<'de>,
{
let mut mappings = if let Some(size_hint) = seq.size_hint() {
Vec::with_capacity(size_hint)
} else {
Vec::new()
};
while let Some(mapping) = seq.next_element_seed(MappingSeed)? {
mappings.push(mapping);
}
Ok(mappings)
}
}
deserializer.deserialize_seq(MappingsVisitor)
}
}
struct MappingsResultVisitor;
impl<'de> Visitor<'de> for MappingsResultVisitor {
type Value = MappingsResult;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("MappingsResult")
}
fn visit_seq<A>(self, mut seq: A) -> std::result::Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let Some(_header) = seq.next_element::<Vec<String>>()? else {
return Err(de::Error::invalid_length(0, &"more elements in sequence"));
};
let Some(mappings) = seq.next_element_seed(MappingsSeed)? else {
return Err(de::Error::invalid_length(1, &"more elements in sequence"));
};
let Some(_stats) = seq.next_element::<Vec<String>>()? else {
return Err(de::Error::invalid_length(2, &"more elements in sequence"));
};
Ok(MappingsResult(mappings))
}
}
deserializer.deserialize_seq(MappingsResultVisitor)
}
}
/// Header part of a graph ['result set`](GraphResultSet)
#[derive(Debug, Default)]
pub struct GraphHeader {
pub column_names: Vec<String>,
}
impl<'de> Deserialize<'de> for GraphHeader {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let header = SmallVec::<[(u16, String); 10]>::deserialize(deserializer)?;
let column_names = header
.into_iter()
.map(|(_colmun_type, column_name)| column_name)
.collect();
Ok(Self { column_names })
}
}
/// Result row for the [`graph_query`](GraphCommands::graph_query) command
#[derive(Debug, Deserialize)]
pub struct GraphResultRow {
/// collection of values
///
/// each value matches a column name in the result set [`header`](GraphHeader)
pub values: Vec<GraphValue>,
}
pub struct GraphResultRowSeed<'a> {
cache: &'a GraphCache,
}
impl<'de, 'a> DeserializeSeed<'de> for GraphResultRowSeed<'a> {
type Value = GraphResultRow;
#[inline]
fn deserialize<D>(self, deserializer: D) -> std::result::Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
let values = GraphValueArraySeed { cache: self.cache }.deserialize(deserializer)?;
Ok(GraphResultRow { values })
}
}
struct GraphResultRowsSeed<'a> {
cache: &'a GraphCache,
}
impl<'de, 'a> Visitor<'de> for GraphResultRowsSeed<'a> {
type Value = Vec<GraphResultRow>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("Vec<GraphResultRow>")
}
fn visit_seq<A>(self, mut seq: A) -> std::result::Result<Self::Value, A::Error>
where
A: de::SeqAccess<'de>,
{
let mut rows = if let Some(size) = seq.size_hint() {
Vec::with_capacity(size)
} else {
Vec::new()
};
while let Some(row) = seq.next_element_seed(GraphResultRowSeed { cache: self.cache })? {
rows.push(row);
}
Ok(rows)
}
}
impl<'de, 'a> DeserializeSeed<'de> for GraphResultRowsSeed<'a> {
type Value = Vec<GraphResultRow>;
#[inline]
fn deserialize<D>(self, deserializer: D) -> std::result::Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_seq(self)
}
}
/// Statistics part of a graph ['result set`](GraphResultSet)
#[derive(Debug, Default)]
pub struct GraphQueryStatistics {
pub labels_added: usize,
pub labels_removed: usize,
pub nodes_created: usize,
pub nodes_deleted: usize,
pub properties_set: usize,
pub properties_removed: usize,
pub relationships_created: usize,
pub relationships_deleted: usize,
pub indices_created: usize,
pub indices_deleted: usize,
pub cached_execution: bool,
pub query_internal_execution_time: f64,
pub additional_statistics: HashMap<String, String>,
}
impl<'de> Deserialize<'de> for GraphQueryStatistics {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct GraphQueryStatisticsVisitor;
impl<'de> Visitor<'de> for GraphQueryStatisticsVisitor {
type Value = GraphQueryStatistics;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("GraphQueryStatistics")
}
fn visit_seq<A>(self, mut seq: A) -> std::result::Result<Self::Value, A::Error>
where
A: de::SeqAccess<'de>,
{
fn parse<'de, A, F>(value: &str) -> std::result::Result<F, A::Error>
where
A: de::SeqAccess<'de>,
F: FromStr,
{
match value.parse::<F>() {
Ok(value) => Ok(value),
Err(_) => Err(de::Error::custom(format!(
"Cannot parse GraphQueryStatistics: {value}"
))),
}
}
fn parse_query_execution_time<'de, A>(
value: &str,
) -> std::result::Result<f64, A::Error>
where
A: de::SeqAccess<'de>,
{
let Some((value, _milliseconds)) = value.split_once(' ') else {
return Err(de::Error::custom(
"Cannot parse GraphQueryStatistics (query exuction time)",
));
};
match value.parse::<f64>() {
Ok(value) => Ok(value),
Err(_) => Err(de::Error::custom(
"Cannot parse GraphQueryStatistics (query exuction time)",
)),
}
}
let mut stats = GraphQueryStatistics::default();
while let Some(str) = seq.next_element::<&str>()? {
let Some((name, value)) = str.split_once(": ") else {
return Err(de::Error::custom("Cannot parse GraphQueryStatistics"));
};
match name {
"Labels added" => stats.labels_added = parse::<A, _>(value)?,
"Labels removed" => stats.labels_removed = parse::<A, _>(value)?,
"Nodes created" => stats.nodes_created = parse::<A, _>(value)?,
"Nodes deleted:" => stats.nodes_deleted = parse::<A, _>(value)?,
"Properties set" => stats.properties_set = parse::<A, _>(value)?,
"Properties removed" => stats.properties_removed = parse::<A, _>(value)?,
"Relationships created" => {
stats.relationships_created = parse::<A, _>(value)?
}
"Relationships deleted" => {
stats.relationships_deleted = parse::<A, _>(value)?
}
"Indices created" => stats.indices_created = parse::<A, _>(value)?,
"Indices deleted" => stats.indices_deleted = parse::<A, _>(value)?,
"Cached execution" => stats.cached_execution = parse::<A, u8>(value)? != 0,
"Query internal execution time" => {
stats.query_internal_execution_time =
parse_query_execution_time::<A>(value)?
}
_ => {
stats
.additional_statistics
.insert(name.to_owned(), value.to_owned());
}
}
}
Ok(stats)
}
}
deserializer.deserialize_seq(GraphQueryStatisticsVisitor)
}
}
/// Result for the [`graph_slowlog`](GraphCommands::graph_slowlog) command
#[derive(Debug, Deserialize)]
pub struct GraphSlowlogResult {
/// A Unix timestamp at which the log entry was processed.
pub processing_time: u64,
/// The issued command.
pub issued_command: String,
/// The issued query.
pub issued_query: String,
/// The amount of time needed for its execution, in milliseconds.
pub execution_duration: f64,
}