JAVA中调用LevelDB用于Linux和Window环境下快速存储KV结构
JAVA中调用LevelDB用于Linux和Window环境下快速存储KV结构
一、简介
JAVA中调用LevelDB用于Linux和Window环境下快速存储KV结构
二、依赖
<!-- https://mvnrepository.com/artifact/org.fusesource.leveldbjni/leveldbjni-all -->
<dependency>
<groupId>org.fusesource.leveldbjni</groupId>
<artifactId>leveldbjni-all</artifactId>
<version>1.8</version>
</dependency>
三、代码
package com.dearcloud.utils.leveldb;
import lombok.extern.log4j.Log4j2;
import org.fusesource.leveldbjni.JniDBFactory;
import org.iq80.leveldb.DB;
import org.iq80.leveldb.Options;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentLinkedQueue;
@Log4j2
public class LeveldbUtils {
private DB db;
private File file;
private Options options;
public LeveldbUtils(File file, Integer cahceSize, Integer blockSize) {
Objects.requireNonNull(file);
File[] files = file.listFiles();
if (files != null && files.length > 0) {
log.warn("levelDb directory is not empty. file=[{}]", file);
}
this.file = file;
if (blockSize == null) blockSize = 1024 * 1024 * 10;
if (cahceSize == null) cahceSize = 1024 * 1024 * 20;
options = new Options();
options.cacheSize(cahceSize);
options.blockSize(blockSize);
options.maxOpenFiles(5);
options.writeBufferSize(10000000);//单个文件32MB
reOpenDb();
}
private void openDb() {
synchronized (this) {
boolean isok = false;
try {
JniDBFactory.factory.destroy(file, options);
isok = true;
} catch (Exception ex) {
log.warn("levelDb destroy failed. file=[{}]", file);
}
try {
db = JniDBFactory.factory.open(file, options);
isok = isok && true;
} catch (IOException e) {
log.error("levelDb rebuild init failed. file=[{}]", file);
}
if (isok)
log.info("leveldb rebuild success.");
}
}
public synchronized void put(String key, byte[] value) {
try {
db.put(JniDBFactory.bytes(key), value);
} catch (Exception ex) {
log.warn("[save to localDb] save single item to localDb failed.", ex);
}
}
public synchronized void put(ConcurrentLinkedQueue<Map.Entry<byte[], byte[]>> data) {
for (Map.Entry<byte[], byte[]> datum : data) {
try {
db.put(datum.getKey(), datum.getValue());
} catch (Exception ex) {
log.warn("[save to localDb] save to localDb failed.", ex);
}
}
}
public synchronized void close() {
try {
db.close();
JniDBFactory.factory.destroy(file, options);
} catch (IOException e) {
log.error("[LeveldbUtils]:levelDb close failed.", e);
}
}
}
JAVA中调用LevelDB用于Linux和Window环境下快速存储KV结构
https://www.dearcloud.cn/2019/02/27/20200310-cnblogs-old-posts/20190227-JAVA中调用LevelDB用于Linux和Window环境下快速存储KV结构/