From 45e43b81df353284a48e1eb6db3611480dd133aa Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Sun, 28 Dec 2014 16:37:57 +0000 Subject: [PATCH] Adds support for db->DefaultColumnFamily() to the Java API --- java/org/rocksdb/RocksDB.java | 10 ++++++ java/org/rocksdb/test/ColumnFamilyTest.java | 34 +++++++++++++++++++++ java/rocksjni/rocksjni.cc | 12 ++++++++ 3 files changed, 56 insertions(+) diff --git a/java/org/rocksdb/RocksDB.java b/java/org/rocksdb/RocksDB.java index 22a608207..6c6e72260 100644 --- a/java/org/rocksdb/RocksDB.java +++ b/java/org/rocksdb/RocksDB.java @@ -1180,6 +1180,15 @@ public class RocksDB extends RocksObject { return iterators; } + /** + * Gets the handle for the default column family + * + * @return The handle of the default column family + */ + public ColumnFamilyHandle getDefaultColumnFamily() { + return new ColumnFamilyHandle(this, getDefaultColumnFamily(nativeHandle_)); + } + /** * Creates a new column family with the name columnFamilyName and * allocates a ColumnFamilyHandle within an internal structure. @@ -1620,6 +1629,7 @@ public class RocksDB extends RocksObject { protected native void releaseSnapshot( long nativeHandle, long snapshotHandle); private native void disposeInternal(long handle); + private native long getDefaultColumnFamily(long handle); private native long createColumnFamily(long handle, ColumnFamilyDescriptor columnFamilyDescriptor) throws RocksDBException; private native void dropColumnFamily(long handle, long cfHandle) throws RocksDBException; diff --git a/java/org/rocksdb/test/ColumnFamilyTest.java b/java/org/rocksdb/test/ColumnFamilyTest.java index 703ed296f..fb95e8010 100644 --- a/java/org/rocksdb/test/ColumnFamilyTest.java +++ b/java/org/rocksdb/test/ColumnFamilyTest.java @@ -56,6 +56,40 @@ public class ColumnFamilyTest { } } + @Test + public void defaultColumnFamily() throws RocksDBException { + RocksDB db = null; + Options options = null; + try { + options = new Options(); + options.setCreateIfMissing(true); + + DBOptions dbOptions = new DBOptions(); + dbOptions.setCreateIfMissing(true); + + db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); + ColumnFamilyHandle cfh = db.getDefaultColumnFamily(); + assertThat(cfh).isNotNull(); + + final byte[] key = "key".getBytes(); + final byte[] value = "value".getBytes(); + + db.put(cfh, key, value); + + final byte[] actualValue = db.get(cfh, key); + + assertThat(cfh).isNotNull(); + assertThat(actualValue).isEqualTo(value); + } finally { + if (db != null) { + db.close(); + } + if (options != null) { + options.dispose(); + } + } + } + @Test public void createColumnFamily() throws RocksDBException { RocksDB db = null; diff --git a/java/rocksjni/rocksjni.cc b/java/rocksjni/rocksjni.cc index be70670ae..fdd1b009f 100644 --- a/java/rocksjni/rocksjni.cc +++ b/java/rocksjni/rocksjni.cc @@ -1174,6 +1174,18 @@ jlongArray Java_org_rocksdb_RocksDB_iterators( return env->NewLongArray(0); } +/* + * Class: org_rocksdb_RocksDB + * Method: getDefaultColumnFamily + * Signature: (J)J + */ +jlong Java_org_rocksdb_RocksDB_getDefaultColumnFamily( + JNIEnv* env, jobject jobj, jlong jdb_handle) { + auto* db_handle = reinterpret_cast(jdb_handle); + auto* cf_handle = db_handle->DefaultColumnFamily(); + return reinterpret_cast(cf_handle); +} + /* * Class: org_rocksdb_RocksDB * Method: createColumnFamily