ashish-kumar-sharma commented on a change in pull request #1610:
URL: https://github.com/apache/hive/pull/1610#discussion_r575038878



##########
File path: 
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/cache/SharedCache.java
##########
@@ -715,154 +747,105 @@ public void removeConstraint(String name) {
       }
     }
 
-    public void refreshPrimaryKeys(List<SQLPrimaryKey> keys) {
-      Map<String, SQLPrimaryKey> newKeys = new ConcurrentHashMap<>();
+    public void refreshAllTableConstraints(SQLAllTableConstraints constraints) 
{
       try {
         tableLock.writeLock().lock();
-        int size = 0;
-        for (SQLPrimaryKey key : keys) {
-          if (compareAndSetMemberCacheUpdated(MemberName.PRIMARY_KEY_CACHE, 
true, false)) {
-            LOG.debug("Skipping primary key cache update for table: " + 
getTable().getTableName()
-                    + "; the primary keys are already refreshed.");
-            return;
-          }
-          String pkName = StringUtils.normalizeIdentifier(key.getPk_name());
-          key.setPk_name(pkName);
-          newKeys.put(pkName, key);
-          size += getObjectSize(SQLPrimaryKey.class, key);
-        }
-        primaryKeyCache = newKeys;
-        updateMemberSize(MemberName.PRIMARY_KEY_CACHE, size, 
SizeMode.Snapshot);
-        LOG.debug("Primary keys refresh in cache was successful for {}.{}.{}",
-            this.getTable().getCatName(), this.getTable().getDbName(), 
this.getTable().getTableName());
+        this.isConstraintsValid =
+            refreshConstraint(constraints.getPrimaryKeys(), 
MemberName.PRIMARY_KEY_CACHE) && refreshConstraint(
+                constraints.getForeignKeys(), MemberName.FOREIGN_KEY_CACHE) && 
refreshConstraint(
+                constraints.getUniqueConstraints(), 
MemberName.UNIQUE_CONSTRAINT_CACHE) && refreshConstraint(
+                constraints.getDefaultConstraints(), 
MemberName.DEFAULT_CONSTRAINT_CACHE) && refreshConstraint(
+                constraints.getNotNullConstraints(), 
MemberName.NOTNULL_CONSTRAINT_CACHE) && refreshConstraint(
+                constraints.getCheckConstraints(), 
MemberName.CHECK_CONSTRAINT_CACHE);
       } finally {
         tableLock.writeLock().unlock();
       }
     }
 
-    public void refreshForeignKeys(List<SQLForeignKey> keys) {
-      Map<String, SQLForeignKey> newKeys = new ConcurrentHashMap<>();
-      try {
-        tableLock.writeLock().lock();
-        int size = 0;
-        for (SQLForeignKey key : keys) {
-          if (compareAndSetMemberCacheUpdated(MemberName.FOREIGN_KEY_CACHE, 
true, false)) {
-            LOG.debug("Skipping foreign key cache update for table: " + 
getTable().getTableName()
-                    + "; the foreign keys are already refreshed.");
-            return;
+    private boolean refreshConstraint(List constraints, MemberName mn) {
+      int size = 0;
+      switch (mn) {
+      case PRIMARY_KEY_CACHE:
+        Map<String, SQLPrimaryKey> newPk = new ConcurrentHashMap<>();
+        if (!CollectionUtils.isEmpty(constraints)) {
+          for (SQLPrimaryKey key : (List<SQLPrimaryKey>) constraints) {
+            String pkName = StringUtils.normalizeIdentifier(key.getPk_name());
+            key.setPk_name(pkName);
+            newPk.put(pkName, key);
+            size += getObjectSize(SQLPrimaryKey.class, key);
           }
-          String fkName = StringUtils.normalizeIdentifier(key.getFk_name());
-          key.setFk_name(fkName);
-          newKeys.put(fkName, key);
-          size += getObjectSize(SQLForeignKey.class, key);
         }
-        foreignKeyCache = newKeys;
-        updateMemberSize(MemberName.FOREIGN_KEY_CACHE, size, 
SizeMode.Snapshot);
-        LOG.debug("Foreign keys refresh in cache was successful for {}.{}.{}",
-            this.getTable().getCatName(), this.getTable().getDbName(), 
this.getTable().getTableName());
-      } finally {
-        tableLock.writeLock().unlock();
-      }
-    }
-
-    public void refreshNotNullConstraints(List<SQLNotNullConstraint> 
constraints) {
-      Map<String, SQLNotNullConstraint> newConstraints = new 
ConcurrentHashMap<>();
-      try {
-        tableLock.writeLock().lock();
-        int size = 0;
-        for (SQLNotNullConstraint constraint : constraints) {
-          if 
(compareAndSetMemberCacheUpdated(MemberName.NOTNULL_CONSTRAINT_CACHE, true, 
false)) {
-            LOG.debug("Skipping not null constraints cache update for table: " 
+ getTable().getTableName()
-                    + "; the not null constraints are already refreshed.");
-            return;
+        primaryKeyCache = newPk;
+        break;
+      case FOREIGN_KEY_CACHE:
+        Map<String, SQLForeignKey> newFk = new ConcurrentHashMap<>();
+        if (!CollectionUtils.isEmpty(constraints)) {
+          for (SQLForeignKey key : (List<SQLForeignKey>) constraints) {
+            String fkName = StringUtils.normalizeIdentifier(key.getFk_name());
+            key.setFk_name(fkName);
+            newFk.put(fkName, key);
+            size += getObjectSize(SQLForeignKey.class, key);
           }
-          String nnName = 
StringUtils.normalizeIdentifier(constraint.getNn_name());
-          constraint.setNn_name(nnName);
-          newConstraints.put(nnName, constraint);
-          size += getObjectSize(SQLNotNullConstraint.class, constraint);
         }
-        notNullConstraintCache = newConstraints;
-        updateMemberSize(MemberName.NOTNULL_CONSTRAINT_CACHE, size, 
SizeMode.Snapshot);
-        LOG.debug("Not null constraints refresh in cache was successful for 
{}.{}.{}",
-            this.getTable().getCatName(), this.getTable().getDbName(), 
this.getTable().getTableName());
-      } finally {
-        tableLock.writeLock().unlock();
-      }
-    }
-
-    public void refreshUniqueConstraints(List<SQLUniqueConstraint> 
constraints) {
-      Map<String, SQLUniqueConstraint> newConstraints = new 
ConcurrentHashMap<>();
-      try {
-        tableLock.writeLock().lock();
-        int size = 0;
-        for (SQLUniqueConstraint constraint : constraints) {
-          if 
(compareAndSetMemberCacheUpdated(MemberName.UNIQUE_CONSTRAINT_CACHE, true, 
false)) {
-            LOG.debug("Skipping unique constraints cache update for table: " + 
getTable().getTableName()
-                    + "; the unique constraints are already refreshed.");
-            return;
+        foreignKeyCache = newFk;
+        break;
+      case UNIQUE_CONSTRAINT_CACHE:
+        Map<String, SQLUniqueConstraint> newUc = new ConcurrentHashMap<>();
+        if (!CollectionUtils.isEmpty(constraints)) {
+          for (SQLUniqueConstraint constraint : (List<SQLUniqueConstraint>) 
constraints) {
+            String ucName = 
StringUtils.normalizeIdentifier(constraint.getUk_name());
+            constraint.setUk_name(ucName);
+            newUc.put(ucName, constraint);
+            size += getObjectSize(SQLUniqueConstraint.class, constraint);
           }
-          String ucName = 
StringUtils.normalizeIdentifier(constraint.getUk_name());
-          constraint.setUk_name(ucName);
-          newConstraints.put(ucName, constraint);
-          size += getObjectSize(SQLUniqueConstraint.class, constraint);
         }
-        uniqueConstraintCache = newConstraints;
-        updateMemberSize(MemberName.UNIQUE_CONSTRAINT_CACHE, size, 
SizeMode.Snapshot);
-        LOG.debug("Unique constraints refresh in cache was successful for 
{}.{}.{}",
-            this.getTable().getCatName(), this.getTable().getDbName(), 
this.getTable().getTableName());
-      } finally {
-        tableLock.writeLock().unlock();
-      }
-    }
-
-    public void refreshDefaultConstraints(List<SQLDefaultConstraint> 
constraints) {
-      Map<String, SQLDefaultConstraint> newConstraints = new 
ConcurrentHashMap<>();
-      try {
-        tableLock.writeLock().lock();
-        int size = 0;
-        for (SQLDefaultConstraint constraint : constraints) {
-          if 
(compareAndSetMemberCacheUpdated(MemberName.DEFAULT_CONSTRAINT_CACHE, true, 
false)) {
-            LOG.debug("Skipping default constraint cache update for table: " + 
getTable().getTableName()
-                + "; the default constraint are already refreshed.");
-            return;
+        uniqueConstraintCache = newUc;
+        break;
+      case NOTNULL_CONSTRAINT_CACHE:
+        Map<String, SQLNotNullConstraint> newNn = new ConcurrentHashMap<>();
+        if (!CollectionUtils.isEmpty(constraints)) {
+          for (SQLNotNullConstraint constraint : (List<SQLNotNullConstraint>) 
constraints) {
+            String nnName = 
StringUtils.normalizeIdentifier(constraint.getNn_name());
+            constraint.setNn_name(nnName);
+            newNn.put(nnName, constraint);
+            size += getObjectSize(SQLNotNullConstraint.class, constraint);
           }
-          String dcName = 
StringUtils.normalizeIdentifier(constraint.getDc_name());
-          constraint.setDc_name(dcName);
-          newConstraints.put(dcName, constraint);
-          size += getObjectSize(SQLDefaultConstraint.class, constraint);
         }
-        defaultConstraintCache = newConstraints;
-        updateMemberSize(MemberName.DEFAULT_CONSTRAINT_CACHE, size, 
SizeMode.Snapshot);
-        LOG.debug("Default constraints refresh in cache was successful for 
{}.{}.{}", this.getTable().getCatName(),
-            this.getTable().getDbName(), this.getTable().getTableName());
-      } finally {
-        tableLock.writeLock().unlock();
-      }
-    }
-
-    public void refreshCheckConstraints(List<SQLCheckConstraint> constraints) {
-      Map<String, SQLCheckConstraint> newConstraints = new 
ConcurrentHashMap<>();
-      try {
-        tableLock.writeLock().lock();
-        int size = 0;
-        for (SQLCheckConstraint constraint : constraints) {
-          if 
(compareAndSetMemberCacheUpdated(MemberName.CHECK_CONSTRAINT_CACHE, true, 
false)) {
-            LOG.debug("Skipping check constraint cache update for table: " + 
getTable().getTableName()
-                + "; the check constraint are already refreshed.");
-            return;
+        notNullConstraintCache = newNn;
+        break;
+      case DEFAULT_CONSTRAINT_CACHE:
+        Map<String, SQLDefaultConstraint> newDc = new ConcurrentHashMap<>();
+        if (!CollectionUtils.isEmpty(constraints)) {
+          for (SQLDefaultConstraint constraint : (List<SQLDefaultConstraint>) 
constraints) {
+            String dcName = 
StringUtils.normalizeIdentifier(constraint.getDc_name());
+            constraint.setDc_name(dcName);
+            newDc.put(dcName, constraint);
+            size += getObjectSize(SQLDefaultConstraint.class, constraint);
           }
-          String ccName = 
StringUtils.normalizeIdentifier(constraint.getDc_name());
-          constraint.setDc_name(ccName);
-          newConstraints.put(ccName, constraint);
-          size += getObjectSize(SQLCheckConstraint.class, constraint);
         }
-        checkConstraintCache = newConstraints;
-        updateMemberSize(MemberName.CHECK_CONSTRAINT_CACHE, size, 
SizeMode.Snapshot);
-        LOG.debug("check constraints refresh in cache was successful for 
{}.{}.{}", this.getTable().getCatName(),
-            this.getTable().getDbName(), this.getTable().getTableName());
-      } finally {
-        tableLock.writeLock().unlock();
-      }
+        defaultConstraintCache = newDc;
+        break;
+      case CHECK_CONSTRAINT_CACHE:
+        Map<String, SQLCheckConstraint> newCc = new ConcurrentHashMap<>();
+        if (!CollectionUtils.isEmpty(constraints)) {
+          for (SQLCheckConstraint constraint : (List<SQLCheckConstraint>) 
constraints) {
+            String ccName = 
StringUtils.normalizeIdentifier(constraint.getDc_name());
+            constraint.setDc_name(ccName);
+            newCc.put(ccName, constraint);
+            size += getObjectSize(SQLCheckConstraint.class, constraint);
+          }
+        }
+        checkConstraintCache = newCc;
+        break;
+      default:
+        LOG.error("Should not reach here");
+        break;
+      }
+      updateMemberSize(mn, size, SizeMode.Snapshot);
+      setMemberCacheUpdated(mn, false);

Review comment:
       because it is a snapshot




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to