Class: Pacer::Neo4j2::Graph

Inherits:
PacerGraph
  • Object
show all
Defined in:
lib/pacer-neo4j2/graph.rb,
lib/pacer-neo4j2/lucene_filter.rb

Constant Summary collapse

JDate =
java.util.Date

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keep_deleted_vertex_stubsObject

Returns the value of attribute keep_deleted_vertex_stubs.



36
37
38
# File 'lib/pacer-neo4j2/graph.rb', line 36

def keep_deleted_vertex_stubs
  @keep_deleted_vertex_stubs
end

Instance Method Details

#allow_auto_read_txObject



32
33
34
# File 'lib/pacer-neo4j2/graph.rb', line 32

def allow_auto_read_tx
  blueprints_graph.allow_auto_read_tx
end

#allow_auto_read_tx=(b) ⇒ Object



28
29
30
# File 'lib/pacer-neo4j2/graph.rb', line 28

def allow_auto_read_tx=(b)
  blueprints_graph.allow_auto_read_tx = b
end

#allow_auto_txObject



24
25
26
# File 'lib/pacer-neo4j2/graph.rb', line 24

def allow_auto_tx
  blueprints_graph.allow_auto_tx
end

#allow_auto_tx=(b) ⇒ Object



20
21
22
# File 'lib/pacer-neo4j2/graph.rb', line 20

def allow_auto_tx=(b)
  blueprints_graph.allow_auto_tx = b
end

#before_commit(wrapper = nil, &block) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/pacer-neo4j2/graph.rb', line 131

def before_commit(wrapper = nil, &block)
  return unless block
  wrapper = false if block.arity == 0
  transaction_event_handler.before_commit_wrapper = wrapper unless wrapper.nil?
  transaction_event_handler.before_commit = block
  nil
end

#create_key_index_fast(name, type = :vertex) ⇒ Object

Creates a Blueprints key index without doing a rebuild.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/pacer-neo4j2/graph.rb', line 166

def create_key_index_fast(name, type = :vertex)
  raise "Invalid index type #{ type }" unless [:vertex, :edge].include? type
  keys = (key_indices(type) + [name.to_s]).to_a
  neo_settings = neo_graph.getNodeManager.getGraphProperties
  iz = neo_graph.index.getNodeAutoIndexer
  prop = ((type == :vertex) ? "Vertex:indexed_keys" : "Edge:indexed_keys")
  transaction do
    create_vertex.delete! # this forces Blueprints to actually start the transaction
    neo_settings.setProperty prop, keys.to_java(:string)
    keys.each do |key|
      iz.startAutoIndexingProperty key
    end
  end
end

#cypher(query) ⇒ Object



55
56
57
# File 'lib/pacer-neo4j2/graph.rb', line 55

def cypher(query)
  [query].to_route(element_type: :string, graph: self).cypher
end

#drop_handler(h) ⇒ Object



161
162
163
# File 'lib/pacer-neo4j2/graph.rb', line 161

def drop_handler(h)
  neo_graph.unregisterTransactionEventHandler h
end

#key_index_cache(type, name, size = :undefined) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/pacer-neo4j2/graph.rb', line 59

def key_index_cache(type, name, size = :undefined)
  indexer = lucene_auto_index(type)
  if size == :undefined
    indexer.getCacheCapacity name
  else
    indexer.setCacheCapacity name, size
  end
end

#lucene(query, opts = {}) ⇒ Object



4
5
6
7
8
9
# File 'lib/pacer-neo4j2/lucene_filter.rb', line 4

def lucene(query, opts = {})
  opts = { back: self, element_type: :vertex }.merge opts
  chain_route(opts.merge(query: query,
                         filter: :lucene,
                         index: choose_index(opts)))
end

#neo_graphObject



116
117
118
# File 'lib/pacer-neo4j2/graph.rb', line 116

def neo_graph
  blueprints_graph.raw_graph
end

#on_commit(wrapper = nil, &block) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/pacer-neo4j2/graph.rb', line 139

def on_commit(wrapper = nil, &block)
  return unless block
  wrapper = false if block.arity == 0
  transaction_event_handler.on_commit_wrapper = wrapper unless wrapper.nil?
  transaction_event_handler.on_commit = block
  nil
end

#on_commit_failed(wrapper = nil, &block) ⇒ Object

This is actually only called if the commit fails and then it internally tries to rollback. It seems that it’s actually possible for it to fail to rollback here, too…

An exception in before_commit can definitely trigger this.

Regular rollbacks do not get seen by the transaction system and no callback happens.



153
154
155
156
157
158
159
# File 'lib/pacer-neo4j2/graph.rb', line 153

def on_commit_failed(wrapper = nil, &block)
  return unless block
  wrapper = false if block.arity == 0
  transaction_event_handler.on_commit_failed_wrapper = wrapper unless wrapper.nil?
  transaction_event_handler.on_commit_failed = block
  nil
end

#prevent_edge_id_reuse!(min_new_id = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/pacer-neo4j2/graph.rb', line 99

def prevent_edge_id_reuse!(min_new_id = nil)
  min_new_id ||= e.element_ids.max
  return unless min_new_id
  g = blueprints_graph
  n = 0
  transaction do |_, rollback|
    v1 = g.addVertex nil
    v2 = g.addVertex nil
    begin
      n += 1
      e = g.addEdge(nil, v1, v2, "temp")
    end while e.getId < min_new_id
    rollback.call
  end
  n
end

#prevent_id_reuse!Object

When a Neo4J graph is restarted, the ids of any elements that were deleted will be reused. Running this code immediately after starting the graph prevents Neo4J from reusing those IDs.

DEPRECATED: Set keep_deleted_vertex_stubs = true



73
74
75
76
77
78
# File 'lib/pacer-neo4j2/graph.rb', line 73

def prevent_id_reuse!
  {
    edges: prevent_edge_id_reuse!,
    vertices: prevent_vertex_id_reuse!
  }
end

#prevent_vertex_id_reuse!(min_new_id = nil) ⇒ Object

This works by simply creating IDs until the ID of a new element is greater than either the max existing ID, or the min_new_id argument.

DEPRECATED: Set keep_deleted_vertex_stubs = true



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/pacer-neo4j2/graph.rb', line 84

def prevent_vertex_id_reuse!(min_new_id = nil)
  min_new_id ||= v.element_ids.max
  return unless min_new_id
  g = blueprints_graph
  n = 0
  transaction do |_, rollback|
    begin
      n += 1
      v = g.addVertex(nil)
    end while v.getId < min_new_id
    rollback.call
  end
  n
end

#remove_vertex(vertex) ⇒ Object

This method accepts only an unwrapped Blueprints Vertex



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pacer-neo4j2/graph.rb', line 39

def remove_vertex(vertex)
  g = blueprints_graph
  if keep_deleted_vertex_stubs
    vertex.getPropertyKeys.each do |key|
      vertex.removeProperty(key)
    end
    vertex.getEdges(Pacer::Pipes::BOTH).each do |edge|
      g.removeEdge edge
    end
    vertex.setProperty '*deleted*', true
    nil
  else
    super
  end
end

#reopen_read_transactionObject



120
121
122
# File 'lib/pacer-neo4j2/graph.rb', line 120

def reopen_read_transaction
  blueprints_graph.autoStartTransaction(false) if in_read_transaction?
end

#safe_transactionsObject



16
17
18
# File 'lib/pacer-neo4j2/graph.rb', line 16

def safe_transactions
  blueprints_graph.getCheckElementsInTransaction
end

#safe_transactions=(b) ⇒ Object

I’m not sure exactly what this impacts but if it is false, many Pacer tests fail.

Presumably Neo4j is faster with it set to false.



12
13
14
# File 'lib/pacer-neo4j2/graph.rb', line 12

def safe_transactions=(b)
  blueprints_graph.setCheckElementsInTransaction b
end

#transaction_event_handlerObject



123
124
125
126
127
128
129
# File 'lib/pacer-neo4j2/graph.rb', line 123

def transaction_event_handler
  unless @teh
    @teh = TransactionEventHandler.new(self)
    neo_graph.registerTransactionEventHandler @teh
  end
  @teh
end