Package be.bagofwords.db.experimental.lmdb

Source Code of be.bagofwords.db.experimental.lmdb.LMDBDataInterfaceFactory

package be.bagofwords.db.experimental.lmdb;

import be.bagofwords.db.DataInterfaceFactory;
import be.bagofwords.db.combinator.Combinator;
import be.bagofwords.application.memory.MemoryManager;
import be.bagofwords.cache.CachesManager;
import be.bagofwords.db.DataInterface;
import org.fusesource.lmdbjni.Env;

import java.io.File;

/**
* Created by Koen Deschacht (koendeschacht@gmail.com) on 9/16/14.
*/
public class LMDBDataInterfaceFactory extends DataInterfaceFactory {

    private Env env;
    private String directory;

    public LMDBDataInterfaceFactory(CachesManager cachesManager, MemoryManager memoryManager, String directory) {
        super(cachesManager, memoryManager);
        env = new Env();
        env.setMaxDbs(100);
        env.setMapSize(200 * 1024 * 1024); //needs to be quite high, otherwise we get EINVAL or MDB_MAP_FULL errors
        File directoryAsFile = new File(directory);
        if (directoryAsFile.isFile()) {
            throw new RuntimeException("Path " + directoryAsFile.getAbsolutePath() + " is a file! Expected a directory...");
        } else if (!directoryAsFile.exists()) {
            boolean success = directoryAsFile.mkdirs();
            if (!success) {
                throw new RuntimeException("Failed to create directory " + directoryAsFile.getAbsolutePath());
            }
        }
        env.open(directory);
    }

    @Override
    protected <T> DataInterface<T> createBaseDataInterface(String nameOfSubset, Class<T> objectClass, Combinator<T> combinator) {
        return new LMDBDataInterface<>(nameOfSubset, objectClass, combinator, env);
    }

    @Override
    public synchronized void close() {
        super.close();
        env.close();
    }
}
TOP

Related Classes of be.bagofwords.db.experimental.lmdb.LMDBDataInterfaceFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.