This is an automated email from the ASF dual-hosted git repository.

dhuo pushed a commit to branch persistence-poc
in repository https://gitbox.apache.org/repos/asf/polaris.git

commit 87a8010fdb32ceb872edd7c128a45a70230214fb
Author: Dennis Huo <[email protected]>
AuthorDate: Wed Feb 26 05:17:50 2025 +0000

    Push all evidence of the two-phase lookupEntityByName into only the 
transactional-style
    PolarisMetaStoreSession, so that BasePersistence properly exposes a 
lookupEntityByName
    method where impls that use secondary indexes can easily just lookup an 
entity by name
    instead of doing two lookups.
---
 .../polaris/core/persistence/BasePersistence.java  | 40 ++++++-------
 .../persistence/PolarisMetaStoreManagerImpl.java   | 68 ++++++----------------
 .../core/persistence/PolarisMetaStoreSession.java  | 56 ++++++++++++++++++
 3 files changed, 92 insertions(+), 72 deletions(-)

diff --git 
a/polaris-core/src/main/java/org/apache/polaris/core/persistence/BasePersistence.java
 
b/polaris-core/src/main/java/org/apache/polaris/core/persistence/BasePersistence.java
index b96e1d6d..a4283bf5 100644
--- 
a/polaris-core/src/main/java/org/apache/polaris/core/persistence/BasePersistence.java
+++ 
b/polaris-core/src/main/java/org/apache/polaris/core/persistence/BasePersistence.java
@@ -26,7 +26,6 @@ import java.util.function.Predicate;
 import org.apache.polaris.core.PolarisCallContext;
 import org.apache.polaris.core.entity.PolarisBaseEntity;
 import org.apache.polaris.core.entity.PolarisChangeTrackingVersions;
-import org.apache.polaris.core.entity.PolarisEntitiesActiveKey;
 import org.apache.polaris.core.entity.PolarisEntityActiveRecord;
 import org.apache.polaris.core.entity.PolarisEntityCore;
 import org.apache.polaris.core.entity.PolarisEntityId;
@@ -128,6 +127,24 @@ public interface BasePersistence {
   PolarisBaseEntity lookupEntity(
       @Nonnull PolarisCallContext callCtx, long catalogId, long entityId);
 
+  /**
+   * Lookup an entity given its catalogId, parentId, typeCode, and name.
+   *
+   * @param callCtx call context
+   * @param catalogId catalog id or NULL_ID
+   * @param parentId id of the parent, either a namespace or a catalog
+   * @param typeCode the PolarisEntityType code of the entity to lookup
+   * @param name the name of the entity
+   * @return null if the specified entity does not exist
+   */
+  @Nullable
+  PolarisBaseEntity lookupEntityByName(
+      @Nonnull PolarisCallContext callCtx,
+      long catalogId,
+      long parentId,
+      int typeCode,
+      @Nonnull String name);
+
   /**
    * Lookup a set of entities given their catalog id/entity id unique 
identifier
    *
@@ -163,27 +180,6 @@ public interface BasePersistence {
   List<PolarisChangeTrackingVersions> lookupEntityVersions(
       @Nonnull PolarisCallContext callCtx, List<PolarisEntityId> entityIds);
 
-  /**
-   * Lookup an entity by entityActiveKey
-   *
-   * @param callCtx call context
-   * @param entityActiveKey key by name
-   * @return null if the specified entity does not exist or has been dropped.
-   */
-  @Nullable
-  PolarisEntityActiveRecord lookupEntityActive(
-      @Nonnull PolarisCallContext callCtx, @Nonnull PolarisEntitiesActiveKey 
entityActiveKey);
-
-  /**
-   * Lookup the specified set of entities by entityActiveKeys Return the 
result, a parallel list of
-   * active records. A record in that list will be null if its associated 
lookup failed
-   *
-   * @return the list of entityActiveKeys for the specified lookup operation
-   */
-  @Nonnull
-  List<PolarisEntityActiveRecord> lookupEntityActiveBatch(
-      @Nonnull PolarisCallContext callCtx, List<PolarisEntitiesActiveKey> 
entityActiveKeys);
-
   /**
    * List all active entities of the specified type which are child entities 
of the specified parent
    *
diff --git 
a/polaris-core/src/main/java/org/apache/polaris/core/persistence/PolarisMetaStoreManagerImpl.java
 
b/polaris-core/src/main/java/org/apache/polaris/core/persistence/PolarisMetaStoreManagerImpl.java
index 3eb32d07..7a016854 100644
--- 
a/polaris-core/src/main/java/org/apache/polaris/core/persistence/PolarisMetaStoreManagerImpl.java
+++ 
b/polaris-core/src/main/java/org/apache/polaris/core/persistence/PolarisMetaStoreManagerImpl.java
@@ -69,38 +69,6 @@ public class PolarisMetaStoreManagerImpl extends 
BaseMetaStoreManager {
   /** mapper, allows to serialize/deserialize properties to/from JSON */
   private static final ObjectMapper MAPPER = new ObjectMapper();
 
-  /**
-   * Lookup an entity by its name
-   *
-   * @param callCtx call context
-   * @param ms meta store
-   * @param entityActiveKey lookup key
-   * @return the entity if it exists, null otherwise
-   */
-  private @Nullable PolarisBaseEntity lookupEntityByName(
-      @Nonnull PolarisCallContext callCtx,
-      @Nonnull PolarisMetaStoreSession ms,
-      @Nonnull PolarisEntitiesActiveKey entityActiveKey) {
-    // ensure that the entity exists
-    PolarisEntityActiveRecord entityActiveRecord = 
ms.lookupEntityActive(callCtx, entityActiveKey);
-
-    // if not found, return null
-    if (entityActiveRecord == null) {
-      return null;
-    }
-
-    // lookup the entity, should be there
-    PolarisBaseEntity entity =
-        ms.lookupEntity(callCtx, entityActiveRecord.getCatalogId(), 
entityActiveRecord.getId());
-    callCtx
-        .getDiagServices()
-        .checkNotNull(
-            entity, "unexpected_not_found_entity", "entityActiveRecord={}", 
entityActiveRecord);
-
-    // return it now
-    return entity;
-  }
-
   /**
    * Write this entity to the meta store.
    *
@@ -512,13 +480,13 @@ public class PolarisMetaStoreManagerImpl extends 
BaseMetaStoreManager {
               catalog);
 
       // lookup catalog admin role, should exist
-      PolarisEntitiesActiveKey adminRoleKey =
-          new PolarisEntitiesActiveKey(
+      PolarisBaseEntity catalogAdminRole =
+          ms.lookupEntityByName(
+              callCtx,
               refreshCatalog.getId(),
               refreshCatalog.getId(),
               PolarisEntityType.CATALOG_ROLE.getCode(),
               PolarisEntityConstants.getNameOfCatalogAdminRole());
-      PolarisBaseEntity catalogAdminRole = this.lookupEntityByName(callCtx, 
ms, adminRoleKey);
 
       // if found, ensure not null
       callCtx
@@ -573,14 +541,13 @@ public class PolarisMetaStoreManagerImpl extends 
BaseMetaStoreManager {
     // immediately assign its catalog_admin role
     if (principalRoles.isEmpty()) {
       // lookup service admin role, should exist
-      PolarisEntitiesActiveKey serviceAdminRoleKey =
-          new PolarisEntitiesActiveKey(
+      PolarisBaseEntity serviceAdminRole =
+          ms.lookupEntityByName(
+              callCtx,
               PolarisEntityConstants.getNullId(),
               PolarisEntityConstants.getRootEntityId(),
               PolarisEntityType.PRINCIPAL_ROLE.getCode(),
               PolarisEntityConstants.getNameOfPrincipalServiceAdminRole());
-      PolarisBaseEntity serviceAdminRole =
-          this.lookupEntityByName(callCtx, ms, serviceAdminRoleKey);
       callCtx.getDiagServices().checkNotNull(serviceAdminRole, 
"missing_service_admin_role");
       this.persistNewGrantRecord(
           callCtx, ms, adminRole, serviceAdminRole, 
PolarisPrivilege.CATALOG_ROLE_USAGE);
@@ -719,10 +686,13 @@ public class PolarisMetaStoreManagerImpl extends 
BaseMetaStoreManager {
     }
 
     // now looking the entity by name
-    PolarisEntitiesActiveKey entityActiveKey =
-        new PolarisEntitiesActiveKey(
-            resolver.getCatalogIdOrNull(), resolver.getParentId(), 
entityType.getCode(), name);
-    PolarisBaseEntity entity = this.lookupEntityByName(callCtx, ms, 
entityActiveKey);
+    PolarisBaseEntity entity =
+        ms.lookupEntityByName(
+            callCtx,
+            resolver.getCatalogIdOrNull(),
+            resolver.getParentId(),
+            entityType.getCode(),
+            name);
 
     // if found, check if subType really matches
     if (entity != null
@@ -2218,9 +2188,8 @@ public class PolarisMetaStoreManagerImpl extends 
BaseMetaStoreManager {
       @Nonnull String entityName) {
 
     // load that entity
-    PolarisEntitiesActiveKey entityActiveKey =
-        new PolarisEntitiesActiveKey(entityCatalogId, parentId, 
entityType.getCode(), entityName);
-    PolarisBaseEntity entity = this.lookupEntityByName(callCtx, ms, 
entityActiveKey);
+    PolarisBaseEntity entity =
+        ms.lookupEntityByName(callCtx, entityCatalogId, parentId, 
entityType.getCode(), entityName);
 
     // null if entity not found
     if (entity == null) {
@@ -2279,14 +2248,13 @@ public class PolarisMetaStoreManagerImpl extends 
BaseMetaStoreManager {
             EntityResult backfillResult =
                 this.createEntityIfNotExists(callCtx, ms, null, rootContainer);
             if (backfillResult.isSuccess()) {
-              PolarisEntitiesActiveKey serviceAdminRoleKey =
-                  new PolarisEntitiesActiveKey(
+              PolarisBaseEntity serviceAdminRole =
+                  ms.lookupEntityByName(
+                      callCtx,
                       0L,
                       0L,
                       PolarisEntityType.PRINCIPAL_ROLE.getCode(),
                       
PolarisEntityConstants.getNameOfPrincipalServiceAdminRole());
-              PolarisBaseEntity serviceAdminRole =
-                  this.lookupEntityByName(callCtx, ms, serviceAdminRoleKey);
               if (serviceAdminRole != null) {
                 this.persistNewGrantRecord(
                     callCtx,
diff --git 
a/polaris-core/src/main/java/org/apache/polaris/core/persistence/PolarisMetaStoreSession.java
 
b/polaris-core/src/main/java/org/apache/polaris/core/persistence/PolarisMetaStoreSession.java
index 208122f0..3a871ffb 100644
--- 
a/polaris-core/src/main/java/org/apache/polaris/core/persistence/PolarisMetaStoreSession.java
+++ 
b/polaris-core/src/main/java/org/apache/polaris/core/persistence/PolarisMetaStoreSession.java
@@ -19,9 +19,13 @@
 package org.apache.polaris.core.persistence;
 
 import jakarta.annotation.Nonnull;
+import jakarta.annotation.Nullable;
+import java.util.List;
 import java.util.function.Supplier;
 import org.apache.polaris.core.PolarisCallContext;
 import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntitiesActiveKey;
+import org.apache.polaris.core.entity.PolarisEntityActiveRecord;
 import org.apache.polaris.core.entity.PolarisEntityCore;
 
 /**
@@ -77,6 +81,58 @@ public interface PolarisMetaStoreSession extends 
BasePersistence {
   void runActionInReadTransaction(
       @Nonnull PolarisCallContext callCtx, @Nonnull Runnable transactionCode);
 
+  /** {@inheritDoc} */
+  @Override
+  default PolarisBaseEntity lookupEntityByName(
+      @Nonnull PolarisCallContext callCtx,
+      long catalogId,
+      long parentId,
+      int typeCode,
+      @Nonnull String name) {
+    PolarisEntitiesActiveKey entityActiveKey =
+        new PolarisEntitiesActiveKey(catalogId, parentId, typeCode, name);
+
+    // ensure that the entity exists
+    PolarisEntityActiveRecord entityActiveRecord = lookupEntityActive(callCtx, 
entityActiveKey);
+
+    // if not found, return null
+    if (entityActiveRecord == null) {
+      return null;
+    }
+
+    // lookup the entity, should be there
+    PolarisBaseEntity entity =
+        lookupEntity(callCtx, entityActiveRecord.getCatalogId(), 
entityActiveRecord.getId());
+    callCtx
+        .getDiagServices()
+        .checkNotNull(
+            entity, "unexpected_not_found_entity", "entityActiveRecord={}", 
entityActiveRecord);
+
+    // return it now
+    return entity;
+  }
+
+  /**
+   * Lookup an entity by entityActiveKey
+   *
+   * @param callCtx call context
+   * @param entityActiveKey key by name
+   * @return null if the specified entity does not exist or has been dropped.
+   */
+  @Nullable
+  PolarisEntityActiveRecord lookupEntityActive(
+      @Nonnull PolarisCallContext callCtx, @Nonnull PolarisEntitiesActiveKey 
entityActiveKey);
+
+  /**
+   * Lookup the specified set of entities by entityActiveKeys Return the 
result, a parallel list of
+   * active records. A record in that list will be null if its associated 
lookup failed
+   *
+   * @return the list of entityActiveKeys for the specified lookup operation
+   */
+  @Nonnull
+  List<PolarisEntityActiveRecord> lookupEntityActiveBatch(
+      @Nonnull PolarisCallContext callCtx, List<PolarisEntitiesActiveKey> 
entityActiveKeys);
+
   /**
    * Write the base entity to the entities_active table. If there is a 
conflict (existing record
    * with the same PK), all attributes of the new record will replace the 
existing one.

Reply via email to