All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.helidon.integrations.microstream.cache.MicrostreamCacheConfigurationBuilder Maven / Gradle / Ivy

There is a newer version: 4.1.4
Show newest version
/*
 * Copyright (c) 2023 Oracle and/or its affiliates.
 *
 * Licensed 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 io.helidon.integrations.microstream.cache;


import javax.cache.configuration.CacheEntryListenerConfiguration;
import javax.cache.configuration.Factory;
import javax.cache.expiry.ExpiryPolicy;
import javax.cache.integration.CacheLoader;
import javax.cache.integration.CacheWriter;

import io.helidon.config.Config;

import one.microstream.cache.types.CacheConfiguration;
import one.microstream.cache.types.CacheConfiguration.Builder;
import one.microstream.cache.types.CacheConfigurationBuilderConfigurationBased;
import one.microstream.cache.types.CacheConfigurationPropertyNames;
import one.microstream.cache.types.EvictionManager;
import one.microstream.configuration.types.Configuration;
import one.microstream.persistence.binary.util.SerializerFoundation;

/**
 * Builder for Microstream-CacheConfigurations.
 *
 * @param  type of the cache key
 * @param  type of the cache value
 */
public class MicrostreamCacheConfigurationBuilder
        implements CacheConfigurationPropertyNames,
                   CacheConfiguration.Builder,
                   io.helidon.common.Builder, CacheConfiguration> {

    private final Builder cacheConfigBuilder;

    protected MicrostreamCacheConfigurationBuilder(Class keyType, Class valueType) {
        super();
        cacheConfigBuilder = CacheConfiguration.Builder(keyType, valueType);
    }

    protected MicrostreamCacheConfigurationBuilder(Configuration configuration, Class keyType, Class valueType) {
        super();
        cacheConfigBuilder = CacheConfigurationBuilderConfigurationBased.New().buildCacheConfiguration(configuration,
                                                                                                       CacheConfiguration.Builder(
                                                                                                               keyType,
                                                                                                               valueType));
    }

    /**
     * creates a new MicrostreamCacheConfigurationBuilder using the supplied helidon configuration.
     *
     * @param config helidon configuration
     * @return a new MicrostreamCacheConfigurationBuilder
     */
    public static MicrostreamCacheConfigurationBuilder builder(Config config) {
        return builder(config, null, null);
    }

    /**
     * Create a CacheConfiguration builder with default values.
     *
     * @param  type of the cache key
     * @param  type of the cache value
     * @param keyType type of the cache key
     * @param valueType type of the cache value
     * @return a new CacheConfiguration builder
     */
    public static  MicrostreamCacheConfigurationBuilder builder(Class keyType, Class valueType) {
        return new MicrostreamCacheConfigurationBuilder<>(keyType, valueType);
    }

    /**
     * Create a CacheConfiguration builder initialized from the supplied helidon
     * configuration node.
     *
     * @param  type of the cache key
     * @param  type of the cache value
     * @param config helidon configuration
     * @param keyType type of the cache key
     * @param valueType type of the cache value
     * @return a new CacheConfiguration builder
     */
    public static  MicrostreamCacheConfigurationBuilder builder(Config config, Class keyType,
                                                                            Class valueType) {
        one.microstream.configuration.types.Configuration.Builder configurationBuilder = Configuration.Builder();
        if (config.exists()) {
            config.detach().asMap().get().forEach(configurationBuilder::set);
        }

        Configuration configuration = configurationBuilder.buildConfiguration();
        configuration.opt(KEY_TYPE).ifPresent((s) -> verifyType(s, keyType));
        configuration.opt(VALUE_TYPE).ifPresent((s) -> verifyType(s, valueType));

        return new MicrostreamCacheConfigurationBuilder<>(configuration, keyType, valueType);
    }

    @Override
    public CacheConfiguration build() {
        return cacheConfigBuilder.build();
    }

    @Override
    public MicrostreamCacheConfigurationBuilder readThrough(boolean readTrough) {
        cacheConfigBuilder.readThrough(readTrough);
        return this;
    }

    @Override
    public MicrostreamCacheConfigurationBuilder writeThrough(boolean writeThrough) {
        cacheConfigBuilder.writeThrough(writeThrough);
        return this;
    }

    @Override
    public MicrostreamCacheConfigurationBuilder storeByValue(boolean storeByValue) {
        cacheConfigBuilder.storeByValue(storeByValue);
        return this;
    }

    @Override
    public MicrostreamCacheConfigurationBuilder enableStatistics(boolean enableStatistics) {
        cacheConfigBuilder.enableStatistics(enableStatistics);
        return this;
    }

    @Override
    public MicrostreamCacheConfigurationBuilder enableManagement(boolean enableManagement) {
        cacheConfigBuilder.enableManagement(enableManagement);
        return this;
    }

    @Override
    public MicrostreamCacheConfigurationBuilder expiryPolicyFactory(Factory expiryPolicyFactory) {
        cacheConfigBuilder.expiryPolicyFactory(expiryPolicyFactory);
        return this;
    }

    @Override
    public MicrostreamCacheConfigurationBuilder evictionManagerFactory(
            Factory> evictionManagerFactory) {
        cacheConfigBuilder.evictionManagerFactory(evictionManagerFactory);
        return this;
    }

    @Override
    public MicrostreamCacheConfigurationBuilder cacheLoaderFactory(
            Factory> cacheLoaderFactory) {
        cacheConfigBuilder.cacheLoaderFactory(cacheLoaderFactory);
        return this;
    }

    @Override
    public MicrostreamCacheConfigurationBuilder cacheWriterFactory(
            Factory> cacheWriterFactory) {
        cacheConfigBuilder.cacheWriterFactory(cacheWriterFactory);
        return this;
    }

    @Override
    public Builder serializerFoundation(SerializerFoundation serializerFoundation) {
        return cacheConfigBuilder.serializerFoundation(serializerFoundation);
    }

    @Override
    public Builder addListenerConfiguration(CacheEntryListenerConfiguration listenerConfiguration) {
        cacheConfigBuilder.addListenerConfiguration(listenerConfiguration);
        return this;
    }

    private static void verifyType(String typeName, Class actualType) {
        if (!typeName.equals(actualType.getTypeName())) {
            throw new ConfigException("Microstream cache-config type mismatch, expected value from configuration: " + typeName
                                              + " but got: " + actualType.getTypeName());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy