Search in sources :

Example 1 with Builder

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());
        }
    }
}
Also used : Builder(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry.Builder) RefSetMemberChange(com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange) ChangeSetProcessorBase(com.b2international.snowowl.core.repository.ChangeSetProcessorBase) Acceptability(com.b2international.snowowl.snomed.core.domain.Acceptability) Revision(com.b2international.index.revision.Revision) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) SnomedDescription(com.b2international.snowowl.snomed.core.domain.SnomedDescription) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) Map(java.util.Map) StagingArea(com.b2international.index.revision.StagingArea) ReferenceSetMembershipUpdater(com.b2international.snowowl.snomed.datastore.index.update.ReferenceSetMembershipUpdater) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) SnomedDescriptionIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry) Collections(java.util.Collections) com.google.common.collect(com.google.common.collect) RefSetMemberChange(com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange) Builder(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry.Builder) SnomedDescriptionIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry) Revision(com.b2international.index.revision.Revision)

Aggregations

Revision (com.b2international.index.revision.Revision)1 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)1 StagingArea (com.b2international.index.revision.StagingArea)1 ChangeSetProcessorBase (com.b2international.snowowl.core.repository.ChangeSetProcessorBase)1 Acceptability (com.b2international.snowowl.snomed.core.domain.Acceptability)1 SnomedDescription (com.b2international.snowowl.snomed.core.domain.SnomedDescription)1 SnomedDescriptionIndexEntry (com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry)1 Builder (com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry.Builder)1 RefSetMemberChange (com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange)1 ReferenceSetMembershipUpdater (com.b2international.snowowl.snomed.datastore.index.update.ReferenceSetMembershipUpdater)1 com.google.common.collect (com.google.common.collect)1 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1