Module: Redgraph::Graph::NodeMethods
- Included in:
- Redgraph::Graph
- Defined in:
- lib/redgraph/graph/node_methods.rb
Instance Method Summary collapse
-
#add_node(node) ⇒ Object
Adds a node.
-
#count_nodes(label: nil, properties: nil) ⇒ Object
Counts nodes.
- #destroy_node(node) ⇒ Object
- #find_node_by_id(id) ⇒ Object
-
#merge_node(node) ⇒ Object
Merges (creates a node unless one with the same label and properties exists).
-
#nodes(label: nil, properties: nil, order: nil, limit: nil, skip: nil) ⇒ Object
Returns nodes.
- #update_node(node) ⇒ Object
Instance Method Details
#add_node(node) ⇒ Object
Adds a node. If successul it returns the created object, otherwise false
8 9 10 |
# File 'lib/redgraph/graph/node_methods.rb', line 8 def add_node(node) merge_or_add_node(node, :create) end |
#count_nodes(label: nil, properties: nil) ⇒ Object
Counts nodes. Options:
-
label: filter by label
-
properties: filter by properties
67 68 69 70 71 72 73 74 |
# File 'lib/redgraph/graph/node_methods.rb', line 67 def count_nodes(label: nil, properties: nil) node = Node.new(label: label, properties: properties) cmd = "MATCH #{node.to_query_string} RETURN COUNT(node)" # RedisGraph bug: if there are no matches COUNT returns zero rows # https://github.com/RedisGraph/RedisGraph/issues/1455 query(cmd).flatten[0] || 0 end |
#destroy_node(node) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/redgraph/graph/node_methods.rb', line 87 def destroy_node(node) return false unless node.persisted? cmd = "MATCH (node) WHERE ID(node) = #{node.id} DELETE node" result = _query(cmd) result.stats["nodes_deleted"] == 1 end |
#find_node_by_id(id) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/redgraph/graph/node_methods.rb', line 19 def find_node_by_id(id) result = _query("MATCH (node) WHERE ID(node) = #{id} RETURN node") return nil if result.resultset.empty? (node_id, labels, properties) = result.resultset.first["node"] attrs = {} properties.each do |(index, type, value)| attrs[get_property(index)] = value end Node.new(label: get_label(labels.first), properties: attrs).tap do |node| node.id = node_id end end |
#merge_node(node) ⇒ Object
Merges (creates a node unless one with the same label and properties exists). If successul it returns the object, otherwise false
15 16 17 |
# File 'lib/redgraph/graph/node_methods.rb', line 15 def merge_node(node) merge_or_add_node(node, :merge) end |
#nodes(label: nil, properties: nil, order: nil, limit: nil, skip: nil) ⇒ Object
Returns nodes. Options:
-
label: filter by label
-
properties: filter by properties
-
order: node.name ASC, node.year DESC
-
limit: number of items
-
skip: items offset (useful for pagination)
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/redgraph/graph/node_methods.rb', line 42 def nodes(label: nil, properties: nil, order: nil, limit: nil, skip: nil) _label = ":`#{label}`" if label _order = if order raise MissingAliasPrefixError unless order.include?("node.") "ORDER BY #{order}" end _limit = "LIMIT #{limit}" if limit _skip = "SKIP #{skip}" if skip node = Node.new(label: label, properties: properties) cmd = "MATCH #{node.to_query_string} RETURN node #{_order} #{_skip} #{_limit}" result = _query(cmd) result.resultset.map do |item| node_from_resultset_item(item["node"]) end end |
#update_node(node) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/redgraph/graph/node_methods.rb', line 76 def update_node(node) return false unless node.persisted? _set = node.properties.map do |(key, val)| "node.#{key} = #{escape_value(val)}" end.join(", ") cmd = "MATCH (node) WHERE ID(node) = #{node.id} SET #{_set} RETURN node" result = _query(cmd) node_from_resultset_item(result.resultset.first["node"]) end |