-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquery_result.rb
171 lines (148 loc) · 5.07 KB
/
query_result.rb
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
# frozen_string_literal: true
# Data types that can be returned in result sets
module ValueType
UNKNOWN = 0
NULL = 1
STRING = 2
INTEGER = 3
BOOLEAN = 4
DOUBLE = 5
ARRAY = 6
EDGE = 7
NODE = 8
PATH = 9 # TODO: not yet implemented
end
class QueryResult
attr_accessor :columns
attr_accessor :resultset
attr_accessor :stats
def initialize(response, opts = {})
# The response for any query is expected to be a nested array.
# If compact (RedisGraph protocol v2)
# The resultset is an array w/ three elements:
# 0] Node/Edge key names w/ the ordinal position used in [1]
# en lieu of the name to compact the result set.
# 1] Node/Edge key/value pairs as an array w/ two elements:
# 0] node/edge name id from [0]
# 1..matches] node/edge values
# 2] Statistics as an array of strings
@metadata = opts[:metadata]
@resultset = parse_resultset(response)
@stats = parse_stats(response)
end
def print_resultset
return unless columns
# Compute max length of each column
column_sizes = resultset.reduce([]) do |lengths, row|
row.each_with_index.map{|iterand, index| [lengths[index] || 0, iterand.to_s.length].max}
end
# Print column headers
puts head = '-' * (column_sizes.inject(&:+) + (3 * column_sizes.count) + 1)
row = columns.fill(nil, columns.size..(column_sizes.size - 1))
row = row.each_with_index.map{|v, i| v = v.to_s + ' ' * (column_sizes[i] - v.to_s.length)}
puts '| ' + row.join(' | ') + ' |'
puts head
# Print result set rows
resultset.each do |row|
row = row.fill(nil, row.size..(column_sizes.size - 1))
row = row.each_with_index.map{|v, i| v = v.to_s + ' ' * (column_sizes[i] - v.to_s.length)}
puts '| ' + row.join(' | ') + ' |'
end
puts head
end
def parse_resultset(response)
# In the v2 protocol, CREATE does not contain an empty row preceding statistics
return unless response.length > 1
# Any non-empty result set will have multiple rows (arrays)
# First row is header describing the returned records, corresponding
# precisely in order and naming to the RETURN clause of the query.
header = response[0]
@columns = header.map { |(_type, name)| name }
# Second row is the actual data returned by the query
# note handling for encountering an id for propertyKey that is out of
# the cached set.
data = response[1].map do |row|
i = -1
header.reduce([]) do |agg, (type, _it)|
i += 1
el = row[i]
case type
when 1 # Column of scalars
agg << map_scalar(el[0], el[1])
when 2 # node
props = el[2]
agg << props.sort_by { |prop| prop[0] }.map { |prop| map_prop(prop) }
when 3 # Column of relations
props = el[4]
agg << props.sort_by { |prop| prop[0] }.map { |prop| map_prop(prop) }
end
end
end
data
end
def map_scalar(type, val)
map_func = case type
when ValueType::NULL
return nil
when ValueType::STRING
:to_s
when ValueType::INTEGER
:to_i
when ValueType::BOOLEAN
# no :to_b
return val == 'true'
when ValueType::DOUBLE
:to_f
when ValueType::ARRAY
return val.map { |it| map_scalar(it[0], it[1]) }
when ValueType::EDGE
props = val[4]
return props.sort_by { |prop| prop[0] }.map { |prop| map_prop(prop) }
when ValueType::NODE
props = val[2]
return props.sort_by { |prop| prop[0] }.map { |prop| map_prop(prop) }
end
val.send(map_func)
end
def map_prop(prop)
# maximally a single @metadata.invalidate should occur
property_keys = @metadata.property_keys
prop_index = prop[0]
if prop_index >= property_keys.length
@metadata.invalidate
property_keys = @metadata.property_keys
end
{ property_keys[prop_index] => map_scalar(prop[1], prop[2]) }
end
# Read metrics about internal query handling
def parse_stats(response)
# In the v2 protocol, CREATE does not contain an empty row preceding statistics
stats_offset = response.length == 1 ? 0 : 2
return nil unless response[stats_offset]
parse_stats_row(response[stats_offset])
end
def parse_stats_row(response_row)
stats = {}
response_row.each do |stat|
line = stat.split(': ')
val = line[1].split(' ')[0].to_i
case line[0]
when /^Labels added/
stats[:labels_added] = val
when /^Nodes created/
stats[:nodes_created] = val
when /^Nodes deleted/
stats[:nodes_deleted] = val
when /^Relationships deleted/
stats[:relationships_deleted] = val
when /^Properties set/
stats[:properties_set] = val
when /^Relationships created/
stats[:relationships_created] = val
when /^Query internal execution time/
stats[:internal_execution_time] = val
end
end
stats
end
end