Class: Pacer::Neo4j::Graph

Inherits:
PacerGraph
  • Object
show all
Defined in:
lib/pacer-neo4j/graph.rb,
lib/pacer-neo4j/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.



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

def keep_deleted_vertex_stubs
  @keep_deleted_vertex_stubs
end

Instance Method Details

#allow_auto_txObject



24
25
26
# File 'lib/pacer-neo4j/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-neo4j/graph.rb', line 20

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

#before_commit(&block) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/pacer-neo4j/graph.rb', line 134

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.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/pacer-neo4j/graph.rb', line 147

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



47
48
49
# File 'lib/pacer-neo4j/graph.rb', line 47

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

#drop_handler(h) ⇒ Object



142
143
144
# File 'lib/pacer-neo4j/graph.rb', line 142

def drop_handler(h)
  neo_graph.unregisterTransactionEventHandler h
end

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



51
52
53
54
55
56
57
58
# File 'lib/pacer-neo4j/graph.rb', line 51

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-neo4j/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



108
109
110
# File 'lib/pacer-neo4j/graph.rb', line 108

def neo_graph
  blueprints_graph.raw_graph
end

#on_commit(&block) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/pacer-neo4j/graph.rb', line 112

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.



126
127
128
129
130
131
132
# File 'lib/pacer-neo4j/graph.rb', line 126

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pacer-neo4j/graph.rb', line 91

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



65
66
67
68
69
70
# File 'lib/pacer-neo4j/graph.rb', line 65

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pacer-neo4j/graph.rb', line 76

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pacer-neo4j/graph.rb', line 31

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

#safe_transactionsObject



16
17
18
# File 'lib/pacer-neo4j/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-neo4j/graph.rb', line 12

def safe_transactions=(b)
  blueprints_graph.setCheckElementsInTransaction b
end