Skip to content

Commit 21d1c23

Browse files
authored
Policy Store: PolicyMappingRecord with Persistence Impl (#1104)
1 parent e32ef89 commit 21d1c23

23 files changed

+2089
-3
lines changed

extension/persistence/eclipselink/src/main/java/org/apache/polaris/extension/persistence/impl/eclipselink/PolarisEclipseLinkMetaStoreSessionImpl.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@
5353
import org.apache.polaris.core.persistence.PrincipalSecretsGenerator;
5454
import org.apache.polaris.core.persistence.RetryOnConcurrencyException;
5555
import org.apache.polaris.core.persistence.transactional.AbstractTransactionalPersistence;
56+
import org.apache.polaris.core.policy.PolarisPolicyMappingRecord;
5657
import org.apache.polaris.core.storage.PolarisStorageConfigurationInfo;
5758
import org.apache.polaris.core.storage.PolarisStorageIntegration;
5859
import org.apache.polaris.core.storage.PolarisStorageIntegrationProvider;
5960
import org.apache.polaris.jpa.models.ModelEntity;
6061
import org.apache.polaris.jpa.models.ModelEntityActive;
6162
import org.apache.polaris.jpa.models.ModelEntityChangeTracking;
6263
import org.apache.polaris.jpa.models.ModelGrantRecord;
64+
import org.apache.polaris.jpa.models.ModelPolicyMappingRecord;
6365
import org.apache.polaris.jpa.models.ModelPrincipalSecrets;
6466
import org.slf4j.Logger;
6567
import org.slf4j.LoggerFactory;
@@ -664,6 +666,88 @@ PolarisStorageIntegration<T> loadPolarisStorageIntegrationInCurrentTxn(
664666
return storageIntegrationProvider.getStorageIntegrationForConfig(storageConfig);
665667
}
666668

669+
/** {@inheritDoc} */
670+
@Override
671+
public void writeToPolicyMappingRecordsInCurrentTxn(
672+
@Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
673+
674+
this.store.writeToPolicyMappingRecords(localSession.get(), record);
675+
}
676+
677+
/** {@inheritDoc} */
678+
@Override
679+
public void deleteFromPolicyMappingRecordsInCurrentTxn(
680+
@Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
681+
this.store.deleteFromPolicyMappingRecords(localSession.get(), record);
682+
}
683+
684+
/** {@inheritDoc} */
685+
@Override
686+
public void deleteAllEntityPolicyMappingRecordsInCurrentTxn(
687+
@Nonnull PolarisCallContext callCtx,
688+
@Nonnull PolarisEntityCore entity,
689+
@Nonnull List<PolarisPolicyMappingRecord> mappingOnTarget,
690+
@Nonnull List<PolarisPolicyMappingRecord> mappingOnPolicy) {
691+
this.store.deleteAllEntityPolicyMappingRecords(localSession.get(), entity);
692+
}
693+
694+
/** {@inheritDoc} */
695+
@Nullable
696+
@Override
697+
public PolarisPolicyMappingRecord lookupPolicyMappingRecordInCurrentTxn(
698+
@Nonnull PolarisCallContext callCtx,
699+
long targetCatalogId,
700+
long targetId,
701+
int policyTypeCode,
702+
long policyCatalogId,
703+
long policyId) {
704+
return ModelPolicyMappingRecord.toPolicyMappingRecord(
705+
this.store.lookupPolicyMappingRecord(
706+
localSession.get(),
707+
targetCatalogId,
708+
targetId,
709+
policyTypeCode,
710+
policyCatalogId,
711+
policyId));
712+
}
713+
714+
/** {@inheritDoc} */
715+
@Nonnull
716+
@Override
717+
public List<PolarisPolicyMappingRecord> loadPoliciesOnTargetByTypeInCurrentTxn(
718+
@Nonnull PolarisCallContext callCtx,
719+
long targetCatalogId,
720+
long targetId,
721+
int policyTypeCode) {
722+
return this.store
723+
.loadPoliciesOnTargetByType(localSession.get(), targetCatalogId, targetId, policyTypeCode)
724+
.stream()
725+
.map(ModelPolicyMappingRecord::toPolicyMappingRecord)
726+
.toList();
727+
}
728+
729+
/** {@inheritDoc} */
730+
@Nonnull
731+
@Override
732+
public List<PolarisPolicyMappingRecord> loadAllPoliciesOnTargetInCurrentTxn(
733+
@Nonnull PolarisCallContext callCtx, long targetCatalogId, long targetId) {
734+
return this.store
735+
.loadAllPoliciesOnTarget(localSession.get(), targetCatalogId, targetId)
736+
.stream()
737+
.map(ModelPolicyMappingRecord::toPolicyMappingRecord)
738+
.toList();
739+
}
740+
741+
/** {@inheritDoc} */
742+
@Nonnull
743+
@Override
744+
public List<PolarisPolicyMappingRecord> loadAllTargetsOnPolicyInCurrentTxn(
745+
@Nonnull PolarisCallContext callCtx, long policyCatalogId, long policyId) {
746+
return this.store.loadAllTargetsOnPolicy(localSession.get(), policyCatalogId, policyId).stream()
747+
.map(ModelPolicyMappingRecord::toPolicyMappingRecord)
748+
.toList();
749+
}
750+
667751
@Override
668752
public void rollback() {
669753
EntityManager session = localSession.get();

extension/persistence/eclipselink/src/main/java/org/apache/polaris/extension/persistence/impl/eclipselink/PolarisEclipseLinkStore.java

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
import org.apache.polaris.core.entity.PolarisEntityType;
3636
import org.apache.polaris.core.entity.PolarisGrantRecord;
3737
import org.apache.polaris.core.entity.PolarisPrincipalSecrets;
38+
import org.apache.polaris.core.policy.PolarisPolicyMappingRecord;
3839
import org.apache.polaris.jpa.models.ModelEntity;
3940
import org.apache.polaris.jpa.models.ModelEntityActive;
4041
import org.apache.polaris.jpa.models.ModelEntityChangeTracking;
4142
import org.apache.polaris.jpa.models.ModelGrantRecord;
43+
import org.apache.polaris.jpa.models.ModelPolicyMappingRecord;
4244
import org.apache.polaris.jpa.models.ModelPrincipalSecrets;
4345
import org.slf4j.Logger;
4446
import org.slf4j.LoggerFactory;
@@ -411,6 +413,120 @@ void deletePrincipalSecrets(EntityManager session, String clientId) {
411413
session.remove(modelPrincipalSecrets);
412414
}
413415

416+
void writeToPolicyMappingRecords(
417+
EntityManager session, PolarisPolicyMappingRecord mappingRecord) {
418+
diagnosticServices.check(session != null, "session_is_null");
419+
checkInitialized();
420+
421+
session.persist(ModelPolicyMappingRecord.fromPolicyMappingRecord(mappingRecord));
422+
}
423+
424+
void deleteFromPolicyMappingRecords(
425+
EntityManager session, PolarisPolicyMappingRecord mappingRecord) {
426+
diagnosticServices.check(session != null, "session_is_null");
427+
checkInitialized();
428+
429+
ModelPolicyMappingRecord lookupPolicyMappingRecord =
430+
lookupPolicyMappingRecord(
431+
session,
432+
mappingRecord.getTargetCatalogId(),
433+
mappingRecord.getTargetId(),
434+
mappingRecord.getPolicyTypeCode(),
435+
mappingRecord.getPolicyCatalogId(),
436+
mappingRecord.getPolicyId());
437+
438+
diagnosticServices.check(lookupPolicyMappingRecord != null, "policy_mapping_record_not_found");
439+
session.remove(lookupPolicyMappingRecord);
440+
}
441+
442+
void deleteAllEntityPolicyMappingRecords(EntityManager session, PolarisEntityCore entity) {
443+
diagnosticServices.check(session != null, "session_is_null");
444+
checkInitialized();
445+
446+
loadAllTargetsOnPolicy(session, entity.getCatalogId(), entity.getId()).forEach(session::remove);
447+
loadAllPoliciesOnTarget(session, entity.getCatalogId(), entity.getId())
448+
.forEach(session::remove);
449+
}
450+
451+
ModelPolicyMappingRecord lookupPolicyMappingRecord(
452+
EntityManager session,
453+
long targetCatalogId,
454+
long targetId,
455+
long policyTypeCode,
456+
long policyCatalogId,
457+
long policyId) {
458+
diagnosticServices.check(session != null, "session_is_null");
459+
checkInitialized();
460+
461+
return session
462+
.createQuery(
463+
"SELECT m from ModelPolicyMappingRecord m "
464+
+ "where m.targetCatalogId=:targetCatalogId "
465+
+ "and m.targetId=:targetId "
466+
+ "and m.policyTypeCode=:policyTypeCode "
467+
+ "and m.policyCatalogId=:policyCatalogId "
468+
+ "and m.policyId=:policyId",
469+
ModelPolicyMappingRecord.class)
470+
.setParameter("targetCatalogId", targetCatalogId)
471+
.setParameter("targetId", targetId)
472+
.setParameter("policyTypeCode", policyTypeCode)
473+
.setParameter("policyCatalogId", policyCatalogId)
474+
.setParameter("policyId", policyId)
475+
.getResultStream()
476+
.findFirst()
477+
.orElse(null);
478+
}
479+
480+
List<ModelPolicyMappingRecord> loadPoliciesOnTargetByType(
481+
EntityManager session, long targetCatalogId, long targetId, int policyTypeCode) {
482+
diagnosticServices.check(session != null, "session_is_null");
483+
checkInitialized();
484+
485+
return session
486+
.createQuery(
487+
"SELECT m from ModelPolicyMappingRecord m "
488+
+ "where m.targetCatalogId=:targetCatalogId "
489+
+ "and m.targetId=:targetId "
490+
+ "and m.policyTypeCode=:policyTypeCode",
491+
ModelPolicyMappingRecord.class)
492+
.setParameter("targetCatalogId", targetCatalogId)
493+
.setParameter("targetId", targetId)
494+
.setParameter("policyTypeCode", policyTypeCode)
495+
.getResultList();
496+
}
497+
498+
List<ModelPolicyMappingRecord> loadAllPoliciesOnTarget(
499+
EntityManager session, long targetCatalogId, long targetId) {
500+
diagnosticServices.check(session != null, "session_is_null");
501+
checkInitialized();
502+
503+
return session
504+
.createQuery(
505+
"SELECT m from ModelPolicyMappingRecord m "
506+
+ " where m.targetCatalogId=:targetCatalogId "
507+
+ "and m.targetId=:targetId",
508+
ModelPolicyMappingRecord.class)
509+
.setParameter("targetCatalogId", targetCatalogId)
510+
.setParameter("targetId", targetId)
511+
.getResultList();
512+
}
513+
514+
List<ModelPolicyMappingRecord> loadAllTargetsOnPolicy(
515+
EntityManager session, long policyCatalogId, long policyId) {
516+
diagnosticServices.check(session != null, "session_is_null");
517+
checkInitialized();
518+
519+
return session
520+
.createQuery(
521+
"SELECT m from ModelPolicyMappingRecord m "
522+
+ "where m.policyCatalogId=:policyCatalogId "
523+
+ "and m.policyId=:policyId",
524+
ModelPolicyMappingRecord.class)
525+
.setParameter("policyCatalogId", policyCatalogId)
526+
.setParameter("policyId", policyId)
527+
.getResultList();
528+
}
529+
414530
private void checkInitialized() {
415531
diagnosticServices.check(this.initialized.get(), "store_not_initialized");
416532
}

extension/persistence/eclipselink/src/main/resources/META-INF/persistence.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<class>org.apache.polaris.jpa.models.ModelEntityActive</class>
3030
<class>org.apache.polaris.jpa.models.ModelEntityChangeTracking</class>
3131
<class>org.apache.polaris.jpa.models.ModelGrantRecord</class>
32+
<class>org.apache.polaris.jpa.models.ModelPolicyMappingRecord</class>
3233
<class>org.apache.polaris.jpa.models.ModelPrincipalSecrets</class>
3334
<class>org.apache.polaris.jpa.models.ModelSequenceId</class>
3435
<shared-cache-mode>NONE</shared-cache-mode>

0 commit comments

Comments
 (0)