Search in sources :

Example 1 with RefSetMemberChange

use of com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange 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)

Example 2 with RefSetMemberChange

use of com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange in project snow-owl by b2ihealthcare.

the class ReferringMemberChangeProcessor method process.

public Multimap<String, RefSetMemberChange> process(StagingArea staging, RevisionSearcher searcher) throws IOException {
    final Multimap<String, RefSetMemberChange> memberChanges = HashMultimap.create();
    // process new members
    staging.getNewObjects(SnomedRefSetMemberIndexEntry.class).filter(this::byReferencedComponentType).forEach((newMember) -> {
        addChange(memberChanges, newMember, MemberChangeKind.ADDED);
    });
    // process dirty members
    staging.getChangedRevisions(SnomedRefSetMemberIndexEntry.class).filter(diff -> byReferencedComponentType((SnomedRefSetMemberIndexEntry) diff.newRevision)).forEach((diff) -> {
        RevisionPropertyDiff propChange = diff.getRevisionPropertyDiff(SnomedRefSetMemberIndexEntry.Fields.ACTIVE);
        if (propChange != null) {
            addChange(memberChanges, (SnomedRefSetMemberIndexEntry) diff.newRevision, MemberChangeKind.CHANGED);
        }
    });
    // process detached members
    staging.getRemovedObjects(SnomedRefSetMemberIndexEntry.class).filter(this::byReferencedComponentType).forEach(doc -> {
        final String uuid = doc.getId();
        final String referencedComponentId = doc.getReferencedComponentId();
        final String refSetId = doc.getRefsetId();
        memberChanges.put(referencedComponentId, new RefSetMemberChange(uuid, refSetId, MemberChangeKind.REMOVED, doc.isActive()));
    });
    return memberChanges;
}
Also used : RefSetMemberChange(com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange) HashMultimap(com.google.common.collect.HashMultimap) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) StagingArea(com.b2international.index.revision.StagingArea) IOException(java.io.IOException) RevisionPropertyDiff(com.b2international.index.revision.StagingArea.RevisionPropertyDiff) Multimap(com.google.common.collect.Multimap) MemberChangeKind(com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange.MemberChangeKind) RefSetMemberChange(com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange) RevisionPropertyDiff(com.b2international.index.revision.StagingArea.RevisionPropertyDiff)

Example 3 with RefSetMemberChange

use of com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange in project snow-owl by b2ihealthcare.

the class ReferringMemberChangeProcessor method addChange.

private void addChange(final Multimap<String, RefSetMemberChange> memberChanges, SnomedRefSetMemberIndexEntry member, MemberChangeKind changeKind) {
    final String uuid = member.getId();
    final String refSetId = member.getRefsetId();
    memberChanges.put(member.getReferencedComponentId(), new RefSetMemberChange(uuid, refSetId, changeKind, member.isActive()));
}
Also used : RefSetMemberChange(com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange)

Example 4 with RefSetMemberChange

use of com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange in project snow-owl by b2ihealthcare.

the class DescriptionAcceptabilityChangeProcessor method process.

public Map<String, Multimap<Acceptability, RefSetMemberChange>> process(StagingArea staging, RevisionSearcher searcher) throws IOException {
    final Multimap<String, RefSetMemberChange> preferredMemberChanges = HashMultimap.create();
    final Multimap<String, RefSetMemberChange> acceptableMemberChanges = HashMultimap.create();
    // add active new and active dirty members
    final Stream<SnomedRefSetMemberIndexEntry> newAndDirtyMembers = Streams.concat(staging.getNewObjects(SnomedRefSetMemberIndexEntry.class).filter(member -> member.getReferenceSetType() == SnomedRefSetType.LANGUAGE), staging.getChangedRevisions(SnomedRefSetMemberIndexEntry.class).map(diff -> (SnomedRefSetMemberIndexEntry) diff.newRevision).filter(member -> member.getReferenceSetType() == SnomedRefSetType.LANGUAGE));
    newAndDirtyMembers.forEach(member -> {
        if (member.isActive()) {
            final String uuid = member.getId();
            final String refSetId = member.getRefsetId();
            final RefSetMemberChange change = new RefSetMemberChange(uuid, refSetId, MemberChangeKind.ADDED, member.isActive());
            registerChange(preferredMemberChanges, acceptableMemberChanges, member.getAcceptabilityId(), member.getReferencedComponentId(), change);
        }
    });
    // remove dirty inactive (and/or changed in acceptability) members
    final List<SnomedRefSetMemberIndexEntry> dirtyMembers = staging.getChangedRevisions(SnomedRefSetMemberIndexEntry.class).map(diff -> (SnomedRefSetMemberIndexEntry) diff.newRevision).filter(member -> member.getReferenceSetType() == SnomedRefSetType.LANGUAGE).collect(Collectors.toList());
    final Set<String> dirtyMemberIds = dirtyMembers.stream().map(SnomedRefSetMemberIndexEntry::getId).collect(Collectors.toSet());
    final List<SnomedRefSetMemberIndexEntry> detachedLanguageMembers = staging.getRemovedObjects(SnomedRefSetMemberIndexEntry.class).filter(member -> member.getReferenceSetType() == SnomedRefSetType.LANGUAGE).collect(Collectors.toList());
    final Map<String, SnomedRefSetMemberIndexEntry> currentRevisionsByMemberId = newHashMap();
    detachedLanguageMembers.forEach(member -> currentRevisionsByMemberId.put(member.getId(), member));
    searcher.get(SnomedRefSetMemberIndexEntry.class, dirtyMemberIds).forEach(member -> currentRevisionsByMemberId.put(member.getId(), member));
    for (SnomedRefSetMemberIndexEntry member : dirtyMembers) {
        final String uuid = member.getId();
        final String refSetId = member.getRefsetId();
        final RefSetMemberChange change = new RefSetMemberChange(uuid, refSetId, MemberChangeKind.REMOVED, member.isActive());
        final SnomedRefSetMemberIndexEntry before = currentRevisionsByMemberId.get(member.getId());
        if (before != null) {
            final String beforeAcceptabilityId = before.getAcceptabilityId();
            final boolean beforeActive = before.isActive();
            final boolean acceptabilityChanged = !member.getAcceptabilityId().equals(beforeAcceptabilityId);
            if (beforeActive && acceptabilityChanged) {
                registerChange(preferredMemberChanges, acceptableMemberChanges, beforeAcceptabilityId, member.getReferencedComponentId(), change);
            }
        }
        if (!member.isActive()) {
            registerChange(preferredMemberChanges, acceptableMemberChanges, member.getAcceptabilityId(), member.getReferencedComponentId(), change);
        }
    }
    for (final SnomedRefSetMemberIndexEntry before : detachedLanguageMembers) {
        if (before.isActive()) {
            final String uuid = before.getId();
            final String refSetId = before.getRefsetId();
            final String referencedComponentId = before.getReferencedComponentId();
            final String beforeAcceptabilityId = before.getAcceptabilityId();
            final RefSetMemberChange change = new RefSetMemberChange(uuid, refSetId, MemberChangeKind.REMOVED, before.isActive());
            registerChange(preferredMemberChanges, acceptableMemberChanges, beforeAcceptabilityId, referencedComponentId, change);
        }
    }
    final Map<String, Multimap<Acceptability, RefSetMemberChange>> changes = newHashMap();
    for (String descriptionId : Iterables.concat(preferredMemberChanges.keySet(), acceptableMemberChanges.keySet())) {
        if (!changes.containsKey(descriptionId)) {
            changes.put(descriptionId, HashMultimap.<Acceptability, RefSetMemberChange>create());
        }
        final Multimap<Acceptability, RefSetMemberChange> memberChanges = changes.get(descriptionId);
        memberChanges.putAll(Acceptability.PREFERRED, preferredMemberChanges.get(descriptionId));
        memberChanges.putAll(Acceptability.ACCEPTABLE, acceptableMemberChanges.get(descriptionId));
    }
    return changes;
}
Also used : RefSetMemberChange(com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange) Acceptability(com.b2international.snowowl.snomed.core.domain.Acceptability) Iterables(com.google.common.collect.Iterables) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) Set(java.util.Set) IOException(java.io.IOException) Multimap(com.google.common.collect.Multimap) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) List(java.util.List) Stream(java.util.stream.Stream) HashMultimap(com.google.common.collect.HashMultimap) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) Map(java.util.Map) StagingArea(com.b2international.index.revision.StagingArea) MemberChangeKind(com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange.MemberChangeKind) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) RefSetMemberChange(com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange) Acceptability(com.b2international.snowowl.snomed.core.domain.Acceptability) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap)

Example 5 with RefSetMemberChange

use of com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange in project snow-owl by b2ihealthcare.

the class RelationshipChangeProcessor method process.

@Override
public void process(StagingArea staging, RevisionSearcher searcher) throws IOException {
    final Multimap<String, RefSetMemberChange> referringRefSets = memberChangeProcessor.process(staging, searcher);
    final Set<String> referencedRelationshipIds = newHashSet(referringRefSets.keySet());
    staging.getNewObjects(SnomedRelationshipIndexEntry.class).map(SnomedRelationshipIndexEntry::getId).forEach(referencedRelationshipIds::remove);
    final Map<String, SnomedRelationshipIndexEntry> changedRelationshipsById = staging.getChangedRevisions(SnomedRelationshipIndexEntry.class).map(diff -> (SnomedRelationshipIndexEntry) diff.newRevision).collect(Collectors.toMap(relationship -> relationship.getId(), relationship -> relationship));
    final Set<String> changedRelationshipIds = newHashSet(changedRelationshipsById.keySet());
    changedRelationshipIds.addAll(referencedRelationshipIds);
    final Iterable<SnomedRelationshipIndexEntry> changedRelationshipHits = searcher.get(SnomedRelationshipIndexEntry.class, changedRelationshipIds);
    final Map<String, SnomedRelationshipIndexEntry> changedRelationshipRevisionsById = Maps.uniqueIndex(changedRelationshipHits, Revision::getId);
    for (final String id : changedRelationshipIds) {
        final SnomedRelationshipIndexEntry currentDoc = changedRelationshipRevisionsById.get(id);
        if (currentDoc == null) {
            throw new IllegalStateException(String.format("Current relationship revision should not be null for %s", id));
        }
        final SnomedRelationshipIndexEntry relationship = changedRelationshipsById.get(id);
        final Builder doc;
        if (relationship != null) {
            doc = SnomedRelationshipIndexEntry.builder(relationship);
        } else {
            doc = SnomedRelationshipIndexEntry.builder(currentDoc);
        }
        final Collection<String> currentMemberOf = currentDoc.getMemberOf();
        final Collection<String> currentActiveMemberOf = currentDoc.getActiveMemberOf();
        new ReferenceSetMembershipUpdater(referringRefSets.removeAll(id), currentMemberOf, currentActiveMemberOf).update(doc);
        stageChange(currentDoc, doc.build());
    }
}
Also used : RefSetMemberChange(com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange) ChangeSetProcessorBase(com.b2international.snowowl.core.repository.ChangeSetProcessorBase) Revision(com.b2international.index.revision.Revision) SnomedRelationshipIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) Multimap(com.google.common.collect.Multimap) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) SnomedRelationship(com.b2international.snowowl.snomed.core.domain.SnomedRelationship) 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) Builder(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Builder) RefSetMemberChange(com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange) Builder(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Builder) ReferenceSetMembershipUpdater(com.b2international.snowowl.snomed.datastore.index.update.ReferenceSetMembershipUpdater) Revision(com.b2international.index.revision.Revision) SnomedRelationshipIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry)

Aggregations

RefSetMemberChange (com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange)5 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)4 StagingArea (com.b2international.index.revision.StagingArea)4 IOException (java.io.IOException)4 Multimap (com.google.common.collect.Multimap)3 Map (java.util.Map)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 Revision (com.b2international.index.revision.Revision)2 ChangeSetProcessorBase (com.b2international.snowowl.core.repository.ChangeSetProcessorBase)2 Acceptability (com.b2international.snowowl.snomed.core.domain.Acceptability)2 SnomedRefSetMemberIndexEntry (com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry)2 MemberChangeKind (com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange.MemberChangeKind)2 ReferenceSetMembershipUpdater (com.b2international.snowowl.snomed.datastore.index.update.ReferenceSetMembershipUpdater)2 HashMultimap (com.google.common.collect.HashMultimap)2 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)2 Collection (java.util.Collection)2 RevisionPropertyDiff (com.b2international.index.revision.StagingArea.RevisionPropertyDiff)1 Concepts (com.b2international.snowowl.snomed.common.SnomedConstants.Concepts)1 SnomedDescription (com.b2international.snowowl.snomed.core.domain.SnomedDescription)1