Class: QueryResult
- Inherits:
-
Object
- Object
- QueryResult
- Defined in:
- lib/redisgraph/query_result.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#resultset ⇒ Object
Returns the value of attribute resultset.
-
#stats ⇒ Object
Returns the value of attribute stats.
Instance Method Summary collapse
-
#initialize(response, opts = {}) ⇒ QueryResult
constructor
A new instance of QueryResult.
- #map_prop(prop) ⇒ Object
- #map_scalar(type, val) ⇒ Object
- #parse_resultset(response) ⇒ Object
-
#parse_stats(response) ⇒ Object
Read metrics about internal query handling.
- #parse_stats_row(response_row) ⇒ Object
- #print_resultset ⇒ Object
Constructor Details
#initialize(response, opts = {}) ⇒ QueryResult
Returns a new instance of QueryResult.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/redisgraph/query_result.rb', line 6 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 = opts[:metadata] @resultset = parse_resultset(response) @stats = parse_stats(response) end |
Instance Attribute Details
#columns ⇒ Object
Returns the value of attribute columns.
2 3 4 |
# File 'lib/redisgraph/query_result.rb', line 2 def columns @columns end |
#resultset ⇒ Object
Returns the value of attribute resultset.
3 4 5 |
# File 'lib/redisgraph/query_result.rb', line 3 def resultset @resultset end |
#stats ⇒ Object
Returns the value of attribute stats.
4 5 6 |
# File 'lib/redisgraph/query_result.rb', line 4 def stats @stats end |
Instance Method Details
#map_prop(prop) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/redisgraph/query_result.rb', line 90 def map_prop(prop) # maximally a single @metadata.invalidate should occur property_keys = .property_keys prop_index = prop[0] if prop_index > property_keys.length .invalidate property_keys = .property_keys end { property_keys[prop_index] => map_scalar(prop[1], prop[2]) } end |
#map_scalar(type, val) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/redisgraph/query_result.rb', line 69 def map_scalar(type, val) map_func = case type when 1 # null return nil when 2 # string :to_s when 3 # integer :to_i when 4 # boolean # no :to_b return val == "true" when 5 # double :to_f # TODO: when in the distro packages and docker images, # the following _should_ work # when 6 # array # val.map { |it| map_scalar(it[0], it[1]) } end val.send(map_func) end |
#parse_resultset(response) ⇒ Object
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 |
# File 'lib/redisgraph/query_result.rb', line 30 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 # scalar 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 # relation props = el[4] agg << props.sort_by { |prop| prop[0] }.map { |prop| map_prop(prop) } end agg end end data end |
#parse_stats(response) ⇒ Object
Read metrics about internal query handling
103 104 105 106 107 108 109 110 |
# File 'lib/redisgraph/query_result.rb', line 103 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 |
#parse_stats_row(response_row) ⇒ Object
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 |
# File 'lib/redisgraph/query_result.rb', line 112 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 |
#print_resultset ⇒ Object
23 24 25 26 27 28 |
# File 'lib/redisgraph/query_result.rb', line 23 def print_resultset pretty = Terminal::Table.new headings: columns do |t| resultset.each { |record| t << record } end puts pretty end |