forked from apache/amoro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Improvement]: Refactor TableFormat to a non-enumeration type to supp…
…ort non-open source formats (apache#3167) * Make TableFormat extensible * Make TableFormat extensible | fix ut * Make TableFormat extensible | fix ut * Make TableFormat extensible | fix ut * Fix json serializer && deserializer for TableFormat * Fix reviewer * Fix checkstyle * Fix checkstyle * Fix checkstyle
- Loading branch information
Showing
27 changed files
with
407 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
amoro-ams/src/main/java/org/apache/amoro/server/dashboard/JavalinJsonMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* 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.amoro.server.dashboard; | ||
|
||
import io.javalin.plugin.json.JsonMapper; | ||
import org.apache.amoro.TableFormat; | ||
import org.apache.amoro.shade.jackson2.com.fasterxml.jackson.core.JsonProcessingException; | ||
import org.apache.amoro.shade.jackson2.com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.apache.amoro.shade.jackson2.com.fasterxml.jackson.databind.module.SimpleModule; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** Json mapper to adapt shaded jackson. */ | ||
public class JavalinJsonMapper implements JsonMapper { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
public static JavalinJsonMapper createDefaultJsonMapper() { | ||
ObjectMapper om = new ObjectMapper(); | ||
SimpleModule module = new SimpleModule(); | ||
module.addSerializer(TableFormat.class, new TableFormat.JsonSerializer()); | ||
module.addDeserializer(TableFormat.class, new TableFormat.JsonDeserializer()); | ||
om.registerModule(module); | ||
return new JavalinJsonMapper(om); | ||
} | ||
|
||
public JavalinJsonMapper(ObjectMapper shadedMapper) { | ||
this.objectMapper = shadedMapper; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public String toJsonString(@NotNull Object obj) { | ||
if (obj instanceof String) { | ||
return (String) obj; | ||
} | ||
try { | ||
return objectMapper.writeValueAsString(obj); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public InputStream toJsonStream(@NotNull Object obj) { | ||
if (obj instanceof String) { | ||
String result = (String) obj; | ||
return new ByteArrayInputStream(result.getBytes()); | ||
} else { | ||
byte[] string = new byte[0]; | ||
try { | ||
string = objectMapper.writeValueAsBytes(obj); | ||
return new ByteArrayInputStream(string); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public <T> T fromJsonString(@NotNull String json, @NotNull Class<T> targetClass) { | ||
try { | ||
return objectMapper.readValue(json, targetClass); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public <T> T fromJsonStream(@NotNull InputStream json, @NotNull Class<T> targetClass) { | ||
try { | ||
return objectMapper.readValue(json, targetClass); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...ams/src/main/java/org/apache/amoro/server/persistence/converter/TableFormatConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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.amoro.server.persistence.converter; | ||
|
||
import org.apache.amoro.TableFormat; | ||
import org.apache.ibatis.type.JdbcType; | ||
import org.apache.ibatis.type.MappedJdbcTypes; | ||
import org.apache.ibatis.type.MappedTypes; | ||
import org.apache.ibatis.type.TypeHandler; | ||
|
||
import java.sql.CallableStatement; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
@MappedTypes(TableFormat.class) | ||
@MappedJdbcTypes(JdbcType.VARCHAR) | ||
public class TableFormatConverter implements TypeHandler<TableFormat> { | ||
|
||
@Override | ||
public void setParameter(PreparedStatement ps, int i, TableFormat parameter, JdbcType jdbcType) | ||
throws SQLException { | ||
if (parameter == null) { | ||
ps.setString(i, ""); | ||
} else { | ||
ps.setString(i, parameter.name()); | ||
} | ||
} | ||
|
||
@Override | ||
public TableFormat getResult(ResultSet rs, String columnName) throws SQLException { | ||
String res = rs.getString(columnName); | ||
if (res == null) { | ||
return null; | ||
} | ||
return TableFormat.valueOf(res); | ||
} | ||
|
||
@Override | ||
public TableFormat getResult(ResultSet rs, int columnIndex) throws SQLException { | ||
String res = rs.getString(columnIndex); | ||
if (res == null) { | ||
return null; | ||
} | ||
return TableFormat.valueOf(res); | ||
} | ||
|
||
@Override | ||
public TableFormat getResult(CallableStatement cs, int columnIndex) throws SQLException { | ||
String res = cs.getString(columnIndex); | ||
if (res == null) { | ||
return null; | ||
} | ||
return TableFormat.valueOf(res); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.