org.iq80.leveldb.DB Java Examples

The following examples show how to use org.iq80.leveldb.DB. 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
public static <T> T getModel(String area, byte[] key, Class<T> clazz) {
    if (!baseCheckArea(area)) {
        return null;
    }
    if (key == null) {
        return null;
    }
    try {
        DB db = AREAS.get(area);
        byte[] bytes = db.get(key);
        if (bytes == null) {
            return null;
        }
        RuntimeSchema schema = SCHEMA_MAP.get(ModelWrapper.class);
        ModelWrapper model = new ModelWrapper();
        ProtostuffIOUtil.mergeFrom(bytes, model, schema);
        if (clazz != null && model.getT() != null) {
            return clazz.cast(model.getT());
        }
        return (T) model.getT();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
Example #2
Source File: LevelDBManager.java    From nuls with MIT License 6 votes vote down vote up
/**
 * 弃用的方法/Deprecated method
 */
@Deprecated
public static byte[] get(String area, String key) {
    if (!baseCheckArea(area)) {
        return null;
    }
    if (StringUtils.isBlank(key)) {
        return null;
    }
    try {
        DB db = AREAS.get(area);
        return db.get(bytes(key));
    } catch (Exception e) {
        return null;
    }
}
 
Example #3
Source File: LevelDBManager.java    From nuls with MIT License 6 votes vote down vote up
public static Result delete(String area, byte[] key) {
    if (!baseCheckArea(area)) {
        return Result.getFailed(DBErrorCode.DB_AREA_NOT_EXIST);
    }
    if (key == null) {
        return Result.getFailed(KernelErrorCode.NULL_PARAMETER);
    }
    try {
        DB db = AREAS.get(area);
        db.delete(key);
        return Result.getSuccess();
    } catch (Exception e) {
        Log.error(e);
        return Result.getFailed(DBErrorCode.DB_UNKOWN_EXCEPTION);
    }
}
 
Example #4
Source File: LevelDBCachePooFactory.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
@Override
public CachePool createCachePool(String poolName, int cacheSize,
                                 int expireSeconds) {
    Options options = new Options();
    options.cacheSize(1048576L * cacheSize); //cacheSize M
    options.createIfMissing(true);
    DB db = null;
    String filePath = "leveldb\\" + poolName;
    try {
        db = factory.open(new File(filePath), options);
        // Use the db in here....
    } catch (IOException e) {
        LOGGER.info("factory try to open file " + filePath + " failed ");
        // Make sure you close the db to shutdown the
        // database and avoid resource leaks.
        // db.close();
    }
    return new LevelDBPool(poolName, db, cacheSize);
}
 
Example #5
Source File: SelectLevelDBData.java    From gsc-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void data(String dataName) {
    Options options = new Options();
    //options.createIfMissing(true);
    DB db = null;
    try {
        System.out.println("Path: " + path + dataName);
        db = factory.open(new File(path + dataName), options);

        logger.info("---------------------------------------------");
        System.out.println();
        DBIterator iterator = db.iterator();
        iterator.seekToFirst();
        int count = 0;
        while (iterator.hasNext()) {
            count++;
            switch (dataName) {
                case "properties":
                    properties(iterator.peekNext().getKey(), iterator.peekNext().getValue());
                    break;
                case "peers":
                    peers(iterator.peekNext().getKey(), iterator.peekNext().getValue());
                    break;
                case "nodes":
                    peers(iterator.peekNext().getKey(), iterator.peekNext().getValue());
                    break;
                default:
                    break;
            }
            iterator.next();
        }
        iterator.close();
        System.out.println(dataName + " Num: " + count);
        System.out.println();
        logger.info("---------------------------------------------");

        db.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #6
Source File: LevelDBManager.java    From nuls with MIT License 6 votes vote down vote up
/**
 * 弃用的方法/Deprecated method
 */
@Deprecated
public static Result delete(String area, String key) {
    if (!baseCheckArea(area)) {
        return Result.getFailed(DBErrorCode.DB_AREA_NOT_EXIST);
    }
    if (StringUtils.isBlank(key)) {
        return Result.getFailed(KernelErrorCode.NULL_PARAMETER);
    }
    try {
        DB db = AREAS.get(area);
        db.delete(bytes(key));
        return Result.getSuccess();
    } catch (Exception e) {
        Log.error(e);
        return Result.getFailed(DBErrorCode.DB_UNKOWN_EXCEPTION);
    }
}
 
Example #7
Source File: LevelDBManager.java    From nuls with MIT License 6 votes vote down vote up
/**
 * 弃用的方法/Deprecated method
 */
@Deprecated
public static Result put(String area, byte[] key, String value) {
    if (!baseCheckArea(area)) {
        return Result.getFailed(DBErrorCode.DB_AREA_NOT_EXIST);
    }
    if (key == null || StringUtils.isBlank(value)) {
        return Result.getFailed(KernelErrorCode.NULL_PARAMETER);
    }
    try {
        DB db = AREAS.get(area);
        db.put(key, bytes(value));
        return Result.getSuccess();
    } catch (Exception e) {
        Log.error(e);
        return Result.getFailed(DBErrorCode.DB_UNKOWN_EXCEPTION);
    }
}
 
Example #8
Source File: LevelDBManager.java    From nuls with MIT License 6 votes vote down vote up
/**
 * 优先初始化BASE_AREA
 * 存放基础数据,比如Area的自定义比较器,下次启动数据库时获取并装载它,否则,若Area自定义了比较器,下次重启数据库时,此Area会启动失败,失败异常:java.lang.IllegalArgumentException: Expected user comparator leveldb.BytewiseComparator to match existing database comparator
 * 比如Area的自定义cacheSize,下次启动数据库时获取并装载它,否则,下次启动已存在的Area时会丢失之前的cacheSize设置。
 * Prioritize BASE_AREA.
 * Based data storage, for example, Area of custom comparator, the next time you start the database access and loaded it, otherwise, if the Area custom the comparator, the next time you restart the database, this Area will start failure, failure exception: java.lang.IllegalArgumentException: Expected user comparator leveldb. BytewiseComparator to match existing database comparator
 * the custom cacheSize of the Area will be retrieved and loaded next time the database is started, otherwise, the previous cacheSize setting will be lost when the existing Area is started.
 */
private static void initBaseDB(String dataPath) {
    if (AREAS.get(BASE_AREA_NAME) == null) {
        String baseAreaPath = dataPath + File.separator + BASE_AREA_NAME;
        File dir = new File(baseAreaPath);
        if (!dir.exists()) {
            dir.mkdir();
        }
        String filePath = baseAreaPath + File.separator + BASE_DB_NAME;
        try {
            DB db = openDB(filePath, true, null, null);
            AREAS.put(BASE_AREA_NAME, db);
        } catch (IOException e) {
            Log.error(e);
        }
    }
}
 
Example #9
Source File: LevelDBManager.java    From nuls with MIT License 6 votes vote down vote up
/**
 * 弃用的方法/Deprecated method
 */
@Deprecated
public static Result put(String area, String key, String value) {
    if (!baseCheckArea(area)) {
        return Result.getFailed(DBErrorCode.DB_AREA_NOT_EXIST);
    }
    if (StringUtils.isBlank(key) || StringUtils.isBlank(value)) {
        return Result.getFailed(KernelErrorCode.NULL_PARAMETER);
    }
    try {
        DB db = AREAS.get(area);
        db.put(bytes(key), bytes(value));
        return Result.getSuccess();
    } catch (Exception e) {
        Log.error(e);
        return Result.getFailed(DBErrorCode.DB_UNKOWN_EXCEPTION);
    }
}
 
Example #10
Source File: LevelDBManager.java    From nuls with MIT License 6 votes vote down vote up
public static Result put(String area, byte[] key, byte[] value) {
    if (!baseCheckArea(area)) {
        return Result.getFailed(DBErrorCode.DB_AREA_NOT_EXIST);
    }
    if (key == null || value == null) {
        return Result.getFailed(KernelErrorCode.NULL_PARAMETER);
    }
    try {
        DB db = AREAS.get(area);
        db.put(key, value);
        return Result.getSuccess();
    } catch (Exception e) {
        Log.error(e);
        return Result.getFailed(DBErrorCode.DB_UNKOWN_EXCEPTION);
    }
}
 
Example #11
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 #12
Source File: LevelDBManager.java    From nuls with MIT License 5 votes vote down vote up
/**
 * close all area
 * 关闭所有数据区域
 */
public static void close() {
    Set<Map.Entry<String, DB>> entries = AREAS.entrySet();
    for (Map.Entry<String, DB> entry : entries) {
        try {
            AREAS.remove(entry.getKey());
            AREAS_COMPARATOR.remove(entry.getKey());
            entry.getValue().close();
        } catch (Exception e) {
            Log.warn("close leveldb error", e);
        }
    }
}
 
Example #13
Source File: MCAFile2LevelDB.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
public synchronized void compact() {
    // Since the library doesn't support it, only way to flush the cache is to loop over everything
    try (DB newDb = Iq80DBFactory.factory.open(new File(folderTo, "db"), new Options()
            .verifyChecksums(false)
            .blockSize(262144) // 256K
            .cacheSize(8388608) // 8MB
            .writeBufferSize(134217728) // >=128MB
    )) {
        newDb.close();
    } catch (Throwable ignore) {}
    Fawe.debug("Done compacting!");
}
 
Example #14
Source File: LevelDBManager.java    From nuls with MIT License 5 votes vote down vote up
public static byte[] get(String area, byte[] key) {
    if (!baseCheckArea(area)) {
        return null;
    }
    if (key == null) {
        return null;
    }
    try {
        DB db = AREAS.get(area);
        return db.get(key);
    } catch (Exception e) {
        return null;
    }
}
 
Example #15
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 #16
Source File: LevelDBManager.java    From nuls with MIT License 5 votes vote down vote up
/**
 * close a area
 * 关闭指定数据区域
 */
public static void closeArea(String area) {
    try {
        AREAS_COMPARATOR.remove(area);
        DB db = AREAS.remove(area);
        db.close();
    } catch (IOException e) {
        Log.warn("close leveldb area error:" + area, e);
    }
}
 
Example #17
Source File: LevelDBManager.java    From nuls with MIT License 5 votes vote down vote up
public static Result destroyArea(String areaName) {
    if (!baseCheckArea(areaName)) {
        return Result.getFailed(DBErrorCode.DB_AREA_NOT_EXIST);
    }
    if (StringUtils.isBlank(dataPath) || !checkPathLegal(areaName)) {
        return Result.getFailed(DBErrorCode.DB_AREA_CREATE_PATH_ERROR);
    }
    Result result;
    try {
        DB db = AREAS.remove(areaName);
        db.close();
        File dir = new File(dataPath + File.separator + areaName);
        if (!dir.exists()) {
            return Result.getFailed(DBErrorCode.DB_AREA_NOT_EXIST);
        }
        String filePath = dataPath + File.separator + areaName + File.separator + BASE_DB_NAME;
        destroyDB(filePath);
        AREAS_COMPARATOR.remove(areaName);
        delete(BASE_AREA_NAME, bytes(areaName + "-comparator"));
        delete(BASE_AREA_NAME, bytes(areaName + "-cacheSize"));
        result = Result.getSuccess();
    } catch (Exception e) {
        Log.error("error destroy area: " + areaName, e);
        result = Result.getFailed(DBErrorCode.DB_AREA_DESTROY_ERROR);
    }
    return result;
}
 
Example #18
Source File: WarpInit.java    From warp10-platform with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
  String path = args[0];
  
  Options options = new Options();
  options.createIfMissing(true);
  options.verifyChecksums(true);
  options.paranoidChecks(true);

  DB db = null;

  boolean nativedisabled = "true".equals(System.getProperty(Configuration.LEVELDB_NATIVE_DISABLE));
  boolean javadisabled = "true".equals(System.getProperty(Configuration.LEVELDB_JAVA_DISABLE));
  
  try {
    if (!nativedisabled) {
      db = JniDBFactory.factory.open(new File(path), options);
    } else {
      throw new UnsatisfiedLinkError("Native LevelDB implementation disabled.");
    }
  } catch (UnsatisfiedLinkError ule) {
    ule.printStackTrace();
    if (!javadisabled) {
      db = Iq80DBFactory.factory.open(new File(path), options);
    } else {
      throw new RuntimeException("No usable LevelDB implementation, aborting.");
    }
  }      
  
  db.close();
}
 
Example #19
Source File: LevelDBManager.java    From nuls with MIT License 5 votes vote down vote up
public static Result createArea(String areaName, Long cacheSize, Comparator<byte[]> comparator) {
    lock.lock();
    try {
        if (StringUtils.isBlank(areaName)) {
            return Result.getFailed(KernelErrorCode.NULL_PARAMETER);
        }
        if (AREAS.containsKey(areaName)) {
            return Result.getFailed(DBErrorCode.DB_AREA_EXIST);
        }
        // prevent too many areas
        if (AREAS.size() > (max - 1)) {
            return Result.getFailed(DBErrorCode.DB_AREA_CREATE_EXCEED_LIMIT);
        }
        if (StringUtils.isBlank(dataPath) || !checkPathLegal(areaName)) {
            return Result.getFailed(DBErrorCode.DB_AREA_CREATE_PATH_ERROR);
        }
        Result result;
        try {
            File dir = new File(dataPath + File.separator + areaName);
            if (!dir.exists()) {
                dir.mkdir();
            }
            String filePath = dataPath + File.separator + areaName + File.separator + BASE_DB_NAME;
            DB db = openDB(filePath, true, cacheSize, comparator);
            AREAS.put(areaName, db);
            result = Result.getSuccess();
        } catch (Exception e) {
            Log.error("error create area: " + areaName, e);
            result = Result.getFailed(DBErrorCode.DB_AREA_CREATE_ERROR);
        }
        return result;
    } finally {
        lock.unlock();
    }
}
 
Example #20
Source File: LevelDBManager.java    From nuls with MIT License 5 votes vote down vote up
public static void init() throws Exception {
    synchronized (LevelDBManager.class) {
        if (!isInit) {
            isInit = true;
            File dir = loadDataPath();
            dataPath = dir.getPath();
            Log.info("LevelDBManager dataPath is " + dataPath);

            initSchema();
            initBaseDB(dataPath);

            File[] areaFiles = dir.listFiles();
            DB db = null;
            String dbPath = null;
            for (File areaFile : areaFiles) {
                if (BASE_AREA_NAME.equals(areaFile.getName())) {
                    continue;
                }
                if (!areaFile.isDirectory()) {
                    continue;
                }
                try {
                    dbPath = areaFile.getPath() + File.separator + BASE_DB_NAME;
                    db = initOpenDB(dbPath);
                    if (db != null) {
                        AREAS.put(areaFile.getName(), db);
                    }
                } catch (Exception e) {
                    Log.warn("load area failed, areaName: " + areaFile.getName() + ", dbPath: " + dbPath, e);
                }

            }
        }
    }

}
 
Example #21
Source File: DbInitConfig.java    From md_blockchain with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnProperty("db.levelDB")
public DB levelDB() throws IOException {
    org.iq80.leveldb.Options options = new org.iq80.leveldb.Options();
    options.createIfMissing(true);
    return Iq80DBFactory.factory.open(new File("./levelDB"), options);
}
 
Example #22
Source File: LevelDBHandler.java    From julongchain with Apache License 2.0 5 votes vote down vote up
/**
 * 创建db
 */
@Override
public DB createDB(String dbPath) throws LevelDBException{
    this.opened = true;
    this.dbName = dbPath;
    return db;
}
 
Example #23
Source File: StandaloneStoreClient.java    From warp10-platform with Apache License 2.0 5 votes vote down vote up
public StandaloneStoreClient(DB db, KeyStore keystore, Properties properties) {
  this.db = db;
  this.keystore = keystore;
  this.properties = properties;
  this.plasmaHandlers = new ArrayList<StandalonePlasmaHandlerInterface>();
  this.blockcacheThreshold = Integer.parseInt(properties.getProperty(Configuration.LEVELDB_BLOCKCACHE_GTS_THRESHOLD, "0"));
  MAX_ENCODER_SIZE = Long.valueOf(properties.getProperty(Configuration.STANDALONE_MAX_ENCODER_SIZE, DEFAULT_MAX_ENCODER_SIZE));
  MAX_DELETE_BATCHSIZE = Integer.parseInt(properties.getProperty(Configuration.STANDALONE_MAX_DELETE_BATCHSIZE, Integer.toString(DEFAULT_MAX_DELETE_BATCHSIZE)));
  
  syncrate = Math.min(1.0D, Math.max(0.0D, Double.parseDouble(properties.getProperty(Configuration.LEVELDB_DATA_SYNCRATE, "1.0"))));
  syncwrites = 0.0 < syncrate && syncrate < 1.0 ;
}
 
Example #24
Source File: EzLevelDbBatch.java    From ezdb with Apache License 2.0 5 votes vote down vote up
public EzLevelDbBatch(DB db, Serde<H> hashKeySerde, Serde<R> rangeKeySerde,
		Serde<V> valueSerde) {
	this.db = db;
	this.writeBatch = db.createWriteBatch();
	this.hashKeySerde = hashKeySerde;
	this.rangeKeySerde = rangeKeySerde;
	this.valueSerde = valueSerde;
}
 
Example #25
Source File: ReaderWithOffsets.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    if (4 != args.length) {
        System.out.println(HELP);
        return;
    }

    String dlUriStr = args[0];
    final String streamName = args[1];
    final String readerId = args[2];
    final String offsetStoreFile = args[3];

    URI uri = URI.create(dlUriStr);
    DistributedLogConfiguration conf = new DistributedLogConfiguration();
    Namespace namespace = NamespaceBuilder.newBuilder()
            .conf(conf)
            .uri(uri)
            .build();

    // open the dlm
    System.out.println("Opening log stream " + streamName);
    DistributedLogManager dlm = namespace.openLog(streamName);

    // open the offset store
    Options options = new Options();
    options.createIfMissing(true);
    final DB offsetDB = factory.open(new File(offsetStoreFile), options);
    final AtomicReference<DLSN> lastDLSN = new AtomicReference<DLSN>(null);
    // offset updater
    final ScheduledExecutorService executorService =
            Executors.newSingleThreadScheduledExecutor();
    executorService.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            if (null != lastDLSN.get()) {
                offsetDB.put(readerId.getBytes(UTF_8), lastDLSN.get().serializeBytes());
                System.out.println("Updated reader " + readerId + " offset to " + lastDLSN.get());
            }
        }
    }, 10, 10, TimeUnit.SECONDS);
    try {
        byte[] offset = offsetDB.get(readerId.getBytes(UTF_8));
        DLSN dlsn;
        if (null == offset) {
            dlsn = DLSN.InitialDLSN;
        } else {
            dlsn = DLSN.deserializeBytes(offset);
        }
        readLoop(dlm, dlsn, lastDLSN);
    } finally {
        offsetDB.close();
        dlm.close();
        namespace.close();
    }
}
 
Example #26
Source File: WarpCompact.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
  
  if (args.length != 3) {
    System.err.println("Usage: WarpCompact /path/to/leveldb/dir STARTKEY(hex) ENDKEY(hex)");
    System.exit(-1);
  }
  
  String path = args[0];
  
  Options options = new Options();
  
  if (null != System.getProperty(Configuration.LEVELDB_BLOCK_SIZE)) {
    options.blockSize(Integer.parseInt(System.getProperty(Configuration.LEVELDB_BLOCK_SIZE)));
  }    

  DB db = null;

  boolean nativedisabled = "true".equals(System.getProperty(Configuration.LEVELDB_NATIVE_DISABLE));
  boolean javadisabled = "true".equals(System.getProperty(Configuration.LEVELDB_JAVA_DISABLE));
  
  try {
    if (!nativedisabled) {
      db = JniDBFactory.factory.open(new File(path), options);
    } else {
      throw new UnsatisfiedLinkError("Native LevelDB implementation disabled.");
    }
  } catch (UnsatisfiedLinkError ule) {
    ule.printStackTrace();
    if (!javadisabled) {
      db = Iq80DBFactory.factory.open(new File(path), options);
    } else {
      throw new RuntimeException("No usable LevelDB implementation, aborting.");
    }
  }      
  
  final DB fdb = db;
  
  Runtime.getRuntime().addShutdownHook(new Thread() {
    @Override
    public void run() {
      try {
        fdb.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  });
  
  byte[] begin = null;
  byte[] end = null;
  
  if (!"".equals(args[1])) {
    begin = Hex.decode(args[1]);
  }
  
  if (!"".equals(args[2])) {
    end = Hex.decode(args[2]);
  }
  
  db.compactRange(begin, end);    
}
 
Example #27
Source File: ReaderWithOffsets.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    if (4 != args.length) {
        System.out.println(HELP);
        return;
    }

    String dlUriStr = args[0];
    final String streamName = args[1];
    final String readerId = args[2];
    final String offsetStoreFile = args[3];

    URI uri = URI.create(dlUriStr);
    DistributedLogConfiguration conf = new DistributedLogConfiguration();
    DistributedLogNamespace namespace = DistributedLogNamespaceBuilder.newBuilder()
            .conf(conf)
            .uri(uri)
            .build();

    // open the dlm
    System.out.println("Opening log stream " + streamName);
    DistributedLogManager dlm = namespace.openLog(streamName);

    // open the offset store
    Options options = new Options();
    options.createIfMissing(true);
    final DB offsetDB = factory.open(new File(offsetStoreFile), options);
    final AtomicReference<DLSN> lastDLSN = new AtomicReference<DLSN>(null);
    // offset updater
    final ScheduledExecutorService executorService =
            Executors.newSingleThreadScheduledExecutor();
    executorService.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            if (null != lastDLSN.get()) {
                offsetDB.put(readerId.getBytes(UTF_8), lastDLSN.get().serializeBytes());
                System.out.println("Updated reader " + readerId + " offset to " + lastDLSN.get());
            }
        }
    }, 10, 10, TimeUnit.SECONDS);
    try {
        byte[] offset = offsetDB.get(readerId.getBytes(UTF_8));
        DLSN dlsn;
        if (null == offset) {
            dlsn = DLSN.InitialDLSN;
        } else {
            dlsn = DLSN.deserializeBytes(offset);
        }
        readLoop(dlm, dlsn, lastDLSN);
    } finally {
        offsetDB.close();
        dlm.close();
        namespace.close();
    }
}
 
Example #28
Source File: LevelDBHandle.java    From SPADE with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param dbPath directory path
 * @param deleteOnClose delete the database on close or not
 * @param db LevelDB database object
 */
protected LevelDBHandle(String dbPath, boolean deleteOnClose, DB db){
	this.dbPath = dbPath;
	this.deleteOnClose = deleteOnClose;
	this.db = db;
}
 
Example #29
Source File: LeveldbIterator.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Create an iterator for the specified database
 */
public LeveldbIterator(DB db) {
  iter = db.iterator();
}
 
Example #30
Source File: LevelDB.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public static void generate(String path, String name, long seed, Class<? extends Generator> generator, Map<String, String> options) throws IOException {
    if (!new File(path + "/db").exists()) {
        new File(path + "/db").mkdirs();
    }

    CompoundTag levelData = new CompoundTag("")
            .putLong("currentTick", 0)
            .putInt("DayCycleStopTime", -1)
            .putInt("GameType", 0)
            .putInt("Generator", Generator.getGeneratorType(generator))
            .putBoolean("hasBeenLoadedInCreative", false)
            .putLong("LastPlayed", System.currentTimeMillis() / 1000)
            .putString("LevelName", name)
            .putFloat("lightningLevel", 0)
            .putInt("lightningTime", new Random().nextInt())
            .putInt("limitedWorldOriginX", 128)
            .putInt("limitedWorldOriginY", 70)
            .putInt("limitedWorldOriginZ", 128)
            .putInt("Platform", 0)
            .putFloat("rainLevel", 0)
            .putInt("rainTime", new Random().nextInt())
            .putLong("RandomSeed", seed)
            .putByte("spawnMobs", 0)
            .putInt("SpawnX", 128)
            .putInt("SpawnY", 70)
            .putInt("SpawnZ", 128)
            .putInt("storageVersion", 4)
            .putLong("Time", 0)
            .putLong("worldStartCount", ((long) Integer.MAX_VALUE) & 0xffffffffL);

    byte[] data = NBTIO.write(levelData, ByteOrder.LITTLE_ENDIAN);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    outputStream.write(Binary.writeLInt(3));
    outputStream.write(Binary.writeLInt(data.length));
    outputStream.write(data);

    Utils.writeFile(path + "level.dat", new ByteArrayInputStream(outputStream.toByteArray()));

    DB db = Iq80DBFactory.factory.open(new File(path + "/db"), new Options().createIfMissing(true));
    db.close();
}