Class: RedisGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/redisgraph.rb,
lib/redisgraph/errors.rb,
lib/redisgraph/version.rb,
lib/redisgraph/connection.rb

Defined Under Namespace

Classes: CallError, DeleteError, ExplainError, Metadata, QueryError, RedisGraphError, ServerError

Constant Summary collapse

VERSION =
'2.0.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, redis_options = {}) ⇒ RedisGraph

The RedisGraph constructor instantiates a Redis connection and validates that the graph module is loaded



51
52
53
54
55
56
# File 'lib/redisgraph.rb', line 51

def initialize(graph, redis_options = {})
  @graphname = graph
  connect_to_server(redis_options)
  @metadata = Metadata.new(graphname: @graphname,
                           connection: @connection)
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



9
10
11
# File 'lib/redisgraph.rb', line 9

def connection
  @connection
end

#graphnameObject

Returns the value of attribute graphname.



10
11
12
# File 'lib/redisgraph.rb', line 10

def graphname
  @graphname
end

#metadataObject

Returns the value of attribute metadata.



11
12
13
# File 'lib/redisgraph.rb', line 11

def 
  @metadata
end

Instance Method Details

#connect_to_server(options) ⇒ Object

Raises:



2
3
4
5
6
7
# File 'lib/redisgraph/connection.rb', line 2

def connect_to_server(options)
  @connection = Redis.new(options)
  @module_version = module_version()
  raise ServerError, "RedisGraph module not loaded." if @module_version.nil?
  raise ServerError, "RedisGraph module incompatible, expecting >= 1.99." if @module_version < 19900
end

#deleteObject

Delete the graph and all associated keys



75
76
77
78
79
# File 'lib/redisgraph.rb', line 75

def delete
  @connection.call('GRAPH.DELETE', @graphname)
rescue Redis::CommandError => e
  raise DeleteError, e
end

#explain(command) ⇒ Object

Return the execution plan for a given command



68
69
70
71
72
# File 'lib/redisgraph.rb', line 68

def explain(command)
  @connection.call('GRAPH.EXPLAIN', @graphname, command)
rescue Redis::CommandError => e
  raise ExplainError, e
end

#module_versionObject

Ensure that the connected Redis server supports modules and has loaded the RedisGraph module

Raises:



11
12
13
14
15
16
17
18
# File 'lib/redisgraph/connection.rb', line 11

def module_version()
  redis_version = @connection.info["redis_version"]
  major_version = redis_version.split('.').first.to_i
  raise ServerError, "Redis 4.0 or greater required for RedisGraph support." unless major_version >= 4
  modules = @connection.call("MODULE", "LIST")
  module_graph = modules.detect { |_name_key, name, _ver_key, _ver| name == 'graph' }
  module_graph[3] if module_graph
end

#query(command) ⇒ Object

Execute a command and return its parsed result



59
60
61
62
63
64
65
# File 'lib/redisgraph.rb', line 59

def query(command)
  resp = @connection.call('GRAPH.QUERY', @graphname, command, '--compact')
  QueryResult.new(resp,
                  metadata:   @metadata)
rescue Redis::CommandError => e
  raise QueryError, e
end