Search in sources :

Example 1 with ObjectId

use of com.b2international.index.revision.ObjectId in project snow-owl by b2ihealthcare.

the class ComponentInactivationChangeProcessor method processInactivations.

private void processInactivations(StagingArea staging, RevisionSearcher searcher, Set<String> inactivatedConceptIds, Set<String> inactivatedComponentIds) throws IOException {
    // inactivate descriptions of inactivated concepts, take current description changes into account
    ServiceProvider context = (ServiceProvider) staging.getContext();
    ModuleIdProvider moduleIdProvider = context.service(ModuleIdProvider.class);
    if (!inactivatedConceptIds.isEmpty()) {
        final Multimap<String, RevisionDiff> changedMembersByReferencedComponentId = HashMultimap.create();
        staging.getChangedRevisions(SnomedRefSetMemberIndexEntry.class).forEach(diff -> {
            changedMembersByReferencedComponentId.put(((SnomedRefSetMemberIndexEntry) diff.newRevision).getReferencedComponentId(), diff);
        });
        // Inactivate active descriptions on inactive concepts
        try {
            Query.select(SnomedDescriptionIndexEntry.class).from(SnomedDescriptionIndexEntry.class).fields(SnomedDescriptionIndexEntry.Fields.ID, SnomedDescriptionIndexEntry.Fields.MODULE_ID).where(Expressions.builder().filter(SnomedDescriptionIndexEntry.Expressions.active()).filter(SnomedDescriptionIndexEntry.Expressions.concepts(inactivatedConceptIds)).build()).limit(PAGE_SIZE).build().stream(searcher).forEachOrdered(hits -> {
                try {
                    inactivateDescriptions(searcher, moduleIdProvider, changedMembersByReferencedComponentId, hits);
                } catch (IOException e) {
                    throw new UndeclaredThrowableException(e);
                }
            });
        } catch (UndeclaredThrowableException ute) {
            // Unwrap and throw checked exception from lambda above
            throw (IOException) ute.getCause();
        }
        final Map<ObjectId, RevisionDiff> changedRevisions = staging.getChangedRevisions();
        // Inactivate active relationships on inactive concepts
        Query.select(SnomedRelationshipIndexEntry.class).where(Expressions.builder().filter(SnomedRelationshipIndexEntry.Expressions.active()).should(SnomedRelationshipIndexEntry.Expressions.sourceIds(inactivatedConceptIds)).should(SnomedRelationshipIndexEntry.Expressions.destinationIds(inactivatedConceptIds)).build()).limit(PAGE_SIZE).build().stream(searcher).flatMap(Hits::stream).forEachOrdered(relationship -> inactivateRelationship(inactivatedComponentIds, moduleIdProvider, changedRevisions, relationship));
    }
    if (inactivatedComponentIds.isEmpty()) {
        return;
    }
    // inactivate referring members of all inactivated core component, and all members of inactivated refsets
    final Map<ObjectId, RevisionDiff> changedRevisions = staging.getChangedRevisions();
    Query.select(SnomedRefSetMemberIndexEntry.class).where(Expressions.builder().filter(SnomedRefSetMemberIndexEntry.Expressions.active()).should(SnomedRefSetMemberIndexEntry.Expressions.referencedComponentIds(inactivatedComponentIds)).should(SnomedRefSetMemberIndexEntry.Expressions.refsetIds(inactivatedComponentIds)).setMinimumNumberShouldMatch(1).build()).limit(PAGE_SIZE).build().stream(searcher).flatMap(Hits::stream).forEachOrdered(member -> inactivateReferenceSetMember(moduleIdProvider, changedRevisions, member));
}
Also used : ObjectId(com.b2international.index.revision.ObjectId) ModuleIdProvider(com.b2international.snowowl.snomed.datastore.request.ModuleRequest.ModuleIdProvider) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) RevisionDiff(com.b2international.index.revision.StagingArea.RevisionDiff) IOException(java.io.IOException)

Example 2 with ObjectId

use of com.b2international.index.revision.ObjectId in project snow-owl by b2ihealthcare.

the class BranchCompareRequest method execute.

@Override
public BranchCompareResult execute(RepositoryContext context) {
    final RevisionIndex index = context.service(RevisionIndex.class);
    final Branch branchToCompare = RepositoryRequests.branching().prepareGet(compare).build().execute(context);
    final long compareHeadTimestamp = branchToCompare.headTimestamp();
    final RevisionCompare compareResult;
    final String baseBranchPath;
    if (base != null) {
        compareResult = index.compare(base, compare, limit, excludeComponentChanges);
        baseBranchPath = base;
    } else {
        compareResult = index.compare(compare, limit, excludeComponentChanges);
        baseBranchPath = branchToCompare.parentPath();
    }
    final BranchCompareResult.Builder result = BranchCompareResult.builder(baseBranchPath, compare, compareHeadTimestamp);
    final Set<ComponentIdentifier> changedContainers = Sets.newHashSet();
    for (RevisionCompareDetail detail : compareResult.getDetails()) {
        final ObjectId affectedId;
        if (detail.isComponentChange()) {
            affectedId = detail.getComponent();
            if (!detail.getObject().isRoot() && !excludeComponentChanges) {
                changedContainers.add(ComponentIdentifier.of(detail.getObject().type(), detail.getObject().id()));
            }
        } else {
            affectedId = detail.getObject();
        }
        final ComponentIdentifier identifier = ComponentIdentifier.of(affectedId.type(), affectedId.id());
        switch(detail.getOp()) {
            case ADD:
                result.putNewComponent(identifier);
                break;
            case CHANGE:
                result.putChangedComponent(identifier);
                break;
            case REMOVE:
                result.putDeletedComponent(identifier);
                break;
        }
    }
    return result.totalNew(compareResult.getTotalAdded()).totalChanged(compareResult.getTotalChanged()).totalDeleted(compareResult.getTotalRemoved()).build(changedContainers);
}
Also used : RevisionIndex(com.b2international.index.revision.RevisionIndex) ObjectId(com.b2international.index.revision.ObjectId) Branch(com.b2international.snowowl.core.branch.Branch) RevisionCompare(com.b2international.index.revision.RevisionCompare) ComponentIdentifier(com.b2international.snowowl.core.ComponentIdentifier) RevisionCompareDetail(com.b2international.index.revision.RevisionCompareDetail)

Example 3 with ObjectId

use of com.b2international.index.revision.ObjectId in project snow-owl by b2ihealthcare.

the class ConceptChangeProcessor method process.

@Override
public void process(StagingArea staging, RevisionSearcher searcher) throws IOException {
    // collect member changes
    this.referringRefSets = HashMultimap.create(memberChangeProcessor.process(staging, searcher));
    processNewConcepts(staging);
    // collect dirty concepts that require additional properties to be set for index
    final Map<String, RevisionDiff> dirtyConceptDiffsById = Maps.uniqueIndex(staging.getChangedRevisions(SnomedConceptDocument.class).iterator(), diff -> diff.newRevision.getId());
    final Set<String> dirtyConceptIds = collectDirtyConceptIds(staging);
    // remaining new/dirty/detached descriptions should be properly processed for preferredDescriptions field
    final Map<String, SnomedDescriptionIndexEntry> affectedDescriptionsById = getDescriptionDocuments(staging, searcher);
    final Multimap<String, SnomedDescriptionIndexEntry> affectedDescriptionsByConcept = Multimaps.index(affectedDescriptionsById.values(), SnomedDescriptionIndexEntry::getConceptId);
    dirtyConceptIds.addAll(affectedDescriptionsByConcept.keySet());
    // remove all new/detached concept IDs, we've already processed them
    staging.getRemovedObjects(SnomedConceptDocument.class).map(SnomedConceptDocument::getId).forEach(dirtyConceptIds::remove);
    staging.getNewObjects(SnomedConceptDocument.class).map(SnomedConceptDocument::getId).forEach(dirtyConceptIds::remove);
    if (!dirtyConceptIds.isEmpty()) {
        final Map<ObjectId, RevisionDiff> changedRevisions = staging.getChangedRevisions();
        // fetch all dirty concept documents by their ID
        final Set<String> missingCurrentConceptIds = dirtyConceptIds.stream().filter(id -> !changedRevisions.containsKey(ObjectId.of(SnomedConcept.TYPE, id))).collect(Collectors.toSet());
        final Map<String, SnomedConceptDocument> currentConceptDocumentsById = newHashMap(Maps.uniqueIndex(searcher.get(SnomedConceptDocument.class, missingCurrentConceptIds), Revision::getId));
        dirtyConceptIds.stream().map(id -> ObjectId.of(SnomedConcept.TYPE, id)).filter(changedRevisions::containsKey).map(changedRevisions::get).map(diff -> (SnomedConceptDocument) diff.oldRevision).forEach(doc -> currentConceptDocumentsById.put(doc.getId(), doc));
        // update dirty concepts
        for (final String id : dirtyConceptIds) {
            final SnomedConceptDocument concept = dirtyConceptDiffsById.containsKey(id) ? (SnomedConceptDocument) dirtyConceptDiffsById.get(id).newRevision : null;
            final SnomedConceptDocument currentDoc = currentConceptDocumentsById.get(id);
            if (currentDoc == null) {
                throw new IllegalStateException("Current concept revision should not be null for: " + id);
            }
            final Builder doc = SnomedConceptDocument.builder(currentDoc);
            final Collection<SnomedDescriptionIndexEntry> affectedDescriptions = affectedDescriptionsByConcept.get(id);
            if (!affectedDescriptions.isEmpty()) {
                final Map<String, SnomedDescriptionFragment> updatedPreferredDescriptions = newHashMap(Maps.uniqueIndex(currentDoc.getPreferredDescriptions(), SnomedDescriptionFragment::getId));
                // add new/dirty fragments if they are preferred and active terms
                for (SnomedDescriptionIndexEntry affectedDescription : affectedDescriptions) {
                    if (staging.isNew(affectedDescription) || staging.isChanged(affectedDescription)) {
                        updatedPreferredDescriptions.remove(affectedDescription.getId());
                        if (affectedDescription.isActive() && !getPreferredLanguageMembers(affectedDescription).isEmpty()) {
                            updatedPreferredDescriptions.put(affectedDescription.getId(), toDescriptionFragment(affectedDescription));
                        }
                    }
                }
                // remove deleted descriptions
                for (SnomedDescriptionIndexEntry affectedDescription : affectedDescriptions) {
                    if (staging.isRemoved(affectedDescription)) {
                        updatedPreferredDescriptions.remove(affectedDescription.getId());
                    }
                }
                final List<SnomedDescriptionFragment> preferredDescriptions = updatedPreferredDescriptions.values().stream().sorted(DESCRIPTION_FRAGMENT_ORDER).collect(Collectors.toList());
                update(doc, preferredDescriptions, concept, currentDoc);
            } else {
                update(doc, currentDoc.getPreferredDescriptions(), concept, currentDoc);
            }
            stageChange(currentDoc, doc.build());
        }
    }
}
Also used : SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) ChangeSetProcessorBase(com.b2international.snowowl.core.repository.ChangeSetProcessorBase) Acceptability(com.b2international.snowowl.snomed.core.domain.Acceptability) java.util(java.util) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) RevisionDiff(com.b2international.index.revision.StagingArea.RevisionDiff) IconIdUpdater(com.b2international.snowowl.snomed.datastore.index.update.IconIdUpdater) TaxonomyGraph(com.b2international.snowowl.snomed.datastore.taxonomy.TaxonomyGraph) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) LongSets(com.b2international.commons.collect.LongSets) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) ObjectId(com.b2international.index.revision.ObjectId) SnomedDescriptionFragment(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionFragment) ReferenceSetMembershipUpdater(com.b2international.snowowl.snomed.datastore.index.update.ReferenceSetMembershipUpdater) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) SnomedConceptDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument) com.google.common.collect(com.google.common.collect) Nullable(javax.annotation.Nullable) RefSetMemberChange(com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Revision(com.b2international.index.revision.Revision) IOException(java.io.IOException) Taxonomy(com.b2international.snowowl.snomed.datastore.taxonomy.Taxonomy) Collectors(java.util.stream.Collectors) ParentageUpdater(com.b2international.snowowl.snomed.datastore.index.update.ParentageUpdater) StagingArea(com.b2international.index.revision.StagingArea) SnomedDescriptionIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry) Builder(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument.Builder) SnomedConceptDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument) ObjectId(com.b2international.index.revision.ObjectId) Builder(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument.Builder) SnomedDescriptionIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry) SnomedDescriptionFragment(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionFragment) RevisionDiff(com.b2international.index.revision.StagingArea.RevisionDiff)

Aggregations

ObjectId (com.b2international.index.revision.ObjectId)3 RevisionDiff (com.b2international.index.revision.StagingArea.RevisionDiff)2 IOException (java.io.IOException)2 LongSets (com.b2international.commons.collect.LongSets)1 Revision (com.b2international.index.revision.Revision)1 RevisionCompare (com.b2international.index.revision.RevisionCompare)1 RevisionCompareDetail (com.b2international.index.revision.RevisionCompareDetail)1 RevisionIndex (com.b2international.index.revision.RevisionIndex)1 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)1 StagingArea (com.b2international.index.revision.StagingArea)1 ComponentIdentifier (com.b2international.snowowl.core.ComponentIdentifier)1 ServiceProvider (com.b2international.snowowl.core.ServiceProvider)1 Branch (com.b2international.snowowl.core.branch.Branch)1 ChangeSetProcessorBase (com.b2international.snowowl.core.repository.ChangeSetProcessorBase)1 Concepts (com.b2international.snowowl.snomed.common.SnomedConstants.Concepts)1 Acceptability (com.b2international.snowowl.snomed.core.domain.Acceptability)1 SnomedConcept (com.b2international.snowowl.snomed.core.domain.SnomedConcept)1 SnomedConceptDocument (com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument)1 Builder (com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument.Builder)1 SnomedDescriptionFragment (com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionFragment)1