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

DG-1893 Support append/remove/replace for Tags #3792

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -61,6 +61,11 @@ public class AtlasEntityHeader extends AtlasStruct implements Serializable {
private String displayText = null;
private List<String> classificationNames = null;
private List<AtlasClassification> classifications = null;
private List<AtlasClassification> appendClassifications = null;


private List<AtlasClassification> updateClassifications = null;
private List<AtlasClassification> removeClassifications = null;
private List<String> meaningNames = null;
private List<AtlasTermAssignmentHeader> meanings = null;
private Boolean isIncomplete = Boolean.FALSE;
Expand Down Expand Up @@ -101,6 +106,29 @@ public AtlasEntityHeader(String typeName, Map<String, Object> attributes) {
setLabels(null);
}

public List<AtlasClassification> getAppendClassifications() {
return appendClassifications;
}

public void setAppendClassifications(List<AtlasClassification> appendClassifications) {
this.appendClassifications = appendClassifications;
}

public List<AtlasClassification> getUpdateClassifications() {
return updateClassifications;
}

public void setUpdateClassifications(List<AtlasClassification> updateClassifications) {
this.updateClassifications = updateClassifications;
}

public List<AtlasClassification> getRemoveClassifications() {
return removeClassifications;
}

public void setRemoveClassifications(List<AtlasClassification> removeClassifications) {
this.removeClassifications = removeClassifications;
}

public AtlasEntityHeader(String typeName, String guid, Map<String, Object> attributes) {
super(typeName, attributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.apache.atlas.AtlasConfiguration.ENTITY_CHANGE_NOTIFY_IGNORE_RELATIONSHIP_ATTRIBUTES;

Expand Down Expand Up @@ -154,7 +156,7 @@ public Updater(AtlasTypeRegistry typeRegistry, AtlasEntityStore entitiesStore,
this(AtlasGraphProvider.getGraphInstance(), typeRegistry, entitiesStore, entityGraphMapper, entityChangeNotifier, instanceConverter);
}

public void setClassifications(Map<String, AtlasEntityHeader> map) throws AtlasBaseException {
public void setClassifications(Map<String, AtlasEntityHeader> map, boolean overrideClassifications) throws AtlasBaseException {
RequestContext.get().setDelayTagNotifications(true);

for (String guid : map.keySet()) {
Expand All @@ -178,8 +180,12 @@ public void setClassifications(Map<String, AtlasEntityHeader> map) throws AtlasB
if (entityToBeChanged == null) {
throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, guid);
}

Map<String, List<AtlasClassification>> operationListMap = computeChanges(incomingEntityHeader, entityToBeChanged);
Map<String, List<AtlasClassification>> operationListMap = new HashMap<>();
if(overrideClassifications) {
operationListMap = computeChanges(incomingEntityHeader, entityToBeChanged);
} else {
operationListMap = validateAndTransfer(incomingEntityHeader, entityToBeChanged);
}
try {
commitChanges(guid, typeName, operationListMap);
} catch (AtlasBaseException e) {
Expand Down Expand Up @@ -236,6 +242,49 @@ public void setClassifications(Map<String, AtlasEntityHeader> map) throws AtlasB
RequestContext.get().setDelayTagNotifications(false);
}

private Map<String, List<AtlasClassification>> validateAndTransfer(AtlasEntityHeader incomingEntityHeader, AtlasEntityHeader entityToBeChanged) throws AtlasBaseException {
Map<String, List<AtlasClassification>> operationListMap = new HashMap<>();
hr2904 marked this conversation as resolved.
Show resolved Hide resolved

Set<String> requiredClassificationKeys = Stream.concat(
Optional.ofNullable(incomingEntityHeader.getRemoveClassifications()).orElse(Collections.emptyList()).stream(),
Optional.ofNullable(incomingEntityHeader.getUpdateClassifications()).orElse(Collections.emptyList()).stream()
hr2904 marked this conversation as resolved.
Show resolved Hide resolved
).filter(classification -> classification.getEntityGuid().equals(entityToBeChanged.getGuid()))
.map(this::generateClassificationComparisonKey)
.collect(Collectors.toSet());

Set<String> preExistingClassificationKeys = Optional.ofNullable(entityToBeChanged.getClassifications())
.orElse(Collections.emptyList())
.stream()
.filter(classification -> classification.getEntityGuid().equals(entityToBeChanged.getGuid()))
.map(this::generateClassificationComparisonKey)
.collect(Collectors.toSet());

Set<String> diff = requiredClassificationKeys.stream()
.filter(key -> !preExistingClassificationKeys.contains(key))
.collect(Collectors.toSet());

if (!diff.isEmpty()) {
String firstTypeName = diff.iterator().next().split("\\|")[1];
throw new AtlasBaseException(AtlasErrorCode.CLASSIFICATION_NOT_ASSOCIATED_WITH_ENTITY, firstTypeName);
nikhilbonte21 marked this conversation as resolved.
Show resolved Hide resolved
}

List<AtlasClassification> filteredClassifications = Optional.ofNullable(incomingEntityHeader.getAppendClassifications())
.orElse(Collections.emptyList())
.stream()
.filter(classification -> classification.getEntityGuid().equals(entityToBeChanged.getGuid()))
.filter(appendClassification -> !preExistingClassificationKeys.contains(generateClassificationComparisonKey(appendClassification)))
.collect(Collectors.toList());

bucket(PROCESS_DELETE, operationListMap, incomingEntityHeader.getRemoveClassifications());
bucket(PROCESS_UPDATE, operationListMap, incomingEntityHeader.getUpdateClassifications());
bucket(PROCESS_ADD, operationListMap, filteredClassifications);
return operationListMap;
}

private String generateClassificationComparisonKey(AtlasClassification classification) {
return classification.getEntityGuid() + "|" + classification.getTypeName();
}

private void commitChanges(String entityGuid, String typeName, Map<String, List<AtlasClassification>> operationListMap) throws AtlasBaseException {
if (MapUtils.isEmpty(operationListMap)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,7 @@ public AtlasEntityHeaders getEntityHeaders(@QueryParam("tagUpdateStartTime") lon
@Produces(Servlets.JSON_MEDIA_TYPE)
@Consumes(Servlets.JSON_MEDIA_TYPE)
@Timed
public void setClassifications(AtlasEntityHeaders entityHeaders) throws AtlasBaseException {
public void setClassifications(AtlasEntityHeaders entityHeaders, @QueryParam("overrideClassifications") @DefaultValue("true") boolean overrideClassifications) throws AtlasBaseException {
hr2904 marked this conversation as resolved.
Show resolved Hide resolved
AtlasPerfTracer perf = null;

try {
Expand All @@ -1274,7 +1274,7 @@ public void setClassifications(AtlasEntityHeaders entityHeaders) throws AtlasBas
}

ClassificationAssociator.Updater associator = new ClassificationAssociator.Updater(typeRegistry, entitiesStore, entityGraphMapper, entityChangeNotifier, instanceConverter);
associator.setClassifications(entityHeaders.getGuidHeaderMap());
associator.setClassifications(entityHeaders.getGuidHeaderMap(), overrideClassifications);
} finally {
AtlasPerfTracer.log(perf);
}
Expand Down
Loading