From 5ee4f539900dbf69e3ffc05647b9a29be13a948e Mon Sep 17 00:00:00 2001 From: hr2904 Date: Fri, 5 Jul 2024 18:14:04 +0530 Subject: [PATCH] Removed term-category bifurcation as we are now allowing term-category intersperse. --- .../v2/preprocessor/PreProcessorUtils.java | 49 ++++++++++--------- .../glossary/CategoryPreProcessor.java | 4 +- .../glossary/GlossaryPreProcessor.java | 4 +- .../glossary/TermPreProcessor.java | 4 +- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/preprocessor/PreProcessorUtils.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/preprocessor/PreProcessorUtils.java index cf7b76495b..26a09daf0b 100644 --- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/preprocessor/PreProcessorUtils.java +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/preprocessor/PreProcessorUtils.java @@ -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); @@ -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 dslQuery = createDSLforCheckingPreExistingLexoRank(entityType.equals(ATLAS_GLOSSARY_TERM_TYPENAME), inputLexorank, glossaryQualifiedName, parentQualifiedName); + Map dslQuery = createDSLforCheckingPreExistingLexoRank(inputLexorank, glossaryQualifiedName, parentQualifiedName); List assetsWithDuplicateRank = new ArrayList<>(); try { IndexSearchParams searchParams = new IndexSearchParams(); @@ -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 categories = null; - Map dslQuery = generateDSLQueryForLastChild(glossaryQualifiedName, parentQualifiedName, isTerm); + Map dslQuery = generateDSLQueryForLastChild(glossaryQualifiedName, parentQualifiedName); try { IndexSearchParams searchParams = new IndexSearchParams(); searchParams.setAttributes(ATTRIBUTES); @@ -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; } } @@ -310,9 +309,9 @@ public static void assignNewLexicographicalSortOrder(AtlasEntity entity, String RequestContext.get().setLexoRankCache(lexoRankCache); } - public static Map createDSLforCheckingPreExistingLexoRank(boolean isTerm, String lexoRank, String glossaryQualifiedName, String parentQualifiedName) { + public static Map createDSLforCheckingPreExistingLexoRank(String lexoRank, String glossaryQualifiedName, String parentQualifiedName) { - Map boolMap = buildBoolQueryDuplicateLexoRank(isTerm, lexoRank, glossaryQualifiedName, parentQualifiedName); + Map boolMap = buildBoolQueryDuplicateLexoRank(lexoRank, glossaryQualifiedName, parentQualifiedName); Map dsl = new HashMap<>(); dsl.put("from", 0); @@ -322,7 +321,7 @@ public static Map createDSLforCheckingPreExistingLexoRank(boolea return dsl; } - private static Map buildBoolQueryDuplicateLexoRank(boolean isTerm, String lexoRank, String glossaryQualifiedName, String parentQualifiedName) { + private static Map buildBoolQueryDuplicateLexoRank(String lexoRank, String glossaryQualifiedName, String parentQualifiedName) { Map boolFilter = new HashMap<>(); List> mustArray = new ArrayList<>(); mustArray.add(mapOf("term", mapOf("__state", "ACTIVE"))); @@ -330,11 +329,13 @@ private static Map buildBoolQueryDuplicateLexoRank(boolean isTer 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> 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)))); @@ -345,13 +346,13 @@ private static Map buildBoolQueryDuplicateLexoRank(boolean isTer return boolFilter; } - public static Map generateDSLQueryForLastChild(String glossaryQualifiedName, String parentQualifiedName, boolean isTerm) { + public static Map generateDSLQueryForLastChild(String glossaryQualifiedName, String parentQualifiedName) { Map sortKeyOrder = mapOf(LEXICOGRAPHICAL_SORT_ORDER, mapOf("order", "desc")); Object[] sortArray = {sortKeyOrder}; - Map boolMap = buildBoolQuery(glossaryQualifiedName, parentQualifiedName, isTerm); + Map boolMap = buildBoolQuery(glossaryQualifiedName, parentQualifiedName); Map dsl = new HashMap<>(); dsl.put("from", 0); @@ -362,20 +363,20 @@ public static Map generateDSLQueryForLastChild(String glossaryQu return dsl; } - private static Map buildBoolQuery(String glossaryQualifiedName, String parentQualifiedName, boolean isTerm) { + private static Map buildBoolQuery(String glossaryQualifiedName, String parentQualifiedName) { Map boolFilter = new HashMap<>(); List> 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> 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")))); diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/preprocessor/glossary/CategoryPreProcessor.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/preprocessor/glossary/CategoryPreProcessor.java index fbc51477e2..4f333d5468 100644 --- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/preprocessor/glossary/CategoryPreProcessor.java +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/preprocessor/glossary/CategoryPreProcessor.java @@ -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)); @@ -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); } diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/preprocessor/glossary/GlossaryPreProcessor.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/preprocessor/glossary/GlossaryPreProcessor.java index c8023e3611..fc0bec0654 100644 --- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/preprocessor/glossary/GlossaryPreProcessor.java +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/preprocessor/glossary/GlossaryPreProcessor.java @@ -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()); @@ -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); } diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/preprocessor/glossary/TermPreProcessor.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/preprocessor/glossary/TermPreProcessor.java index 761c8673f2..e6195787fa 100644 --- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/preprocessor/glossary/TermPreProcessor.java +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/preprocessor/glossary/TermPreProcessor.java @@ -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()); @@ -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); }