-
Notifications
You must be signed in to change notification settings - Fork 306
Add PolarisObjectMapper #2164
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
Add PolarisObjectMapper #2164
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.polaris.core.persistence; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.Map; | ||
import org.apache.iceberg.rest.RESTSerializers; | ||
import org.apache.polaris.core.PolarisDiagnostics; | ||
|
||
/** A mapper to serialize/deserialize polaris objects. */ | ||
public class PolarisObjectMapper { | ||
/** mapper, allows to serialize/deserialize properties to/from JSON */ | ||
private static final ObjectMapper MAPPER = configureMapper(); | ||
|
||
private static ObjectMapper configureMapper() { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false); | ||
RESTSerializers.registerAll(mapper); | ||
return mapper; | ||
} | ||
|
||
private final PolarisDiagnostics diagnostics; | ||
|
||
public PolarisObjectMapper(PolarisDiagnostics diagnostics) { | ||
this.diagnostics = diagnostics; | ||
} | ||
|
||
public String serialize(Object object) { | ||
try { | ||
return MAPPER.writeValueAsString(object); | ||
} catch (JsonProcessingException e) { | ||
diagnostics.fail("got_json_processing_exception", e.getMessage()); | ||
} | ||
return ""; | ||
} | ||
|
||
public <T> T deserialize(String json, Class<T> klass) { | ||
try { | ||
return MAPPER.readValue(json, klass); | ||
} catch (JsonProcessingException e) { | ||
diagnostics.fail("got_json_processing_exception", e.getMessage()); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Given the internal property as a map of key/value pairs, serialize it to a String | ||
* | ||
* @param properties a map of key/value pairs | ||
* @return a String, the JSON representation of the map | ||
*/ | ||
public String serializeProperties(Map<String, String> properties) { | ||
try { | ||
// Serialize the Map<String, String> to a JSON string | ||
return MAPPER.writeValueAsString(properties); | ||
} catch (JsonProcessingException ex) { | ||
diagnostics.fail("got_json_processing_exception", ex.getMessage()); | ||
} | ||
return ""; | ||
} | ||
|
||
/** | ||
* Given the serialized properties, deserialize those to a {@code Map<String, String>} | ||
* | ||
* @param properties a JSON string representing the set of properties | ||
* @return a Map of string | ||
*/ | ||
public Map<String, String> deserializeProperties(String properties) { | ||
try { | ||
// Deserialize the JSON string to a Map<String, String> | ||
return MAPPER.readValue(properties, new TypeReference<>() {}); | ||
} catch (JsonMappingException ex) { | ||
diagnostics.fail("got_json_mapping_exception", "properties={}, ex={}", properties, ex); | ||
} catch (JsonProcessingException ex) { | ||
diagnostics.fail("got_json_processing_exception", "properties={}, ex={}", properties, ex); | ||
} | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,98 +20,19 @@ | |
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.annotation.Nullable; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
import org.apache.iceberg.rest.RESTSerializers; | ||
import org.apache.polaris.core.PolarisCallContext; | ||
import org.apache.polaris.core.entity.PolarisBaseEntity; | ||
import org.apache.polaris.core.entity.PolarisTaskConstants; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** A mapper to serialize/deserialize polaris objects. */ | ||
public class PolarisObjectMapperUtil { | ||
public final class PolarisObjectMapperUtil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we go forward with this PR we might want to rename this class as it now only deals with task (execution) state? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either way works for me :) |
||
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisObjectMapperUtil.class); | ||
|
||
/** mapper, allows to serialize/deserialize properties to/from JSON */ | ||
private static final ObjectMapper MAPPER = configureMapper(); | ||
|
||
private static ObjectMapper configureMapper() { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false); | ||
RESTSerializers.registerAll(mapper); | ||
return mapper; | ||
} | ||
|
||
/** | ||
* Given the internal property as a map of key/value pairs, serialize it to a String | ||
* | ||
* @param properties a map of key/value pairs | ||
* @return a String, the JSON representation of the map | ||
*/ | ||
public static String serializeProperties( | ||
PolarisCallContext callCtx, Map<String, String> properties) { | ||
|
||
String jsonString = null; | ||
try { | ||
// Deserialize the JSON string to a Map<String, String> | ||
jsonString = MAPPER.writeValueAsString(properties); | ||
} catch (JsonProcessingException ex) { | ||
callCtx.getDiagServices().fail("got_json_processing_exception", ex.getMessage()); | ||
} | ||
|
||
return jsonString; | ||
} | ||
|
||
public static String serialize(PolarisCallContext callCtx, Object object) { | ||
try { | ||
return MAPPER.writeValueAsString(object); | ||
} catch (JsonProcessingException e) { | ||
callCtx.getDiagServices().fail("got_json_processing_exception", e.getMessage()); | ||
} | ||
return ""; | ||
} | ||
|
||
public static <T> T deserialize(PolarisCallContext callCtx, String text, Class<T> klass) { | ||
try { | ||
return MAPPER.readValue(text, klass); | ||
} catch (JsonProcessingException e) { | ||
callCtx.getDiagServices().fail("got_json_processing_exception", e.getMessage()); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Given the serialized properties, deserialize those to a {@code Map<String, String>} | ||
* | ||
* @param properties a JSON string representing the set of properties | ||
* @return a Map of string | ||
*/ | ||
public static Map<String, String> deserializeProperties( | ||
PolarisCallContext callCtx, String properties) { | ||
|
||
Map<String, String> retProperties = null; | ||
try { | ||
// Deserialize the JSON string to a Map<String, String> | ||
retProperties = MAPPER.readValue(properties, new TypeReference<>() {}); | ||
} catch (JsonMappingException ex) { | ||
callCtx | ||
.getDiagServices() | ||
.fail("got_json_mapping_exception", "properties={}, ex={}", properties, ex); | ||
} catch (JsonProcessingException ex) { | ||
callCtx | ||
.getDiagServices() | ||
.fail("got_json_processing_exception", "properties={}, ex={}", properties, ex); | ||
} | ||
|
||
return retProperties; | ||
private PolarisObjectMapperUtil() { | ||
// utility class | ||
} | ||
|
||
public static class TaskExecutionState { | ||
|
@@ -187,8 +108,4 @@ public int getAttemptCount() { | |
return null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you look above this line, json errors around task state dont get reported to |
||
} | ||
} | ||
|
||
long now() { | ||
return 0; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if
PolarisObjectMapperUtil
methods did not require thePolarisDiagnostics
for reporting json-related errors, then this usage ofCallContext.getCurrentContext()
could go away (also below in this file).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
found this gem a well:
polaris/polaris-core/src/main/java/org/apache/polaris/core/entity/CatalogEntity.java
Lines 219 to 221 in 987c554