Skip to content

Commit

Permalink
Add RocksDB.destroyColumnFamilyHandle() method
Browse files Browse the repository at this point in the history
  • Loading branch information
alucarded authored and jay-zhuang committed Oct 14, 2020
1 parent b09308b commit d152856
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
18 changes: 16 additions & 2 deletions java/src/main/java/org/rocksdb/RocksDB.java
Expand Up @@ -780,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 @@ -4506,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
29 changes: 25 additions & 4 deletions java/src/test/java/org/rocksdb/ColumnFamilyTest.java
Expand Up @@ -5,16 +5,17 @@

package org.rocksdb;

import java.util.*;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.*;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;

public class ColumnFamilyTest {

@ClassRule
Expand Down Expand Up @@ -559,6 +560,26 @@ public void testCFNameSimplifiedChinese() throws RocksDBException {
}
}

@Test
public void testDestroyColumnFamilyHandle() throws RocksDBException {
try (final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());) {
final byte[] name1 = "cf1".getBytes();
final byte[] name2 = "cf2".getBytes();
final ColumnFamilyDescriptor desc1 = new ColumnFamilyDescriptor(name1);
final ColumnFamilyDescriptor desc2 = new ColumnFamilyDescriptor(name2);
final ColumnFamilyHandle cf1 = db.createColumnFamily(desc1);
final ColumnFamilyHandle cf2 = db.createColumnFamily(desc2);
assertTrue(cf1.isOwningHandle());
assertTrue(cf2.isOwningHandle());
assertFalse(cf1.isDefaultColumnFamily());
db.destroyColumnFamilyHandle(cf1);
// At this point cf1 should not be used!
assertFalse(cf1.isOwningHandle());
assertTrue(cf2.isOwningHandle());
}
}

@Test
@Deprecated
/**
Expand Down

0 comments on commit d152856

Please sign in to comment.