Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make RocksDB instance responsible for closing associated ColumnFamilyHandle instances #7428

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions java/src/main/java/org/rocksdb/ColumnFamilyHandle.java
Expand Up @@ -13,6 +13,12 @@
* ColumnFamily Pointers.
*/
public class ColumnFamilyHandle extends RocksObject {
/**
* Constructs column family Java object, which operates on underlying native object.
*
* @param rocksDB db instance associated with this column family
* @param nativeHandle native handle to underlying native ColumnFamily object
*/
ColumnFamilyHandle(final RocksDB rocksDB,
final long nativeHandle) {
super(nativeHandle);
Expand Down
63 changes: 52 additions & 11 deletions java/src/main/java/org/rocksdb/RocksDB.java
Expand Up @@ -38,6 +38,8 @@ private enum LibraryState {
RocksDB.loadLibrary();
}

private List<ColumnFamilyHandle> ownedColumnFamilyHandles = new ArrayList<>();

/**
* Loads the necessary library files.
* Calling this method twice will have no effect.
Expand Down Expand Up @@ -307,9 +309,12 @@ public static RocksDB open(final DBOptions options, final String path,
db.storeOptionsInstance(options);

for (int i = 1; i < handles.length; i++) {
columnFamilyHandles.add(new ColumnFamilyHandle(db, handles[i]));
final ColumnFamilyHandle columnFamilyHandle = new ColumnFamilyHandle(db, handles[i]);
columnFamilyHandles.add(columnFamilyHandle);
}

alucarded marked this conversation as resolved.
Show resolved Hide resolved
db.ownedColumnFamilyHandles.addAll(columnFamilyHandles);

return db;
}

Expand Down Expand Up @@ -484,9 +489,12 @@ public static RocksDB openReadOnly(final DBOptions options, final String path,
db.storeOptionsInstance(options);

for (int i = 1; i < handles.length; i++) {
columnFamilyHandles.add(new ColumnFamilyHandle(db, handles[i]));
final ColumnFamilyHandle columnFamilyHandle = new ColumnFamilyHandle(db, handles[i]);
columnFamilyHandles.add(columnFamilyHandle);
}

db.ownedColumnFamilyHandles.addAll(columnFamilyHandles);

return db;
}

Expand Down Expand Up @@ -577,9 +585,12 @@ public static RocksDB openAsSecondary(final DBOptions options, final String path
db.storeOptionsInstance(options);

for (int i = 1; i < handles.length; i++) {
columnFamilyHandles.add(new ColumnFamilyHandle(db, handles[i]));
final ColumnFamilyHandle columnFamilyHandle = new ColumnFamilyHandle(db, handles[i]);
columnFamilyHandles.add(columnFamilyHandle);
}

db.ownedColumnFamilyHandles.addAll(columnFamilyHandles);

return db;
}

Expand All @@ -597,6 +608,11 @@ public static RocksDB openAsSecondary(final DBOptions options, final String path
* @throws RocksDBException if an error occurs whilst closing.
*/
public void closeE() throws RocksDBException {
for (final ColumnFamilyHandle columnFamilyHandle : ownedColumnFamilyHandles) {
columnFamilyHandle.close();
}
ownedColumnFamilyHandles.clear();

if (owningHandle_.compareAndSet(true, false)) {
try {
closeDatabase(nativeHandle_);
Expand All @@ -619,6 +635,11 @@ public void closeE() throws RocksDBException {
*/
@Override
public void close() {
for (final ColumnFamilyHandle columnFamilyHandle : ownedColumnFamilyHandles) {
columnFamilyHandle.close();
}
ownedColumnFamilyHandles.clear();

if (owningHandle_.compareAndSet(true, false)) {
try {
closeDatabase(nativeHandle_);
Expand Down Expand Up @@ -661,10 +682,12 @@ public static List<byte[]> listColumnFamilies(final Options options,
public ColumnFamilyHandle createColumnFamily(
final ColumnFamilyDescriptor columnFamilyDescriptor)
throws RocksDBException {
return new ColumnFamilyHandle(this, createColumnFamily(nativeHandle_,
columnFamilyDescriptor.getName(),
columnFamilyDescriptor.getName().length,
columnFamilyDescriptor.getOptions().nativeHandle_));
final ColumnFamilyHandle columnFamilyHandle = new ColumnFamilyHandle(this,
createColumnFamily(nativeHandle_, columnFamilyDescriptor.getName(),
columnFamilyDescriptor.getName().length,
columnFamilyDescriptor.getOptions().nativeHandle_));
ownedColumnFamilyHandles.add(columnFamilyHandle);
return columnFamilyHandle;
}

/**
Expand All @@ -688,8 +711,10 @@ public List<ColumnFamilyHandle> createColumnFamilies(
final List<ColumnFamilyHandle> columnFamilyHandles =
new ArrayList<>(cfHandles.length);
for (int i = 0; i < cfHandles.length; i++) {
columnFamilyHandles.add(new ColumnFamilyHandle(this, cfHandles[i]));
final ColumnFamilyHandle columnFamilyHandle = new ColumnFamilyHandle(this, cfHandles[i]);
columnFamilyHandles.add(columnFamilyHandle);
}
ownedColumnFamilyHandles.addAll(columnFamilyHandles);
return columnFamilyHandles;
}

Expand Down Expand Up @@ -719,8 +744,10 @@ public List<ColumnFamilyHandle> createColumnFamilies(
final List<ColumnFamilyHandle> columnFamilyHandles =
new ArrayList<>(cfHandles.length);
for (int i = 0; i < cfHandles.length; i++) {
columnFamilyHandles.add(new ColumnFamilyHandle(this, cfHandles[i]));
final ColumnFamilyHandle columnFamilyHandle = new ColumnFamilyHandle(this, cfHandles[i]);
columnFamilyHandles.add(columnFamilyHandle);
}
alucarded marked this conversation as resolved.
Show resolved Hide resolved
ownedColumnFamilyHandles.addAll(columnFamilyHandles);
return columnFamilyHandles;
}

Expand Down Expand Up @@ -753,7 +780,22 @@ public void dropColumnFamilies(
dropColumnFamilies(nativeHandle_, cfHandles);
}

//TODO(AR) what about DestroyColumnFamilyHandle
/**
* Deletes native column family handle of given {@link ColumnFamilyHandle} Java object
* and removes reference from {@link RocksDB#ownedColumnFamilyHandles}.
*
* @param columnFamilyHandle column family handle object.
*/
public void destroyColumnFamilyHandle(final ColumnFamilyHandle columnFamilyHandle) {
for (int i = 0; i < ownedColumnFamilyHandles.size(); ++i) {
final ColumnFamilyHandle ownedHandle = ownedColumnFamilyHandles.get(i);
if (ownedHandle.equals(columnFamilyHandle)) {
columnFamilyHandle.close();
ownedColumnFamilyHandles.remove(i);
return;
}
}
}

/**
* Set the database entry for "key" to "value".
Expand Down Expand Up @@ -4479,7 +4521,6 @@ private native void dropColumnFamily(
final long handle, final long cfHandle) throws RocksDBException;
private native void dropColumnFamilies(final long handle,
final long[] cfHandles) throws RocksDBException;
//TODO(AR) best way to express DestroyColumnFamilyHandle? ...maybe in ColumnFamilyHandle?
private native void put(final long handle, final byte[] key,
final int keyOffset, final int keyLength, final byte[] value,
final int valueOffset, int valueLength) throws RocksDBException;
Expand Down