Class: Neo4j::Label

Inherits:
Object
  • Object
show all
Defined in:
lib/neo4j/label.rb,
lib/neo4j-server/label.rb,
lib/neo4j-embedded/label.rb

Overview

A label is a named graph construct that is used to group nodes. See Neo4j::Node how to create and delete nodes

Direct Known Subclasses

Embedded::EmbeddedLabel, Server::CypherLabel

Defined Under Namespace

Classes: InvalidQueryError

Constant Summary collapse

INDEX_PATH =
'/db/data/schema/index/'
CONSTRAINT_PATH =
'/db/data/schema/constraint/'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.constraint?(label, property) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/neo4j-server/label.rb', line 8

def constraint?(label_name, property, session = Neo4j::Session.current)
  label_constraints = session.connection.get("#{CONSTRAINT_PATH}/#{label_name}").body
  !label_constraints.select { |c| c[:label] == label_name.to_s && c[:property_keys].first == property.to_s }.empty?
end

.constraintsObject



4
5
6
# File 'lib/neo4j-server/label.rb', line 4

def constraints(session = Neo4j::Session.current)
  session.connection.get(CONSTRAINT_PATH).body
end

.create(name, session = Neo4j::Session.current) ⇒ Object

Returns a label of given name that can be used to specifying constraints

Parameters:

  • name (Symbol, String)

    the name of the label



75
76
77
# File 'lib/neo4j/label.rb', line 75

def create(name, session = Neo4j::Session.current)
  session.create_label(name)
end

.drop_all_constraintsObject



33
34
35
36
37
# File 'lib/neo4j-server/label.rb', line 33

def drop_all_constraints(session = Neo4j::Session.current)
  constraints.each do |c|
    session._query_or_fail("DROP CONSTRAINT ON (n:`#{c[:label]}`) ASSERT n.`#{c[:property_keys].first}` IS UNIQUE")
  end
end

.drop_all_indexesObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/neo4j-server/label.rb', line 22

def drop_all_indexes(session = Neo4j::Session.current)
  indexes.each do |i|
    begin
      session._query_or_fail("DROP INDEX ON :`#{i[:label]}`(#{i[:property_keys].first})")
    rescue Neo4j::Server::CypherResponse::ResponseError
      # This will error on each constraint. Ignore and continue.
      next
    end
  end
end

.find_all_nodes(label_name, session = Neo4j::Session.current) ⇒ Enumerable<Neo4j::Node>

Returns all nodes having given label. Nodes can be wrapped in your own model ruby classes.

Returns:

  • (Enumerable<Neo4j::Node>)

    all nodes having given label. Nodes can be wrapped in your own model ruby classes.



80
81
82
# File 'lib/neo4j/label.rb', line 80

def find_all_nodes(label_name, session = Neo4j::Session.current)
  session.find_all_nodes(label_name)
end

.find_nodes(label_name, key, value, session = Neo4j::Session.current) ⇒ Enumerable<Neo4j::Node>

Returns all nodes having given label and properties. Nodes can be wrapped in your own model ruby classes.

Returns:

  • (Enumerable<Neo4j::Node>)

    all nodes having given label and properties. Nodes can be wrapped in your own model ruby classes.



85
86
87
# File 'lib/neo4j/label.rb', line 85

def find_nodes(label_name, key, value, session = Neo4j::Session.current)
  session.find_nodes(label_name, key, value)
end

.index?(label, property) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/neo4j-server/label.rb', line 17

def index?(label_name, property, session = Neo4j::Session.current)
  label_indexes = session.connection.get("#{INDEX_PATH}/#{label_name}").body
  !label_indexes.select { |i| i[:label] == label_name.to_s && i[:property_keys].first == property.to_s }.empty?
end

.indexesObject



13
14
15
# File 'lib/neo4j-server/label.rb', line 13

def indexes(session = Neo4j::Session.current)
  session.connection.get(INDEX_PATH).body
end

Instance Method Details

#create_constraint(property, constraints, session = Neo4j::Session.current) ⇒ Object

Creates a neo4j constraint on a property See docs.neo4j.org/chunked/stable/query-constraints.html

Examples:

label = Neo4j::Label.create(:person, session)
label.create_constraint(:name, {type: :unique}, session)


35
36
37
38
39
40
41
42
43
# File 'lib/neo4j/label.rb', line 35

def create_constraint(property, constraints, session = Neo4j::Session.current)
  cypher = case constraints[:type]
           when :unique
             "CREATE CONSTRAINT ON (n:`#{name}`) ASSERT n.`#{property}` IS UNIQUE"
           else
             fail "Not supported constrain #{constraints.inspect} for property #{property} (expected :type => :unique)"
           end
  session._query_or_fail(cypher)
end

#create_index(*properties) ⇒ Object

This method is abstract.


14
15
16
# File 'lib/neo4j/label.rb', line 14

def create_index(*properties)
  fail 'not implemented'
end

#drop_constraint(property, constraint, session = Neo4j::Session.current) ⇒ Object

Drops a neo4j constraint on a property See docs.neo4j.org/chunked/stable/query-constraints.html

Examples:

label = Neo4j::Label.create(:person, session)
label.create_constraint(:name, {type: :unique}, session)
label.drop_constraint(:name, {type: :unique}, session)


52
53
54
55
56
57
58
59
60
# File 'lib/neo4j/label.rb', line 52

def drop_constraint(property, constraint, session = Neo4j::Session.current)
  cypher = case constraint[:type]
           when :unique
             "DROP CONSTRAINT ON (n:`#{name}`) ASSERT n.`#{property}` IS UNIQUE"
           else
             fail "Not supported constrain #{constraint.inspect}"
           end
  session._query_or_fail(cypher)
end

#drop_index(*properties) ⇒ Object

This method is abstract.


19
20
21
# File 'lib/neo4j/label.rb', line 19

def drop_index(*properties)
  fail 'not implemented'
end

#indexesObject

This method is abstract.

List indices for a label



25
26
27
# File 'lib/neo4j/label.rb', line 25

def indexes
  fail 'not implemented'
end

#nameObject

This method is abstract.


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

def name
  fail 'not implemented'
end