/**
 * Copyright 2010 - 2015 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.log.Log;
import jetbrains.exodus.log.LogConfig;
import org.jetbrains.annotations.NotNull;

import java.io.File;

@SuppressWarnings("UnusedDeclaration")
public final class Environments {

    private Environments() {
    }

    @NotNull
    public static Environment newInstance(@NotNull final String dir, @NotNull final String subDir, @NotNull final EnvironmentConfig ec) {
        return prepare(new EnvironmentImpl(newLogInstance(new File(dir, subDir), ec), ec));
    }

    @NotNull
    public static Environment newInstance(@NotNull final String dir) {
        return newInstance(dir, EnvironmentConfig.DEFAULT);
    }

    @NotNull
    public static Environment newInstance(@NotNull final String dir, @NotNull final EnvironmentConfig ec) {
        return prepare(new EnvironmentImpl(newLogInstance(new File(dir), ec), ec));
    }

    @NotNull
    public static Environment newInstance(@NotNull final File dir) {
        return newInstance(dir, EnvironmentConfig.DEFAULT);
    }

    @NotNull
    public static Environment newInstance(@NotNull final File dir, @NotNull final EnvironmentConfig ec) {
        return prepare(new EnvironmentImpl(newLogInstance(dir, ec), ec));
    }

    @NotNull
    public static Environment newInstance(@NotNull final LogConfig config, @NotNull final EnvironmentConfig ec) {
        return prepare(new EnvironmentImpl(newLogInstance(config, ec), ec));
    }

    @NotNull
    public static ContextualEnvironment newContextualInstance(@NotNull final String dir, @NotNull final String subDir, @NotNull final EnvironmentConfig ec) {
        return prepare(new ContextualEnvironmentImpl(newLogInstance(new File(dir, subDir), ec), ec));
    }

    @NotNull
    public static ContextualEnvironment newContextualInstance(@NotNull final String dir) {
        return newContextualInstance(dir, EnvironmentConfig.DEFAULT);
    }

    @NotNull
    public static ContextualEnvironment newContextualInstance(@NotNull final String dir, @NotNull final EnvironmentConfig ec) {
        return prepare(new ContextualEnvironmentImpl(newLogInstance(new File(dir), ec), ec));
    }

    @NotNull
    public static ContextualEnvironment newContextualInstance(@NotNull final File dir) {
        return newContextualInstance(dir, EnvironmentConfig.DEFAULT);
    }

    @NotNull
    public static ContextualEnvironment newContextualInstance(@NotNull final File dir, @NotNull final EnvironmentConfig ec) {
        return prepare(new ContextualEnvironmentImpl(newLogInstance(dir, ec), ec));
    }

    @NotNull
    public static ContextualEnvironment newContextualInstance(@NotNull final LogConfig config, @NotNull final EnvironmentConfig ec) {
        return prepare(new ContextualEnvironmentImpl(newLogInstance(config, ec), ec));
    }

    @NotNull
    public static Log newLogInstance(@NotNull final File dir, @NotNull final EnvironmentConfig ec) {
        final LogConfig config = new LogConfig();
        config.setDir(dir);
        return newLogInstance(config, ec);
    }

    @NotNull
    public static Log newLogInstance(@NotNull final LogConfig config, @NotNull final EnvironmentConfig ec) {
        config.setFileSize(ec.getLogFileSize());
        config.setLockTimeout(ec.getLogLockTimeout());
        config.setCachePageSize(ec.getLogCachePageSize());
        config.setCacheOpenFilesCount(ec.getLogCacheOpenFilesCount());
        config.setDurableWrite(ec.getLogDurableWrite());
        config.setSharedCache(ec.isLogCacheShared());
        config.setNonBlockingCache(ec.isLogCacheNonBlocking());
        config.setCleanDirectoryExpected(ec.isLogCleanDirectoryExpected());
        config.setClearInvalidLog(ec.isLogClearInvalid());
        config.setSyncPeriod(ec.getLogSyncPeriod());
        final Long maxMemory = ec.getMemoryUsage();
        if (maxMemory != null) {
            config.setMemoryUsage(maxMemory);
        } else {
            config.setMemoryUsagePercentage(ec.getMemoryUsagePercentage());
        }
        return newLogInstance(config);
    }

    @NotNull
    public static Log newLogInstance(@NotNull final LogConfig config) {
        // In order to avoid XD-96, we need to load the DatabaseRoot class before creating Log instance
        DatabaseRoot.register();
        return new Log(config);
    }

    @NotNull
    static <T extends EnvironmentImpl> T prepare(@NotNull final T env) {
        env.getGC().getUtilizationProfile().load();
        return env;
    }
}

@@ -1232,32 +1232,36 @@
 newInstance(dir,
+ new
  EnvironmentConf
@@ -1258,32 +1258,26 @@
 onmentConfig
-.DEFAULT
+()
 );%0A    %7D%0A%0A  
@@ -1591,32 +1591,36 @@
 newInstance(dir,
+ new
  EnvironmentConf
@@ -1617,32 +1617,26 @@
 onmentConfig
-.DEFAULT
+()
 );%0A    %7D%0A%0A  
@@ -2461,32 +2461,36 @@
 ualInstance(dir,
+ new
  EnvironmentConf
@@ -2487,32 +2487,26 @@
 onmentConfig
-.DEFAULT
+()
 );%0A    %7D%0A%0A  
@@ -2888,16 +2888,20 @@
 nce(dir,
+ new
  Environ
@@ -2914,16 +2914,10 @@
 nfig
-.DEFAULT
+()
 );%0A 

create Environment instances using the new instance of EnvironmentConfig, not EnvironmentConfig.DEFAULT