use of com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry.Builder in project snow-owl by b2ihealthcare.
the class DescriptionChangeProcessor method process.
@Override
public void process(StagingArea staging, RevisionSearcher searcher) throws IOException {
final Map<String, Multimap<Acceptability, RefSetMemberChange>> acceptabilityChangesByDescription = new DescriptionAcceptabilityChangeProcessor().process(staging, searcher);
final Multimap<String, RefSetMemberChange> referringRefSets = HashMultimap.create(memberChangeProcessor.process(staging, searcher));
// (re)index new and dirty descriptions
final Map<String, SnomedDescriptionIndexEntry> newDescriptionsById = staging.getNewObjects(SnomedDescriptionIndexEntry.class).collect(Collectors.toMap(description -> description.getId(), description -> description));
final Map<String, SnomedDescriptionIndexEntry> changedDescriptionsById = staging.getChangedRevisions(SnomedDescriptionIndexEntry.class).collect(Collectors.toMap(diff -> diff.newRevision.getId(), diff -> (SnomedDescriptionIndexEntry) diff.newRevision));
final Set<String> changedDescriptionIds = newHashSet(changedDescriptionsById.keySet());
final Set<String> referencedDescriptionIds = newHashSet(referringRefSets.keySet());
referencedDescriptionIds.removeAll(newDescriptionsById.keySet());
changedDescriptionIds.addAll(referencedDescriptionIds);
// load the known descriptions
final Iterable<SnomedDescriptionIndexEntry> changedDescriptionHits = searcher.get(SnomedDescriptionIndexEntry.class, changedDescriptionIds);
final Map<String, SnomedDescriptionIndexEntry> changedDescriptionRevisionsById = Maps.uniqueIndex(changedDescriptionHits, Revision::getId);
// load missing descriptions with only changed acceptability values
final Set<String> descriptionsToBeLoaded = newHashSet();
for (String descriptionWithAccepatibilityChange : acceptabilityChangesByDescription.keySet()) {
if (!newDescriptionsById.containsKey(descriptionWithAccepatibilityChange) && !changedDescriptionIds.contains(descriptionWithAccepatibilityChange)) {
descriptionsToBeLoaded.add(descriptionWithAccepatibilityChange);
}
}
// process changes
for (final String id : Iterables.concat(newDescriptionsById.keySet(), changedDescriptionIds)) {
if (newDescriptionsById.containsKey(id)) {
final SnomedDescriptionIndexEntry description = newDescriptionsById.get(id);
final Builder doc = SnomedDescriptionIndexEntry.builder(description);
processChanges(id, doc, null, acceptabilityChangesByDescription.get(id), referringRefSets);
stageNew(doc.build());
} else if (changedDescriptionIds.contains(id)) {
final SnomedDescriptionIndexEntry currentDoc = changedDescriptionRevisionsById.get(id);
if (currentDoc == null) {
throw new IllegalStateException(String.format("Current description revision should not be null for: %s", id));
}
final SnomedDescriptionIndexEntry description = changedDescriptionsById.get(id);
final Builder doc;
if (description != null) {
doc = SnomedDescriptionIndexEntry.builder(description);
} else {
doc = SnomedDescriptionIndexEntry.builder(currentDoc);
}
processChanges(id, doc, currentDoc, acceptabilityChangesByDescription.get(id), referringRefSets);
stageChange(currentDoc, doc.build());
} else {
throw new IllegalStateException(String.format("Description %s is missing from new and dirty maps", id));
}
}
// process cascading acceptability changes in unchanged docs
if (!descriptionsToBeLoaded.isEmpty()) {
for (SnomedDescriptionIndexEntry unchangedDescription : searcher.get(SnomedDescriptionIndexEntry.class, descriptionsToBeLoaded)) {
final Builder doc = SnomedDescriptionIndexEntry.builder(unchangedDescription);
processChanges(unchangedDescription.getId(), doc, unchangedDescription, acceptabilityChangesByDescription.get(unchangedDescription.getId()), HashMultimap.<String, RefSetMemberChange>create());
stageChange(unchangedDescription, doc.build());
}
}
}
Aggregations