Skip to content

Commit

Permalink
Removed term-category bifurcation as we are now allowing term-categor…
Browse files Browse the repository at this point in the history
…y intersperse.
  • Loading branch information
hr2904 committed Jul 5, 2024
1 parent c4e1cbb commit 5ee4f53
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public static void verifyDuplicateAssetByName(String typeName, String assetName,
}
}

public static void isValidLexoRank(String entityType, String inputLexorank, String glossaryQualifiedName, String parentQualifiedName, EntityDiscoveryService discovery) throws AtlasBaseException {
public static void isValidLexoRank(String inputLexorank, String glossaryQualifiedName, String parentQualifiedName, EntityDiscoveryService discovery) throws AtlasBaseException {

Matcher matcher = LEXORANK_VALIDITY_PATTERN.matcher(inputLexorank);

Expand All @@ -233,11 +233,11 @@ public static void isValidLexoRank(String entityType, String inputLexorank, Stri
if(Objects.isNull(lexoRankCache)) {
lexoRankCache = new HashMap<>();
}
String cacheKey = entityType + "-" + glossaryQualifiedName + "-" + parentQualifiedName;
String cacheKey = glossaryQualifiedName + "-" + parentQualifiedName;
if(lexoRankCache.containsKey(cacheKey) && lexoRankCache.get(cacheKey).equals(inputLexorank)){
throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "Duplicate value for the attribute :" + LEXICOGRAPHICAL_SORT_ORDER +" found");
}
Map<String, Object> dslQuery = createDSLforCheckingPreExistingLexoRank(entityType.equals(ATLAS_GLOSSARY_TERM_TYPENAME), inputLexorank, glossaryQualifiedName, parentQualifiedName);
Map<String, Object> dslQuery = createDSLforCheckingPreExistingLexoRank(inputLexorank, glossaryQualifiedName, parentQualifiedName);
List<AtlasEntityHeader> assetsWithDuplicateRank = new ArrayList<>();
try {
IndexSearchParams searchParams = new IndexSearchParams();
Expand Down Expand Up @@ -269,15 +269,14 @@ public static void assignNewLexicographicalSortOrder(AtlasEntity entity, String
}
String lexoRank = "";
String lastLexoRank = "";
boolean isTerm = entity.getTypeName().equals(ATLAS_GLOSSARY_TERM_TYPENAME);
String cacheKey = entity.getTypeName() + "-" + glossaryQualifiedName + "-" + parentQualifiedName;
String cacheKey = glossaryQualifiedName + "-" + parentQualifiedName;

if(lexoRankCache.containsKey(cacheKey)) {
lastLexoRank = lexoRankCache.get(cacheKey);
} else {

List<AtlasEntityHeader> categories = null;
Map<String, Object> dslQuery = generateDSLQueryForLastChild(glossaryQualifiedName, parentQualifiedName, isTerm);
Map<String, Object> dslQuery = generateDSLQueryForLastChild(glossaryQualifiedName, parentQualifiedName);
try {
IndexSearchParams searchParams = new IndexSearchParams();
searchParams.setAttributes(ATTRIBUTES);
Expand All @@ -294,10 +293,10 @@ public static void assignNewLexicographicalSortOrder(AtlasEntity entity, String
if (StringUtils.isNotEmpty(lexicographicalSortOrder)) {
lastLexoRank = lexicographicalSortOrder;
} else {
lastLexoRank = isTerm ? INIT_TERM_LEXORANK_OFFSET : INIT_LEXORANK_OFFSET;
lastLexoRank = INIT_LEXORANK_OFFSET;
}
} else {
lastLexoRank = isTerm ? INIT_TERM_LEXORANK_OFFSET : INIT_LEXORANK_OFFSET;
lastLexoRank = INIT_LEXORANK_OFFSET;
}
}

Expand All @@ -310,9 +309,9 @@ public static void assignNewLexicographicalSortOrder(AtlasEntity entity, String
RequestContext.get().setLexoRankCache(lexoRankCache);
}

public static Map<String, Object> createDSLforCheckingPreExistingLexoRank(boolean isTerm, String lexoRank, String glossaryQualifiedName, String parentQualifiedName) {
public static Map<String, Object> createDSLforCheckingPreExistingLexoRank(String lexoRank, String glossaryQualifiedName, String parentQualifiedName) {

Map<String, Object> boolMap = buildBoolQueryDuplicateLexoRank(isTerm, lexoRank, glossaryQualifiedName, parentQualifiedName);
Map<String, Object> boolMap = buildBoolQueryDuplicateLexoRank(lexoRank, glossaryQualifiedName, parentQualifiedName);

Map<String, Object> dsl = new HashMap<>();
dsl.put("from", 0);
Expand All @@ -322,19 +321,21 @@ public static Map<String, Object> createDSLforCheckingPreExistingLexoRank(boolea
return dsl;
}

private static Map<String, Object> buildBoolQueryDuplicateLexoRank(boolean isTerm, String lexoRank, String glossaryQualifiedName, String parentQualifiedName) {
private static Map<String, Object> buildBoolQueryDuplicateLexoRank(String lexoRank, String glossaryQualifiedName, String parentQualifiedName) {
Map<String, Object> boolFilter = new HashMap<>();
List<Map<String, Object>> mustArray = new ArrayList<>();
mustArray.add(mapOf("term", mapOf("__state", "ACTIVE")));
mustArray.add(mapOf("term", mapOf(LEXICOGRAPHICAL_SORT_ORDER, lexoRank)));
if(StringUtils.isNotEmpty(glossaryQualifiedName)) {
mustArray.add(mapOf("terms", mapOf("__typeName.keyword", Arrays.asList(ATLAS_GLOSSARY_TERM_TYPENAME, ATLAS_GLOSSARY_CATEGORY_TYPENAME))));
mustArray.add(mapOf("term", mapOf("__glossary", glossaryQualifiedName)));
String parentAttribute = isTerm ? "__categories" : "__parentCategory";
if(StringUtils.isEmpty(parentQualifiedName)) {
boolFilter.put("must_not", Arrays.asList(mapOf("exists", mapOf("field", parentAttribute))));
boolFilter.put("must_not", Arrays.asList(mapOf("exists", mapOf("field", "__categories")),mapOf("exists", mapOf("field", "__parentCategory"))));
} else {
mustArray.add(mapOf("term", mapOf(parentAttribute, parentQualifiedName)));
List<Map<String, Object>> shouldParentArray = new ArrayList<>();
shouldParentArray.add(mapOf("term", mapOf("__categories", parentQualifiedName)));
shouldParentArray.add(mapOf("term", mapOf("__parentCategory", parentQualifiedName)));
mustArray.add(mapOf("bool",mapOf("should", shouldParentArray)));
}
} else{
mustArray.add(mapOf("terms", mapOf("__typeName.keyword", Arrays.asList(ATLAS_GLOSSARY_ENTITY_TYPE))));
Expand All @@ -345,13 +346,13 @@ private static Map<String, Object> buildBoolQueryDuplicateLexoRank(boolean isTer
return boolFilter;
}

public static Map<String, Object> generateDSLQueryForLastChild(String glossaryQualifiedName, String parentQualifiedName, boolean isTerm) {
public static Map<String, Object> generateDSLQueryForLastChild(String glossaryQualifiedName, String parentQualifiedName) {

Map<String, Object> sortKeyOrder = mapOf(LEXICOGRAPHICAL_SORT_ORDER, mapOf("order", "desc"));

Object[] sortArray = {sortKeyOrder};

Map<String, Object> boolMap = buildBoolQuery(glossaryQualifiedName, parentQualifiedName, isTerm);
Map<String, Object> boolMap = buildBoolQuery(glossaryQualifiedName, parentQualifiedName);

Map<String, Object> dsl = new HashMap<>();
dsl.put("from", 0);
Expand All @@ -362,20 +363,20 @@ public static Map<String, Object> generateDSLQueryForLastChild(String glossaryQu
return dsl;
}

private static Map<String, Object> buildBoolQuery(String glossaryQualifiedName, String parentQualifiedName, boolean isTerm) {
private static Map<String, Object> buildBoolQuery(String glossaryQualifiedName, String parentQualifiedName) {
Map<String, Object> boolFilter = new HashMap<>();
List<Map<String, Object>> mustArray = new ArrayList<>();
mustArray.add(mapOf("term", mapOf("__state", "ACTIVE")));
if(StringUtils.isNotEmpty(glossaryQualifiedName)) {
String typeName = isTerm ? ATLAS_GLOSSARY_TERM_TYPENAME : ATLAS_GLOSSARY_CATEGORY_TYPENAME;
mustArray.add(mapOf("term", mapOf("__typeName.keyword", typeName)));
mustArray.add(mapOf("terms", mapOf("__typeName.keyword", Arrays.asList("AtlasGlossaryTerm", "AtlasGlossaryCategory"))));
mustArray.add(mapOf("term", mapOf("__glossary", glossaryQualifiedName)));
String parentAttribute = isTerm ? "__categories" : "__parentCategory";
if(StringUtils.isEmpty(parentQualifiedName)) {
boolFilter.put("must_not", Arrays.asList(mapOf("exists", mapOf("field", parentAttribute))));
}
else {
mustArray.add(mapOf("term", mapOf(parentAttribute, parentQualifiedName)));
boolFilter.put("must_not", Arrays.asList(mapOf("exists", mapOf("field", "__categories")),mapOf("exists", mapOf("field", "__parentCategory"))));
} else {
List<Map<String, Object>> shouldParentArray = new ArrayList<>();
shouldParentArray.add(mapOf("term", mapOf("__categories", parentQualifiedName)));
shouldParentArray.add(mapOf("term", mapOf("__parentCategory", parentQualifiedName)));
mustArray.add(mapOf("bool",mapOf("should", shouldParentArray)));
}
} else{
mustArray.add(mapOf("terms", mapOf("__typeName.keyword", Arrays.asList("AtlasGlossary"))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private void processCreateCategory(AtlasEntity entity, AtlasVertex vertex) throw
if(StringUtils.isEmpty(lexicographicalSortOrder)){
assignNewLexicographicalSortOrder(entity,glossaryQualifiedName, parentQname, this.discovery);
} else {
isValidLexoRank(entity.getTypeName(), lexicographicalSortOrder, glossaryQualifiedName, parentQname, this.discovery);
isValidLexoRank(lexicographicalSortOrder, glossaryQualifiedName, parentQname, this.discovery);
}

entity.setAttribute(QUALIFIED_NAME, createQualifiedName(vertex));
Expand Down Expand Up @@ -167,7 +167,7 @@ private void processUpdateCategory(AtlasEntity entity, AtlasVertex vertex) throw
parentQname = (String) parentCategory.getAttribute(QUALIFIED_NAME);
}
if(StringUtils.isNotEmpty(lexicographicalSortOrder)) {
isValidLexoRank(entity.getTypeName(), lexicographicalSortOrder, newGlossaryQualifiedName, parentQname, this.discovery);
isValidLexoRank(lexicographicalSortOrder, newGlossaryQualifiedName, parentQname, this.discovery);
} else {
entity.removeAttribute(LEXICOGRAPHICAL_SORT_ORDER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private void processCreateGlossary(AtlasStruct entity) throws AtlasBaseException
if(StringUtils.isEmpty(lexicographicalSortOrder)) {
assignNewLexicographicalSortOrder((AtlasEntity) entity, null, null, this.discovery);
} else {
isValidLexoRank(entity.getTypeName(), lexicographicalSortOrder, "", "", this.discovery);
isValidLexoRank(lexicographicalSortOrder, "", "", this.discovery);
}

entity.setAttribute(QUALIFIED_NAME, createQualifiedName());
Expand All @@ -115,7 +115,7 @@ private void processUpdateGlossary(AtlasStruct entity, AtlasVertex vertex) throw
}
String lexicographicalSortOrder = (String) entity.getAttribute(LEXICOGRAPHICAL_SORT_ORDER);
if(StringUtils.isNotEmpty(lexicographicalSortOrder)) {
isValidLexoRank(entity.getTypeName(), lexicographicalSortOrder, "", "", this.discovery);
isValidLexoRank(lexicographicalSortOrder, "", "", this.discovery);
} else {
entity.removeAttribute(LEXICOGRAPHICAL_SORT_ORDER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void processCreateTerm(AtlasEntity entity, AtlasVertex vertex) throws At
if(StringUtils.isEmpty(lexicographicalSortOrder)){
assignNewLexicographicalSortOrder(entity, glossaryQName, parentQname, this.discovery);
} else {
isValidLexoRank(entity.getTypeName(), lexicographicalSortOrder, glossaryQName, parentQname, this.discovery);
isValidLexoRank(lexicographicalSortOrder, glossaryQName, parentQname, this.discovery);
}

entity.setAttribute(QUALIFIED_NAME, createQualifiedName());
Expand Down Expand Up @@ -135,7 +135,7 @@ private void processUpdateTerm(AtlasEntity entity, AtlasVertex vertex) throws At

String lexicographicalSortOrder = (String) entity.getAttribute(LEXICOGRAPHICAL_SORT_ORDER);
if(StringUtils.isNotEmpty(lexicographicalSortOrder)) {
isValidLexoRank(entity.getTypeName(), lexicographicalSortOrder, newGlossaryQualifiedName, parentQname, this.discovery);
isValidLexoRank(lexicographicalSortOrder, newGlossaryQualifiedName, parentQname, this.discovery);
} else {
entity.removeAttribute(LEXICOGRAPHICAL_SORT_ORDER);
}
Expand Down

0 comments on commit 5ee4f53

Please sign in to comment.