Skip to content

Commit 50f39ff

Browse files
Fix property keys invalidation on accessing new entities (#7)
1 parent daf7c70 commit 50f39ff

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

lib/redisgraph.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def initialize(opts = {})
2323
end
2424

2525
def invalidate
26-
@labels = @property_key = @relationship_types
26+
@labels = @property_keys = @relationship_types = nil
2727
end
2828

2929
def labels
@@ -57,7 +57,7 @@ def initialize(graph, redis_options = {})
5757

5858
# Execute a command and return its parsed result
5959
def query(command)
60-
resp = @connection.call("GRAPH.QUERY", @graphname, command, '--compact')
60+
resp = @connection.call('GRAPH.QUERY', @graphname, command, '--compact')
6161
QueryResult.new(resp,
6262
metadata: @metadata)
6363
rescue Redis::CommandError => e
@@ -66,14 +66,14 @@ def query(command)
6666

6767
# Return the execution plan for a given command
6868
def explain(command)
69-
@connection.call("GRAPH.EXPLAIN", @graphname, command)
69+
@connection.call('GRAPH.EXPLAIN', @graphname, command)
7070
rescue Redis::CommandError => e
7171
raise ExplainError, e
7272
end
7373

7474
# Delete the graph and all associated keys
7575
def delete
76-
@connection.call("GRAPH.DELETE", @graphname)
76+
@connection.call('GRAPH.DELETE', @graphname)
7777
rescue Redis::CommandError => e
7878
raise DeleteError, e
7979
end

lib/redisgraph/query_result.rb

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
# Data types that can be returned in result sets
4+
module ValueType
5+
UNKNOWN = 0
6+
NULL = 1
7+
STRING = 2
8+
INTEGER = 3
9+
BOOLEAN = 4
10+
DOUBLE = 5
11+
ARRAY = 6
12+
EDGE = 7
13+
NODE = 8
14+
PATH = 9 # TODO: not yet implemented
15+
end
16+
117
class QueryResult
218
attr_accessor :columns
319
attr_accessor :resultset
@@ -21,6 +37,8 @@ def initialize(response, opts = {})
2137
end
2238

2339
def print_resultset
40+
return unless columns
41+
2442
pretty = Terminal::Table.new headings: columns do |t|
2543
resultset.each { |record| t << record }
2644
end
@@ -33,7 +51,6 @@ def parse_resultset(response)
3351

3452
# Any non-empty result set will have multiple rows (arrays)
3553

36-
3754
# First row is header describing the returned records, corresponding
3855
# precisely in order and naming to the RETURN clause of the query.
3956
header = response[0]
@@ -47,19 +64,7 @@ def parse_resultset(response)
4764
header.reduce([]) do |agg, (type, _it)|
4865
i += 1
4966
el = row[i]
50-
51-
case type
52-
when 1 # scalar
53-
agg << map_scalar(el[0], el[1])
54-
when 2 # node
55-
props = el[2]
56-
agg << props.sort_by { |prop| prop[0] }.map { |prop| map_prop(prop) }
57-
when 3 # relation
58-
props = el[4]
59-
agg << props.sort_by { |prop| prop[0] }.map { |prop| map_prop(prop) }
60-
end
61-
62-
agg
67+
agg << map_scalar(el[0], el[1])
6368
end
6469
end
6570

@@ -68,25 +73,23 @@ def parse_resultset(response)
6873

6974
def map_scalar(type, val)
7075
map_func = case type
71-
when 1 # null
76+
when ValueType::NULL
7277
return nil
73-
when 2 # string
78+
when ValueType::STRING
7479
:to_s
75-
when 3 # integer
80+
when ValueType::INTEGER
7681
:to_i
77-
when 4 # boolean
82+
when ValueType::BOOLEAN
7883
# no :to_b
79-
return val == "true"
80-
when 5 # double
84+
return val == 'true'
85+
when ValueType::DOUBLE
8186
:to_f
82-
# TODO: when in the distro packages and docker images,
83-
# the following _should_ work
84-
# when 6 # array
85-
# val.map { |it| map_scalar(it[0], it[1]) }
86-
when 7 # relation
87+
when ValueType::ARRAY
88+
return val.map { |it| map_scalar(it[0], it[1]) }
89+
when ValueType::EDGE
8790
props = val[4]
8891
return props.sort_by { |prop| prop[0] }.map { |prop| map_prop(prop) }
89-
when 8 # node
92+
when ValueType::NODE
9093
props = val[2]
9194
return props.sort_by { |prop| prop[0] }.map { |prop| map_prop(prop) }
9295
end
@@ -98,7 +101,7 @@ def map_prop(prop)
98101

99102
property_keys = @metadata.property_keys
100103
prop_index = prop[0]
101-
if prop_index > property_keys.length
104+
if prop_index >= property_keys.length
102105
@metadata.invalidate
103106
property_keys = @metadata.property_keys
104107
end

0 commit comments

Comments
 (0)