Module: ASpaceExport::LazyChildEnumerations

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



178
179
180
181
182
183
184
# File 'backend/app/exporters/lib/export_helpers.rb', line 178

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

#ensure_prefetched(index) ⇒ Object



189
190
191
192
193
194
195
196
197
198
# File 'backend/app/exporters/lib/export_helpers.rb', line 189

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



200
201
202
203
204
205
206
207
208
# File 'backend/app/exporters/lib/export_helpers.rb', line 200

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