<commit_before>/**
 * Copyright 2010 - 2016 JetBrains s.r.o.
 *
 * 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 jetbrains.exodus.env;

import jetbrains.exodus.AbstractConfig;
import jetbrains.exodus.ConfigurationStrategy;
import jetbrains.exodus.InvalidSettingException;
import jetbrains.exodus.core.dataStructures.Pair;
import org.jetbrains.annotations.NotNull;

@SuppressWarnings("UnusedDeclaration")
public final class EnvironmentConfig extends AbstractConfig {

    public static final EnvironmentConfig DEFAULT = new EnvironmentConfig(ConfigurationStrategy.IGNORE);

    public static final String MEMORY_USAGE = "exodus.memoryUsage";

    public static final String MEMORY_USAGE_PERCENTAGE = "exodus.memoryUsagePercentage";

    public static final String LOG_DURABLE_WRITE = "exodus.log.durableWrite";

    public static final String LOG_FILE_SIZE = "exodus.log.fileSize"; // in Kb

    public static final String LOG_LOCK_TIMEOUT = "exodus.log.lockTimeout"; // in milliseconds

    public static final String LOG_CACHE_PAGE_SIZE = "exodus.log.cache.pageSize"; // in bytes

    public static final String LOG_CACHE_OPEN_FILES = "exodus.log.cache.openFilesCount";

    public static final String LOG_CACHE_USE_NIO = "exodus.log.cache.useNIO";

    public static final String LOG_CACHE_FREE_PHYSICAL_MEMORY_THRESHOLD = "exodus.log.cache.freePhysicalMemoryThreshold";

    public static final String LOG_CACHE_SHARED = "exodus.log.cache.shared";

    public static final String LOG_CACHE_NON_BLOCKING = "exodus.log.cache.nonBlocking";

    public static final String LOG_CLEAN_DIRECTORY_EXPECTED = "exodus.log.cleanDirectoryExpected";

    public static final String LOG_CLEAR_INVALID = "exodus.log.clearInvalid";

    public static final String LOG_SYNC_PERIOD = "exodus.log.syncPeriod"; // in milliseconds

    public static final String LOG_FULL_FILE_READ_ONLY = "exodus.log.fullFileReadonly";

    public static final String ENV_IS_READONLY = "exodus.env.isReadonly";

    /**
     * If this setting is set to {@code true} and exodus.env.isReadonly is also {@code true},
     * env.openStore() doesn't try to create a store, but returns an empty immutable instance instead
     */
    public static final String ENV_READONLY_EMPTY_STORES = "exodus.env.readonly.emptyStores";

    public static final String ENV_STOREGET_CACHE_SIZE = "exodus.env.storeGetCacheSize";

    public static final String ENV_CLOSE_FORCEDLY = "exodus.env.closeForcedly";

    public static final String ENV_TXN_REPLAY_TIMEOUT = "exodus.env.txn.replayTimeout"; // in milliseconds

    public static final String ENV_TXN_REPLAY_MAX_COUNT = "exodus.env.txn.replayMaxCount";

    public static final String ENV_TXN_DOWNGRADE_AFTER_FLUSH = "exodus.env.txn.downgradeAfterFlush";

    public static final String ENV_MAX_PARALLEL_TXNS = "exodus.env.maxParallelTxns";

    public static final String ENV_MAX_PARALLEL_READONLY_TXNS = "exodus.env.maxParallelReadonlyTxns";

    public static final String ENV_MONITOR_TXNS_TIMEOUT = "exodus.env.monitorTxns.timeout"; // in milliseconds

    public static final String ENV_MONITOR_TXNS_CHECK_FREQ = "exodus.env.monitorTxns.checkFreq"; // in milliseconds

    public static final String ENV_GATHER_STATISTICS = "exodus.env.gatherStatistics";

    public static final String TREE_MAX_PAGE_SIZE = "exodus.tree.maxPageSize";

    public static final String TREE_NODES_CACHE_SIZE = "exodus.tree.nodesCacheSize";

    public static final String GC_ENABLED = "exodus.gc.enabled";

    public static final String GC_START_IN = "exodus.gc.startIn"; // in milliseconds

    public static final String GC_MIN_UTILIZATION = "exodus.gc.minUtilization";

    public static final String GC_RENAME_FILES = "exodus.gc.renameFiles";

    public static final String GC_USE_EXPIRATION_CHECKER = "exodus.gc.useExpirationChecker";

    /**
     * Minimum age of a file to consider it for cleaning.
     */
    public static final String GC_MIN_FILE_AGE = "exodus.gc.fileMinAge";

    /**
     * Cleaner checks log utilization and runs if necessary after this many new files are created in the log.
     */
    public static final String GC_FILES_INTERVAL = "exodus.gc.filesInterval";

    /**
     * If a single cleaner run didn't reach target utilization then next run will happen in this number of milliseconds.
     */
    public static final String GC_RUN_PERIOD = "exodus.gc.runPeriod"; // in milliseconds

    public static final String GC_UTILIZATION_FROM_SCRATCH = "exodus.gc.utilization.fromScratch";

    public static final String GC_USE_EXCLUSIVE_TRANSACTION = "exodus.gc.useExclusiveTransaction";

    /**
     * If GC uses exclusive transactions this number of milliseconds is used as timeout acquiring exclusive permit.
     */
    public static final String GC_TRANSACTION_ACQUIRE_TIMEOUT = "exodus.gc.transactionAcquireTimeout"; // in milliseconds

    /**
     * If a file is successfully cleaned then delete it after this number of milliseconds.
     */
    public static final String GC_FILES_DELETION_DELAY = "exodus.gc.filesDeletionDelay"; // in milliseconds

    public static final String MANAGEMENT_ENABLED = "exodus.managementEnabled";

    public EnvironmentConfig() {
        this(ConfigurationStrategy.SYSTEM_PROPERTY);
    }

    public EnvironmentConfig(@NotNull final ConfigurationStrategy strategy) {
        //noinspection unchecked
        super(new Pair[]{
                new Pair(MEMORY_USAGE_PERCENTAGE, 50),
                new Pair(LOG_DURABLE_WRITE, false),
                new Pair(LOG_FILE_SIZE, 8192L),
                new Pair(LOG_LOCK_TIMEOUT, 0L),
                new Pair(LOG_CACHE_PAGE_SIZE, 65536),
                new Pair(LOG_CACHE_OPEN_FILES, 500),
                new Pair(LOG_CACHE_USE_NIO, true),
                new Pair(LOG_CACHE_FREE_PHYSICAL_MEMORY_THRESHOLD, 1_000_000_000L),
                new Pair(LOG_CACHE_SHARED, true),
                new Pair(LOG_CACHE_NON_BLOCKING, true),
                new Pair(LOG_CLEAN_DIRECTORY_EXPECTED, false),
                new Pair(LOG_CLEAR_INVALID, false),
                new Pair(LOG_SYNC_PERIOD, 1000L),
                new Pair(LOG_FULL_FILE_READ_ONLY, true),
                new Pair(ENV_IS_READONLY, false),
                new Pair(ENV_READONLY_EMPTY_STORES, false),
                new Pair(ENV_STOREGET_CACHE_SIZE, 0),
                new Pair(ENV_CLOSE_FORCEDLY, false),
                new Pair(ENV_TXN_REPLAY_TIMEOUT, 2000L),
                new Pair(ENV_TXN_REPLAY_MAX_COUNT, 2),
                new Pair(ENV_TXN_DOWNGRADE_AFTER_FLUSH, true),
                new Pair(ENV_MAX_PARALLEL_TXNS, Integer.MAX_VALUE),
                new Pair(ENV_MAX_PARALLEL_READONLY_TXNS, Integer.MAX_VALUE),
                new Pair(ENV_MONITOR_TXNS_TIMEOUT, 0),
                new Pair(ENV_MONITOR_TXNS_CHECK_FREQ, 60000),
                new Pair(ENV_GATHER_STATISTICS, true),
                new Pair(TREE_MAX_PAGE_SIZE, 128),
                new Pair(TREE_NODES_CACHE_SIZE, 4096),
                new Pair(GC_ENABLED, true),
                new Pair(GC_START_IN, 5000),
                new Pair(GC_MIN_UTILIZATION, 50),
                new Pair(GC_RENAME_FILES, false),
                new Pair(GC_USE_EXPIRATION_CHECKER, true),
                new Pair(GC_MIN_FILE_AGE, 2),
                new Pair(GC_FILES_INTERVAL, 3),
                new Pair(GC_RUN_PERIOD, 30000),
                new Pair(GC_UTILIZATION_FROM_SCRATCH, false),
                new Pair(GC_FILES_DELETION_DELAY, 0),
                new Pair(GC_USE_EXCLUSIVE_TRANSACTION, true),
                new Pair(GC_TRANSACTION_ACQUIRE_TIMEOUT, 20),
                new Pair(MANAGEMENT_ENABLED, true)
        }, strategy);
    }

    @Override
    public EnvironmentConfig setSetting(@NotNull final String key, @NotNull final Object value) {
        return (EnvironmentConfig) super.setSetting(key, value);
    }

    public Long /* NB! do not change to long */ getMemoryUsage() {
        return (Long) getSetting(MEMORY_USAGE);
    }

    public EnvironmentConfig setMemoryUsage(final long maxMemory) {
        return setSetting(MEMORY_USAGE, maxMemory);
    }

    public int getMemoryUsagePercentage() {
        return (Integer) getSetting(MEMORY_USAGE_PERCENTAGE);
    }

    public EnvironmentConfig setMemoryUsagePercentage(final int memoryUsagePercentage) {
        return setSetting(MEMORY_USAGE_PERCENTAGE, memoryUsagePercentage);
    }

    public boolean getLogDurableWrite() {<commit_msg>XD-545: by default defer deletion of garbage collected files for two seconds<commit_after>/**
 * Copyright 2010 - 2016 JetBrains s.r.o.
 *
 * 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 jetbrains.exodus.env;

import jetbrains.exodus.AbstractConfig;
import jetbrains.exodus.ConfigurationStrategy;
import jetbrains.exodus.InvalidSettingException;
import jetbrains.exodus.core.dataStructures.Pair;
import org.jetbrains.annotations.NotNull;

@SuppressWarnings("UnusedDeclaration")
public final class EnvironmentConfig extends AbstractConfig {

    public static final EnvironmentConfig DEFAULT = new EnvironmentConfig(ConfigurationStrategy.IGNORE);

    public static final String MEMORY_USAGE = "exodus.memoryUsage";

    public static final String MEMORY_USAGE_PERCENTAGE = "exodus.memoryUsagePercentage";

    public static final String LOG_DURABLE_WRITE = "exodus.log.durableWrite";

    public static final String LOG_FILE_SIZE = "exodus.log.fileSize"; // in Kb

    public static final String LOG_LOCK_TIMEOUT = "exodus.log.lockTimeout"; // in milliseconds

    public static final String LOG_CACHE_PAGE_SIZE = "exodus.log.cache.pageSize"; // in bytes

    public static final String LOG_CACHE_OPEN_FILES = "exodus.log.cache.openFilesCount";

    public static final String LOG_CACHE_USE_NIO = "exodus.log.cache.useNIO";

    public static final String LOG_CACHE_FREE_PHYSICAL_MEMORY_THRESHOLD = "exodus.log.cache.freePhysicalMemoryThreshold";

    public static final String LOG_CACHE_SHARED = "exodus.log.cache.shared";

    public static final String LOG_CACHE_NON_BLOCKING = "exodus.log.cache.nonBlocking";

    public static final String LOG_CLEAN_DIRECTORY_EXPECTED = "exodus.log.cleanDirectoryExpected";

    public static final String LOG_CLEAR_INVALID = "exodus.log.clearInvalid";

    public static final String LOG_SYNC_PERIOD = "exodus.log.syncPeriod"; // in milliseconds

    public static final String LOG_FULL_FILE_READ_ONLY = "exodus.log.fullFileReadonly";

    public static final String ENV_IS_READONLY = "exodus.env.isReadonly";

    /**
     * If this setting is set to {@code true} and exodus.env.isReadonly is also {@code true},
     * env.openStore() doesn't try to create a store, but returns an empty immutable instance instead
     */
    public static final String ENV_READONLY_EMPTY_STORES = "exodus.env.readonly.emptyStores";

    public static final String ENV_STOREGET_CACHE_SIZE = "exodus.env.storeGetCacheSize";

    public static final String ENV_CLOSE_FORCEDLY = "exodus.env.closeForcedly";

    public static final String ENV_TXN_REPLAY_TIMEOUT = "exodus.env.txn.replayTimeout"; // in milliseconds

    public static final String ENV_TXN_REPLAY_MAX_COUNT = "exodus.env.txn.replayMaxCount";

    public static final String ENV_TXN_DOWNGRADE_AFTER_FLUSH = "exodus.env.txn.downgradeAfterFlush";

    public static final String ENV_MAX_PARALLEL_TXNS = "exodus.env.maxParallelTxns";

    public static final String ENV_MAX_PARALLEL_READONLY_TXNS = "exodus.env.maxParallelReadonlyTxns";

    public static final String ENV_MONITOR_TXNS_TIMEOUT = "exodus.env.monitorTxns.timeout"; // in milliseconds

    public static final String ENV_MONITOR_TXNS_CHECK_FREQ = "exodus.env.monitorTxns.checkFreq"; // in milliseconds

    public static final String ENV_GATHER_STATISTICS = "exodus.env.gatherStatistics";

    public static final String TREE_MAX_PAGE_SIZE = "exodus.tree.maxPageSize";

    public static final String TREE_NODES_CACHE_SIZE = "exodus.tree.nodesCacheSize";

    public static final String GC_ENABLED = "exodus.gc.enabled";

    public static final String GC_START_IN = "exodus.gc.startIn"; // in milliseconds

    public static final String GC_MIN_UTILIZATION = "exodus.gc.minUtilization";

    public static final String GC_RENAME_FILES = "exodus.gc.renameFiles";

    public static final String GC_USE_EXPIRATION_CHECKER = "exodus.gc.useExpirationChecker";

    /**
     * Minimum age of a file to consider it for cleaning.
     */
    public static final String GC_MIN_FILE_AGE = "exodus.gc.fileMinAge";

    /**
     * Cleaner checks log utilization and runs if necessary after this many new files are created in the log.
     */
    public static final String GC_FILES_INTERVAL = "exodus.gc.filesInterval";

    /**
     * If a single cleaner run didn't reach target utilization then next run will happen in this number of milliseconds.
     */
    public static final String GC_RUN_PERIOD = "exodus.gc.runPeriod"; // in milliseconds

    public static final String GC_UTILIZATION_FROM_SCRATCH = "exodus.gc.utilization.fromScratch";

    public static final String GC_USE_EXCLUSIVE_TRANSACTION = "exodus.gc.useExclusiveTransaction";

    /**
     * If GC uses exclusive transactions this number of milliseconds is used as timeout acquiring exclusive permit.
     */
    public static final String GC_TRANSACTION_ACQUIRE_TIMEOUT = "exodus.gc.transactionAcquireTimeout"; // in milliseconds

    /**
     * If a file is successfully cleaned then delete it after this number of milliseconds.
     */
    public static final String GC_FILES_DELETION_DELAY = "exodus.gc.filesDeletionDelay"; // in milliseconds

    public static final String MANAGEMENT_ENABLED = "exodus.managementEnabled";

    public EnvironmentConfig() {
        this(ConfigurationStrategy.SYSTEM_PROPERTY);
    }

    public EnvironmentConfig(@NotNull final ConfigurationStrategy strategy) {
        //noinspection unchecked
        super(new Pair[]{
                new Pair(MEMORY_USAGE_PERCENTAGE, 50),
                new Pair(LOG_DURABLE_WRITE, false),
                new Pair(LOG_FILE_SIZE, 8192L),
                new Pair(LOG_LOCK_TIMEOUT, 0L),
                new Pair(LOG_CACHE_PAGE_SIZE, 65536),
                new Pair(LOG_CACHE_OPEN_FILES, 500),
                new Pair(LOG_CACHE_USE_NIO, true),
                new Pair(LOG_CACHE_FREE_PHYSICAL_MEMORY_THRESHOLD, 1_000_000_000L),
                new Pair(LOG_CACHE_SHARED, true),
                new Pair(LOG_CACHE_NON_BLOCKING, true),
                new Pair(LOG_CLEAN_DIRECTORY_EXPECTED, false),
                new Pair(LOG_CLEAR_INVALID, false),
                new Pair(LOG_SYNC_PERIOD, 1000L),
                new Pair(LOG_FULL_FILE_READ_ONLY, true),
                new Pair(ENV_IS_READONLY, false),
                new Pair(ENV_READONLY_EMPTY_STORES, false),
                new Pair(ENV_STOREGET_CACHE_SIZE, 0),
                new Pair(ENV_CLOSE_FORCEDLY, false),
                new Pair(ENV_TXN_REPLAY_TIMEOUT, 2000L),
                new Pair(ENV_TXN_REPLAY_MAX_COUNT, 2),
                new Pair(ENV_TXN_DOWNGRADE_AFTER_FLUSH, true),
                new Pair(ENV_MAX_PARALLEL_TXNS, Integer.MAX_VALUE),
                new Pair(ENV_MAX_PARALLEL_READONLY_TXNS, Integer.MAX_VALUE),
                new Pair(ENV_MONITOR_TXNS_TIMEOUT, 0),
                new Pair(ENV_MONITOR_TXNS_CHECK_FREQ, 60000),
                new Pair(ENV_GATHER_STATISTICS, true),
                new Pair(TREE_MAX_PAGE_SIZE, 128),
                new Pair(TREE_NODES_CACHE_SIZE, 4096),
                new Pair(GC_ENABLED, true),
                new Pair(GC_START_IN, 5000),
                new Pair(GC_MIN_UTILIZATION, 50),
                new Pair(GC_RENAME_FILES, false),
                new Pair(GC_USE_EXPIRATION_CHECKER, true),
                new Pair(GC_MIN_FILE_AGE, 2),
                new Pair(GC_FILES_INTERVAL, 3),
                new Pair(GC_RUN_PERIOD, 30000),
                new Pair(GC_UTILIZATION_FROM_SCRATCH, false),
                new Pair(GC_FILES_DELETION_DELAY, 2000),
                new Pair(GC_USE_EXCLUSIVE_TRANSACTION, true),
                new Pair(GC_TRANSACTION_ACQUIRE_TIMEOUT, 20),
                new Pair(MANAGEMENT_ENABLED, true)
        }, strategy);
    }

    @Override
    public EnvironmentConfig setSetting(@NotNull final String key, @NotNull final Object value) {
        return (EnvironmentConfig) super.setSetting(key, value);
    }

    public Long /* NB! do not change to long */ getMemoryUsage() {
        return (Long) getSetting(MEMORY_USAGE);
    }

    public EnvironmentConfig setMemoryUsage(final long maxMemory) {
        return setSetting(MEMORY_USAGE, maxMemory);
    }

    public int getMemoryUsagePercentage() {
        return (Integer) getSetting(MEMORY_USAGE_PERCENTAGE);
    }

    public EnvironmentConfig setMemoryUsagePercentage(final int memoryUsagePercentage) {
        return setSetting(MEMORY_USAGE_PERCENTAGE, memoryUsagePercentage);
    }

    public boolean getLogDurableWrite() {
{'fraction_non_alphanumeric': 0.07416453869212834, 'fraction_numerical': 0.008770955923170867, 'mean_word_length': 3.3149700598802396, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7589733', 'n_tokens_mistral': 5747, 'n_tokens_neox': 5219, 'n_words': 1365}