Module: Neo4j::IdentityMap

Defined in:
lib/neo4j/identity_map.rb

Overview

Neo4j Identity Map

Ensures that each object gets loaded only once by keeping every loaded object in a map. Looks up objects using the map when referring to them.

More information on Identity Map pattern:

http://www.martinfowler.com/eaaCatalog/identityMap.html

The identity map cache is cleared after each transaction. When used from rails the Rack Middle ware will also make sure that the cache is emptied after each request.

When used from batch import scripts (e.g. Rake) you should probably disable the identity map, because the identity map cache will not be used (the same object is not loaded more than once). If not used from rails and transactions does not occur then the cache will never be cleared and you will have a memory leak.

Configuration

In order to enable IdentityMap, set config.neo4j.identity_map = true in your config/application.rb file. If used outside rails, set Neo4j::Config = true.

Class Method Summary collapse

Class Method Details

.add(neo_entity, wrapped_entity) ⇒ Object



76
77
78
79
# File 'lib/neo4j/identity_map.rb', line 76

def add(neo_entity, wrapped_entity)
  r = repository_for(neo_entity)
  r && r.put(neo_entity.neo_id, wrapped_entity)
end

.clearObject



94
95
96
97
# File 'lib/neo4j/identity_map.rb', line 94

def clear
  node_repository.clear
  rel_repository.clear
end

.enabledObject Also known as: enabled?



31
32
33
# File 'lib/neo4j/identity_map.rb', line 31

def enabled
   Thread.current[:neo4j_identity_map] == true
end

.enabled=(flag) ⇒ Object



27
28
29
# File 'lib/neo4j/identity_map.rb', line 27

def enabled=(flag)
  Thread.current[:neo4j_identity_map] = flag
end

.get(neo_entity) ⇒ Object



71
72
73
74
# File 'lib/neo4j/identity_map.rb', line 71

def get(neo_entity)
  r = repository_for(neo_entity)
  r && r.get(neo_entity.neo_id)
end

.node_repositoryObject



37
38
39
# File 'lib/neo4j/identity_map.rb', line 37

def node_repository
  Thread.current[:node_identity_map] ||= java.util.HashMap.new
end

.on_after_commitObject



99
100
101
# File 'lib/neo4j/identity_map.rb', line 99

def on_after_commit(*)
  clear
end

.on_neo4j_started(db) ⇒ Object



103
104
105
106
107
# File 'lib/neo4j/identity_map.rb', line 103

def on_neo4j_started(db)
  if !Neo4j::Config[:identity_map] && !enabled
    db.event_handler.remove(self)
  end
end

.rel_repositoryObject



41
42
43
# File 'lib/neo4j/identity_map.rb', line 41

def rel_repository
  Thread.current[:rel_identity_map] ||= java.util.HashMap.new
end

.remove(neo_entity) ⇒ Object



81
82
83
84
# File 'lib/neo4j/identity_map.rb', line 81

def remove(neo_entity)
  r = repository_for(neo_entity)
  r && r.remove(neo_entity.neo_id)
end

.remove_node_by_id(node_id) ⇒ Object



86
87
88
# File 'lib/neo4j/identity_map.rb', line 86

def remove_node_by_id(node_id)
  node_repository.remove(node_id)
end

.remove_rel_by_id(rel_id) ⇒ Object



90
91
92
# File 'lib/neo4j/identity_map.rb', line 90

def remove_rel_by_id(rel_id)
  rel_repository.remove(rel_id)
end

.repository_for(neo_entity) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/neo4j/identity_map.rb', line 45

def repository_for(neo_entity)
  return nil unless enabled?
  if neo_entity.class == Neo4j::Node
    node_repository
  elsif neo_entity.class == Neo4j::Relationship
    rel_repository
  else
    nil
  end
end

.useObject



56
57
58
59
60
61
62
# File 'lib/neo4j/identity_map.rb', line 56

def use
  old, self.enabled = enabled, true
  yield if block_given?
ensure
  self.enabled = old
  clear
end

.withoutObject



64
65
66
67
68
69
# File 'lib/neo4j/identity_map.rb', line 64

def without
  old, self.enabled = enabled, false
  yield if block_given?
ensure
  self.enabled = old
end