Skip to content

Use asMap property helpers #2347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2025
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 @@ -19,12 +19,9 @@
package org.apache.polaris.core.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.apache.polaris.core.persistence.PolarisObjectMapperUtil;

/**
* Base polaris entity representing all attributes of a Polaris Entity. This is used to exchange
Expand All @@ -34,9 +31,6 @@ public class PolarisBaseEntity extends PolarisEntityCore {

public static final String EMPTY_MAP_STRING = "{}";

// to serialize/deserialize properties
private static final ObjectMapper MAPPER = new ObjectMapper();

// the type of the entity when it was resolved
protected final int subTypeCode;

Expand Down Expand Up @@ -106,25 +100,15 @@ public PolarisBaseEntity withGrantRecordsVersion(int grantRecordsVersion) {

@JsonIgnore
public Map<String, String> getPropertiesAsMap() {
if (properties == null) {
return new HashMap<>();
}
try {
return MAPPER.readValue(properties, new TypeReference<>() {});
} catch (JsonProcessingException ex) {
throw new IllegalStateException(
String.format("Failed to deserialize json. properties %s", properties), ex);
}
return PolarisObjectMapperUtil.deserializeProperties(getProperties());
}

@JsonIgnore
public static String convertPropertiesToJson(Map<String, String> properties) {
try {
return properties == null ? null : MAPPER.writeValueAsString(properties);
} catch (JsonProcessingException ex) {
throw new IllegalStateException(
String.format("Failed to serialize json. properties %s", properties), ex);
protected static String convertPropertiesToJson(Map<String, String> properties) {
if (properties == null || properties.isEmpty()) {
return EMPTY_MAP_STRING;
}
return PolarisObjectMapperUtil.serializeProperties(properties);
}

public String getInternalProperties() {
Expand All @@ -133,17 +117,7 @@ public String getInternalProperties() {

@JsonIgnore
public Map<String, String> getInternalPropertiesAsMap() {
if (this.internalProperties == null) {
return new HashMap<>();
}
try {
return MAPPER.readValue(this.internalProperties, new TypeReference<>() {});
} catch (JsonProcessingException ex) {
throw new IllegalStateException(
String.format(
"Failed to deserialize json. internalProperties %s", this.internalProperties),
ex);
}
return PolarisObjectMapperUtil.deserializeProperties(getInternalProperties());
}

public int getGrantRecordsVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -858,9 +858,7 @@ private void revokeGrantRecord(
}

PolarisBaseEntity principal = loadEntityResult.getEntity();
Map<String, String> internalProps =
PolarisObjectMapperUtil.deserializeProperties(
principal.getInternalProperties() == null ? "{}" : principal.getInternalProperties());
Map<String, String> internalProps = principal.getInternalPropertiesAsMap();

boolean doReset =
reset
Expand All @@ -877,15 +875,13 @@ private void revokeGrantRecord(
PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE)) {
internalProps.put(
PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE, "true");
principalBuilder.internalProperties(
PolarisObjectMapperUtil.serializeProperties(internalProps));
principalBuilder.internalPropertiesAsMap(internalProps);
principalBuilder.entityVersion(principal.getEntityVersion() + 1);
ms.writeEntity(callCtx, principalBuilder.build(), true, principal);
} else if (internalProps.containsKey(
PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE)) {
internalProps.remove(PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE);
principalBuilder.internalProperties(
PolarisObjectMapperUtil.serializeProperties(internalProps));
principalBuilder.internalPropertiesAsMap(internalProps);
principalBuilder.entityVersion(principal.getEntityVersion() + 1);
ms.writeEntity(callCtx, principalBuilder.build(), true, principal);
}
Expand Down Expand Up @@ -1230,16 +1226,15 @@ private void revokeGrantRecord(
PolarisTaskConstants.TASK_DATA, PolarisObjectMapperUtil.serialize(refreshEntityToDrop));
PolarisBaseEntity.Builder taskEntityBuilder =
new PolarisBaseEntity.Builder()
.properties(PolarisObjectMapperUtil.serializeProperties(properties))
.propertiesAsMap(properties)
.id(ms.generateNewId(callCtx))
.catalogId(0L)
.name("entityCleanup_" + entityToDrop.getId())
.typeCode(PolarisEntityType.TASK.getCode())
.subTypeCode(PolarisEntitySubType.NULL_SUBTYPE.getCode())
.createTimestamp(clock.millis());
if (cleanupProperties != null) {
taskEntityBuilder.internalProperties(
PolarisObjectMapperUtil.serializeProperties(cleanupProperties));
taskEntityBuilder.internalPropertiesAsMap(cleanupProperties);
}
// TODO: Add a way to create the task entities atomically with dropping the entity;
// in the meantime, if the server fails partway through a dropEntity, it's possible that
Expand Down Expand Up @@ -1523,8 +1518,7 @@ private void revokeGrantRecord(
task -> {
PolarisBaseEntity.Builder updatedTaskBuilder =
new PolarisBaseEntity.Builder(task);
Map<String, String> properties =
PolarisObjectMapperUtil.deserializeProperties(task.getProperties());
Map<String, String> properties = task.getPropertiesAsMap();
properties.put(PolarisTaskConstants.LAST_ATTEMPT_EXECUTOR_ID, executorId);
properties.put(
PolarisTaskConstants.LAST_ATTEMPT_START_TIME, String.valueOf(clock.millis()));
Expand All @@ -1534,8 +1528,7 @@ private void revokeGrantRecord(
Integer.parseInt(
properties.getOrDefault(PolarisTaskConstants.ATTEMPT_COUNT, "0"))
+ 1));
updatedTaskBuilder.properties(
PolarisObjectMapperUtil.serializeProperties(properties));
updatedTaskBuilder.propertiesAsMap(properties);
EntityResult result =
updateEntityPropertiesIfNotChanged(callCtx, null, updatedTaskBuilder.build());
if (result.getReturnStatus() == BaseResult.ReturnStatus.SUCCESS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public abstract class BaseMetaStoreManager implements PolarisMetaStoreManager {

public static PolarisStorageConfigurationInfo extractStorageConfiguration(
@Nonnull PolarisDiagnostics diagnostics, PolarisBaseEntity reloadedEntity) {
Map<String, String> propMap =
PolarisObjectMapperUtil.deserializeProperties(reloadedEntity.getInternalProperties());
Map<String, String> propMap = reloadedEntity.getInternalPropertiesAsMap();
String storageConfigInfoStr =
propMap.get(PolarisEntityConstants.getStorageConfigInfoPropertyName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -893,9 +893,7 @@ private void bootstrapPolarisService(

PolarisBaseEntity principal = loadEntityResult.getEntity();
PolarisBaseEntity.Builder principalBuilder = new PolarisBaseEntity.Builder(principal);
Map<String, String> internalProps =
PolarisObjectMapperUtil.deserializeProperties(
principal.getInternalProperties() == null ? "{}" : principal.getInternalProperties());
Map<String, String> internalProps = principal.getInternalPropertiesAsMap();

boolean doReset =
reset
Expand All @@ -911,15 +909,13 @@ private void bootstrapPolarisService(
PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE)) {
internalProps.put(
PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE, "true");
principalBuilder.internalProperties(
PolarisObjectMapperUtil.serializeProperties(internalProps));
principalBuilder.internalPropertiesAsMap(internalProps);
principalBuilder.entityVersion(principal.getEntityVersion() + 1);
ms.writeEntityInCurrentTxn(callCtx, principalBuilder.build(), true, principal);
} else if (internalProps.containsKey(
PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE)) {
internalProps.remove(PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE);
principalBuilder.internalProperties(
PolarisObjectMapperUtil.serializeProperties(internalProps));
principalBuilder.internalPropertiesAsMap(internalProps);
principalBuilder.entityVersion(principal.getEntityVersion() + 1);
ms.writeEntityInCurrentTxn(callCtx, principalBuilder.build(), true, principal);
}
Expand Down Expand Up @@ -1438,10 +1434,9 @@ private void bootstrapPolarisService(
.typeCode(PolarisEntityType.TASK.getCode())
.subTypeCode(PolarisEntitySubType.NULL_SUBTYPE.getCode())
.createTimestamp(clock.millis())
.properties(PolarisObjectMapperUtil.serializeProperties(properties));
.propertiesAsMap(properties);
if (cleanupProperties != null) {
taskEntityBuilder.internalProperties(
PolarisObjectMapperUtil.serializeProperties(cleanupProperties));
taskEntityBuilder.internalPropertiesAsMap(cleanupProperties);
}
PolarisBaseEntity taskEntity = taskEntityBuilder.build();
createEntityIfNotExists(callCtx, ms, null, taskEntity);
Expand Down Expand Up @@ -1975,8 +1970,7 @@ private PolarisEntityResolver resolveSecurableToRoleGrant(
.map(
task -> {
PolarisBaseEntity.Builder updatedTask = new PolarisBaseEntity.Builder(task);
Map<String, String> properties =
PolarisObjectMapperUtil.deserializeProperties(task.getProperties());
Map<String, String> properties = task.getPropertiesAsMap();
properties.put(PolarisTaskConstants.LAST_ATTEMPT_EXECUTOR_ID, executorId);
properties.put(
PolarisTaskConstants.LAST_ATTEMPT_START_TIME, String.valueOf(clock.millis()));
Expand All @@ -1986,7 +1980,7 @@ private PolarisEntityResolver resolveSecurableToRoleGrant(
Integer.parseInt(
properties.getOrDefault(PolarisTaskConstants.ATTEMPT_COUNT, "0"))
+ 1));
updatedTask.properties(PolarisObjectMapperUtil.serializeProperties(properties));
updatedTask.propertiesAsMap(properties);
EntityResult result =
updateEntityPropertiesIfNotChanged(callCtx, ms, null, updatedTask.build());
if (result.getReturnStatus() == BaseResult.ReturnStatus.SUCCESS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.apache.polaris.core.entity.PolarisEntitySubType;
import org.apache.polaris.core.entity.PolarisEntityType;
import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
import org.apache.polaris.core.persistence.PolarisObjectMapperUtil;
import org.apache.polaris.core.persistence.dao.entity.BaseResult;
import org.apache.polaris.core.persistence.dao.entity.ScopedCredentialsResult;
import org.apache.polaris.core.persistence.transactional.TransactionalPersistence;
Expand Down Expand Up @@ -246,9 +245,7 @@ public void testCacheGenerateNewEntries() {
internalMap.put(
PolarisEntityConstants.getStorageConfigInfoPropertyName(), "newStorageConfig");
PolarisBaseEntity updateEntity =
new PolarisBaseEntity.Builder(entity)
.internalProperties(PolarisObjectMapperUtil.serializeProperties(internalMap))
.build();
new PolarisBaseEntity.Builder(entity).internalPropertiesAsMap(internalMap).build();
storageCredentialCache.getOrGenerateSubScopeCreds(
metaStoreManager,
callCtx,
Expand Down Expand Up @@ -286,9 +283,7 @@ public void testCacheGenerateNewEntries() {
internalMap.put(
PolarisEntityConstants.getStorageConfigInfoPropertyName(), "newStorageConfig");
PolarisBaseEntity updateEntity =
new PolarisBaseEntity.Builder(entity)
.internalProperties(PolarisObjectMapperUtil.serializeProperties(internalMap))
.build();
new PolarisBaseEntity.Builder(entity).internalPropertiesAsMap(internalMap).build();
storageCredentialCache.getOrGenerateSubScopeCreds(
metaStoreManager,
callCtx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ protected void testLoadTasks() {
entry ->
Assertions.assertThat(entry)
.extracting(
e -> PolarisObjectMapperUtil.deserializeProperties(e.getProperties()))
.asInstanceOf(InstanceOfAssertFactories.map(String.class, String.class))
PolarisBaseEntity::getPropertiesAsMap,
InstanceOfAssertFactories.map(String.class, String.class))
.containsEntry(PolarisTaskConstants.LAST_ATTEMPT_EXECUTOR_ID, executorId)
.containsEntry(PolarisTaskConstants.ATTEMPT_COUNT, "1"));
Set<String> firstTasks =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/
package org.apache.polaris.core.persistence;

import static org.apache.polaris.core.entity.PolarisBaseEntity.convertPropertiesToJson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -419,11 +417,8 @@ PolarisBaseEntity createPrincipal(String name) {
.subTypeCode(PolarisEntitySubType.NULL_SUBTYPE.getCode())
.parentId(PolarisEntityConstants.getRootEntityId())
.name(name)
.internalProperties(
PolarisObjectMapperUtil.serializeProperties(
Map.of(
PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE,
"true")))
.internalPropertiesAsMap(
Map.of(PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE, "true"))
.build();

CreatePrincipalResult createPrincipalResult =
Expand Down Expand Up @@ -466,8 +461,7 @@ PolarisBaseEntity createPrincipal(String name) {
.isEqualTo(secrets.getSecondarySecretHash());

Map<String, String> internalProperties =
PolarisObjectMapperUtil.deserializeProperties(
createPrincipalResult.getPrincipal().getInternalProperties());
createPrincipalResult.getPrincipal().getInternalPropertiesAsMap();
Assertions.assertThat(
internalProperties.get(
PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE))
Expand Down Expand Up @@ -510,8 +504,7 @@ PolarisBaseEntity createPrincipal(String name) {
createPrincipalResult.getPrincipal().getId(),
createPrincipalResult.getPrincipal().getType())
.getEntity();
internalProperties =
PolarisObjectMapperUtil.deserializeProperties(reloadPrincipal.getInternalProperties());
internalProperties = reloadPrincipal.getInternalPropertiesAsMap();
Assertions.assertThat(
internalProperties.get(
PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE))
Expand Down Expand Up @@ -567,8 +560,7 @@ PolarisBaseEntity createPrincipal(String name) {
.loadEntity(
this.polarisCallContext, 0L, principalEntity.getId(), principalEntity.getType())
.getEntity();
internalProperties =
PolarisObjectMapperUtil.deserializeProperties(newPrincipal.getInternalProperties());
internalProperties = newPrincipal.getInternalPropertiesAsMap();
Assertions.assertThat(
internalProperties.get(
PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE))
Expand Down Expand Up @@ -601,8 +593,7 @@ PolarisBaseEntity createPrincipal(String name) {
.loadEntity(
this.polarisCallContext, 0L, principalEntity.getId(), principalEntity.getType())
.getEntity();
internalProperties =
PolarisObjectMapperUtil.deserializeProperties(finalPrincipal.getInternalProperties());
internalProperties = finalPrincipal.getInternalPropertiesAsMap();
Assertions.assertThat(
internalProperties.get(
PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE))
Expand Down Expand Up @@ -669,7 +660,7 @@ PolarisBaseEntity createEntity(
.subTypeCode(entitySubType.getCode())
.parentId(parentId)
.name(name)
.properties(convertPropertiesToJson(properties))
.propertiesAsMap(properties)
.build();
PolarisBaseEntity entity =
polarisMetaStoreManager
Expand Down Expand Up @@ -870,11 +861,9 @@ void dropEntity(List<PolarisEntityCore> catalogPath, PolarisBaseEntity entityToD
Assertions.assertThat(cleanupTask).isNotNull();
Assertions.assertThat(cleanupTask.getType()).isEqualTo(PolarisEntityType.TASK);
Assertions.assertThat(cleanupTask.getInternalProperties()).isNotNull();
Map<String, String> internalProperties =
PolarisObjectMapperUtil.deserializeProperties(cleanupTask.getInternalProperties());
Map<String, String> internalProperties = cleanupTask.getInternalPropertiesAsMap();
Assertions.assertThat(internalProperties).isEqualTo(cleanupProperties);
Map<String, String> properties =
PolarisObjectMapperUtil.deserializeProperties(cleanupTask.getProperties());
Map<String, String> properties = cleanupTask.getPropertiesAsMap();
Assertions.assertThat(properties).isNotNull();
Assertions.assertThat(properties.get(PolarisTaskConstants.TASK_DATA)).isNotNull();
PolarisBaseEntity droppedEntity =
Expand Down