org.iq80.leveldb.impl.Iq80DBFactory Java Examples
The following examples show how to use
org.iq80.leveldb.impl.Iq80DBFactory.
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: LevelCacheStore.java From halo with GNU General Public License v3.0 | 6 votes |
@PostConstruct public void init() { if (LEVEL_DB != null) { return; } try { //work path File folder = new File(haloProperties.getWorkDir() + ".leveldb"); DBFactory factory = new Iq80DBFactory(); Options options = new Options(); options.createIfMissing(true); //open leveldb store folder LEVEL_DB = factory.open(folder, options); timer = new Timer(); timer.scheduleAtFixedRate(new CacheExpiryCleaner(), 0, PERIOD); } catch (Exception ex) { log.error("init leveldb error ", ex); } }
Example #2
Source File: DbManager.java From Almost-Famous with MIT License | 6 votes |
/** * 不指定目录,默认配置文件 leveldb_datadir 这个参数获取 * * @param dataDir 数据文件目录 */ public void open(String dataDir) { if (dataDir != null && dataDir.trim().length() > 0) { this.defaultDataDir = dataDir; } else { this.defaultDataDir = ConfigManager.getStringValue("unique", "leveldb_datadir"); } DBFactory factory = Iq80DBFactory.factory; File file = new File(defaultDataDir); if (!file.exists()) { file.mkdir(); } try { //如果数据不需要reload,则每次重启,尝试清理磁盘中path下的旧数据。 if (cleanup) { factory.destroy(file, null);//清除文件夹内的所有文件。 } Options options = new Options().createIfMissing(true); //重新open新的db db = factory.open(file, options); log.error("Db open"); } catch (IOException e) { log.error("Db open fail ", e); } }
Example #3
Source File: LevelDBManager.java From nuls with MIT License | 6 votes |
/** * 装载数据库 * 如果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 #4
Source File: LevelDBKeyValueStore.java From boon with Apache License 2.0 | 6 votes |
/** * Opens the database * * @param file filename to open * @param options options * @return */ private boolean openDB(File file, Options options) { try { database = JniDBFactory.factory.open(file, options); logger.info("Using JNI Level DB"); return true; } catch (IOException ex1) { try { database = Iq80DBFactory.factory.open(file, options); logger.info("Using Java Level DB"); return false; } catch (IOException ex2) { return Exceptions.handle(Boolean.class, ex2); } } }
Example #5
Source File: DbInitConfig.java From md_blockchain with Apache License 2.0 | 5 votes |
@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 #6
Source File: LevelDBStorage.java From greycat with Apache License 2.0 | 5 votes |
@Override public void connect(Graph graph, Callback<Boolean> callback) { if (isConnected) { if (callback != null) { callback.on(null); } return; } this.graph = graph; //by default activate snappy compression of bytes Options options = new Options() .createIfMissing(true) .compressionType(CompressionType.SNAPPY); File location = new File(storagePath); if (!location.exists()) { location.mkdirs(); } File targetDB = new File(location, "data"); targetDB.mkdirs(); try { if (useNative) { db = JniDBFactory.factory.open(targetDB, options); } else { db = Iq80DBFactory.factory.open(targetDB, options); } isConnected = true; if (callback != null) { callback.on(true); } } catch (Exception e) { e.printStackTrace(); if (callback != null) { callback.on(null); } } }
Example #7
Source File: LevelDBManager.java From nuls with MIT License | 5 votes |
/** * @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 #8
Source File: LevelDbStore.java From dubbox with Apache License 2.0 | 5 votes |
@Override public void put(String transactionId, Transaction transaction) { db.put( Iq80DBFactory.bytes(transactionId), SerializationUtil.serialize(transaction) ); }
Example #9
Source File: JLevelDBState.java From jesos with Apache License 2.0 | 5 votes |
public JLevelDBState(final String path) throws IOException { checkNotNull(path, "path is null"); final Options options = new Options(); options.createIfMissing(true); this.db = Iq80DBFactory.factory.open(new File(path), options); }
Example #10
Source File: MCAFile2LevelDB.java From FastAsyncWorldedit with GNU General Public License v3.0 | 5 votes |
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 #11
Source File: MCAFile2LevelDB.java From FastAsyncWorldedit with GNU General Public License v3.0 | 5 votes |
public MCAFile2LevelDB(File folderFrom, File folderTo) { super(folderFrom, folderTo); this.startTime = System.currentTimeMillis(); try { if (!folderTo.exists()) { folderTo.mkdirs(); } String worldName = folderTo.getName(); try (PrintStream out = new PrintStream(new FileOutputStream(new File(folderTo, "levelname.txt")))) { out.print(worldName); } this.pool = new ForkJoinPool(); this.remapper = new ClipboardRemapper(ClipboardRemapper.RemapPlatform.PC, ClipboardRemapper.RemapPlatform.PE); BundledBlockData.getInstance().loadFromResource(); int bufferSize = (int) Math.min(Integer.MAX_VALUE, Math.max((long) (MemUtil.getFreeBytes() * 0.8), 134217728)); this.db = Iq80DBFactory.factory.open(new File(folderTo, "db"), new Options() .createIfMissing(true) .verifyChecksums(false) .compressionType(CompressionType.ZLIB) .blockSize(262144) // 256K .cacheSize(8388608) // 8MB .writeBufferSize(134217728) // >=512MB ); // try { // this.db.suspendCompactions(); // } catch (InterruptedException e) { // e.printStackTrace(); // } } catch (IOException e) { throw new RuntimeException(e); } }
Example #12
Source File: WarpInit.java From warp10-platform with Apache License 2.0 | 5 votes |
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 #13
Source File: WarpDB.java From warp10-platform with Apache License 2.0 | 5 votes |
private synchronized void open(boolean nativedisabled, boolean javadisabled, String home, Options options) throws IOException { try { mutex.lockInterruptibly(); // Wait for iterators and other ops to finish while(pendingOps.get() > 0 || compactionsSuspended.get()) { LockSupport.parkNanos(100000000L); } if (null != db) { this.db.close(); this.db = null; } try { if (!nativedisabled) { db = JniDBFactory.factory.open(new File(home), options); } else { throw new UnsatisfiedLinkError("Native LevelDB implementation disabled."); } } catch (UnsatisfiedLinkError ule) { ule.printStackTrace(); if (!javadisabled) { System.out.println("WARNING: falling back to pure java implementation of LevelDB."); db = Iq80DBFactory.factory.open(new File(home), options); } else { throw new RuntimeException("No usable LevelDB implementation, aborting."); } } } catch (InterruptedException ie) { throw new RuntimeException("Interrupted while opending LevelDB.", ie); } finally { if (mutex.isHeldByCurrentThread()) { mutex.unlock(); } } }
Example #14
Source File: EzLevelDbJavaFactory.java From ezdb with Apache License 2.0 | 4 votes |
@Override public void destroy(File path, Options options) throws IOException { Iq80DBFactory.factory.destroy(path, options); }
Example #15
Source File: EzLevelDbJavaFactory.java From ezdb with Apache License 2.0 | 4 votes |
@Override public DB open(File path, Options options) throws IOException { return Iq80DBFactory.factory.open(path, options); }
Example #16
Source File: LevelDB.java From Nukkit with GNU General Public License v3.0 | 4 votes |
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(); }
Example #17
Source File: WarpCompact.java From warp10-platform with Apache License 2.0 | 4 votes |
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 #18
Source File: LevelDB.java From BukkitPE with GNU General Public License v3.0 | 4 votes |
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(); }
Example #19
Source File: LevelDB.java From Nukkit with GNU General Public License v3.0 | 4 votes |
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(); }
Example #20
Source File: LevelDbStore.java From dubbox with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws Exception { Kryo kryo = new Kryo(); kryo.setReferences(false); kryo.setRegistrationRequired(false); kryo.setInstantiatorStrategy(new DefaultInstantiatorStrategy(new StdInstantiatorStrategy())); // kryo.register(Transaction.class); // kryo.register(Xid.class); // kryo.register(TransactionType.class); // kryo.register(TransactionStatus.class); // kryo.register(Participant.class); // Output output = new Output(new ByteArrayOutputStream());//new Output(new FileOutputStream("file.bin")); // // DefaultTransaction defaultTransaction = new DefaultTransaction(); // defaultTransaction.setXid(new DefaultXid("1", "2", "3")); // // kryo.writeObject(output, defaultTransaction); // output.flush(); // output.close(); Options options = new Options(); options.createIfMissing(true); options.cacheSize(100 * 1048576); // 100MB cache options.logger(new org.iq80.leveldb.Logger() { public void log(String message) { TransactionLog.REPOSITORY.info(message); } }); DB db = Iq80DBFactory.factory.open(new File("txtreedb"), options); // String stats = db.getProperty("leveldb.stats"); // System.out.println(stats); DBIterator iterator = null; try { iterator = db.iterator(); for(iterator.seekToFirst(); iterator.hasNext(); iterator.next()) { String key = Iq80DBFactory.asString(iterator.peekNext().getKey()); Transaction value = (Transaction) SerializationUtil.deserialize(iterator.peekNext().getValue()); System.out.println(key+" = "+value); } } finally { if (iterator != null) { iterator.close(); } } // String transactionId = "f282205a-e794-4bda-83a2-bb28b8839cad"; // Input input = new Input(db.get(Iq80DBFactory.bytes(transactionId))); // Transaction transaction = (Transaction) kryo.readClassAndObject(input); // System.out.println(transaction); }
Example #21
Source File: LevelDbStore.java From dubbox with Apache License 2.0 | 4 votes |
@Override public void delete(String transactionId) { db.delete(Iq80DBFactory.bytes(transactionId)); }
Example #22
Source File: LevelDbStore.java From dubbox with Apache License 2.0 | 4 votes |
@Override public Transaction get(String transactionId) { return (Transaction) SerializationUtil.deserialize(db.get(Iq80DBFactory.bytes(transactionId))); }
Example #23
Source File: LevelDB.java From Jupiter with GNU General Public License v3.0 | 4 votes |
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(); }
Example #24
Source File: LevelDBManager.java From nuls with MIT License | 4 votes |
private static void destroyDB(String dbPath) throws IOException { File file = new File(dbPath); Options options = new Options(); DBFactory factory = Iq80DBFactory.factory; factory.destroy(file, options); }