Module: Schron::IdentityMapRepository

Includes:
Archive::Interface, Repository, Repository::Interface
Defined in:
lib/schron/identity_map_repository.rb

Instance Attribute Summary

Attributes included from Archive

#datastore, #entity_class, #indexed_fields, #kind

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Archive

#all, #batched_each, #batches, #dump, #dump_all, #exec_count, #first, #initialize, #load, #load_all, #query

Methods included from Archive::Interface

#dump, #dump_all, #first, #identity, #load, #load_all, #query

Methods included from Repository::Interface

#dump, #load

Class Method Details

.included(archive_class) ⇒ Object



13
14
15
# File 'lib/schron/identity_map_repository.rb', line 13

def self.included(archive_class)
  archive_class.extend Schron::DSL
end

Instance Method Details

#exec_query(query) ⇒ Object

def query(&block)

Query.new(self, &block)

end



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/schron/identity_map_repository.rb', line 82

def exec_query(query)
  results = super
  results.map do |object|
    if identity_map.has?(object.id)
      id_map_object = identity_map.get(object.id)
      assign_attributes(from: object, to: id_map_object)
      id_map_object
    else
      identity_map.put(object.id, object)
      object
    end
  end
end

#get(id) ⇒ Object



96
97
98
99
100
# File 'lib/schron/identity_map_repository.rb', line 96

def get(id)
  identity_map.fetch(id) do
    super
  end
end

#identity_mapObject



174
175
176
# File 'lib/schron/identity_map_repository.rb', line 174

def identity_map
  @sessions.last || raise('Must start a session before accessing data from an IdentityMapRepository')
end

#insert(object) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/schron/identity_map_repository.rb', line 126

def insert(object)
  result = super
  # result = @repo.insert(object)
  assign_attributes(from: result, to: object)
  identity_map.put(object.id, object)
  object
end

#multi_get(ids) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/schron/identity_map_repository.rb', line 102

def multi_get(ids)
  ids = ids.to_a unless ids.kind_of?(Array)
  result = Array.new(ids.size)
  needed_ids = []

  ids.each_with_index do |id, index|
    if identity_map.has?(id)
      result[index] = identity_map.get(id)
    else
      needed_ids << id
    end
  end
  
  needed_objects = super(needed_ids)#@repo.multi_get(needed_ids)
  needed_objects.each do |object|
    if object
      identity_map.put(object.id, object)
      result[ids.index(object.id)] = object
    end
  end
  
  result.compact
end

#multi_insert(objects) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/schron/identity_map_repository.rb', line 134

def multi_insert(objects)
  # results = @repo.multi_insert(objects)
  results = super
  objects.each_with_index do |o, i|
    assign_attributes(from: results[i], to: o)
    identity_map.put(o.id, o)
  end
  objects
end

#multi_remove(ids) ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/schron/identity_map_repository.rb', line 165

def multi_remove(ids)
  super
  ids.each do |id|
    object = identity_map.delete(id)
    object.freeze if object
  end
  nil
end

#multi_update(objects) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/schron/identity_map_repository.rb', line 150

def multi_update(objects)
  results = super
  objects.each_with_index do |o, i|
    assign_attributes(from: results[i], to: o)
  end
  objects
end

#remove(id) ⇒ Object



158
159
160
161
162
163
# File 'lib/schron/identity_map_repository.rb', line 158

def remove(id)
  super
  object = identity_map.delete(id)
  object.freeze if object
  nil
end

#session(&block) ⇒ Object



57
58
59
60
61
62
# File 'lib/schron/identity_map_repository.rb', line 57

def session(&block)
  session_begin
  block.call
ensure
  session_end
end

#session!(&block) ⇒ Object



64
65
66
67
68
69
# File 'lib/schron/identity_map_repository.rb', line 64

def session!(&block)
  session_begin
  block.call
ensure
  session_end!
end

#session_beginObject

def respond_to_missing?(method_name, include_private = false)

@repo.respond_to?(method_name, include_private)

end



32
33
34
35
36
37
38
39
# File 'lib/schron/identity_map_repository.rb', line 32

def session_begin
  @sessions ||= []
  @sessions.push IdentityMap.new
  # @id_map ||= IdentityMap.new
  # @sessions ||= 0
  # @sessions += 1
  nil
end

#session_endObject



41
42
43
44
45
46
47
# File 'lib/schron/identity_map_repository.rb', line 41

def session_end
  @sessions.pop
  # raise 'no session is open' unless @id_map
  # @sessions -= 1
  # @id_map = nil if @sessions == 0
  nil
end

#session_end!Object



49
50
51
52
53
54
55
# File 'lib/schron/identity_map_repository.rb', line 49

def session_end!
  @sessions = []
  # raise 'no session is open' unless @id_map
  # @sessions = 0
  # @id_map = nil
  nil
end

#update(object) ⇒ Object



144
145
146
147
148
# File 'lib/schron/identity_map_repository.rb', line 144

def update(object)
  result = super
  assign_attributes from: result, to: object
  object
end