From 49c39f6bd778a528bde966088b0cca93ace1f421 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Thu, 2 Jan 2025 06:35:37 +0000 Subject: [PATCH] [core] allows to override the default factory by priority . --- .../java/org/apache/paimon/factories/Factory.java | 12 ++++++++++++ .../org/apache/paimon/factories/FactoryUtil.java | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/paimon-common/src/main/java/org/apache/paimon/factories/Factory.java b/paimon-common/src/main/java/org/apache/paimon/factories/Factory.java index 74796879ef4b..95640a219bf9 100644 --- a/paimon-common/src/main/java/org/apache/paimon/factories/Factory.java +++ b/paimon-common/src/main/java/org/apache/paimon/factories/Factory.java @@ -37,4 +37,16 @@ public interface Factory { * using "-" (e.g. {@code elasticsearch-7}). */ String identifier(); + + /** + * This is the priority of the factory, allows you to override the default factory + * implementation. The default officially implemented factory priority is 9. If the number is + * less than 9, the smaller number will be selected for execution. If the smallest priority also + * conflicts, an exception will be thrown. + * + * @return priority + */ + default int priority() { + return 9; + } } diff --git a/paimon-common/src/main/java/org/apache/paimon/factories/FactoryUtil.java b/paimon-common/src/main/java/org/apache/paimon/factories/FactoryUtil.java index a492aeece7a8..e579b54ef601 100644 --- a/paimon-common/src/main/java/org/apache/paimon/factories/FactoryUtil.java +++ b/paimon-common/src/main/java/org/apache/paimon/factories/FactoryUtil.java @@ -25,6 +25,7 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.ServiceLoader; @@ -74,6 +75,12 @@ public static T discoverFactory( .collect(Collectors.joining("\n")))); } if (matchingFactories.size() > 1) { + if (matchingFactories.stream().map(Factory::priority).distinct().count() > 1) { + return (T) + matchingFactories.stream() + .min(Comparator.comparingInt(Factory::priority)) + .get(); + } throw new FactoryException( String.format( "Multiple factories for identifier '%s' that implement '%s' found in the classpath.\n\n"