Class: Redgraph::Graph

Inherits:
Object
  • Object
show all
Includes:
EdgeMethods, NodeMethods, Util
Defined in:
lib/redgraph/graph.rb,
lib/redgraph/graph/edge_methods.rb,
lib/redgraph/graph/node_methods.rb

Defined Under Namespace

Modules: EdgeMethods, NodeMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#escape_value, #properties_to_string

Methods included from EdgeMethods

#add_edge, #count_edges, #edges, #merge_edge

Methods included from NodeMethods

#add_node, #count_nodes, #destroy_node, #find_node_by_id, #merge_node, #nodes, #update_node

Constructor Details

#initialize(graph_name, redis_options = {}) ⇒ Graph

Returns a new instance of Graph.

Examples:

Graph.new(“foobar”, url: “redis://localhost:6379/0”, logger: Logger.new(STDOUT))

Parameters:

  • graph_name (String)

    Name of the graph

  • redis_options (Hash) (defaults to: {})

    Redis client options

Raises:



18
19
20
21
22
23
# File 'lib/redgraph/graph.rb', line 18

def initialize(graph_name, redis_options = {})
  @graph_name = graph_name
  @connection = Redis.new(redis_options)
  @module_version = module_version
  raise ServerError, "Can't find RedisGraph module" unless @module_version
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



12
13
14
# File 'lib/redgraph/graph.rb', line 12

def connection
  @connection
end

#graph_nameObject

Returns the value of attribute graph_name.



12
13
14
# File 'lib/redgraph/graph.rb', line 12

def graph_name
  @graph_name
end

Instance Method Details

#deleteObject

Deletes an existing graph



35
36
37
38
39
40
41
# File 'lib/redgraph/graph.rb', line 35

def delete
  @connection.call("GRAPH.DELETE", graph_name)
rescue Redis::CommandError => e
  # Catch exception if the graph was already deleted
  return nil if e.message =~ /ERR Invalid graph operation on empty key/
  raise e
end

#get_label(id) ⇒ String?

Returns label.

Parameters:

  • id (Integer)

    label id

Returns:

  • (String, nil)

    label



78
79
80
81
# File 'lib/redgraph/graph.rb', line 78

def get_label(id)
  @labels ||= labels
  @labels[id] || (@labels = labels)[id]
end

#get_property(id) ⇒ String?

Returns property.

Parameters:

  • id (Integer)

    property id

Returns:

  • (String, nil)

    property



86
87
88
89
# File 'lib/redgraph/graph.rb', line 86

def get_property(id)
  @properties ||= properties
  @properties[id] || (@properties = properties)[id]
end

#get_relationship_type(id) ⇒ String?

Returns relationship type.

Parameters:

  • id (Integer)

    relationship type id

Returns:

  • (String, nil)

    relationship type



94
95
96
97
# File 'lib/redgraph/graph.rb', line 94

def get_relationship_type(id)
  @relationship_types ||= relationship_types
  @relationship_types[id] || (@relationship_types = relationship_types)[id]
end

#labelsArray

Returns Existing labels.

Returns:

  • (Array)

    Existing labels



51
52
53
54
# File 'lib/redgraph/graph.rb', line 51

def labels
  result = _query("CALL db.labels()")
  result.resultset.map(&:values).flatten
end

#listArray

Returns Existing graph names.

Returns:

  • (Array)

    Existing graph names



45
46
47
# File 'lib/redgraph/graph.rb', line 45

def list
  @connection.call("GRAPH.LIST")
end

#module_versionObject

Returns the version of the RedisGraph module



27
28
29
30
31
# File 'lib/redgraph/graph.rb', line 27

def module_version
  modules = @connection.call("MODULE", "LIST")
  module_graph = modules.detect { |_, name, _, version| name == 'graph' }
  module_graph[3] if module_graph
end

#propertiesArray

Returns Existing properties.

Returns:

  • (Array)

    Existing properties



58
59
60
61
# File 'lib/redgraph/graph.rb', line 58

def properties
  result = _query("CALL db.propertyKeys()")
  result.resultset.map(&:values).flatten
end

#query(cmd) ⇒ Object

You can run custom cypher queries



71
72
73
# File 'lib/redgraph/graph.rb', line 71

def query(cmd)
  _query(cmd).rows
end

#relationship_typesArray

Returns Existing relationship types.

Returns:

  • (Array)

    Existing relationship types



65
66
67
68
# File 'lib/redgraph/graph.rb', line 65

def relationship_types
  result = _query("CALL db.relationshipTypes()")
  result.resultset.map(&:values).flatten
end