This is an automated email from the ASF dual-hosted git repository.

dataroaring pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 1108cc5fb2a [feature](merge-cloud) Register runtime adaptor factory 
for CloudReplica/CloudPartition (#30251)
1108cc5fb2a is described below

commit 1108cc5fb2a8d65b5e5954aa56008cdf7b692db6
Author: walter <[email protected]>
AuthorDate: Tue Jan 23 19:22:26 2024 +0800

    [feature](merge-cloud) Register runtime adaptor factory for 
CloudReplica/CloudPartition (#30251)
    
    For compatible, add `RuntimeTypeAdaptorFactory.registerDefaultSubtype`
    to deserialize json object when label not found.
---
 .../org/apache/doris/persist/gson/GsonUtils.java   | 20 +++++++++++++++++
 .../persist/gson/RuntimeTypeAdapterFactory.java    | 25 ++++++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/persist/gson/GsonUtils.java 
b/fe/fe-core/src/main/java/org/apache/doris/persist/gson/GsonUtils.java
index 54c109b61a5..95071a43051 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/persist/gson/GsonUtils.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/persist/gson/GsonUtils.java
@@ -33,10 +33,12 @@ import org.apache.doris.catalog.ListPartitionInfo;
 import org.apache.doris.catalog.MapType;
 import org.apache.doris.catalog.OdbcCatalogResource;
 import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
 import org.apache.doris.catalog.PartitionInfo;
 import org.apache.doris.catalog.PartitionKey;
 import org.apache.doris.catalog.RandomDistributionInfo;
 import org.apache.doris.catalog.RangePartitionInfo;
+import org.apache.doris.catalog.Replica;
 import org.apache.doris.catalog.Resource;
 import org.apache.doris.catalog.S3Resource;
 import org.apache.doris.catalog.ScalarType;
@@ -64,6 +66,8 @@ import 
org.apache.doris.catalog.external.PaimonExternalDatabase;
 import org.apache.doris.catalog.external.PaimonExternalTable;
 import org.apache.doris.catalog.external.TestExternalDatabase;
 import org.apache.doris.catalog.external.TestExternalTable;
+import org.apache.doris.cloud.catalog.CloudPartition;
+import org.apache.doris.cloud.catalog.CloudReplica;
 import org.apache.doris.common.util.RangeUtils;
 import org.apache.doris.datasource.CatalogIf;
 import org.apache.doris.datasource.EsExternalCatalog;
@@ -278,6 +282,20 @@ public class GsonUtils {
             .registerSubtype(FrontendHbResponse.class, 
FrontendHbResponse.class.getSimpleName())
             .registerSubtype(BrokerHbResponse.class, 
BrokerHbResponse.class.getSimpleName());
 
+    // runtime adapter for class "CloudReplica".
+    private static RuntimeTypeAdapterFactory<Replica> 
replicaTypeAdapterFactory = RuntimeTypeAdapterFactory
+            .of(Replica.class, "clazz")
+            .registerDefaultSubtype(Replica.class)
+            .registerSubtype(Replica.class, Replica.class.getSimpleName())
+            .registerSubtype(CloudReplica.class, 
CloudReplica.class.getSimpleName());
+
+    // runtime adapter for class "CloudPartition".
+    private static RuntimeTypeAdapterFactory<Partition> 
partitionTypeAdapterFactory = RuntimeTypeAdapterFactory
+            .of(Partition.class, "clazz")
+            .registerDefaultSubtype(Partition.class)
+            .registerSubtype(Partition.class, Partition.class.getSimpleName())
+            .registerSubtype(CloudPartition.class, 
CloudPartition.class.getSimpleName());
+
     // the builder of GSON instance.
     // Add any other adapters if necessary.
     private static final GsonBuilder GSON_BUILDER = new 
GsonBuilder().addSerializationExclusionStrategy(
@@ -293,6 +311,8 @@ public class GsonUtils {
             
.registerTypeAdapterFactory(loadJobStateUpdateInfoTypeAdapterFactory)
             
.registerTypeAdapterFactory(policyTypeAdapterFactory).registerTypeAdapterFactory(dsTypeAdapterFactory)
             
.registerTypeAdapterFactory(dbTypeAdapterFactory).registerTypeAdapterFactory(tblTypeAdapterFactory)
+            .registerTypeAdapterFactory(replicaTypeAdapterFactory)
+            .registerTypeAdapterFactory(partitionTypeAdapterFactory)
             .registerTypeAdapterFactory(partitionInfoTypeAdapterFactory)
             .registerTypeAdapterFactory(hbResponseTypeAdapterFactory)
             .registerTypeAdapterFactory(rdsTypeAdapterFactory)
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/persist/gson/RuntimeTypeAdapterFactory.java
 
b/fe/fe-core/src/main/java/org/apache/doris/persist/gson/RuntimeTypeAdapterFactory.java
index 141a4464186..380e39771f4 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/persist/gson/RuntimeTypeAdapterFactory.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/persist/gson/RuntimeTypeAdapterFactory.java
@@ -183,6 +183,7 @@ public final class RuntimeTypeAdapterFactory<T> implements 
TypeAdapterFactory {
     private final Map<String, Class<?>> labelToSubtype = new 
LinkedHashMap<String, Class<?>>();
     private final Map<Class<?>, String> subtypeToLabel = new 
LinkedHashMap<Class<?>, String>();
     private final boolean maintainType;
+    private Class<? extends T> defaultType = null;
 
     private RuntimeTypeAdapterFactory(Class<?> baseType, String typeFieldName, 
boolean maintainType) {
         if (typeFieldName == null || baseType == null) {
@@ -251,6 +252,23 @@ public final class RuntimeTypeAdapterFactory<T> implements 
TypeAdapterFactory {
         return registerSubtype(type, type.getSimpleName());
     }
 
+    /**
+     * Registers {@code type} for using when label not found
+     *
+     * @throws IllegalArgumentException if {@code type}
+     *     have already been registered on this type adapter.
+     */
+    public RuntimeTypeAdapterFactory<T> registerDefaultSubtype(Class<? extends 
T> type) {
+        if (type == null) {
+            throw new NullPointerException();
+        }
+        if (subtypeToLabel.containsKey(type)) {
+            throw new IllegalArgumentException("types must be unique");
+        }
+        defaultType = type;
+        return this;
+    }
+
     public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
         if (type.getRawType() != baseType && 
!subtypeToLabel.containsKey(type.getRawType())) {
             return null;
@@ -258,6 +276,8 @@ public final class RuntimeTypeAdapterFactory<T> implements 
TypeAdapterFactory {
 
         final Map<String, TypeAdapter<?>> labelToDelegate = new 
LinkedHashMap<String, TypeAdapter<?>>();
         final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new 
LinkedHashMap<Class<?>, TypeAdapter<?>>();
+        final TypeAdapter<?> defaultDelegate = defaultType == null ? null :
+                gson.getDelegateAdapter(this, TypeToken.get(defaultType));
         for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
             TypeAdapter<?> delegate = gson.getDelegateAdapter(this, 
TypeToken.get(entry.getValue()));
             labelToDelegate.put(entry.getKey(), delegate);
@@ -276,6 +296,11 @@ public final class RuntimeTypeAdapterFactory<T> implements 
TypeAdapterFactory {
                 }
 
                 if (labelJsonElement == null) {
+                    if (defaultDelegate != null) {
+                        // registration requires that subtype extends T
+                        return (R) defaultDelegate.fromJsonTree(jsonElement);
+                    }
+
                     throw new JsonParseException("cannot deserialize " + 
baseType
                             + " because it does not define a field named " + 
typeFieldName);
                 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to