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(&block) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/pacer-neo4j2/graph.rb', line 145

def before_commit(&block)
  return unless block
  TransactionEventHandler.new(self).tap do |h|
    h.before_commit = block
    neo_graph.registerTransactionEventHandler h
  end
end

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

Creates a Blueprints key index without doing a rebuild.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/pacer-neo4j2/graph.rb', line 158

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



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

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

#drop_handler(h) ⇒ Object



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

def drop_handler(h)
  neo_graph.unregisterTransactionEventHandler h
end

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



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

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



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

def neo_graph
  blueprints_graph.raw_graph
end

#on_commit(&block) ⇒ Object



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

def on_commit(&block)
  return unless block
  TransactionEventHandler.new(self).tap do |h|
    h.on_commit = block
    neo_graph.registerTransactionEventHandler h
  end
end

#on_commit_failed(&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.



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

def on_commit_failed(&block)
  return unless block
  TransactionEventHandler.new(self).tap do |h|
    h.on_commit_failed = block
    neo_graph.registerTransactionEventHandler h
  end
end

#prevent_edge_id_reuse!(min_new_id = nil) ⇒ Object



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

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



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

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



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

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
# 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
    nil
  else
    super
  end
end

#reopen_read_transactionObject



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

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