Java Code Examples for org.rocksdb.RocksDB#close()

The following examples show how to use org.rocksdb.RocksDB#close() . 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: RocksDBManager.java    From WebCollector with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void inject(CrawlDatums datums, boolean force) throws Exception {
    RocksDB crawldbDatabase = RocksDBUtils.openCrawldbDatabase(crawlPath);

    for (int i = 0; i < datums.size(); i++) {
        CrawlDatum datum = datums.get(i);
        String key = datum.key();
        if (!force) {
            if(RocksDBUtils.get(crawldbDatabase, key) != null){
                continue;
            }
        }
        RocksDBUtils.put(crawldbDatabase, key, datum.asJsonArray().toString());
    }
    crawldbDatabase.close();
}
 
Example 2
Source File: RocksDBOperationsUtilsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testPathExceptionOnWindows() throws Exception {
	assumeTrue(OperatingSystem.isWindows());

	final File folder = TMP_DIR.newFolder();
	final File rocksDir = new File(folder, getLongString(247 - folder.getAbsolutePath().length()));

	Files.createDirectories(rocksDir.toPath());

	try (DBOptions dbOptions = new DBOptions().setCreateIfMissing(true);
		ColumnFamilyOptions colOptions = new ColumnFamilyOptions()) {

		RocksDB rocks = RocksDBOperationUtils.openDB(
				rocksDir.getAbsolutePath(),
				Collections.emptyList(),
				Collections.emptyList(),
				colOptions, dbOptions);
		rocks.close();

		// do not provoke a test failure if this passes, because some setups may actually
		// support long paths, in which case: great!
	}
	catch (IOException e) {
		assertThat(e.getMessage(), containsString("longer than the directory path length limit for Windows"));
	}
}
 
Example 3
Source File: RocksDbDataStoreFactory.java    From biomedicus with Apache License 2.0 6 votes vote down vote up
@Override
public SuffixDataStore openSuffixDataStore(int id) {
  RocksDB.loadLibrary();
  try {
    LOGGER.info("Opening TnT suffix model: {}", id);
    RocksDB rocksDB = RocksDB.openReadOnly(dbPath.resolve(getSuffixesName(id)).toString());
    RocksDbSuffixDataStore rocksDbSuffixDataStore = new RocksDbSuffixDataStore(rocksDB);
    if (inMemory) {
      LOGGER.info("Loading TnT suffix model into memory: {}", id);
      InMemorySuffixDataStore inMemorySuffixDataStore = rocksDbSuffixDataStore.inMemory();
      LOGGER.info("Done loading TnT suffix model into memory: {}", id);
      rocksDB.close();
      return inMemorySuffixDataStore;
    }
    rocksDBS.add(rocksDB);
    return rocksDbSuffixDataStore;
  } catch (RocksDBException e) {
    throw new RuntimeException(e);
  }
}
 
Example 4
Source File: RocksDbDataStoreFactory.java    From biomedicus with Apache License 2.0 6 votes vote down vote up
@Override
public KnownWordsDataStore openKnownWordDataStore(int id) {
  RocksDB.loadLibrary();
  try {
    LOGGER.info("Opening TnT model known word model: {}", id);
    RocksDB rocksDB = RocksDB.openReadOnly(dbPath.resolve(getWordsName(id)).toString());
    RocksDB candidatesDB = RocksDB.openReadOnly(dbPath.resolve(getCandidatesName(id)).toString());

    RocksDbKnownWordsDataStore rocksDbKnownWordsDataStore = new RocksDbKnownWordsDataStore(
        rocksDB, candidatesDB);
    if (inMemory) {
      LOGGER.info("Loading TnT known word model into memory: {}", id);
      InMemoryKnownWordDataStore inMemoryKnownWordDataStore = rocksDbKnownWordsDataStore
          .inMemory();
      LOGGER.info("Done loading TnT known word model into memory: {}", id);
      rocksDB.close();
      candidatesDB.close();
      return inMemoryKnownWordDataStore;
    }
    rocksDBS.add(rocksDB);
    rocksDBS.add(candidatesDB);
    return rocksDbKnownWordsDataStore;
  } catch (RocksDBException e) {
    throw new RuntimeException(e);
  }
}
 
Example 5
Source File: BackupEngineTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Test
    public void backupDb2() throws RocksDBException {
//        String originPath = dbFolder.getRoot().getAbsolutePath();
//        String backupPath = backupFolder.getRoot().getAbsolutePath();
        String originPath = "/tmp/rocksdb";
        String originPath2 = "/tmp/rocksdb2";
        String backupPath = "/tmp/rocksdb_backup";
        System.out.println("originPath=" + originPath);
        System.out.println("backupPath=" + backupPath);
        // Open empty database.
        try (final Options opt = new Options().setCreateIfMissing(true);
             final RocksDB db = RocksDB.open(opt, originPath)) {

            // Fill database with some test values
            prepareDatabase(db);

            try (RocksIterator it = db.newIterator()) {
                for (it.seekToFirst(); it.isValid(); it.next()) {
                    System.out.println(originPath + ":" + new String(it.key()) + ":" + new String(it.value()));
                }
            }

            // Create two backups
            try (final BackupableDBOptions bopt = new BackupableDBOptions(backupPath);
                 final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) {
                be.createNewBackup(db, true);
                be.createNewBackup(db, true);

                //restore the backup
                final List<BackupInfo> backupInfo = verifyNumberOfValidBackups(be, 2);
                // restore db from first backup
                be.restoreDbFromBackup(backupInfo.get(0).backupId(), originPath2, originPath2, new RestoreOptions(true));
                // Open database again.
                RocksDB db2 = RocksDB.open(opt, originPath2);

                try (RocksIterator it = db2.newIterator()) {
                    for (it.seekToFirst(); it.isValid(); it.next()) {
                        System.out.println(originPath2 + ":" + new String(it.key()) + ":" + new String(it.value()));
                    }
                }

                db2.close();
            }
        }


    }
 
Example 6
Source File: RocksDBManager.java    From WebCollector with GNU General Public License v3.0 5 votes vote down vote up
@Override
    public void merge() throws Exception {
        LOG.info("start merge");
        RocksDB crawldbDatabase = RocksDBUtils.openCrawldbDatabase(crawlPath);


        /*合并fetch库*/
        LOG.info("merge fetch database");
        RocksDB fetchDatabase = RocksDBUtils.openFetchDatabase(crawlPath);
        RocksIterator fetchIterator = fetchDatabase.newIterator();
        for(fetchIterator.seekToFirst(); fetchIterator.isValid(); fetchIterator.next()){
            crawldbDatabase.put(fetchIterator.key(), fetchIterator.value());
        }
        fetchDatabase.close();

        /*合并link库*/
        LOG.info("merge link database");
        RocksDB linkDatabase = RocksDBUtils.openLinkDatabase(crawlPath);
        RocksIterator linkIterator = linkDatabase.newIterator();
        for(linkIterator.seekToFirst(); linkIterator.isValid(); linkIterator.next()){
            if(crawldbDatabase.get(linkIterator.key()) == null){
                crawldbDatabase.put(linkIterator.key(), linkIterator.value());
            }
        }
        linkDatabase.close();

        LOG.info("end merge");
        crawldbDatabase.close();



//        env.removeDatabase(null, "fetch");
        RocksDBUtils.destroyFetchDatabase(crawlPath);
        LOG.debug("remove fetch database");
//        env.removeDatabase(null, "link");
        RocksDBUtils.destroyLinkDatabase(crawlPath);
        LOG.debug("remove link database");

    }
 
Example 7
Source File: RocksDBManager.java    From WebCollector with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void inject(CrawlDatum datum, boolean force) throws Exception {
    RocksDB crawldbDatabase = RocksDBUtils.openCrawldbDatabase(crawlPath);
    String key = datum.key();
    if (!force) {
        if(RocksDBUtils.get(crawldbDatabase, key) != null){
            crawldbDatabase.close();
            return;
        }
    }
    RocksDBUtils.put(crawldbDatabase, key, datum.asJsonArray().toString());
    crawldbDatabase.close();
}
 
Example 8
Source File: RocksDBManager.java    From WebCollector with GNU General Public License v3.0 5 votes vote down vote up
public void list() throws Exception {
    String crawldbPath = FilenameUtils.concat(crawlPath, "crawldb");
    RocksDB crawldbDatabase = RocksDBUtils.open(crawldbPath);
    RocksIterator crawldbIterator = crawldbDatabase.newIterator();

    for(crawldbIterator.seekToFirst(); crawldbIterator.isValid(); crawldbIterator.next()){
        CrawlDatum datum = RocksDBUtils.createCrawlDatum(crawldbIterator.key(), crawldbIterator.value());
        System.out.println(CrawlDatumFormater.datumToString(datum));
    }

    crawldbDatabase.close();

}
 
Example 9
Source File: BrendaSupportingEntries.java    From act with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create an on-disk index of reaction-supporting data from BRENDA using RocksDB.  This DB will contain a number
 * of `column families` (i.e. per-table namespaces).  Each FromBrendaDB instance
 *
 * All index rows are keyed on EC number, literature reference (individually, lists are split during construction),
 * and organism names.   Values are serialized (via Serializable) lists of FromBrendaDB objects; each column family
 * contains one type of object.
 *
 * Creating this data on an in-office MBP with a BRENDA MySQL instance running in EC2 takes just a couple of minutes.
 * Looking up supporting data locally vs. running MySQL queries for every data type * every reaction results in a
 * ~30x speedup of reaction processing.
 *
 * @param pathToIndex The local path where the index should be built.  This will become a directory containing
 *                    RocksDB files.
 * @param conn A connection to the BRENDA MySQL DB (`brenda` database) from which data will be read.
 * @throws IOException
 * @throws ClassNotFoundException
 * @throws RocksDBException
 * @throws SQLException
 */
public void constructOnDiskBRENDAIndex(File pathToIndex, Connection conn)
    throws IOException, ClassNotFoundException, RocksDBException, SQLException {
  if (pathToIndex.exists()) {
    System.out.println("Index already exists, not recreating.");
    return;
  }

  RocksDB db = null; // Not auto-closable.
  List<? extends FromBrendaDB> instances = allFromBrendaDBInstances();
  try {
    Options options = new Options().setCreateIfMissing(true);
    System.out.println("Opening index at " + pathToIndex.getAbsolutePath());
    db = RocksDB.open(options, pathToIndex.getAbsolutePath());

    for (FromBrendaDB instance : instances) {
      System.out.println("Writing index for " + instance.getColumnFamilyName());
      ColumnFamilyHandle cfh =
          db.createColumnFamily(new ColumnFamilyDescriptor(instance.getColumnFamilyName().getBytes(UTF8)));
      IndexWriter writer = new IndexWriter(cfh, db, instance);
      writer.run(conn);
      db.flush(new FlushOptions());
    }
  } finally {
    if (db != null) {
      db.close();
    }
  }
}
 
Example 10
Source File: RocksDbUnitTest.java    From jstorm with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    Map conf = JStormHelper.LoadConf(args[0]);
    putNum = JStormUtils.parseInt(conf.get("put.number"), 100);
    isFlush = JStormUtils.parseBoolean(conf.get("is.flush"), true);
    isCheckpoint = JStormUtils.parseBoolean(conf.get("is.checkpoint"), true);
    sleepTime = JStormUtils.parseInt(conf.get("sleep.time"), 5000);
    compactionInterval = JStormUtils.parseInt(conf.get("compaction.interval"), 30000);
    flushInterval = JStormUtils.parseInt(conf.get("flush.interval"), 3000);
    isCompaction = JStormUtils.parseBoolean(conf.get("is.compaction"), true);
    fileSizeBase = JStormUtils.parseLong(conf.get("file.size.base"), 10 * SizeUnit.KB);
    levelNum = JStormUtils.parseInt(conf.get("db.level.num"), 1);
    compactionTriggerNum = JStormUtils.parseInt(conf.get("db.compaction.trigger.num"), 4);
    LOG.info("Conf={}", conf);
    
    RocksDB db;
    File file = new File(cpPath);
    file.mkdirs();

    List<ColumnFamilyHandle> columnFamilyHandles = new ArrayList<>();
    try {
        Options options = new Options();
        options.setCreateMissingColumnFamilies(true);
        options.setCreateIfMissing(true);
        options.setTargetFileSizeBase(fileSizeBase);
        options.setMaxBackgroundFlushes(2);
        options.setMaxBackgroundCompactions(2);
        options.setCompactionStyle(CompactionStyle.LEVEL);
        options.setNumLevels(levelNum);
        options.setLevelZeroFileNumCompactionTrigger(compactionTriggerNum);

        DBOptions dbOptions = new DBOptions();
        dbOptions.setCreateMissingColumnFamilies(true);
        dbOptions.setCreateIfMissing(true);
        dbOptions.setMaxBackgroundFlushes(2);
        dbOptions.setMaxBackgroundCompactions(2);
        ColumnFamilyOptions familyOptions = new ColumnFamilyOptions();
        familyOptions.setTargetFileSizeBase(fileSizeBase);
        familyOptions.setCompactionStyle(CompactionStyle.LEVEL);
        familyOptions.setNumLevels(levelNum);
        familyOptions.setLevelZeroFileNumCompactionTrigger(compactionTriggerNum);
        List<byte[]> families = RocksDB.listColumnFamilies(options, dbPath);
        List<ColumnFamilyDescriptor> columnFamilyDescriptors = new ArrayList<>();
        if (families != null) {
            for (byte[] bytes : families) {
                columnFamilyDescriptors.add(new ColumnFamilyDescriptor(bytes, familyOptions));
                LOG.info("Load colum family of {}", new String(bytes));
            }
        }
        
        if (columnFamilyDescriptors.size() > 0) {
            db = RocksDB.open(dbOptions, dbPath, columnFamilyDescriptors, columnFamilyHandles);
        } else {
            db = RocksDB.open(options, dbPath);
        }
    } catch (RocksDBException e) {
        LOG.error("Failed to open db", e);
        return;
    }

    rocksDbTest(db, columnFamilyHandles);
    
    db.close();
}
 
Example 11
Source File: BackupDB.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
public static RestoreState restore() throws RocksDBException {
    if (restoring) {
        LOGGER.info("is restoring, return");
        return RestoreState.BEING_RESTORE;
    }

    LOGGER.info("start restore");
    restoring = true;
    RocksDB restoreDB = null;
    try (final BackupableDBOptions bopt = new BackupableDBOptions(DB_PATH_BACKUP);
         final BackupEngine be = BackupEngine.open(Env.getDefault(), bopt)) {
        // restore db from first backup

        /**
         * @param keepLogFiles If true, restore won't overwrite the existing log files
         *   in wal_dir. It will also move all log files from archive directory to
         *   wal_dir. Use this option in combination with
         *   BackupableDBOptions::backup_log_files = false for persisting in-memory
         *   databases.
         *   Default: false
         */
        boolean keepLogFiles = false;
        be.restoreDbFromLatestBackup(DB_PATH_RESTORE, DB_PATH_RESTORE, new RestoreOptions(keepLogFiles));
        // open database again.
        restoreDB = RocksDB.open(OptionsConfig.DB_OPTIONS, DB_PATH_RESTORE, CFManager.CF_DESCRIPTORS, CFManager.CF_HANDLES);

        int i = 0;
        try (RocksIterator it = restoreDB.newIterator()) {
            for (it.seekToFirst(); it.isValid(); it.next()) {
                LOGGER.info("i:{}, key:{}, value:{}", i++, new String(it.key()), new String(it.value()));
                if (i == 10) {
                    break;
                }
            }
        }

        return RestoreState.SUCCESS;
    } catch (RocksDBException e) {
        LOGGER.error("error while restore, path:{}, err:{}", DB_PATH_RESTORE, e.getMessage(), e);
        return RestoreState.FAIL;
    } finally {
        if (restoreDB != null) {
            restoreDB.close();
        }

        restoring = false;
        LOGGER.info("end restore");
    }
}
 
Example 12
Source File: RocksDbDataStoreFactory.java    From biomedicus with Apache License 2.0 4 votes vote down vote up
@Override
public void doShutdown() {
  for (RocksDB rocksDB : rocksDBS) {
    rocksDB.close();
  }
}
 
Example 13
Source File: BrendaSQL.java    From act with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Install all BRENDA reactions in the DB specified in the constructor.
 *
 * This installation follows a three step process for each reaction:
 * 1) A BRENDA reaction is added to the DB without protein information (which includes sequence references); this
 * generates a new id for the reaction.
 * 2) The reaction's sequence entries are added to the DB with references to the reaction's id.
 * 3) The reaction is updated with a protein entry, which contains references to the sequences' ids created in (2).
 *
 * The bi-directional id references require that one object (reaction or sequence) have its id generated first, which
 * means creating a new but incomplete object in the DB.
 *
 * @throws IOException
 * @throws ClassNotFoundException
 * @throws RocksDBException
 * @throws SQLException
 */
public void installReactions() throws IOException, ClassNotFoundException, RocksDBException, SQLException {
  int numEntriesAdded = 0;
  SQLConnection brendaDB = new SQLConnection();
  System.out.println("Connecting to brenda DB.");
  // This expects an SSH tunnel to be running, like the one created with the command
  // $ ssh -L10000:brenda-mysql-1.ciuibkvm9oks.us-west-1.rds.amazonaws.com:3306 [email protected]
  establishDefaultBrendaConnection(brendaDB);
  
  System.out.println("Connection established.");

  // Create a local index of the BRENDA tables that share the same simple access pattern.
  System.out.println("Creating supporting index of BRENDA data");
  try {
    brendaDB.createSupportingIndex(supportingIndex);
  } catch (Exception e) {
    System.err.println("Caught exception while building BRENDA index: " + e.getMessage());
    e.printStackTrace(System.err);
    throw new RuntimeException(e);
  }
  System.out.println("Supporting index creation complete.");

  // Open the BRENDA index for reading.
  System.out.println("Opening supporting idex at " + this.supportingIndex.getAbsolutePath());
  Pair<RocksDB, Map<String, ColumnFamilyHandle>> openedDbObjects = brendaDB.openSupportingIndex(supportingIndex);
  RocksDB rocksDB = openedDbObjects.getLeft();
  Map<String, ColumnFamilyHandle> columnFamilyHandleMap = openedDbObjects.getRight();

  /* Create a table of recommended names.  There's at most 1 per EC number, so these can just live in memory for
   * super fast lookup. */
  System.out.println("Retrieving recommended name table.");
  BrendaSupportingEntries.RecommendNameTable recommendNameTable = brendaDB.fetchRecommendNameTable();
  System.out.println("Recommended name table retrieved.");

  Iterator<BrendaRxnEntry> rxns = brendaDB.getRxns();

  numEntriesAdded += installReactions(brendaDB, rocksDB, columnFamilyHandleMap, recommendNameTable,
      brendaDB.getRxns(), numEntriesAdded);
  numEntriesAdded += installReactions(brendaDB, rocksDB, columnFamilyHandleMap, recommendNameTable,
      brendaDB.getNaturalRxns(), numEntriesAdded);

  rocksDB.close();
  brendaDB.disconnect();

  if (this.cleanUpSupportingIndex) {
    brendaDB.deleteSupportingIndex(supportingIndex);
  }

  System.out.format("Main.addBrendaReactionsFromSQL: Num entries added %d\n", numEntriesAdded);
}
 
Example 14
Source File: BackupEngineTest.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Test
public void restoreFromBackup()
        throws RocksDBException {
    try (final Options opt = new Options().setCreateIfMissing(true)) {
        RocksDB db = null;
        try {
            // Open empty database.
            db = RocksDB.open(opt,
                    dbFolder.getRoot().getAbsolutePath());
            // Fill database with some test values
            prepareDatabase(db);
            try (final BackupableDBOptions bopt = new BackupableDBOptions(
                    backupFolder.getRoot().getAbsolutePath());
                 final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) {
                be.createNewBackup(db, true);
                verifyNumberOfValidBackups(be, 1);
                db.put("key1".getBytes(), "valueV2".getBytes());
                db.put("key2".getBytes(), "valueV2".getBytes());
                be.createNewBackup(db, true);
                verifyNumberOfValidBackups(be, 2);
                db.put("key1".getBytes(), "valueV3".getBytes());
                db.put("key2".getBytes(), "valueV3".getBytes());
                assertThat(new String(db.get("key1".getBytes()))).endsWith("V3");
                assertThat(new String(db.get("key2".getBytes()))).endsWith("V3");

                //close the database
                db.close();
                db = null;

                //restore the backup
                final List<BackupInfo> backupInfo = verifyNumberOfValidBackups(be, 2);
                // restore db from first backup
                be.restoreDbFromBackup(backupInfo.get(0).backupId(),
                        dbFolder.getRoot().getAbsolutePath(),
                        dbFolder.getRoot().getAbsolutePath(),
                        new RestoreOptions(false));
                // Open database again.
                db = RocksDB.open(opt,
                        dbFolder.getRoot().getAbsolutePath());
                // Values must have suffix V2 because of restoring latest backup.
                assertThat(new String(db.get("key1".getBytes()))).endsWith("V1");
                assertThat(new String(db.get("key2".getBytes()))).endsWith("V1");
            }
        } finally {
            if (db != null) {
                db.close();
            }
        }
    }
}
 
Example 15
Source File: BackupEngineTest.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Test
public void restoreLatestBackup() throws RocksDBException {
    try (final Options opt = new Options().setCreateIfMissing(true)) {
        // Open empty database.
        RocksDB db = null;
        try {
            db = RocksDB.open(opt,
                    dbFolder.getRoot().getAbsolutePath());
            // Fill database with some test values
            prepareDatabase(db);

            try (final BackupableDBOptions bopt = new BackupableDBOptions(
                    backupFolder.getRoot().getAbsolutePath());
                 final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) {
                be.createNewBackup(db, true);
                verifyNumberOfValidBackups(be, 1);
                db.put("key1".getBytes(), "valueV2".getBytes());
                db.put("key2".getBytes(), "valueV2".getBytes());
                be.createNewBackup(db, true);
                verifyNumberOfValidBackups(be, 2);
                db.put("key1".getBytes(), "valueV3".getBytes());
                db.put("key2".getBytes(), "valueV3".getBytes());
                assertThat(new String(db.get("key1".getBytes()))).endsWith("V3");
                assertThat(new String(db.get("key2".getBytes()))).endsWith("V3");

                db.close();
                db = null;

                verifyNumberOfValidBackups(be, 2);
                // restore db from latest backup
                try (final RestoreOptions ropts = new RestoreOptions(false)) {
                    be.restoreDbFromLatestBackup(dbFolder.getRoot().getAbsolutePath(),
                            dbFolder.getRoot().getAbsolutePath(), ropts);
                }

                // Open database again.
                db = RocksDB.open(opt, dbFolder.getRoot().getAbsolutePath());

                // Values must have suffix V2 because of restoring latest backup.
                assertThat(new String(db.get("key1".getBytes()))).endsWith("V2");
                assertThat(new String(db.get("key2".getBytes()))).endsWith("V2");
            }
        } finally {
            if (db != null) {
                db.close();
            }
        }
    }
}
 
Example 16
Source File: BackupEngineTest.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Test
    public void backupDb2() throws RocksDBException {
//        String originPath = dbFolder.getRoot().getAbsolutePath();
//        String backupPath = backupFolder.getRoot().getAbsolutePath();
        String originPath = "/tmp/rocksdb";
        String originPath2 = "/tmp/rocksdb2";
        String backupPath = "/tmp/rocksdb_backup";
        System.out.println("originPath=" + originPath);
        System.out.println("backupPath=" + backupPath);
        // Open empty database.
        try (final Options opt = new Options().setCreateIfMissing(true);
             final RocksDB db = RocksDB.open(opt, originPath)) {

            // Fill database with some test values
            prepareDatabase(db);

            try (RocksIterator it = db.newIterator()) {
                for (it.seekToFirst(); it.isValid(); it.next()) {
                    System.out.println(originPath + ":" + new String(it.key()) + ":" + new String(it.value()));
                }
            }

            // Create two backups
            try (final BackupableDBOptions bopt = new BackupableDBOptions(backupPath);
                 final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) {
                be.createNewBackup(db, true);
                be.createNewBackup(db, true);

                //restore the backup
                final List<BackupInfo> backupInfo = verifyNumberOfValidBackups(be, 2);
                // restore db from first backup
                be.restoreDbFromBackup(backupInfo.get(0).backupId(), originPath2, originPath2, new RestoreOptions(true));
                // Open database again.
                RocksDB db2 = RocksDB.open(opt, originPath2);

                try (RocksIterator it = db2.newIterator()) {
                    for (it.seekToFirst(); it.isValid(); it.next()) {
                        System.out.println(originPath2 + ":" + new String(it.key()) + ":" + new String(it.value()));
                    }
                }

                db2.close();
            }
        }


    }
 
Example 17
Source File: BackupDB.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
public static RestoreState restore() throws RocksDBException {
    if (restoring) {
        LOGGER.info("is restoring, return");
        return RestoreState.BEING_RESTORE;
    }

    LOGGER.info("start restore");
    restoring = true;
    RocksDB restoreDB = null;
    try (final BackupableDBOptions bopt = new BackupableDBOptions(DB_PATH_BACKUP);
         final BackupEngine be = BackupEngine.open(Env.getDefault(), bopt)) {
        // restore db from first backup

        /**
         * @param keepLogFiles If true, restore won't overwrite the existing log files
         *   in wal_dir. It will also move all log files from archive directory to
         *   wal_dir. Use this option in combination with
         *   BackupableDBOptions::backup_log_files = false for persisting in-memory
         *   databases.
         *   Default: false
         */
        boolean keepLogFiles = false;
        be.restoreDbFromLatestBackup(DB_PATH_RESTORE, DB_PATH_RESTORE, new RestoreOptions(keepLogFiles));
        // open database again.
        restoreDB = RocksDB.open(OptionsConfig.DB_OPTIONS, DB_PATH_RESTORE, CFManager.CF_DESCRIPTORS, CFManager.CF_HANDLES);

        int i = 0;
        try (RocksIterator it = restoreDB.newIterator()) {
            for (it.seekToFirst(); it.isValid(); it.next()) {
                LOGGER.info("i:{}, key:{}, value:{}", i++, new String(it.key()), new String(it.value()));
                if (i == 10) {
                    break;
                }
            }
        }

        return RestoreState.SUCCESS;
    } catch (RocksDBException e) {
        LOGGER.error("error while restore, path:{}, err:{}", DB_PATH_RESTORE, e.getMessage(), e);
        return RestoreState.FAIL;
    } finally {
        if (restoreDB != null) {
            restoreDB.close();
        }

        restoring = false;
        LOGGER.info("end restore");
    }
}
 
Example 18
Source File: BackupEngineTest.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Test
public void restoreFromBackup()
        throws RocksDBException {
    try (final Options opt = new Options().setCreateIfMissing(true)) {
        RocksDB db = null;
        try {
            // Open empty database.
            db = RocksDB.open(opt,
                    dbFolder.getRoot().getAbsolutePath());
            // Fill database with some test values
            prepareDatabase(db);
            try (final BackupableDBOptions bopt = new BackupableDBOptions(
                    backupFolder.getRoot().getAbsolutePath());
                 final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) {
                be.createNewBackup(db, true);
                verifyNumberOfValidBackups(be, 1);
                db.put("key1".getBytes(), "valueV2".getBytes());
                db.put("key2".getBytes(), "valueV2".getBytes());
                be.createNewBackup(db, true);
                verifyNumberOfValidBackups(be, 2);
                db.put("key1".getBytes(), "valueV3".getBytes());
                db.put("key2".getBytes(), "valueV3".getBytes());
                assertThat(new String(db.get("key1".getBytes()))).endsWith("V3");
                assertThat(new String(db.get("key2".getBytes()))).endsWith("V3");

                //close the database
                db.close();
                db = null;

                //restore the backup
                final List<BackupInfo> backupInfo = verifyNumberOfValidBackups(be, 2);
                // restore db from first backup
                be.restoreDbFromBackup(backupInfo.get(0).backupId(),
                        dbFolder.getRoot().getAbsolutePath(),
                        dbFolder.getRoot().getAbsolutePath(),
                        new RestoreOptions(false));
                // Open database again.
                db = RocksDB.open(opt,
                        dbFolder.getRoot().getAbsolutePath());
                // Values must have suffix V2 because of restoring latest backup.
                assertThat(new String(db.get("key1".getBytes()))).endsWith("V1");
                assertThat(new String(db.get("key2".getBytes()))).endsWith("V1");
            }
        } finally {
            if (db != null) {
                db.close();
            }
        }
    }
}
 
Example 19
Source File: BackupEngineTest.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Test
public void restoreLatestBackup() throws RocksDBException {
    try (final Options opt = new Options().setCreateIfMissing(true)) {
        // Open empty database.
        RocksDB db = null;
        try {
            db = RocksDB.open(opt,
                    dbFolder.getRoot().getAbsolutePath());
            // Fill database with some test values
            prepareDatabase(db);

            try (final BackupableDBOptions bopt = new BackupableDBOptions(
                    backupFolder.getRoot().getAbsolutePath());
                 final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) {
                be.createNewBackup(db, true);
                verifyNumberOfValidBackups(be, 1);
                db.put("key1".getBytes(), "valueV2".getBytes());
                db.put("key2".getBytes(), "valueV2".getBytes());
                be.createNewBackup(db, true);
                verifyNumberOfValidBackups(be, 2);
                db.put("key1".getBytes(), "valueV3".getBytes());
                db.put("key2".getBytes(), "valueV3".getBytes());
                assertThat(new String(db.get("key1".getBytes()))).endsWith("V3");
                assertThat(new String(db.get("key2".getBytes()))).endsWith("V3");

                db.close();
                db = null;

                verifyNumberOfValidBackups(be, 2);
                // restore db from latest backup
                try (final RestoreOptions ropts = new RestoreOptions(false)) {
                    be.restoreDbFromLatestBackup(dbFolder.getRoot().getAbsolutePath(),
                            dbFolder.getRoot().getAbsolutePath(), ropts);
                }

                // Open database again.
                db = RocksDB.open(opt, dbFolder.getRoot().getAbsolutePath());

                // Values must have suffix V2 because of restoring latest backup.
                assertThat(new String(db.get("key1".getBytes()))).endsWith("V2");
                assertThat(new String(db.get("key2".getBytes()))).endsWith("V2");
            }
        } finally {
            if (db != null) {
                db.close();
            }
        }
    }
}