Module: ReindexTopContainers

Defined in:
backend/app/model/mixins/reindex_top_containers.rb

Instance Method Summary collapse

Instance Method Details

#accession_instance_root_record_update(id) ⇒ Object



65
66
67
68
69
# File 'backend/app/model/mixins/reindex_top_containers.rb', line 65

def accession_instance_root_record_update(id)
  TopContainer.linked_instance_ds.filter(
    :instance__accession_id => id
  ).update(:top_container__system_mtime => Time.now)
end

#ao_instance_root_record_update(id) ⇒ Object



58
59
60
61
62
63
# File 'backend/app/model/mixins/reindex_top_containers.rb', line 58

def ao_instance_root_record_update(id)
  TopContainer.linked_instance_ds.join(
    :archival_object, :archival_object__id => :instance__archival_object_id).
    filter(:archival_object__root_record_id => id
  ).update(:top_container__system_mtime => Time.now)
end

#deleteObject



82
83
84
85
# File 'backend/app/model/mixins/reindex_top_containers.rb', line 82

def delete
  reindex_top_containers
  super
end

#reindex_top_containers(extra_ids = []) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'backend/app/model/mixins/reindex_top_containers.rb', line 3

def reindex_top_containers(extra_ids = [])
  if !DB.respond_to?(:supports_join_updates?) || !DB.supports_join_updates?
    Log.warn("Invoking slow path for reindexing top containers")
    return reindex_top_containers_by_any_means_necessary(extra_ids)
  end

  # Find any relationships between a top container and any instance within the current tree.
  root_record = if self.class == ArchivalObject
                  self.class.root_model[self.root_record_id]
                else
                  self
                end

  if !extra_ids.empty?
    TopContainer.filter(:id => extra_ids).update(:system_mtime => Time.now)
  end

  if root_record.is_a?(Resource)
    resource_instance_update(root_record.id)
    ao_instance_root_record_update(root_record.id)
  elsif root_record.is_a?(ArchivalObject)
    Log.warn("Invoking slow path for reindexing top containers")
    reindex_top_containers_by_any_means_necessary(extra_ids)
  elsif root_record.is_a?(Accession)
    accession_instance_root_record_update(root_record.id)
  end
end

#reindex_top_containers_by_any_means_necessary(extra_ids) ⇒ Object

Slow path for weird data or DBs that don’t support updates on joins (like derby/h2)



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'backend/app/model/mixins/reindex_top_containers.rb', line 33

def reindex_top_containers_by_any_means_necessary(extra_ids)
  # Find any relationships between a top container and any instance within the current tree.
  root_record = if self.class == ArchivalObject
                  self.class.root_model[self.root_record_id]
                else
                  self
                end
  tree_object_graph = root_record.object_graph
  top_container_link_rlshp = SubContainer.find_relationship(:top_container_link)
  relationship_ids = tree_object_graph.ids_for(top_container_link_rlshp)

  # Update the mtimes of each top container
  DB.open do |db|
    top_container_ids = db[:top_container_link_rlshp].filter(:id => relationship_ids).map(:top_container_id)
    top_container_ids.concat(extra_ids)
    TopContainer.filter(:id => top_container_ids).update(:system_mtime => Time.now)
  end
end

#resource_instance_update(id) ⇒ Object



52
53
54
55
56
# File 'backend/app/model/mixins/reindex_top_containers.rb', line 52

def resource_instance_update(id)
  TopContainer.linked_instance_ds.filter(
    instance__resource_id: id
  ).update(:top_container__system_mtime => Time.now)
end

#set_parent_and_position(*) ⇒ Object

not defined in accession or resource



72
73
74
75
# File 'backend/app/model/mixins/reindex_top_containers.rb', line 72

def set_parent_and_position(*)
  super
  reindex_top_containers
end

#set_root(*) ⇒ Object



77
78
79
80
# File 'backend/app/model/mixins/reindex_top_containers.rb', line 77

def set_root(*)
  super
  reindex_top_containers
end

#update_from_json(json, opts = {}, apply_nested_records = true) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'backend/app/model/mixins/reindex_top_containers.rb', line 88

def update_from_json(json, opts = {}, apply_nested_records = true)
  # we need to reindex top containers for instances about to be zapped
  # so remember the top containers we currently link to ...
  top_container_ids = instance.map {|instance|
                        # don't assume a sub_container - it might be a digital object instance
                        instance.sub_container.map {|sc| sc.related_records(:top_container_link).id}
                      }.flatten.compact

  result = super

  # ... and pass them in as extras
  reindex_top_containers(top_container_ids) unless opts[:skip_reindex_top_containers]

  result
end