Class: Neo4j::Core::Label

Inherits:
Object
  • Object
show all
Defined in:
lib/neo4j/core/label.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, session) ⇒ Label

Returns a new instance of Label.



6
7
8
9
# File 'lib/neo4j/core/label.rb', line 6

def initialize(name, session)
  @name = name
  @session = session
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.drop_indexes_for(session) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/neo4j/core/label.rb', line 84

def self.drop_indexes_for(session)
  indexes_for(session).each do |definition|
    begin
      session.query("DROP INDEX ON :`#{definition[:label]}`(#{definition[:properties][0]})")
    rescue Neo4j::Server::CypherResponse::ResponseError
      # This will error on each constraint. Ignore and continue.
      next
    end
  end
end

.drop_uniqueness_constraints_for(session) ⇒ Object



117
118
119
120
121
# File 'lib/neo4j/core/label.rb', line 117

def self.drop_uniqueness_constraints_for(session)
  session.constraints.each do |definition|
    session.query("DROP CONSTRAINT ON (n:`#{definition[:label]}`) ASSERT n.`#{definition[:properties][0]}` IS UNIQUE")
  end
end

.indexes_for(session) ⇒ Object



69
70
71
# File 'lib/neo4j/core/label.rb', line 69

def self.indexes_for(session)
  session.indexes
end

.schema_threads(session) ⇒ Object



149
150
151
# File 'lib/neo4j/core/label.rb', line 149

def schema_threads(session)
  session.instance_variable_get('@_schema_threads') || []
end

.set_schema_threads(session, array) ⇒ Object



153
154
155
# File 'lib/neo4j/core/label.rb', line 153

def set_schema_threads(session, array)
  session.instance_variable_set('@_schema_threads', array)
end

.wait_for_schema_changes(session) ⇒ Object



131
132
133
134
# File 'lib/neo4j/core/label.rb', line 131

def self.wait_for_schema_changes(session)
  schema_threads(session).map(&:join)
  set_schema_threads(session, [])
end

Instance Method Details

#constraint?(property) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/neo4j/core/label.rb', line 123

def constraint?(property)
  constraints.any? { |definition| definition[:properties] == [property.to_sym] }
end

#constraints(_options = {}) ⇒ Object



99
100
101
102
103
# File 'lib/neo4j/core/label.rb', line 99

def constraints(_options = {})
  @session.constraints.select do |definition|
    definition[:label] == @name.to_sym
  end
end

#create_constraint(property, constraints) ⇒ 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)


28
29
30
31
32
33
34
35
36
# File 'lib/neo4j/core/label.rb', line 28

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

#create_index(property, options = {}) ⇒ Object



11
12
13
14
15
# File 'lib/neo4j/core/label.rb', line 11

def create_index(property, options = {})
  validate_index_options!(options)
  properties = property.is_a?(Array) ? property.join(',') : property
  schema_query("CREATE INDEX ON :`#{@name}`(#{properties})")
end

#create_uniqueness_constraint(property, options = {}) ⇒ Object



38
39
40
# File 'lib/neo4j/core/label.rb', line 38

def create_uniqueness_constraint(property, options = {})
  create_constraint(property, options.merge(type: :unique))
end

#drop_constraint(property, constraint) ⇒ 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)


49
50
51
52
53
54
55
56
57
# File 'lib/neo4j/core/label.rb', line 49

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

#drop_index(property, options = {}) ⇒ Object



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

def drop_index(property, options = {})
  validate_index_options!(options)
  schema_query("DROP INDEX ON :`#{@name}`(#{property})")
end

#drop_indexesObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/neo4j/core/label.rb', line 73

def drop_indexes
  indexes.each do |definition|
    begin
      @session.query("DROP INDEX ON :`#{definition[:label]}`(#{definition[:properties][0]})")
    rescue Neo4j::Server::CypherResponse::ResponseError
      # This will error on each constraint. Ignore and continue.
      next
    end
  end
end

#drop_uniqueness_constraint(property, options = {}) ⇒ Object



59
60
61
# File 'lib/neo4j/core/label.rb', line 59

def drop_uniqueness_constraint(property, options = {})
  drop_constraint(property, options.merge(type: :unique))
end

#drop_uniqueness_constraintsObject



111
112
113
114
115
# File 'lib/neo4j/core/label.rb', line 111

def drop_uniqueness_constraints
  uniqueness_constraints.each do |definition|
    @session.query("DROP CONSTRAINT ON (n:`#{definition[:label]}`) ASSERT n.`#{definition[:properties][0]}` IS UNIQUE")
  end
end

#index?(property) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/neo4j/core/label.rb', line 95

def index?(property)
  indexes.any? { |definition| definition[:properties] == [property.to_sym] }
end

#indexesObject



63
64
65
66
67
# File 'lib/neo4j/core/label.rb', line 63

def indexes
  @session.indexes.select do |definition|
    definition[:label] == @name.to_sym
  end
end

#uniqueness_constraint?(property) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/neo4j/core/label.rb', line 127

def uniqueness_constraint?(property)
  uniqueness_constraints.include?([property])
end

#uniqueness_constraints(_options = {}) ⇒ Object



105
106
107
108
109
# File 'lib/neo4j/core/label.rb', line 105

def uniqueness_constraints(_options = {})
  constraints.select do |definition|
    definition[:type] == :uniqueness
  end
end