Skip to content

Commit

Permalink
META-2694 Use UUID in qualifiedName instead of name (#13)
Browse files Browse the repository at this point in the history
* META-2694 Use UUID in qualifiedName instead of name

(cherry picked from commit d2be391)

* META-2694 Remove redundant code

Co-authored-by: Anshul Mehta <[email protected]>
  • Loading branch information
nikhilbonte21 and mehtaanshul authored Oct 5, 2021
1 parent 18a6548 commit 1136924
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 36 deletions.
23 changes: 23 additions & 0 deletions 3party-licenses/jnanoid-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
MIT License

Copyright (c) 2017 The JNanoID Authors
Copyright (c) 2017 Aventrix LLC
Copyright (c) 2017 Andrey Sitnik

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions intg/src/main/java/org/apache/atlas/AtlasErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public enum AtlasErrorCode {
GLOSSARY_ALREADY_EXISTS(409, "ATLAS-409-00-007", "Glossary with name {0} already exists"),
GLOSSARY_TERM_ALREADY_EXISTS(409, "ATLAS-409-00-009", "Glossary term with qualifiedName {0} already exists"),
GLOSSARY_CATEGORY_ALREADY_EXISTS(409, "ATLAS-409-00-00A", "Glossary category with qualifiedName {0} already exists"),
ACHOR_UPDATION_NOT_SUPPORTED(409, "ATLAS-400-00-0010", "Anchor(glossary) change not supported"),
GLOSSARY_IMPORT_FAILED(409, "ATLAS-409-00-011", "Glossary import failed"),

// All internal errors go here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ private void processParentCategory(AtlasGlossaryCategory storeObject, AtlasGloss
} else {
// Delete link to existing parent and link to new parent
relationshipStore.deleteById(parentRelationship.getGuid(), true);
updateQualifiedName(storeObject, newParent.getCategoryGuid());
createRelationship(defineCategoryHierarchyLink(newParent, storeObject.getGuid()));
}
}
Expand All @@ -187,8 +188,7 @@ private void processNewParent(AtlasGlossaryCategory storeObject, AtlasRelatedCat

// New parent added, qualifiedName needs recomputation
// Derive the qualifiedName of the Glossary
AtlasGlossaryCategory parentCategory = dataAccess.load(getAtlasGlossaryCategorySkeleton(newParent.getCategoryGuid()));
storeObject.setQualifiedName(storeObject.getName() + "." + parentCategory.getQualifiedName());
updateQualifiedName(storeObject, newParent.getCategoryGuid());

if (LOG.isDebugEnabled()) {
LOG.debug("Derived qualifiedName = {}", storeObject.getQualifiedName());
Expand All @@ -197,6 +197,14 @@ private void processNewParent(AtlasGlossaryCategory storeObject, AtlasRelatedCat
updateChildCategories(storeObject, storeObject.getChildrenCategories(), impactedCategories, false);
}

private void updateQualifiedName(AtlasGlossaryCategory storeObject, String guid) throws AtlasBaseException {
AtlasRelatedCategoryHeader parentCat = new AtlasRelatedCategoryHeader();
parentCat.setCategoryGuid(guid);
AtlasGlossaryCategory glossaryCategory = new AtlasGlossaryCategory(storeObject);
glossaryCategory.setParentCategory(parentCat);
storeObject.setQualifiedName(createQualifiedName(glossaryCategory));
}

private void processParentRemoval(AtlasGlossaryCategory storeObject, AtlasGlossaryCategory updatedCategory, AtlasRelatedCategoryHeader existingParent, Map<String, AtlasGlossaryCategory> impactedCategories) throws AtlasBaseException {
if (DEBUG_ENABLED) {
LOG.debug("Removing category parent, category = {}, parent = {}", storeObject.getGuid(), existingParent.getDisplayText());
Expand All @@ -207,8 +215,9 @@ private void processParentRemoval(AtlasGlossaryCategory storeObject, AtlasGlossa

// Derive the qualifiedName of the Glossary
String anchorGlossaryGuid = updatedCategory.getAnchor().getGlossaryGuid();
AtlasGlossary glossary = dataAccess.load(getGlossarySkeleton(anchorGlossaryGuid));
storeObject.setQualifiedName(storeObject.getName() + "@" + glossary.getQualifiedName());
AtlasGlossaryCategory glossaryCategory = new AtlasGlossaryCategory(storeObject);
glossaryCategory.setAnchor(new AtlasGlossaryHeader(anchorGlossaryGuid));
storeObject.setQualifiedName(createQualifiedName(glossaryCategory, true));

if (LOG.isDebugEnabled()) {
LOG.debug("Derived qualifiedName = {}", storeObject.getQualifiedName());
Expand Down Expand Up @@ -393,6 +402,7 @@ private void processCategoryChildren(AtlasGlossaryCategory storeObject, AtlasGlo
.stream()
.filter(c -> updatedExistingCategoryRelation(existingChildren, c))
.collect(Collectors.toSet());
updateChildCategories(storeObject, toUpdate, impactedCategories, false);
updateCategoryRelationships(storeObject, toUpdate);

Set<AtlasRelatedCategoryHeader> toDelete = existingChildren
Expand Down Expand Up @@ -535,20 +545,25 @@ private void updateChildCategories(AtlasGlossaryCategory parentCategory, Collect

for (AtlasRelatedCategoryHeader childCategoryHeader : childCategories) {
AtlasGlossaryCategory child = dataAccess.load(getAtlasGlossaryCategorySkeleton(childCategoryHeader.getCategoryGuid()));
String qualifiedName = child.getName() + ".";
String childAnchorGuid = child.getAnchor().getGlossaryGuid();
String qualifiedName = "";

if (isParentRemoved) {
if (LOG.isDebugEnabled()) {
LOG.debug("Parent removed, deriving qualifiedName using Glossary");
}
AtlasGlossary glossary = dataAccess.load(getGlossarySkeleton(childAnchorGuid));
qualifiedName += glossary.getQualifiedName();
qualifiedName = getNanoid(child.getQualifiedName()) + glossary.getQualifiedName();
child.setParentCategory(null);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Using parent to derive qualifiedName");
}
qualifiedName += parentCategory.getQualifiedName();
AtlasGlossaryCategory category = new AtlasGlossaryCategory(child);
AtlasRelatedCategoryHeader parentCat = new AtlasRelatedCategoryHeader();
parentCat.setCategoryGuid(parentCategory.getGuid());
category.setParentCategory(parentCat);
qualifiedName = createQualifiedName(category, parentCategory, false);
}
child.setQualifiedName(qualifiedName);

Expand Down Expand Up @@ -576,4 +591,52 @@ private void updateChildCategories(AtlasGlossaryCategory parentCategory, Collect
}
}

protected String createQualifiedName(AtlasGlossaryCategory cat) throws AtlasBaseException {
return createQualifiedName(cat, null, false);
}

protected String createQualifiedName(AtlasGlossaryCategory cat, boolean parentRemoval) throws AtlasBaseException {
return createQualifiedName(cat, null, parentRemoval);
}

protected String createQualifiedName(AtlasGlossaryCategory cat, AtlasGlossaryCategory parentCategory, boolean parentRemoval) throws AtlasBaseException {
String ret = "" ;
String qName = "";

if (!StringUtils.isEmpty(cat.getQualifiedName())) {
//extract existing nanoid for category
String[] t1 = cat.getQualifiedName().split("\\.");
qName = t1[t1.length -1].split("@")[0];
}

qName = StringUtils.isEmpty(qName) ? getUUID() : qName;
if (parentRemoval) {
AtlasGlossary glossary = dataAccess.load(getGlossarySkeleton(cat.getAnchor().getGlossaryGuid()));
ret = qName + "@" + glossary.getQualifiedName();

} else if (parentCategory != null) {
String[] parentCatQname = parentCategory.getQualifiedName().split("@");
ret = parentCatQname[0] + "." + qName + "@" + parentCatQname[1];

} else if (cat.getParentCategory() != null) {
AtlasGlossaryCategory parentCat = dataAccess.load(getAtlasGlossaryCategorySkeleton(cat.getParentCategory().getCategoryGuid()));
String[] parentCatQname = parentCat.getQualifiedName().split("@");
ret = parentCatQname[0] + "." + qName + "@" + parentCatQname[1];

} else {
String anchorGlossaryGuid = cat.getAnchor().getGlossaryGuid();
AtlasGlossary glossary = dataAccess.load(getGlossarySkeleton(anchorGlossaryGuid));
if (glossary == null) {
throw new AtlasBaseException("Glossary not found with guid: " + anchorGlossaryGuid);
}
ret = qName + "@" + glossary.getQualifiedName();
}
return ret;
}

private String getNanoid(String qualifiedName){
String[] split_0 = qualifiedName.split("@");
String[] split_1 = split_0[0].split("\\.");
return split_1[split_1.length-1];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,12 @@ public AtlasGlossary createGlossary(AtlasGlossary atlasGlossary) throws AtlasBas
throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "Glossary definition missing");
}

if (isNameInvalid(atlasGlossary.getName())) {
throw new AtlasBaseException(AtlasErrorCode.INVALID_DISPLAY_NAME);
}

if (StringUtils.isEmpty(atlasGlossary.getQualifiedName())) {
if (StringUtils.isEmpty(atlasGlossary.getName())) {
throw new AtlasBaseException(AtlasErrorCode.GLOSSARY_QUALIFIED_NAME_CANT_BE_DERIVED);
}
if (isNameInvalid(atlasGlossary.getName())){
throw new AtlasBaseException(AtlasErrorCode.INVALID_DISPLAY_NAME);
} else {
atlasGlossary.setQualifiedName(atlasGlossary.getName());
}
atlasGlossary.setQualifiedName(GlossaryUtils.createQualifiedName());
}

if (glossaryExists(atlasGlossary)) {
Expand Down Expand Up @@ -343,15 +340,12 @@ public AtlasGlossaryTerm createTerm(AtlasGlossaryTerm glossaryTerm) throws Atlas

if (isNameInvalid(glossaryTerm.getName())){
throw new AtlasBaseException(AtlasErrorCode.INVALID_DISPLAY_NAME);
} else {
// Derive the qualifiedName
String anchorGlossaryGuid = glossaryTerm.getAnchor().getGlossaryGuid();
AtlasGlossary glossary = dataAccess.load(getGlossarySkeleton(anchorGlossaryGuid));
glossaryTerm.setQualifiedName(glossaryTerm.getName() + "@" + glossary.getQualifiedName());
}

if (LOG.isDebugEnabled()) {
LOG.debug("Derived qualifiedName = {}", glossaryTerm.getQualifiedName());
}
glossaryTerm.setQualifiedName(glossaryTermUtils.createQualifiedName(glossaryTerm));

if (LOG.isDebugEnabled()) {
LOG.debug("Derived qualifiedName = {}", glossaryTerm.getQualifiedName());
}

// This might fail for the case when the term's qualifiedName has been updated and the duplicate request comes in with old name
Expand Down Expand Up @@ -410,7 +404,7 @@ public AtlasGlossaryTerm updateTerm(AtlasGlossaryTerm atlasGlossaryTerm, boolean
}

if (StringUtils.isEmpty(atlasGlossaryTerm.getName())) {
throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "DisplayName can't be null/empty");
throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "Name can't be null/empty");
}

if (isNameInvalid(atlasGlossaryTerm.getName())) {
Expand All @@ -426,6 +420,11 @@ public AtlasGlossaryTerm updateTerm(AtlasGlossaryTerm atlasGlossaryTerm, boolean
}

AtlasGlossaryTerm storeObject = dataAccess.load(atlasGlossaryTerm);

if (!storeObject.getAnchor().getGlossaryGuid().equals(atlasGlossaryTerm.getAnchor().getGlossaryGuid())){
throw new AtlasBaseException(AtlasErrorCode.ACHOR_UPDATION_NOT_SUPPORTED);
}

if (!storeObject.equals(atlasGlossaryTerm)) {
atlasGlossaryTerm.setGuid(storeObject.getGuid());
atlasGlossaryTerm.setQualifiedName(storeObject.getQualifiedName());
Expand Down Expand Up @@ -560,19 +559,11 @@ public AtlasGlossaryCategory createCategory(AtlasGlossaryCategory glossaryCatego
}
if (isNameInvalid(glossaryCategory.getName())){
throw new AtlasBaseException(AtlasErrorCode.INVALID_DISPLAY_NAME);
} else {
// Derive the qualifiedName
String anchorGlossaryGuid = glossaryCategory.getAnchor().getGlossaryGuid();
AtlasGlossary glossary = dataAccess.load(getGlossarySkeleton(anchorGlossaryGuid));
glossaryCategory.setQualifiedName(glossaryCategory.getName()+ "@" + glossary.getQualifiedName());

if (LOG.isDebugEnabled()) {
LOG.debug("Derived qualifiedName = {}", glossaryCategory.getQualifiedName());
}


}

// Derive the qualifiedName
glossaryCategory.setQualifiedName(glossaryCategoryUtils.createQualifiedName(glossaryCategory));

// This might fail for the case when the category's qualifiedName has been updated during a hierarchy change
// and the duplicate request comes in with old name
if (categoryExists(glossaryCategory)) {
Expand Down Expand Up @@ -639,7 +630,7 @@ public AtlasGlossaryCategory updateCategory(AtlasGlossaryCategory glossaryCatego
}

if (StringUtils.isEmpty(glossaryCategory.getName())) {
throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "DisplayName can't be null/empty");
throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "Name can't be null/empty");
}

if (isNameInvalid(glossaryCategory.getName())) {
Expand All @@ -648,6 +639,10 @@ public AtlasGlossaryCategory updateCategory(AtlasGlossaryCategory glossaryCatego

AtlasGlossaryCategory storeObject = dataAccess.load(glossaryCategory);

if (!storeObject.getAnchor().getGlossaryGuid().equals(glossaryCategory.getAnchor().getGlossaryGuid())){
throw new AtlasBaseException(AtlasErrorCode.ACHOR_UPDATION_NOT_SUPPORTED);
}

if (!storeObject.equals(glossaryCategory)) {
try {
glossaryCategory.setGuid(storeObject.getGuid());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,4 +975,21 @@ private void copyRelations(AtlasGlossaryTerm toGlossaryTerm, AtlasGlossaryTerm f
}
}
}

protected String createQualifiedName(AtlasGlossaryTerm term) throws AtlasBaseException{
String qName = "";
if (!StringUtils.isEmpty(term.getQualifiedName())) {
//extract existing nanoid for term
qName = term.getQualifiedName().split("@")[0];
}
qName = StringUtils.isEmpty(qName) ? getUUID() : qName;


String anchorGlossaryGuid = term.getAnchor().getGlossaryGuid();
AtlasGlossary glossary = dataAccess.load(getGlossarySkeleton(anchorGlossaryGuid));
if (glossary == null) {
throw new AtlasBaseException("Glossary not found with guid: " + anchorGlossaryGuid);
}
return qName + "@" + glossary.getQualifiedName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.atlas.repository.ogm.DataAccess;
import org.apache.atlas.repository.store.graph.AtlasRelationshipStore;
import org.apache.atlas.type.AtlasTypeRegistry;
import org.apache.atlas.util.NanoIdUtils;

import java.util.Objects;

Expand Down Expand Up @@ -109,4 +110,11 @@ protected void updateRelationshipAttributes(AtlasRelationship relationship, Atla
enum RelationshipOperation {
CREATE, UPDATE, DELETE
}

protected static String createQualifiedName() {
return getUUID();
}
protected static String getUUID(){
return NanoIdUtils.randomNanoId();
}
}
Loading

0 comments on commit 1136924

Please sign in to comment.