package uk.ac.ebi.atlas.geneannotation;

import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.collections.StoredMap;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;

import java.io.File;

//@Named("annotationEnvironment")
public class AnnotationEnvironment {

    private static final String GENES_DB = "genes.db";
    private Environment environment;

    private Database geneNameDatabase;

    private String environmentLocation;

    public AnnotationEnvironment(String environmentLocation) {
        this.environmentLocation = environmentLocation;
    }

    public void setup() {
        setupEnvironment();
        setupGeneNameDatabase();

    }

    private void setupEnvironment() {
        EnvironmentConfig envConfig = new EnvironmentConfig();

        envConfig.setTransactional(true);
        envConfig.setAllowCreate(true);

        File envHome = new File(environmentLocation);
        if (!envHome.exists()) {
            envHome.mkdirs();
        }
        environment = new Environment(envHome, envConfig);
    }

    public void setupGeneNameDatabase() {
        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setAllowCreate(true);

        geneNameDatabase = environment.openDatabase(null, GENES_DB, dbConfig);
    }

    public StoredMap<String, String> geneNames() {
        TupleBinding<String> keyBinding = TupleBinding.getPrimitiveBinding(String.class);
        TupleBinding<String> dataBinding = TupleBinding.getPrimitiveBinding(String.class);

        StoredMap<String, String> storedMap = new StoredMap<String, String>
                (geneNameDatabase, keyBinding, dataBinding, true);

        return storedMap;
    }

    public Environment getEnvironment() {
        return environment;
    }

    public Database getGeneNameDatabase() {
        return geneNameDatabase;
    }

    public void close() {
        if (geneNameDatabase != null) {
            geneNameDatabase.close();
        }
        if (environment != null) {
            environment.close();
        }
    }
}

@@ -943,32 +943,131 @@
 lowCreate(true);
+%0A        //ToDo: find better solution to allow testing on lime%0A        envConfig.setLocking(false);
 %0A%0A        File e

set no locking