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

+ (Object) add(neo_entity, wrapped_entity)



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

+ (Object) clear



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

def clear
  node_repository.clear
  rel_repository.clear
end

+ (Object) enabled Also known as: enabled?



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

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

+ (Object) enabled=(flag)



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

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

+ (Object) get(neo_entity)



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

+ (Object) node_repository



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

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

+ (Object) on_after_commit



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

def on_after_commit(*)
  clear
end

+ (Object) on_neo4j_started(db)



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

+ (Object) rel_repository



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

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

+ (Object) remove(neo_entity)



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

+ (Object) remove_node_by_id(node_id)



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

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

+ (Object) remove_rel_by_id(rel_id)



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

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

+ (Object) repository_for(neo_entity)



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

+ (Object) use



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

+ (Object) without



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