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);
        }
    }

}

 

转载于:https://www.cnblogs.com/songxingzhu/p/10441888.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值