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

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.

IdentityMap is disabled by default and still in development (i.e. use it with care).

Class Method Summary collapse

Class Method Details

.add(neo_entity, wrapped_entity) ⇒ Object



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

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

.clearObject



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

def clear
  node_repository.clear
  rel_repository.clear
end

.enabledObject Also known as: enabled?



25
26
27
# File 'lib/neo4j/identity_map.rb', line 25

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

.enabled=(flag) ⇒ Object



21
22
23
# File 'lib/neo4j/identity_map.rb', line 21

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

.get(neo_entity) ⇒ Object



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

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

.node_repositoryObject



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

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

.on_after_commitObject



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

def on_after_commit(*)
  clear
end

.on_neo4j_started(db) ⇒ Object



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

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

.rel_repositoryObject



35
36
37
# File 'lib/neo4j/identity_map.rb', line 35

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

.remove(neo_entity) ⇒ Object



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

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

.remove_node_by_id(node_id) ⇒ Object



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

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

.remove_rel_by_id(rel_id) ⇒ Object



84
85
86
# File 'lib/neo4j/identity_map.rb', line 84

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

.repository_for(neo_entity) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/neo4j/identity_map.rb', line 39

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



50
51
52
53
54
55
56
# File 'lib/neo4j/identity_map.rb', line 50

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

.withoutObject



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

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