• 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);
            }
        }
    
    }
  • 相关阅读:
    SPOJ GSS4 Can you answer these queries IV ——树状数组 并查集
    SPOJ GSS3 Can you answer these queries III ——线段树
    SPOJ GSS2 Can you answer these queries II ——线段树
    SPOJ GSS1 Can you answer these queries I ——线段树
    BZOJ 2178 圆的面积并 ——Simpson积分
    SPOJ CIRU The area of the union of circles ——Simpson积分
    HDU 1724 Ellipse ——Simpson积分
    HDU 1071 The area ——微积分
    HDU 4609 3-idiots ——FFT
    BZOJ 2194 快速傅立叶之二 ——FFT
  • 原文地址:https://www.cnblogs.com/songxingzhu/p/10441888.html
Copyright © 2020-2023  润新知