Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): transaction referential integrity check improvements #1303

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.xtext.util.Pair;
import org.eclipse.xtext.util.Tuples;

import com.b2international.commons.CompareUtils;
import com.b2international.commons.exceptions.AlreadyExistsException;
import com.b2international.commons.exceptions.BadRequestException;
import com.b2international.commons.exceptions.ConflictException;
import com.b2international.commons.exceptions.CycleDetectedException;
import com.b2international.index.Index;
Expand Down Expand Up @@ -304,11 +306,34 @@
if (!ensurePresent.isEmpty()) {
// check if any of the listed IDs are already present in the index and if NOT, report ComponentNotFoundException for the first registered ID (same behavior as before with explicit checks)
for (Class<? extends Revision> type : ensurePresent.keySet()) {
final Collection<String> idsToCheck = ensurePresent.get(type).stream().filter(id -> !IComponent.ROOT_ID.equals(id)).toList();
final Map<String, ?> existingComponents = Maps.uniqueIndex(fetchComponents(idsToCheck, type), Revision::getId);
for (String idToCheck : idsToCheck) {
if (!existingComponents.containsKey(idToCheck)) {
throw new ComponentNotFoundException(DocumentMapping.getDocType(type), idToCheck).toBadRequestException();
final Set<String> idsToCheck = ensurePresent.get(type).stream().filter(id -> !IComponent.ROOT_ID.equals(id)).collect(Collectors.toSet());

// first check if there are any deletion that are present in the ID set, if yes, then those IDs won't be available anymore, report a transaction error
var deletedIds = staging.getRemovedObjects(Revision.class)
.map(Revision::getId)
.filter(idsToCheck::contains)
.collect(Collectors.toSet());

if (!deletedIds.isEmpty()) {
throw new BadRequestException("Transaction would delete components that are still referenced by other components.")
.withAdditionalInfo(Map.of("ids", deletedIds));

Check warning on line 319 in core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/repository/RepositoryTransactionContext.java

View check run for this annotation

Codecov / codecov/patch

core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/repository/RepositoryTransactionContext.java#L318-L319

Added lines #L318 - L319 were not covered by tests
}

// then remove all new/changed objects that are already present in the tx staging area, those can be considered ensured
staging.getNewObjects(Revision.class)
.map(Revision::getId)
.forEach(idsToCheck::remove);
staging.getChangedObjects(Revision.class)
.map(Revision::getId)
.forEach(idsToCheck::remove);

// then if there are any remaining IDs to check, fetch them from the store
if (!idsToCheck.isEmpty()) {
final Map<String, ?> existingComponents = Maps.uniqueIndex(fetchComponents(idsToCheck, type), Revision::getId);
for (String idToCheck : idsToCheck) {
if (!existingComponents.containsKey(idToCheck)) {
throw new ComponentNotFoundException(DocumentMapping.getDocType(type), idToCheck).toBadRequestException();

Check warning on line 335 in core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/repository/RepositoryTransactionContext.java

View check run for this annotation

Codecov / codecov/patch

core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/repository/RepositoryTransactionContext.java#L335

Added line #L335 was not covered by tests
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public Request<TransactionContext, String> toCreateRequest(String containerId) {
return SnomedRequests.prepareNewMember()
.setId(getId())
.setActive(isActive())
.setReferencedComponentId(containerId)
.setReferencedComponentId(containerId != null ? containerId : getReferencedComponentId())
.setRefsetId(getRefsetId())
.setModuleId(getModuleId())
.setProperties(getProperties())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2021 B2i Healthcare, https://b2ihealthcare.com
* Copyright 2011-2024 B2i Healthcare, https://b2ihealthcare.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -102,7 +102,7 @@ public void init(CB component, TransactionContext context) {

// check that the module does exist in the system
if (!id.equals(moduleId)) {
context.lookup(moduleId, SnomedConceptDocument.class);
context.ensurePresent(SnomedConceptDocument.class, moduleId);
}

if (effectiveTime == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2021 B2i Healthcare, https://b2ihealthcare.com
* Copyright 2011-2024 B2i Healthcare, https://b2ihealthcare.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -59,7 +59,7 @@ protected SnomedConceptDocument.Builder create() {
public void init(SnomedConceptDocument.Builder component, TransactionContext context) {
super.init(component, context);
// check that the definitionStatus concept does exist before using it in this concept
context.lookup(definitionStatusId, SnomedConceptDocument.class);
context.ensurePresent(SnomedConceptDocument.class, definitionStatusId);
component.definitionStatusId(definitionStatusId);
component.exhaustive(exhaustive);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2020 B2i Healthcare, https://b2ihealthcare.com
* Copyright 2011-2024 B2i Healthcare, https://b2ihealthcare.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -89,12 +89,18 @@ public SnomedDescriptionBuilder withConcept(String concept) {
@Override
public void init(SnomedDescriptionIndexEntry.Builder component, TransactionContext context) {
super.init(component, context);
component.caseSignificanceId(context.lookup(caseSignificanceId, SnomedConceptDocument.class).getId());
component.typeId(context.lookup(type, SnomedConceptDocument.class).getId());
context.ensurePresent(SnomedConceptDocument.class, caseSignificanceId);
component.caseSignificanceId(caseSignificanceId);

context.ensurePresent(SnomedConceptDocument.class, type);
component.typeId(type);

component.term(term);
component.languageCode(languageCode);

if (concept != null) {
component.conceptId(context.lookup(concept, SnomedConceptDocument.class).getId());
context.ensurePresent(SnomedConceptDocument.class, concept);
component.conceptId(concept);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2021 B2i Healthcare, https://b2ihealthcare.com
* Copyright 2011-2024 B2i Healthcare, https://b2ihealthcare.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -141,28 +141,31 @@ protected SnomedRelationshipIndexEntry.Builder create() {
return SnomedRelationshipIndexEntry.builder();
}

private String ensureConceptExists(final String conceptId, final TransactionContext context) {
return context.lookup(conceptId, SnomedConceptDocument.class).getId();
}

@Override
public void init(final SnomedRelationshipIndexEntry.Builder component, final TransactionContext context) {
super.init(component, context);

if (sourceId != null) {
component.sourceId(ensureConceptExists(sourceId, context));
if (sourceId != null) {
context.ensurePresent(SnomedConceptDocument.class, sourceId);
component.sourceId(sourceId);
}

if (destinationId != null) {
component.destinationId(ensureConceptExists(destinationId, context));
context.ensurePresent(SnomedConceptDocument.class, destinationId);
component.destinationId(destinationId);
}

component.typeId(ensureConceptExists(typeId, context));
context.ensurePresent(SnomedConceptDocument.class, typeId);
component.typeId(typeId);
component.destinationNegated(destinationNegated);
component.value(value);
component.relationshipGroup(relationshipGroup);
component.unionGroup(unionGroup);
component.characteristicTypeId(ensureConceptExists(characteristicTypeId, context));
component.modifierId(ensureConceptExists(modifierId, context));

context.ensurePresent(SnomedConceptDocument.class, characteristicTypeId);
component.characteristicTypeId(characteristicTypeId);

context.ensurePresent(SnomedConceptDocument.class, modifierId);
component.modifierId(modifierId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,19 @@

import static com.google.common.collect.Sets.newHashSet;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

import com.b2international.collections.PrimitiveSets;
import com.b2international.commons.exceptions.BadRequestException;
import com.b2international.snowowl.core.domain.TransactionContext;
import com.b2international.snowowl.core.events.Request;
import com.b2international.snowowl.core.exceptions.ComponentNotFoundException;
import com.b2international.snowowl.snomed.common.SnomedConstants.Concepts;
import com.b2international.snowowl.snomed.common.SnomedRf2Headers;
import com.b2international.snowowl.snomed.core.domain.Acceptability;
import com.b2international.snowowl.snomed.core.domain.ConstantIdStrategy;
import com.b2international.snowowl.snomed.core.domain.SnomedConcept;
import com.b2international.snowowl.snomed.core.domain.SnomedConcepts;
import com.b2international.snowowl.snomed.core.domain.SubclassDefinitionStatus;
import com.b2international.snowowl.snomed.core.domain.*;
import com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType;
import com.b2international.snowowl.snomed.core.store.SnomedComponents;
import com.b2international.snowowl.snomed.datastore.SnomedRefSetUtil;
Expand All @@ -50,6 +39,9 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multiset;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

/**
* @since 4.5
*/
Expand Down
Loading