Class: Neo4j::Core::EventHandler

Inherits:
Object
  • Object
show all
Includes:
Java::OrgNeo4jGraphdbEvent::TransactionEventHandler
Defined in:
lib/neo4j-core/event_handler.rb

Overview

Handles Transactional Events

You can use this to receive event before the transaction commits. The following events are supported:

  • on_neo4j_started

  • on_neo4j_shutdown

  • on_node_created

  • on_node_deleted

  • on_relationship_created

  • on_relationship_deleted

  • on_property_changed

  • on_rel_property_changed

  • on_after_commit

on_neo4j_started(db)

Called when the neo4j engine starts. Notice that the neo4j will be started automatically when the first neo4j operation is performed. You can also start Neo4j: Neo4j.start

  • db

    the Neo4j::Database instance

on_neo4j_shutdown(db)

Called when the neo4j engine shutdown. You don’t need to call Neo4j.shutdown since the it will automatically be shutdown when the application exits (using the at_exit ruby hook).

  • db

    the Neo4j::Database instance

on_after_commit(data, state)

Called after the transaction has successfully committed. See api.neo4j.org/1.4/org/neo4j/graphdb/event/TransactionEventHandler.html for the data and state parameter.

on_node_created(node)

  • node

    the node that was created

on_node_deleted(node, old_props, deleted_relationship_set, deleted_node_identity_map)

  • node

    the node that was deleted

  • old_props

    a hash of the old properties this node had

  • deleted_relationship_set

    the set of deleted relationships. See Neo4j::Core::RelationshipSet

  • deleted_node_identity_map

    the identity map of deleted nodes. The key is the node id, and the value is the node

on_relationship_created(rel, created_node_identity_map)

  • rel

    the relationship that was created

  • created_node_identity_map

    the identity map of created nodes. The key is the node id, and the value is the node

on_relationship_deleted(rel, old_props, deleted_relationship_set, deleted_node_identity_map)

  • rel

    the relationship that was created

  • old_props

    a hash of the old properties this relationship had

  • deleted_relationship_set

    the set of deleted relationships. See Neo4j::Core::RelationshipSet

  • deleted_node_identity_map

    the identity map of deleted nodes. The key is the node id, and the value is the node

on_property_changed(node, key, old_value, new_value)

  • node

    the node

  • key

    the name of the property that was changed (String)

  • old_value

    old value of the property

  • new_value

    new value of the property

on_rel_property_changed(rel, key, old_value, new_value)

  • rel

    the node that was created

  • key

    the name of the property that was changed (String)

  • old_value

    old value of the property

  • new_value

    new value of the property

classes_changed(class_change_map)

  • class_change_map

    a hash with class names as keys, and class changes as values. See Neo4j::ClassChanges

Usage

class MyListener
  def on_node_deleted(node, old_props, deleted_relationship_set, deleted_node_identity_map)
  end
end

# to add an listener without starting neo4j:
Neo4j.unstarted_db.event_handler.add(MyListener.new)

You only need to implement the methods that you need.

Instance Method Summary collapse

Constructor Details

#initializeEventHandler

Returns a new instance of EventHandler.



93
94
95
# File 'lib/neo4j-core/event_handler.rb', line 93

def initialize
  @listeners = []
end

Instance Method Details

#add(listener) ⇒ Object



168
169
170
# File 'lib/neo4j-core/event_handler.rb', line 168

def add(listener)
  @listeners << listener unless @listeners.include?(listener)
end

#add_deleted_nodes(data, class_change_map, removed_node_properties_map) ⇒ Object



217
218
219
# File 'lib/neo4j-core/event_handler.rb', line 217

def add_deleted_nodes(data, class_change_map, removed_node_properties_map)
  data.deleted_nodes.each { |node| instance_deleted(node, removed_node_properties_map, class_change_map) }
end

#add_map(key, map) ⇒ Object



163
164
165
166
# File 'lib/neo4j-core/event_handler.rb', line 163

def add_map(key, map)
  map.put(key, java.util.HashMap.new)
  map.get(key)
end

#after_commit(data, state) ⇒ Object



98
99
100
# File 'lib/neo4j-core/event_handler.rb', line 98

def after_commit(data, state)
  @listeners.each { |li| li.on_after_commit(data, state) if li.respond_to?(:on_after_commit) }
end

#after_rollback(data, state) ⇒ Object



102
103
# File 'lib/neo4j-core/event_handler.rb', line 102

def after_rollback(data, state)
end

#before_commit(data) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/neo4j-core/event_handler.rb', line 105

def before_commit(data)
  class_change_map = java.util.HashMap.new
  created_node_identity_map = iterate_created_nodes(data.created_nodes, class_change_map)
  deleted_node_identity_map = deleted_node_identity_map(data.deleted_nodes)
  deleted_relationship_set = relationship_set(data.deleted_relationships)
  removed_node_properties_map = property_map(data.removed_node_properties)
  removed_relationship_properties_map = property_map(data.removed_relationship_properties)
  add_deleted_nodes(data, class_change_map, removed_node_properties_map)
  empty_map = java.util.HashMap.new
  data.assigned_node_properties.each { |tx_data| property_changed(tx_data.entity, tx_data.key, tx_data.previously_commited_value, tx_data.value) unless tx_data.key == '_classname' }
  data.removed_node_properties.each { |tx_data| property_changed(tx_data.entity, tx_data.key, tx_data.previously_commited_value, nil) unless deleted_node_identity_map.containsKey(tx_data.entity.getId) }
  data.deleted_nodes.each { |node| node_deleted(node, removed_node_properties_map.get(node.getId)||empty_map, deleted_relationship_set, deleted_node_identity_map) }
  data.created_relationships.each { |rel| relationship_created(rel, created_node_identity_map) }
  data.deleted_relationships.each { |rel| relationship_deleted(rel, removed_relationship_properties_map.get(rel.getId)||empty_map, deleted_relationship_set, deleted_node_identity_map) }
  data.assigned_relationship_properties.each { |tx_data| rel_property_changed(tx_data.entity, tx_data.key, tx_data.previously_commited_value, tx_data.value) }
  data.removed_relationship_properties.each { |tx_data| rel_property_changed(tx_data.entity, tx_data.key, tx_data.previously_commited_value, nil) unless deleted_relationship_set.contains_rel?(tx_data.entity) }
  classes_changed(class_change_map)
rescue Exception => e
  # since these exceptions gets swallowed
  puts "ERROR in before commit hook #{e}"
  puts e.backtrace.join("\n")
end

#class_change(classname, class_change_map) ⇒ Object



234
235
236
237
# File 'lib/neo4j-core/event_handler.rb', line 234

def class_change(classname, class_change_map)
  class_change_map.put(classname, ClassChanges.new) if class_change_map.get(classname).nil?
  class_change_map.get(classname)
end

#classes_changed(changed_class_map) ⇒ Object



239
240
241
# File 'lib/neo4j-core/event_handler.rb', line 239

def classes_changed(changed_class_map)
  @listeners.each { |li| li.classes_changed(changed_class_map) if li.respond_to?(:classes_changed) }
end

#deleted_node_identity_map(nodes) ⇒ Object



139
140
141
142
143
# File 'lib/neo4j-core/event_handler.rb', line 139

def deleted_node_identity_map(nodes)
  identity_map = java.util.HashMap.new
  nodes.each { |node| identity_map.put(node.neo_id, node) } #using put due to a performance regression in JRuby 1.6.4
  identity_map
end

#instance_created(node, class_change_map) ⇒ Object



221
222
223
224
# File 'lib/neo4j-core/event_handler.rb', line 221

def instance_created(node, class_change_map)
  classname = node[:_classname]
  class_change(classname, class_change_map).add(node) if classname
end

#instance_deleted(node, removed_node_properties_map, class_change_map) ⇒ Object



226
227
228
229
230
231
232
# File 'lib/neo4j-core/event_handler.rb', line 226

def instance_deleted(node, removed_node_properties_map, class_change_map)
  properties = removed_node_properties_map.get(node.getId)
  if properties
    classname = properties.get("_classname")
    class_change(classname, class_change_map).delete(node) if classname
  end
end

#iterate_created_nodes(nodes, class_change_map) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/neo4j-core/event_handler.rb', line 129

def iterate_created_nodes(nodes, class_change_map)
  identity_map = java.util.HashMap.new
  nodes.each do |node|
    identity_map.put(node.neo_id, node) #using put due to a performance regression in JRuby 1.6.4
    instance_created(node, class_change_map)
    node_created(node)
  end
  identity_map
end

#map(key, map) ⇒ Object



159
160
161
# File 'lib/neo4j-core/event_handler.rb', line 159

def map(key, map)
  map.get(key) || add_map(key, map)
end

#neo4j_shutdown(db) ⇒ Object



189
190
191
# File 'lib/neo4j-core/event_handler.rb', line 189

def neo4j_shutdown(db)
  @listeners.each { |li| li.on_neo4j_shutdown(db) if li.respond_to?(:on_neo4j_shutdown) }
end

#neo4j_started(db) ⇒ Object



185
186
187
# File 'lib/neo4j-core/event_handler.rb', line 185

def neo4j_started(db)
  @listeners.each { |li| li.on_neo4j_started(db) if li.respond_to?(:on_neo4j_started) }
end

#node_created(node) ⇒ Object



193
194
195
# File 'lib/neo4j-core/event_handler.rb', line 193

def node_created(node)
  @listeners.each { |li| li.on_node_created(node) if li.respond_to?(:on_node_created) }
end

#node_deleted(node, old_properties, deleted_relationship_set, deleted_node_identity_map) ⇒ Object



197
198
199
# File 'lib/neo4j-core/event_handler.rb', line 197

def node_deleted(node, old_properties, deleted_relationship_set, deleted_node_identity_map)
  @listeners.each { |li| li.on_node_deleted(node, old_properties, deleted_relationship_set, deleted_node_identity_map) if li.respond_to?(:on_node_deleted) }
end


180
181
182
183
# File 'lib/neo4j-core/event_handler.rb', line 180

def print
  puts "Listeners #{@listeners.size}"
  @listeners.each { |li| puts "  Listener '#{li}'" }
end

#property_changed(node, key, old_value, new_value) ⇒ Object



209
210
211
# File 'lib/neo4j-core/event_handler.rb', line 209

def property_changed(node, key, old_value, new_value)
  @listeners.each { |li| li.on_property_changed(node, key, old_value, new_value) if li.respond_to?(:on_property_changed) }
end

#property_map(properties) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/neo4j-core/event_handler.rb', line 151

def property_map(properties)
  map = java.util.HashMap.new
  properties.each do |property|
    map(property.entity.getId, map).put(property.key, property.previously_commited_value)
  end
  map
end

#rel_property_changed(rel, key, old_value, new_value) ⇒ Object



213
214
215
# File 'lib/neo4j-core/event_handler.rb', line 213

def rel_property_changed(rel, key, old_value, new_value)
  @listeners.each { |li| li.on_rel_property_changed(rel, key, old_value, new_value) if li.respond_to?(:on_rel_property_changed) }
end

#relationship_created(relationship, created_node_identity_map) ⇒ Object



201
202
203
# File 'lib/neo4j-core/event_handler.rb', line 201

def relationship_created(relationship, created_node_identity_map)
  @listeners.each { |li| li.on_relationship_created(relationship, created_node_identity_map) if li.respond_to?(:on_relationship_created) }
end

#relationship_deleted(relationship, old_props, deleted_relationship_set, deleted_node_identity_map) ⇒ Object



205
206
207
# File 'lib/neo4j-core/event_handler.rb', line 205

def relationship_deleted(relationship, old_props, deleted_relationship_set, deleted_node_identity_map)
  @listeners.each { |li| li.on_relationship_deleted(relationship, old_props, deleted_relationship_set, deleted_node_identity_map) if li.respond_to?(:on_relationship_deleted) }
end

#relationship_set(relationships) ⇒ Object



145
146
147
148
149
# File 'lib/neo4j-core/event_handler.rb', line 145

def relationship_set(relationships)
  relationship_set = Neo4j::Core::RelationshipSet.new #(relationships.size)
  relationships.each { |rel| relationship_set.add(rel) }
  relationship_set
end

#remove(listener) ⇒ Object



172
173
174
# File 'lib/neo4j-core/event_handler.rb', line 172

def remove(listener)
  @listeners.delete(listener)
end

#remove_allObject



176
177
178
# File 'lib/neo4j-core/event_handler.rb', line 176

def remove_all
  @listeners = []
end