Class: EADModel::IndexedArchivalObjectPrefetcher

Inherits:
Object
  • Object
show all
Defined in:
backend/app/exporters/models/ead.rb

Overview

For a given set of ArchivalObject IDs attempt to pull the records from Solr. If they’re in there and are up-to-date, this saves us hitting the database.

In the future we might want to generalize this to other record types and use it elsewhere, but for now I’ll let it incubate here :)

Defined Under Namespace

Classes: RecordVersion

Instance Method Summary collapse

Instance Method Details

#fetch(ao_ids, resolve) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'backend/app/exporters/models/ead.rb', line 41

def fetch(ao_ids, resolve)
  # For each record of interest, calculate its URI and obtain its latest lock_version
  # and position.
  uri_to_version = {}

  ArchivalObject.filter(:id => ao_ids).select(:id, :lock_version, :position).each do |row|
    uri = ArchivalObject.uri_for(:archival_object, row[:id])
    uri_to_version[uri] = RecordVersion.new(row[:id], row[:lock_version], row[:position])
  end

  # Try the search index
  results = Search.records_for_uris(uri_to_version.keys)
  indexed_records = results['results'].map {|result| ASUtils.json_parse(result['json'])}

  # Walk through our results and throw out any that aren't up-to-date
  good_records = []
  indexed_records.each do |indexed|
    desired_version = uri_to_version.fetch(indexed['uri']).lock_version
    indexed_version = indexed['lock_version']

    desired_position = uri_to_version.fetch(indexed['uri']).position
    indexed_position = indexed['position']

    if desired_version == indexed_version
      # Reorder mode updates the position in the database without incrementing
      # the lock_version. If position in the database is different from
      # position in the index even though the lock versions are the same, set
      # position in the good record to database position which corresponds to
      # the position from re-ordering
      indexed['position'] = desired_position if indexed_position != desired_position
      good_records << indexed
    end
  end

  uris_needing_refetch = uri_to_version.keys - (good_records.map {|json| json['uri']})

  unless uris_needing_refetch.empty?
    # We've got some records that weren't available in the index.  Plan B...
    ids_needing_refetch = uris_needing_refetch.map {|uri| uri_to_version[uri][:id]}

    objs = ArchivalObject.sequel_to_jsonmodel(ArchivalObject.filter(:id => ids_needing_refetch).all)
    good_records.concat(URIResolver.resolve_references(objs, resolve))
  end

  good_records.sort_by {|record| record.fetch('position')}
end