Class: RedisGraph
- Inherits:
-
Object
- Object
- RedisGraph
- 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
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#graphname ⇒ Object
Returns the value of attribute graphname.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
Instance Method Summary collapse
-
#check_module_version ⇒ Object
Ensure that the connected Redis server supports modules and has loaded the RedisGraph module.
- #connect_to_server(options) ⇒ Object
-
#delete ⇒ Object
Delete the graph and all associated keys.
-
#explain(command) ⇒ Object
Return the execution plan for a given command.
-
#initialize(graph, redis_options = {}) ⇒ RedisGraph
constructor
The RedisGraph constructor instantiates a Redis connection and validates that the graph module is loaded.
-
#query(command) ⇒ Object
Execute a command and return its parsed result.
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, = {}) @graphname = graph connect_to_server() @metadata = Metadata.new(graphname: @graphname, connection: @connection) end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
8 9 10 |
# File 'lib/redisgraph.rb', line 8 def connection @connection end |
#graphname ⇒ Object
Returns the value of attribute graphname.
9 10 11 |
# File 'lib/redisgraph.rb', line 9 def graphname @graphname end |
#metadata ⇒ Object
Returns the value of attribute metadata.
10 11 12 |
# File 'lib/redisgraph.rb', line 10 def @metadata end |
Instance Method Details
#check_module_version ⇒ Object
Ensure that the connected Redis server supports modules and has loaded the RedisGraph module
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() @connection = Redis.new() check_module_version end |
#delete ⇒ Object
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 |