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.3'

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



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

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.



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

def connection
  @connection
end

#graphnameObject

Returns the value of attribute graphname.



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

def graphname
  @graphname
end

#metadataObject

Returns the value of attribute metadata.



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

def 
  @metadata
end

Instance Method Details

#check_module_versionObject

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

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/redisgraph/connection.rb', line 9

def check_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

  begin
    modules = @connection.call("MODULE", "LIST")
  rescue Redis::CommandError
    # Ignore check if the connected server does not support the "MODULE LIST" command
    return
  end

  module_graph = modules.detect { |_name_key, name, _ver_key, _ver| name == 'graph' }
  module_version = module_graph[3] if module_graph
  raise ServerError, "RedisGraph module not loaded." if module_version.nil?
  raise ServerError, "RedisGraph module incompatible, expecting >= 1.99." if module_version < 19900
end

#connect_to_server(options) ⇒ Object



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

def connect_to_server(options)
  @connection = Redis.new(options)
  check_module_version
end

#deleteObject

Delete the graph and all associated keys



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

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



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

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

#query(command) ⇒ Object

Execute a command and return its parsed result



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

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