Module: ASpaceExport::LazyChildEnumerations

Included in:
EADModel
Defined in:
backend/app/exporters/lib/export_helpers.rb

Constant Summary collapse

PREFETCH_SIZE =

If we’re asked for child 0, grab records 0..PREFETCH_SIZE from the DB

20

Instance Method Summary collapse

Instance Method Details

#children_indexesObject



160
161
162
163
164
165
166
# File 'backend/app/exporters/lib/export_helpers.rb', line 160

def children_indexes
  if @children.count > 0
    (0...@children.count)
  else
    []
  end
end

#ensure_prefetched(index) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'backend/app/exporters/lib/export_helpers.rb', line 171

def ensure_prefetched(index)
  unless @prefetched_ids && @prefetched_ids.cover?(index)
    new_start = (index / PREFETCH_SIZE) * PREFETCH_SIZE
    new_end = [new_start + PREFETCH_SIZE,
               @children.count].min

    @prefetched_ids = Range.new(new_start, new_end, true)
    @prefetched_records = @child_class.prefetch(@prefetched_ids.map {|index| @children[index]}, @repo_id)
  end
end

#get_child(index) ⇒ Object



182
183
184
185
186
187
188
189
190
# File 'backend/app/exporters/lib/export_helpers.rb', line 182

def get_child(index)
  if @child_class.respond_to?(:prefetch)
    ensure_prefetched(index)
    rec = @prefetched_records[index % PREFETCH_SIZE]
    @child_class.from_prefetched(@children[index], rec, @repo_id)
  else
    @child_class.new(@children[index], @repo_id)
  end
end