Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;
import javax.sql.DataSource;
import org.apache.polaris.extension.persistence.relational.jdbc.models.Converter;
Expand Down Expand Up @@ -96,20 +95,14 @@ public void executeScript(String scriptFilePath) throws SQLException {
* @param query : Query to executed
* @param converterInstance : An instance of the type being selected, used to convert to a
* business entity like PolarisBaseEntity
* @param transformer Transformation of entity class to Result class
* @return The list of results yielded by the query
* @param <T> : Persistence entity class
* @param <R> : Business entity class
* @param <T> : Business entity class
* @throws SQLException : Exception during the query execution.
*/
public <T, R> List<R> executeSelect(
@Nonnull String query,
@Nonnull Converter<T> converterInstance,
@Nonnull Function<T, R> transformer)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No transformer is required after this change.

public <T> List<T> executeSelect(@Nonnull String query, @Nonnull Converter<T> converterInstance)
throws SQLException {
ArrayList<R> results = new ArrayList<>();
executeSelectOverStream(
query, converterInstance, stream -> stream.map(transformer).forEach(results::add));
ArrayList<T> results = new ArrayList<>();
executeSelectOverStream(query, converterInstance, stream -> stream.forEach(results::add));
return results;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ public PolarisBaseEntity lookupEntityByName(
@Nullable
private PolarisBaseEntity getPolarisBaseEntity(String query) {
try {
List<PolarisBaseEntity> results =
datasourceOperations.executeSelect(query, new ModelEntity(), ModelEntity::toEntity);
var results = datasourceOperations.executeSelect(query, new ModelEntity());
if (results.isEmpty()) {
return null;
} else if (results.size() > 1) {
Expand All @@ -324,7 +323,7 @@ public List<PolarisBaseEntity> lookupEntities(
if (entityIds == null || entityIds.isEmpty()) return new ArrayList<>();
String query = generateSelectQueryWithEntityIds(realmId, entityIds);
try {
return datasourceOperations.executeSelect(query, new ModelEntity(), ModelEntity::toEntity);
return datasourceOperations.executeSelect(query, new ModelEntity());
} catch (SQLException e) {
throw new RuntimeException(
String.format("Failed to retrieve polaris entities due to %s", e.getMessage()), e);
Expand Down Expand Up @@ -420,7 +419,7 @@ public <T> Page<T> listEntities(
query,
new ModelEntity(),
stream -> {
var data = stream.map(ModelEntity::toEntity).filter(entityFilter);
var data = stream.filter(entityFilter);
if (pageToken instanceof HasPageSize hasPageSize) {
data = data.limit(hasPageSize.getPageSize());
}
Expand Down Expand Up @@ -472,9 +471,7 @@ public PolarisGrantRecord lookupGrantRecord(
realmId);
String query = generateSelectQuery(new ModelGrantRecord(), params);
try {
List<PolarisGrantRecord> results =
datasourceOperations.executeSelect(
query, new ModelGrantRecord(), ModelGrantRecord::toGrantRecord);
var results = datasourceOperations.executeSelect(query, new ModelGrantRecord());
if (results.size() > 1) {
throw new IllegalStateException(
String.format(
Expand Down Expand Up @@ -503,9 +500,7 @@ public List<PolarisGrantRecord> loadAllGrantRecordsOnSecurable(
realmId);
String query = generateSelectQuery(new ModelGrantRecord(), params);
try {
List<PolarisGrantRecord> results =
datasourceOperations.executeSelect(
query, new ModelGrantRecord(), ModelGrantRecord::toGrantRecord);
var results = datasourceOperations.executeSelect(query, new ModelGrantRecord());
return results == null ? Collections.emptyList() : results;
} catch (SQLException e) {
throw new RuntimeException(
Expand All @@ -525,9 +520,7 @@ public List<PolarisGrantRecord> loadAllGrantRecordsOnGrantee(
"grantee_catalog_id", granteeCatalogId, "grantee_id", granteeId, "realm_id", realmId);
String query = generateSelectQuery(new ModelGrantRecord(), params);
try {
List<PolarisGrantRecord> results =
datasourceOperations.executeSelect(
query, new ModelGrantRecord(), ModelGrantRecord::toGrantRecord);
var results = datasourceOperations.executeSelect(query, new ModelGrantRecord());
return results == null ? Collections.emptyList() : results;
} catch (SQLException e) {
throw new RuntimeException(
Expand All @@ -553,8 +546,7 @@ public boolean hasChildren(
}
String query = generateSelectQuery(new ModelEntity(), params);
try {
List<ModelEntity> results =
datasourceOperations.executeSelect(query, new ModelEntity(), Function.identity());
var results = datasourceOperations.executeSelect(query, new ModelEntity());
return results != null && !results.isEmpty();
} catch (SQLException e) {
throw new RuntimeException(
Expand All @@ -571,11 +563,8 @@ public PolarisPrincipalSecrets loadPrincipalSecrets(
Map<String, Object> params = Map.of("principal_client_id", clientId, "realm_id", realmId);
String query = generateSelectQuery(new ModelPrincipalAuthenticationData(), params);
try {
List<PolarisPrincipalSecrets> results =
datasourceOperations.executeSelect(
query,
new ModelPrincipalAuthenticationData(),
ModelPrincipalAuthenticationData::toPrincipalAuthenticationData);
var results =
datasourceOperations.executeSelect(query, new ModelPrincipalAuthenticationData());
return results == null || results.isEmpty() ? null : results.getFirst();
} catch (SQLException e) {
LOGGER.error(
Expand Down Expand Up @@ -875,11 +864,7 @@ public List<PolarisPolicyMappingRecord> loadAllTargetsOnPolicy(

private List<PolarisPolicyMappingRecord> fetchPolicyMappingRecords(String query) {
try {
List<PolarisPolicyMappingRecord> results =
datasourceOperations.executeSelect(
query,
new ModelPolicyMappingRecord(),
ModelPolicyMappingRecord::toPolicyMappingRecord);
var results = datasourceOperations.executeSelect(query, new ModelPolicyMappingRecord());
return results == null ? Collections.emptyList() : results;
} catch (SQLException e) {
throw new RuntimeException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface Converter<T> {
* Converts a ResultSet to model.
*
* @param rs : ResultSet from JDBC.
* @return Model of Entity
* @return the corresponding business entity
* @throws SQLException : Exception while fetching from ResultSet.
*/
T fromResultSet(ResultSet rs) throws SQLException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.polaris.core.entity.PolarisEntitySubType;
import org.apache.polaris.core.entity.PolarisEntityType;

public class ModelEntity implements Converter<ModelEntity> {
public class ModelEntity implements Converter<PolarisBaseEntity> {
// the id of the catalog associated to that entity. use 0 if this entity is top-level
// like a catalog
private long catalogId;
Expand Down Expand Up @@ -138,27 +138,29 @@ public static Builder builder() {
}

@Override
public ModelEntity fromResultSet(ResultSet r) throws SQLException {
return ModelEntity.builder()
.catalogId(r.getObject("catalog_id", Long.class))
.id(r.getObject("id", Long.class))
.parentId(r.getObject("parent_id", Long.class))
.typeCode(r.getObject("type_code", Integer.class))
.name(r.getObject("name", String.class))
.entityVersion(r.getObject("entity_version", Integer.class))
.subTypeCode(r.getObject("sub_type_code", Integer.class))
.createTimestamp(r.getObject("create_timestamp", Long.class))
.dropTimestamp(r.getObject("drop_timestamp", Long.class))
.purgeTimestamp(r.getObject("purge_timestamp", Long.class))
.toPurgeTimestamp(r.getObject("to_purge_timestamp", Long.class))
.lastUpdateTimestamp(r.getObject("last_update_timestamp", Long.class))
.properties(
r.getString("properties")) // required for extracting when the underlying type is JSONB
.internalProperties(
r.getString(
"internal_properties")) // required for extracting when the underlying type is JSONB
.grantRecordsVersion(r.getObject("grant_records_version", Integer.class))
.build();
public PolarisBaseEntity fromResultSet(ResultSet r) throws SQLException {
var modelEntity =
ModelEntity.builder()
.catalogId(r.getObject("catalog_id", Long.class))
.id(r.getObject("id", Long.class))
.parentId(r.getObject("parent_id", Long.class))
.typeCode(r.getObject("type_code", Integer.class))
.name(r.getObject("name", String.class))
.entityVersion(r.getObject("entity_version", Integer.class))
.subTypeCode(r.getObject("sub_type_code", Integer.class))
.createTimestamp(r.getObject("create_timestamp", Long.class))
.dropTimestamp(r.getObject("drop_timestamp", Long.class))
.purgeTimestamp(r.getObject("purge_timestamp", Long.class))
.toPurgeTimestamp(r.getObject("to_purge_timestamp", Long.class))
.lastUpdateTimestamp(r.getObject("last_update_timestamp", Long.class))
// JSONB: use getString(), not getObject().
.properties(r.getString("properties"))
// JSONB: use getString(), not getObject().
.internalProperties(r.getString("internal_properties"))
.grantRecordsVersion(r.getObject("grant_records_version", Integer.class))
.build();

return toEntity(modelEntity);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.Map;
import org.apache.polaris.core.entity.PolarisGrantRecord;

public class ModelGrantRecord implements Converter<ModelGrantRecord> {
public class ModelGrantRecord implements Converter<PolarisGrantRecord> {
// id of the catalog where the securable entity resides, use 0, if this entity is a
// top-level account entity.
private long securableCatalogId;
Expand Down Expand Up @@ -67,14 +67,17 @@ public static Builder builder() {
}

@Override
public ModelGrantRecord fromResultSet(ResultSet rs) throws SQLException {
return ModelGrantRecord.builder()
.securableCatalogId(rs.getObject("securable_catalog_id", Long.class))
.securableId(rs.getObject("securable_id", Long.class))
.granteeCatalogId(rs.getObject("grantee_catalog_id", Long.class))
.granteeId(rs.getObject("grantee_id", Long.class))
.privilegeCode(rs.getObject("privilege_code", Integer.class))
.build();
public PolarisGrantRecord fromResultSet(ResultSet rs) throws SQLException {
var modelGrantRecord =
ModelGrantRecord.builder()
.securableCatalogId(rs.getObject("securable_catalog_id", Long.class))
.securableId(rs.getObject("securable_id", Long.class))
.granteeCatalogId(rs.getObject("grantee_catalog_id", Long.class))
.granteeId(rs.getObject("grantee_id", Long.class))
.privilegeCode(rs.getObject("privilege_code", Integer.class))
.build();

return toGrantRecord(modelGrantRecord);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.Map;
import org.apache.polaris.core.policy.PolarisPolicyMappingRecord;

public class ModelPolicyMappingRecord implements Converter<ModelPolicyMappingRecord> {
public class ModelPolicyMappingRecord implements Converter<PolarisPolicyMappingRecord> {
// id of the catalog where target entity resides
private long targetCatalogId;

Expand Down Expand Up @@ -140,15 +140,18 @@ public static PolarisPolicyMappingRecord toPolicyMappingRecord(ModelPolicyMappin
}

@Override
public ModelPolicyMappingRecord fromResultSet(ResultSet rs) throws SQLException {
return ModelPolicyMappingRecord.builder()
.targetCatalogId(rs.getObject("target_catalog_id", Long.class))
.targetId(rs.getObject("target_id", Long.class))
.policyTypeCode(rs.getObject("policy_type_code", Integer.class))
.policyCatalogId(rs.getObject("policy_catalog_id", Long.class))
.policyId(rs.getObject("policy_id", Long.class))
.parameters(rs.getString("parameters"))
.build();
public PolarisPolicyMappingRecord fromResultSet(ResultSet rs) throws SQLException {
var modelRecord =
ModelPolicyMappingRecord.builder()
.targetCatalogId(rs.getObject("target_catalog_id", Long.class))
.targetId(rs.getObject("target_id", Long.class))
.policyTypeCode(rs.getObject("policy_type_code", Integer.class))
.policyCatalogId(rs.getObject("policy_catalog_id", Long.class))
.policyId(rs.getObject("policy_id", Long.class))
.parameters(rs.getString("parameters"))
.build();

return toPolicyMappingRecord(modelRecord);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
import java.util.Map;
import org.apache.polaris.core.entity.PolarisPrincipalSecrets;

public class ModelPrincipalAuthenticationData
implements Converter<ModelPrincipalAuthenticationData> {
public class ModelPrincipalAuthenticationData implements Converter<PolarisPrincipalSecrets> {
// the id of the principal
private long principalId;

Expand Down Expand Up @@ -65,14 +64,17 @@ public static Builder builder() {
}

@Override
public ModelPrincipalAuthenticationData fromResultSet(ResultSet rs) throws SQLException {
return ModelPrincipalAuthenticationData.builder()
.principalId(rs.getObject("principal_id", Long.class))
.principalClientId(rs.getObject("principal_client_id", String.class))
.mainSecretHash(rs.getObject("main_secret_hash", String.class))
.secondarySecretHash(rs.getObject("secondary_secret_hash", String.class))
.secretSalt(rs.getObject("secret_salt", String.class))
.build();
public PolarisPrincipalSecrets fromResultSet(ResultSet rs) throws SQLException {
var modelRecord =
ModelPrincipalAuthenticationData.builder()
.principalId(rs.getObject("principal_id", Long.class))
.principalClientId(rs.getObject("principal_client_id", String.class))
.mainSecretHash(rs.getObject("main_secret_hash", String.class))
.secondarySecretHash(rs.getObject("secondary_secret_hash", String.class))
.secretSalt(rs.getObject("secret_salt", String.class))
.build();

return toPrincipalAuthenticationData(modelRecord);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.function.Function;
import javax.sql.DataSource;
import org.apache.polaris.extension.persistence.relational.jdbc.DatasourceOperations;
import org.apache.polaris.extension.persistence.relational.jdbc.models.ModelEntity;
Expand Down Expand Up @@ -76,8 +75,7 @@ void testExecuteSelect_exception() throws Exception {
when(mockStatement.executeQuery(query)).thenThrow(new SQLException());

assertThrows(
SQLException.class,
() -> datasourceOperations.executeSelect(query, new ModelEntity(), Function.identity()));
SQLException.class, () -> datasourceOperations.executeSelect(query, new ModelEntity()));
}

@Test
Expand Down