org.iq80.leveldb.DBFactory Java Examples

The following examples show how to use org.iq80.leveldb.DBFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LevelDBManager.java    From nuls with MIT License 6 votes vote down vote up
/**
 * 装载数据库
 * 如果Area自定义了比较器,保存area的自定义比较器,下次启动数据库时获取并装载它
 * 如果Area自定义了cacheSize,保存area的自定义cacheSize,下次启动数据库时获取并装载它,否则,启动已存在的Area时会丢失之前的cacheSize设置。
 * load database
 * If the area custom comparator, save area define the comparator, the next time you start the database access and loaded it
 * If the area custom cacheSize, save the area's custom cacheSize, get and load it the next time you start the database, or you'll lose the cacheSize setting before starting the existing area.
 */
private static DB openDB(String dbPath, boolean createIfMissing, Long cacheSize, Comparator<byte[]> comparator) throws IOException {
    File file = new File(dbPath);
    String areaName = getAreaNameFromDbPath(dbPath);
    Options options = new Options().createIfMissing(createIfMissing);
    if (cacheSize != null) {
        putModel(BASE_AREA_NAME, bytes(areaName + "-cacheSize"), cacheSize);
        options.cacheSize(cacheSize);
    }
    if (comparator != null) {
        putModel(BASE_AREA_NAME, bytes(areaName + "-comparator"), comparator);
        AREAS_COMPARATOR.put(areaName, comparator);
    }
    DBFactory factory = Iq80DBFactory.factory;
    return factory.open(file, options);
}
 
Example #2
Source File: LevelDBEntityStoreMixin.java    From attic-polygene-java with Apache License 2.0 6 votes vote down vote up
/**
 * Tries in order: JNI and then pure Java LevelDB implementations.
 */
private DBFactory newDBFactory()
{
    try
    {
        return newJniDBFactory();
    }
    catch( Exception ex )
    {
        try
        {
            return newJavaDBFactory();
        }
        catch( Exception ex2 )
        {
            throw new RuntimeException( "Unable to create a LevelDB DBFactory instance. "
                                        + "Tried JNI and pure Java. "
                                        + "The stacktrace is the pure Java attempt.", ex2 );
        }
    }
}
 
Example #3
Source File: LevelDBManager.java    From nuls with MIT License 5 votes vote down vote up
/**
 * @param dbPath
 * @return
 * @throws IOException
 */
private static DB initOpenDB(String dbPath) throws IOException {
    File checkFile = new File(dbPath + File.separator + "CURRENT");
    if (!checkFile.exists()) {
        return null;
    }
    Options options = new Options().createIfMissing(false);

    /*
     * Area的自定义比较器,启动数据库时获取并装载它
     * Area的自定义cacheSize,启动数据库时获取并装载它,否则,启动已存在的Area时会丢失之前的cacheSize设置。
     * Area of custom comparator, you start the database access and loaded it
     * the custom cacheSize of the Area will be retrieved and loaded on the database is started, otherwise, the previous cacheSize setting will be lost when the existing Area is started.
     */
    String areaName = getAreaNameFromDbPath(dbPath);
    Comparator comparator = getModel(BASE_AREA_NAME, bytes(areaName + "-comparator"), Comparator.class);
    if (comparator != null) {
        AREAS_COMPARATOR.put(areaName, comparator);
    }
    Long cacheSize = getModel(BASE_AREA_NAME, bytes(areaName + "-cacheSize"), Long.class);
    if (cacheSize != null) {
        options.cacheSize(cacheSize);
    }
    File file = new File(dbPath);
    DBFactory factory = Iq80DBFactory.factory;
    return factory.open(file, options);
}
 
Example #4
Source File: LevelDBManager.java    From nuls with MIT License 4 votes vote down vote up
private static void destroyDB(String dbPath) throws IOException {
    File file = new File(dbPath);
    Options options = new Options();
    DBFactory factory = Iq80DBFactory.factory;
    factory.destroy(file, options);
}
 
Example #5
Source File: LevelDBEntityStoreMixin.java    From attic-polygene-java with Apache License 2.0 4 votes vote down vote up
@Override
public void activateService()
    throws Exception
{
    charset = Charset.forName( "UTF-8" );
    configuration.refresh();
    LevelDBEntityStoreConfiguration config = configuration.get();

    // Choose flavour
    String flavour = config.flavour().get();
    DBFactory factory;
    if( "jni".equalsIgnoreCase( flavour ) )
    {
        factory = newJniDBFactory();
    }
    else if( "java".equalsIgnoreCase( flavour ) )
    {
        factory = newJavaDBFactory();
    }
    else
    {
        factory = newDBFactory();
    }

    // Apply configuration
    Options options = new Options();
    options.createIfMissing( true );
    if( config.blockRestartInterval().get() != null )
    {
        options.blockRestartInterval( config.blockRestartInterval().get() );
    }
    if( config.blockSize().get() != null )
    {
        options.blockSize( config.blockSize().get() );
    }
    if( config.cacheSize().get() != null )
    {
        options.cacheSize( config.cacheSize().get() );
    }
    if( config.compression().get() != null )
    {
        options.compressionType( config.compression().get()
                                 ? CompressionType.SNAPPY
                                 : CompressionType.NONE );
    }
    if( config.maxOpenFiles().get() != null )
    {
        options.maxOpenFiles( config.maxOpenFiles().get() );
    }
    if( config.paranoidChecks().get() != null )
    {
        options.paranoidChecks( config.paranoidChecks().get() );
    }
    if( config.verifyChecksums().get() != null )
    {
        options.verifyChecksums( config.verifyChecksums().get() );
    }
    if( config.writeBufferSize().get() != null )
    {
        options.writeBufferSize( config.writeBufferSize().get() );
    }
    if( config.errorIfExists().get() != null )
    {
        options.errorIfExists( config.errorIfExists().get() );
    }

    // Open/Create the database
    File dbFile = new File( fileConfig.dataDirectory(), descriptor.identity().toString() );
    db = factory.open( dbFile, options );
}
 
Example #6
Source File: LevelDBEntityStoreMixin.java    From attic-polygene-java with Apache License 2.0 4 votes vote down vote up
private DBFactory newJniDBFactory()
    throws Exception
{
    return (DBFactory) Class.forName( "org.fusesource.leveldbjni.JniDBFactory" ).newInstance();
}
 
Example #7
Source File: LevelDBEntityStoreMixin.java    From attic-polygene-java with Apache License 2.0 4 votes vote down vote up
private DBFactory newJavaDBFactory()
    throws Exception
{
    return (DBFactory) Class.forName( "org.iq80.leveldb.impl.Iq80DBFactory" ).newInstance();
}