2424
2525import com .google .common .base .Strings ;
2626import jakarta .annotation .Nonnull ;
27+ import jakarta .annotation .Nullable ;
2728import java .util .ArrayList ;
2829import java .util .Arrays ;
2930import java .util .HashSet ;
4344import org .apache .polaris .core .context .CallContext ;
4445import org .apache .polaris .core .entity .CatalogEntity ;
4546import org .apache .polaris .core .entity .PolarisEntity ;
47+ import org .apache .polaris .core .entity .PolarisEntityCore ;
4648import org .apache .polaris .core .entity .PolarisEntitySubType ;
4749import org .apache .polaris .core .entity .PolarisEntityType ;
4850import org .apache .polaris .core .persistence .PolarisMetaStoreManager ;
4951import org .apache .polaris .core .persistence .PolarisResolvedPathWrapper ;
5052import org .apache .polaris .core .persistence .PolicyMappingAlreadyExistsException ;
5153import org .apache .polaris .core .persistence .dao .entity .EntityResult ;
54+ import org .apache .polaris .core .persistence .dao .entity .ListEntitiesResult ;
5255import org .apache .polaris .core .persistence .dao .entity .LoadPolicyMappingsResult ;
56+ import org .apache .polaris .core .persistence .pagination .PageToken ;
5357import org .apache .polaris .core .persistence .resolver .PolarisResolutionManifestCatalogView ;
5458import org .apache .polaris .core .policy .PolicyEntity ;
5559import org .apache .polaris .core .policy .PolicyType ;
@@ -71,8 +75,8 @@ public class PolicyCatalog {
7175 private final CallContext callContext ;
7276 private final PolarisResolutionManifestCatalogView resolvedEntityView ;
7377 private final CatalogEntity catalogEntity ;
74- private long catalogId = - 1 ;
75- private PolarisMetaStoreManager metaStoreManager ;
78+ private final long catalogId ;
79+ private final PolarisMetaStoreManager metaStoreManager ;
7680
7781 public PolicyCatalog (
7882 PolarisMetaStoreManager metaStoreManager ,
@@ -153,24 +157,46 @@ public Policy createPolicy(
153157 return constructPolicy (resultEntity );
154158 }
155159
156- public List <PolicyIdentifier > listPolicies (Namespace namespace , PolicyType policyType ) {
160+ public List <PolicyIdentifier > listPolicies (Namespace namespace , @ Nullable PolicyType policyType ) {
157161 PolarisResolvedPathWrapper resolvedEntities = resolvedEntityView .getResolvedPath (namespace );
158162 if (resolvedEntities == null ) {
159163 throw new IllegalStateException (
160164 String .format ("Failed to fetch resolved namespace '%s'" , namespace ));
161165 }
162166
163- List <PolarisEntity > catalogPath = resolvedEntities .getRawFullPath ();
164- // TODO: when the "policyType" filter is null we should only call "listEntities" instead
167+ List <PolarisEntityCore > catalogPath =
168+ PolarisEntity .toCoreList (resolvedEntities .getRawFullPath ());
169+ if (policyType == null ) {
170+ // without a policyType filter we can call listEntities to acquire the entity names
171+ ListEntitiesResult listEntitiesResult =
172+ metaStoreManager .listEntities (
173+ callContext .getPolarisCallContext (),
174+ catalogPath ,
175+ PolarisEntityType .POLICY ,
176+ PolarisEntitySubType .NULL_SUBTYPE ,
177+ PageToken .readEverything ());
178+ if (!listEntitiesResult .isSuccess ()) {
179+ throw new IllegalStateException ("Failed to list policies in namespace: " + namespace );
180+ }
181+ return listEntitiesResult .getEntities ().stream ()
182+ .map (
183+ entity ->
184+ PolicyIdentifier .builder ()
185+ .setNamespace (namespace )
186+ .setName (entity .getName ())
187+ .build ())
188+ .toList ();
189+ }
190+ // with a policyType filter we need to load the full PolicyEntity to apply the filter
165191 return metaStoreManager
166192 .loadEntitiesAll (
167193 callContext .getPolarisCallContext (),
168- PolarisEntity . toCoreList ( catalogPath ) ,
194+ catalogPath ,
169195 PolarisEntityType .POLICY ,
170196 PolarisEntitySubType .NULL_SUBTYPE )
171197 .stream ()
172198 .map (PolicyEntity ::of )
173- .filter (policyEntity -> policyType == null || policyEntity .getPolicyType () == policyType )
199+ .filter (policyEntity -> policyEntity .getPolicyType () == policyType )
174200 .map (
175201 entity ->
176202 PolicyIdentifier .builder ()
0 commit comments