leveldb-rocksdb java使用
leveldb-rocksdb在java中的demo
(arangodb储存引擎用的rocksdb,然而rocksdb是在leveldb上开发来的)
rocksdb
package net.oschina.itags.gateway.service;
import org.rocksdb.Options;
import org.rocksdb.RocksDB;
import org.rocksdb.RocksDBException;
public class BaseRocksDb {
public final static RocksDB rocksDB() throws RocksDBException {
Options options = new Options().setCreateIfMissing(true);
RocksDB.loadLibrary();
RocksDB db=RocksDB.open(options,"./rock");
return db;
}
}
leveldb
package net.oschina.itags.gateway.service;
import org.iq80.leveldb.*;
import org.iq80.leveldb.impl.Iq80DBFactory;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
public class BaseLevelDb {
public static final DB db() throws IOException {
boolean cleanup = true;
Charset charset = Charset.forName("utf-8");
String path = "./level";
//init
DBFactory factory = Iq80DBFactory.factory;
File dir = new File(path);
//如果数据不需要reload,则每次重启,尝试清理磁盘中path下的旧数据。
if(cleanup) {
factory.destroy(dir,null);//清除文件夹内的所有文件。
}
Options options = new Options().createIfMissing(true);
//重新open新的db
DB db = factory.open(dir,options);
return db;
}
}
点击评论