2014-04-18 02:28:51 +02:00
|
|
|
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
|
|
|
|
package org.rocksdb;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A subclass of RocksDB which supports backup-related operations.
|
|
|
|
*
|
|
|
|
* @see BackupableDBOptions
|
|
|
|
*/
|
|
|
|
public class BackupableDB extends RocksDB {
|
|
|
|
/**
|
|
|
|
* Open a BackupableDB under the specified path.
|
|
|
|
* Note that the backup path should be set properly in the
|
|
|
|
* input BackupableDBOptions.
|
|
|
|
*
|
|
|
|
* @param opt options for db.
|
|
|
|
* @param bopt backup related options.
|
|
|
|
* @param the db path for storing data. The path for storing
|
|
|
|
* backup should be specified in the BackupableDBOptions.
|
|
|
|
* @return reference to the opened BackupableDB.
|
|
|
|
*/
|
|
|
|
public static BackupableDB open(
|
|
|
|
Options opt, BackupableDBOptions bopt, String db_path)
|
|
|
|
throws RocksDBException {
|
2014-05-27 20:54:02 +02:00
|
|
|
|
|
|
|
RocksDB db = RocksDB.open(opt, db_path);
|
|
|
|
BackupableDB bdb = new BackupableDB();
|
|
|
|
bdb.open(db.nativeHandle_, bopt.nativeHandle_);
|
2014-04-18 02:28:51 +02:00
|
|
|
|
2014-05-21 01:52:27 +02:00
|
|
|
// Prevent the RocksDB object from attempting to delete
|
|
|
|
// the underly C++ DB object.
|
[Java] Generalize dis-own native handle and refine dispose framework.
Summary:
1. Move disOwnNativeHandle() function from RocksDB to RocksObject
to allow other RocksObject to use disOwnNativeHandle() when its
ownership of native handle has been transferred.
2. RocksObject now has an abstract implementation of dispose(),
which does the following two things. First, it checks whether
both isOwningNativeHandle() and isInitialized() return true.
If so, it will call the protected abstract function dispose0(),
which all the subclasses of RocksObject should implement. Second,
it sets nativeHandle_ = 0. This redesign ensure all subclasses
of RocksObject have the same dispose behavior.
3. All subclasses of RocksObject now should implement dispose0()
instead of dispose(), and dispose0() will be called only when
isInitialized() returns true.
Test Plan:
make rocksdbjava
make jtest
Reviewers: dhruba, sdong, ankgup87, rsumbaly, swapnilghike, zzbennett, haobo
Reviewed By: haobo
Subscribers: leveldb
Differential Revision: https://reviews.facebook.net/D18801
2014-05-29 03:16:29 +02:00
|
|
|
db.disOwnNativeHandle();
|
2014-05-21 01:52:27 +02:00
|
|
|
|
2014-04-18 02:28:51 +02:00
|
|
|
return bdb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Captures the state of the database in the latest backup.
|
|
|
|
* Note that this function is not thread-safe.
|
|
|
|
*
|
|
|
|
* @param flushBeforeBackup if true, then all data will be flushed
|
|
|
|
* before creating backup.
|
|
|
|
*/
|
|
|
|
public void createNewBackup(boolean flushBeforeBackup) {
|
|
|
|
createNewBackup(nativeHandle_, flushBeforeBackup);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the BackupableDB instance and release resource.
|
|
|
|
*
|
|
|
|
* Internally, BackupableDB owns the rocksdb::DB pointer to its
|
|
|
|
* associated RocksDB. The release of that RocksDB pointer is
|
|
|
|
* handled in the destructor of the c++ rocksdb::BackupableDB and
|
|
|
|
* should be transparent to Java developers.
|
|
|
|
*/
|
|
|
|
@Override public synchronized void close() {
|
2014-05-01 10:44:46 +02:00
|
|
|
if (isInitialized()) {
|
|
|
|
super.close();
|
2014-04-18 02:28:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A protected construction that will be used in the static factory
|
|
|
|
* method BackupableDB.open().
|
|
|
|
*/
|
2014-05-27 20:54:02 +02:00
|
|
|
protected BackupableDB() {
|
2014-04-18 02:28:51 +02:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override protected void finalize() {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected native void open(long rocksDBHandle, long backupDBOptionsHandle);
|
|
|
|
protected native void createNewBackup(long handle, boolean flag);
|
|
|
|
}
|