Class: ActiveGraph::Core::Label

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Label

Returns a new instance of Label.



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

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.drop_constraintsObject



116
117
118
119
120
121
122
123
# File 'lib/active_graph/core/label.rb', line 116

def drop_constraints
  result = ActiveGraph::Base.read_transaction { |tx| tx.run('CALL db.constraints').to_a }
  ActiveGraph::Base.write_transaction do |tx|
    result.each do |record|
      tx.run("DROP #{record.keys.include?(:name) ? "CONSTRAINT #{record[:name]}" : record[:description]}")
    end
  end
end

.drop_indexesObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/active_graph/core/label.rb', line 105

def drop_indexes
  indexes.each do |definition|
    begin
      ActiveGraph::Base.query("DROP INDEX ON :`#{definition[:label]}`(#{definition[:properties][0]})")
    rescue Neo4j::Driver::Exceptions::DatabaseException
      # This will error on each constraint. Ignore and continue.
      next
    end
  end
end

.indexesObject



101
102
103
# File 'lib/active_graph/core/label.rb', line 101

def indexes
  ActiveGraph::Base.indexes
end

Instance Method Details

#constraint?(property) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/active_graph/core/label.rb', line 90

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

#constraints(_options = {}) ⇒ Object



78
79
80
81
82
# File 'lib/active_graph/core/label.rb', line 78

def constraints(_options = {})
  ActiveGraph::Base.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 = ActiveGraph::Label.create(:person)
label.create_constraint(:name, {type: :unique})


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

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



10
11
12
13
14
# File 'lib/active_graph/core/label.rb', line 10

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



37
38
39
# File 'lib/active_graph/core/label.rb', line 37

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 = ActiveGraph::Label.create(:person)
label.create_constraint(:name, {type: :unique})
label.drop_constraint(:name, {type: :unique})


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

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

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



16
17
18
19
# File 'lib/active_graph/core/label.rb', line 16

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

#drop_indexesObject



70
71
72
# File 'lib/active_graph/core/label.rb', line 70

def drop_indexes
  self.class.drop_indexes
end

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



60
61
62
# File 'lib/active_graph/core/label.rb', line 60

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

#index?(property) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/active_graph/core/label.rb', line 74

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

#indexesObject



64
65
66
67
68
# File 'lib/active_graph/core/label.rb', line 64

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

#uniqueness_constraint?(property) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/active_graph/core/label.rb', line 94

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

#uniqueness_constraints(_options = {}) ⇒ Object



84
85
86
87
88
# File 'lib/active_graph/core/label.rb', line 84

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