Skip to content

Commit

Permalink
[core] Add FACTORIES cache in FactoryUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
JingsongLi committed Oct 16, 2024
1 parent b6da0bb commit 3825f43
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

package org.apache.paimon.factories;

import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Cache;
import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Caffeine;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -29,13 +32,17 @@

/** Utility for working with {@link Factory}s. */
public class FactoryUtil {

private static final Logger LOG = LoggerFactory.getLogger(FactoryUtil.class);

private static final Cache<ClassLoader, List<Factory>> FACTORIES =
Caffeine.newBuilder().softValues().maximumSize(100).executor(Runnable::run).build();

/** Discovers a factory using the given factory base class and identifier. */
@SuppressWarnings("unchecked")
public static <T extends Factory> T discoverFactory(
ClassLoader classLoader, Class<T> factoryClass, String identifier) {
final List<Factory> factories = discoverFactories(classLoader);
final List<Factory> factories = getFactories(classLoader);

final List<Factory> foundFactories =
factories.stream()
Expand Down Expand Up @@ -85,14 +92,18 @@ public static <T extends Factory> T discoverFactory(

public static <T extends Factory> List<String> discoverIdentifiers(
ClassLoader classLoader, Class<T> factoryClass) {
final List<Factory> factories = discoverFactories(classLoader);
final List<Factory> factories = getFactories(classLoader);

return factories.stream()
.filter(f -> factoryClass.isAssignableFrom(f.getClass()))
.map(Factory::identifier)
.collect(Collectors.toList());
}

private static List<Factory> getFactories(ClassLoader classLoader) {
return FACTORIES.get(classLoader, FactoryUtil::discoverFactories);
}

private static List<Factory> discoverFactories(ClassLoader classLoader) {
final Iterator<Factory> serviceLoaderIterator =
ServiceLoader.load(Factory.class, classLoader).iterator();
Expand All @@ -110,9 +121,8 @@ private static List<Factory> discoverFactories(ClassLoader classLoader) {
} catch (Throwable t) {
if (t instanceof NoClassDefFoundError) {
LOG.debug(
"NoClassDefFoundError when loading a "
+ Factory.class.getCanonicalName()
+ ". This is expected when trying to load factory but no implementation is loaded.",
"NoClassDefFoundError when loading a {}. This is expected when trying to load factory but no implementation is loaded.",
Factory.class.getCanonicalName(),
t);
} else {
throw new RuntimeException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

org.apache.paimon.mergetree.compact.aggregate.factory.FieldBoolAndAggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldBoolOrAggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldCollectAggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldFirstNonNullValueAggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldFirstNonNullValueAggLegacyFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldFirstValueAggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldHllSketchAggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldLastNonNullValueAggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldLastValueAggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldListaggAggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldMaxAggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldMergeMapAggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldMinAggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldNestedUpdateAggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldPrimaryKeyAggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldProductAggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldRoaringBitmap32AggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldRoaringBitmap64AggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldSumAggFactory
org.apache.paimon.mergetree.compact.aggregate.factory.FieldThetaSketchAggFactory
org.apache.paimon.mergetree.compact.aggregate.TestCostomAggFactory

0 comments on commit 3825f43

Please sign in to comment.