Skip to content

Commit 8561a5e

Browse files
committed
Merge branch 'feat/starSupport' into staging
2 parents 5e4b4f7 + 5750df2 commit 8561a5e

File tree

5 files changed

+140
-10
lines changed

5 files changed

+140
-10
lines changed

common/src/main/java/org/apache/atlas/repository/Constants.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,14 @@ public enum SupportedFileExtensions { XLSX, XLS, CSV }
390390
public static final String ATTR_VIEWER_USERS = "viewerUsers";
391391
public static final String ATTR_VIEWER_GROUPS = "viewerGroups";
392392

393+
public static final String ATTR_STARRED_BY = "starredBy";
394+
public static final String ATTR_STARRED_COUNT = "starredCount";
395+
public static final String ATTR_STARRED_DETAILS_LIST = "starredDetailsList";
396+
public static final String ATTR_ASSET_STARRED_BY = "assetStarredBy";
397+
public static final String ATTR_ASSET_STARRED_AT = "assetStarredAt";
398+
399+
public static final String STRUCT_STARRED_DETAILS = "StarredDetails";
400+
393401
public static final String KEYCLOAK_ROLE_ADMIN = "$admin";
394402
public static final String KEYCLOAK_ROLE_MEMBER = "$member";
395403
public static final String KEYCLOAK_ROLE_GUEST = "$guest";

graphdb/api/src/main/java/org/apache/atlas/repository/graphdb/AtlasElement.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public interface AtlasElement {
7373

7474
<V> List<V> getMultiValuedProperty(String propertyName, Class<V> elementType);
7575

76+
<V> Set<V> getMultiValuedSetProperty(String propertyName, Class<V> elementType);
77+
7678
/**
7779
* Gets the value of a multiplicity one property whose value is a list. It
7880
* attempts to convert the elements in the list to the specified type. Currently

graphdb/janus/src/main/java/org/apache/atlas/repository/graphdb/janus/AtlasJanusElement.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,19 @@ public <V> List<V> getMultiValuedProperty(String propertyName, Class<V> elementT
215215
return value;
216216
}
217217

218+
@Override
219+
public <V> Set<V> getMultiValuedSetProperty(String propertyName, Class<V> elementType) {
220+
Set<V> value = new HashSet<>();
221+
Iterator<? extends Property<Object>> it = getWrappedElement().properties(propertyName);
222+
223+
while (it.hasNext()) {
224+
Property currentProperty = it.next();
225+
Object currentPropertyValue = currentProperty.value();
226+
value.add((V) currentPropertyValue);
227+
}
228+
return value;
229+
}
230+
218231
@Override
219232
public void setListProperty(String propertyName, List<String> values) {
220233
setProperty(propertyName, values);

intg/src/main/java/org/apache/atlas/model/instance/AtlasEntity.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public enum Status { ACTIVE, DELETED, PURGED }
8989
private Date createTime = null;
9090
private Date updateTime = null;
9191
private Long version = 0L;
92+
private Boolean starred = null;
9293

9394
private Map<String, Object> relationshipAttributes;
9495
private List<AtlasClassification> classifications;
@@ -280,6 +281,14 @@ public void setStatus(Status status) {
280281
this.status = status;
281282
}
282283

284+
public Boolean getStarred() {
285+
return starred;
286+
}
287+
288+
public void setStarred(Boolean starred) {
289+
this.starred = starred;
290+
}
291+
283292
public String getCreatedBy() {
284293
return createdBy;
285294
}

repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasEntityStoreV2.java

Lines changed: 108 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,16 +1501,10 @@ private EntityMutationResponse createOrUpdate(EntityStream entityStream, boolean
15011501
}
15021502

15031503
AtlasEntity diffEntity = reqContext.getDifferentialEntity(entity.getGuid());
1504-
1505-
if (diffEntity != null &&
1506-
MapUtils.isNotEmpty(diffEntity.getRelationshipAttributes()) &&
1507-
diffEntity.getRelationshipAttributes().containsKey("meanings") &&
1508-
diffEntity.getRelationshipAttributes().size() == 1 &&
1509-
MapUtils.isEmpty(diffEntity.getAttributes()) &&
1510-
MapUtils.isEmpty(diffEntity.getCustomAttributes()) &&
1511-
MapUtils.isEmpty(diffEntity.getBusinessAttributes()) &&
1512-
CollectionUtils.isEmpty(diffEntity.getClassifications()) &&
1513-
CollectionUtils.isEmpty(diffEntity.getLabels())) {
1504+
boolean skipAuthBaseConditions = diffEntity != null && MapUtils.isEmpty(diffEntity.getCustomAttributes()) && MapUtils.isEmpty(diffEntity.getBusinessAttributes()) && CollectionUtils.isEmpty(diffEntity.getClassifications()) && CollectionUtils.isEmpty(diffEntity.getLabels());
1505+
boolean skipAuthMeaningsUpdate = diffEntity != null && MapUtils.isNotEmpty(diffEntity.getRelationshipAttributes()) && diffEntity.getRelationshipAttributes().containsKey("meanings") && diffEntity.getRelationshipAttributes().size() == 1 && MapUtils.isEmpty(diffEntity.getAttributes());
1506+
boolean skipAuthStarredDetailsUpdate = diffEntity != null && MapUtils.isEmpty(diffEntity.getRelationshipAttributes()) && MapUtils.isNotEmpty(diffEntity.getAttributes()) && diffEntity.getAttributes().size() == 3 && diffEntity.getAttributes().containsKey(ATTR_STARRED_BY) && diffEntity.getAttributes().containsKey(ATTR_STARRED_COUNT) && diffEntity.getAttributes().containsKey(ATTR_STARRED_DETAILS_LIST);
1507+
if (skipAuthBaseConditions && (skipAuthMeaningsUpdate || skipAuthStarredDetailsUpdate)) {
15141508
//do nothing, only diff is relationshipAttributes.meanings, allow update
15151509
} else {
15161510
AtlasAuthorizationUtils.verifyUpdateEntityAccess(typeRegistry, entityHeader,"update entity: type=" + entity.getTypeName());
@@ -1596,6 +1590,8 @@ private EntityMutationContext preCreateOrUpdate(EntityStream entityStream, Entit
15961590

15971591
AtlasVertex vertex = getResolvedEntityVertex(discoveryContext, entity);
15981592

1593+
autoUpdateStarredDetailsAttributes(entity, vertex);
1594+
15991595
try {
16001596
if (vertex != null) {
16011597
if (!isPartialUpdate) {
@@ -1692,6 +1688,102 @@ private EntityMutationContext preCreateOrUpdate(EntityStream entityStream, Entit
16921688
return context;
16931689
}
16941690

1691+
private void autoUpdateStarredDetailsAttributes(AtlasEntity entity, AtlasVertex vertex) {
1692+
1693+
MetricRecorder metric = RequestContext.get().startMetricRecord("autoUpdateStarredDetailsAttributes");
1694+
1695+
Boolean starEntityForUser = entity.getStarred();
1696+
1697+
if (starEntityForUser != null) {
1698+
1699+
long requestTime = RequestContext.get().getRequestTime();
1700+
String requestUser = RequestContext.get().getUser();
1701+
1702+
Set<String> starredBy = new HashSet<>();
1703+
Set<AtlasStruct> starredDetailsList = new HashSet<>();
1704+
int starredCount = 0;
1705+
1706+
if (vertex != null) {
1707+
Set<String> vertexStarredBy = vertex.getMultiValuedSetProperty(ATTR_STARRED_BY, String.class);
1708+
if (vertexStarredBy != null) {
1709+
starredBy = vertexStarredBy;
1710+
}
1711+
1712+
Iterable<AtlasEdge> starredDetailsEdges = vertex.getEdges(AtlasEdgeDirection.OUT, "__" + ATTR_STARRED_DETAILS_LIST);
1713+
for (AtlasEdge starredDetailsEdge : starredDetailsEdges) {
1714+
AtlasVertex starredDetailsVertex = starredDetailsEdge.getInVertex();
1715+
String assetStarredBy = starredDetailsVertex.getProperty(ATTR_ASSET_STARRED_BY, String.class);
1716+
Long assetStarredAt = starredDetailsVertex.getProperty(ATTR_ASSET_STARRED_AT, Long.class);
1717+
AtlasStruct starredDetails = getStarredDetailsStruct(assetStarredBy, assetStarredAt);
1718+
starredDetailsList.add(starredDetails);
1719+
}
1720+
1721+
starredCount = starredBy.size();
1722+
}
1723+
1724+
if (starEntityForUser) {
1725+
addUserToStarredAttributes(requestUser, requestTime, starredBy, starredDetailsList);
1726+
} else {
1727+
removeUserFromStarredAttributes(requestUser, starredBy, starredDetailsList);
1728+
}
1729+
1730+
// Update entity attributes
1731+
if (starredBy.size() != starredCount) {
1732+
entity.setAttribute(ATTR_STARRED_BY, starredBy);
1733+
entity.setAttribute(ATTR_STARRED_DETAILS_LIST, starredDetailsList);
1734+
entity.setAttribute(ATTR_STARRED_COUNT, starredBy.size());
1735+
}
1736+
1737+
}
1738+
1739+
RequestContext.get().endMetricRecord(metric);
1740+
}
1741+
1742+
private void addUserToStarredAttributes(String requestUser, long requestTime, Set<String> starredBy, Set<AtlasStruct> starredDetailsList) {
1743+
//Check and update starredBy Attribute
1744+
if (!starredBy.contains(requestUser)){
1745+
starredBy.add(requestUser);
1746+
}
1747+
1748+
//Check and update starredDetailsList Attribute
1749+
boolean isStarredDetailsListUpdated = false;
1750+
for (AtlasStruct starredDetails : starredDetailsList) {
1751+
String assetStarredBy = (String) starredDetails.getAttribute(ATTR_ASSET_STARRED_BY);
1752+
if (assetStarredBy.equals(requestUser)) {
1753+
starredDetails.setAttribute(ATTR_ASSET_STARRED_AT, requestTime);
1754+
isStarredDetailsListUpdated = true;
1755+
break;
1756+
}
1757+
}
1758+
if (!isStarredDetailsListUpdated) {
1759+
AtlasStruct starredDetails = getStarredDetailsStruct(requestUser, requestTime);
1760+
starredDetailsList.add(starredDetails);
1761+
}
1762+
}
1763+
1764+
private void removeUserFromStarredAttributes(String requestUser, Set<String> starredBy, Set<AtlasStruct> starredDetailsList) {
1765+
//Check and update starredBy Attribute
1766+
if (starredBy.contains(requestUser)){
1767+
starredBy.remove(requestUser);
1768+
}
1769+
1770+
for (AtlasStruct starredDetails : starredDetailsList) {
1771+
String assetStarredBy = (String) starredDetails.getAttribute(ATTR_ASSET_STARRED_BY);
1772+
if (assetStarredBy.equals(requestUser)) {
1773+
starredDetailsList.remove(starredDetails);
1774+
break;
1775+
}
1776+
}
1777+
}
1778+
1779+
private AtlasStruct getStarredDetailsStruct(String assetStarredBy, long assetStarredAt) {
1780+
AtlasStruct starredDetails = new AtlasStruct();
1781+
starredDetails.setTypeName(STRUCT_STARRED_DETAILS);
1782+
starredDetails.setAttribute(ATTR_ASSET_STARRED_BY, assetStarredBy);
1783+
starredDetails.setAttribute(ATTR_ASSET_STARRED_AT, assetStarredAt);
1784+
return starredDetails;
1785+
}
1786+
16951787
public PreProcessor getPreProcessor(String typeName) {
16961788
PreProcessor preProcessor = null;
16971789

@@ -1983,6 +2075,12 @@ private void flushAutoUpdateAttributes(AtlasEntity entity, AtlasEntityType entit
19832075
}
19842076
}
19852077

2078+
// for (String attrName : entityType.getAllAttributes().keySet()) {
2079+
// if (ATTR_STARRED_BY.equals(attrName) || ATTR_STARRED_COUNT.equals(attrName) || ATTR_STARRED_DETAILS_LIST.equals(attrName)) {
2080+
// flushAttributes.add(attrName);
2081+
// }
2082+
// }
2083+
19862084
flushAttributes.forEach(entity::removeAttribute);
19872085
}
19882086
}

0 commit comments

Comments
 (0)