From c39a808cb6dfab686a9fa069aff73484fef2d331 Mon Sep 17 00:00:00 2001 From: Jermy Li Date: Fri, 10 Dec 2021 09:16:47 -0800 Subject: [PATCH] Deprecate WriteBatch.remove() and use the new style delete() (#9256) Summary: Pull Request resolved: https://github.com/facebook/rocksdb/pull/9256 Reviewed By: mrambacher Differential Revision: D32971447 Pulled By: jay-zhuang fbshipit-source-id: 6954d7287229a8c776092bd82af3a8a8cd92b35e --- java/rocksjni/write_batch.cc | 4 +- java/rocksjni/write_batch_with_index.cc | 4 +- .../java/org/rocksdb/AbstractWriteBatch.java | 56 +++++++----- .../src/main/java/org/rocksdb/WriteBatch.java | 2 +- .../java/org/rocksdb/WriteBatchInterface.java | 88 ++++++++++++------- .../java/org/rocksdb/WriteBatchWithIndex.java | 2 +- .../test/java/org/rocksdb/WriteBatchTest.java | 2 +- 7 files changed, 99 insertions(+), 59 deletions(-) diff --git a/java/rocksjni/write_batch.cc b/java/rocksjni/write_batch.cc index 4ef8035c4..cd43a3481 100644 --- a/java/rocksjni/write_batch.cc +++ b/java/rocksjni/write_batch.cc @@ -363,10 +363,10 @@ void Java_org_rocksdb_WriteBatch_singleDelete__J_3BIJ(JNIEnv* env, jobject jobj, /* * Class: org_rocksdb_WriteBatch - * Method: removeDirect + * Method: deleteDirect * Signature: (JLjava/nio/ByteBuffer;IIJ)V */ -void Java_org_rocksdb_WriteBatch_removeDirect(JNIEnv* env, jobject /*jobj*/, +void Java_org_rocksdb_WriteBatch_deleteDirect(JNIEnv* env, jobject /*jobj*/, jlong jwb_handle, jobject jkey, jint jkey_offset, jint jkey_len, jlong jcf_handle) { diff --git a/java/rocksjni/write_batch_with_index.cc b/java/rocksjni/write_batch_with_index.cc index 021eb385c..7d3e207f6 100644 --- a/java/rocksjni/write_batch_with_index.cc +++ b/java/rocksjni/write_batch_with_index.cc @@ -301,10 +301,10 @@ void Java_org_rocksdb_WriteBatchWithIndex_singleDelete__J_3BIJ( /* * Class: org_rocksdb_WriteBatchWithIndex - * Method: removeDirect + * Method: deleteDirect * Signature: (JLjava/nio/ByteBuffer;IIJ)V */ -void Java_org_rocksdb_WriteBatchWithIndex_removeDirect( +void Java_org_rocksdb_WriteBatchWithIndex_deleteDirect( JNIEnv* env, jobject /*jobj*/, jlong jwb_handle, jobject jkey, jint jkey_offset, jint jkey_len, jlong jcf_handle) { auto* wb = reinterpret_cast(jwb_handle); diff --git a/java/src/main/java/org/rocksdb/AbstractWriteBatch.java b/java/src/main/java/org/rocksdb/AbstractWriteBatch.java index 1f81c99e3..cc8e5c741 100644 --- a/java/src/main/java/org/rocksdb/AbstractWriteBatch.java +++ b/java/src/main/java/org/rocksdb/AbstractWriteBatch.java @@ -56,7 +56,21 @@ public abstract class AbstractWriteBatch extends RocksObject delete(nativeHandle_, key, key.length, columnFamilyHandle.nativeHandle_); } - public void put(ByteBuffer key, ByteBuffer value) throws RocksDBException { + @Override + @Deprecated + public void remove(final ByteBuffer key) throws RocksDBException { + this.delete(key); + } + + @Override + @Deprecated + public void remove(ColumnFamilyHandle columnFamilyHandle, final ByteBuffer key) + throws RocksDBException { + this.delete(columnFamilyHandle, key); + } + + @Override + public void put(final ByteBuffer key, final ByteBuffer value) throws RocksDBException { assert key.isDirect() && value.isDirect(); putDirect(nativeHandle_, key, key.position(), key.remaining(), value, value.position(), value.remaining(), 0); @@ -65,8 +79,8 @@ public abstract class AbstractWriteBatch extends RocksObject } @Override - public void put(ColumnFamilyHandle columnFamilyHandle, ByteBuffer key, ByteBuffer value) - throws RocksDBException { + public void put(ColumnFamilyHandle columnFamilyHandle, final ByteBuffer key, + final ByteBuffer value) throws RocksDBException { assert key.isDirect() && value.isDirect(); putDirect(nativeHandle_, key, key.position(), key.remaining(), value, value.position(), value.remaining(), columnFamilyHandle.nativeHandle_); @@ -85,6 +99,19 @@ public abstract class AbstractWriteBatch extends RocksObject delete(nativeHandle_, key, key.length, columnFamilyHandle.nativeHandle_); } + @Override + public void delete(final ByteBuffer key) throws RocksDBException { + deleteDirect(nativeHandle_, key, key.position(), key.remaining(), 0); + key.position(key.limit()); + } + + @Override + public void delete(ColumnFamilyHandle columnFamilyHandle, final ByteBuffer key) + throws RocksDBException { + deleteDirect( + nativeHandle_, key, key.position(), key.remaining(), columnFamilyHandle.nativeHandle_); + key.position(key.limit()); + } @Override public void singleDelete(byte[] key) throws RocksDBException { @@ -110,19 +137,6 @@ public abstract class AbstractWriteBatch extends RocksObject columnFamilyHandle.nativeHandle_); } - public void remove(ByteBuffer key) throws RocksDBException { - removeDirect(nativeHandle_, key, key.position(), key.remaining(), 0); - key.position(key.limit()); - } - - @Override - public void remove(ColumnFamilyHandle columnFamilyHandle, ByteBuffer key) - throws RocksDBException { - removeDirect( - nativeHandle_, key, key.position(), key.remaining(), columnFamilyHandle.nativeHandle_); - key.position(key.limit()); - } - @Override public void putLogData(byte[] blob) throws RocksDBException { putLogData(nativeHandle_, blob, blob.length); @@ -184,13 +198,13 @@ public abstract class AbstractWriteBatch extends RocksObject abstract void delete(final long handle, final byte[] key, final int keyLen, final long cfHandle) throws RocksDBException; - abstract void singleDelete(final long handle, final byte[] key, - final int keyLen) throws RocksDBException; + abstract void singleDelete(final long handle, final byte[] key, final int keyLen) + throws RocksDBException; - abstract void singleDelete(final long handle, final byte[] key, - final int keyLen, final long cfHandle) throws RocksDBException; + abstract void singleDelete(final long handle, final byte[] key, final int keyLen, + final long cfHandle) throws RocksDBException; - abstract void removeDirect(final long handle, final ByteBuffer key, final int keyOffset, + abstract void deleteDirect(final long handle, final ByteBuffer key, final int keyOffset, final int keyLength, final long cfHandle) throws RocksDBException; abstract void deleteRange(final long handle, final byte[] beginKey, final int beginKeyLen, diff --git a/java/src/main/java/org/rocksdb/WriteBatch.java b/java/src/main/java/org/rocksdb/WriteBatch.java index 01dbe5a5a..a0ee1424d 100644 --- a/java/src/main/java/org/rocksdb/WriteBatch.java +++ b/java/src/main/java/org/rocksdb/WriteBatch.java @@ -243,7 +243,7 @@ public class WriteBatch extends AbstractWriteBatch { @Override final native void singleDelete(final long handle, final byte[] key, final int keyLen, final long cfHandle) throws RocksDBException; @Override - final native void removeDirect(final long handle, final ByteBuffer key, final int keyOffset, + final native void deleteDirect(final long handle, final ByteBuffer key, final int keyOffset, final int keyLength, final long cfHandle) throws RocksDBException; @Override final native void deleteRange(final long handle, final byte[] beginKey, final int beginKeyLen, diff --git a/java/src/main/java/org/rocksdb/WriteBatchInterface.java b/java/src/main/java/org/rocksdb/WriteBatchInterface.java index 1f1ddc4ad..a755066d9 100644 --- a/java/src/main/java/org/rocksdb/WriteBatchInterface.java +++ b/java/src/main/java/org/rocksdb/WriteBatchInterface.java @@ -39,8 +39,8 @@ public interface WriteBatchInterface { * @param value the value associated with the specified key. * @throws RocksDBException thrown if error happens in underlying native library. */ - void put(ColumnFamilyHandle columnFamilyHandle, - byte[] key, byte[] value) throws RocksDBException; + void put(ColumnFamilyHandle columnFamilyHandle, byte[] key, byte[] value) + throws RocksDBException; /** *

Store the mapping "key->value" within given column @@ -52,7 +52,7 @@ public interface WriteBatchInterface { * Supports direct buffer only. * @throws RocksDBException */ - void put(ByteBuffer key, ByteBuffer value) throws RocksDBException; + void put(final ByteBuffer key, final ByteBuffer value) throws RocksDBException; /** *

Store the mapping "key->value" within given column @@ -66,7 +66,7 @@ public interface WriteBatchInterface { * Supports direct buffer only. * @throws RocksDBException */ - void put(ColumnFamilyHandle columnFamilyHandle, ByteBuffer key, ByteBuffer value) + void put(ColumnFamilyHandle columnFamilyHandle, final ByteBuffer key, final ByteBuffer value) throws RocksDBException; /** @@ -90,8 +90,8 @@ public interface WriteBatchInterface { * the specified key. * @throws RocksDBException thrown if error happens in underlying native library. */ - void merge(ColumnFamilyHandle columnFamilyHandle, - byte[] key, byte[] value) throws RocksDBException; + void merge(ColumnFamilyHandle columnFamilyHandle, byte[] key, byte[] value) + throws RocksDBException; /** *

If the database contains a mapping for "key", erase it. Else do nothing.

@@ -114,7 +114,31 @@ public interface WriteBatchInterface { * @throws RocksDBException thrown if error happens in underlying native library. */ @Deprecated - void remove(ColumnFamilyHandle columnFamilyHandle, byte[] key) + void remove(ColumnFamilyHandle columnFamilyHandle, byte[] key) throws RocksDBException; + + /** + *

If column family contains a mapping for "key", erase it. Else do nothing.

+ * + * @param key Key to delete within database. It is using position and limit. + * Supports direct buffer only. + * + * @deprecated Use {@link #delete(ByteBuffer)} + * @throws RocksDBException thrown if error happens in underlying native library. + */ + @Deprecated void remove(final ByteBuffer key) throws RocksDBException; + + /** + *

If column family contains a mapping for "key", erase it. Else do nothing.

+ * + * @param columnFamilyHandle {@link ColumnFamilyHandle} instance + * @param key Key to delete within database. It is using position and limit. + * Supports direct buffer only. + * + * @deprecated Use {@link #delete(ColumnFamilyHandle, ByteBuffer)} + * @throws RocksDBException thrown if error happens in underlying native library. + */ + @Deprecated + void remove(ColumnFamilyHandle columnFamilyHandle, final ByteBuffer key) throws RocksDBException; /** @@ -132,7 +156,28 @@ public interface WriteBatchInterface { * @param key Key to delete within database * @throws RocksDBException thrown if error happens in underlying native library. */ - void delete(ColumnFamilyHandle columnFamilyHandle, byte[] key) + void delete(ColumnFamilyHandle columnFamilyHandle, byte[] key) throws RocksDBException; + + /** + *

If column family contains a mapping for "key", erase it. Else do nothing.

+ * + * @param key Key to delete within database. It is using position and limit. + * Supports direct buffer only. + * + * @throws RocksDBException thrown if error happens in underlying native library. + */ + void delete(final ByteBuffer key) throws RocksDBException; + + /** + *

If column family contains a mapping for "key", erase it. Else do nothing.

+ * + * @param columnFamilyHandle {@link ColumnFamilyHandle} instance + * @param key Key to delete within database. It is using position and limit. + * Supports direct buffer only. + * + * @throws RocksDBException thrown if error happens in underlying native library. + */ + void delete(ColumnFamilyHandle columnFamilyHandle, final ByteBuffer key) throws RocksDBException; /** @@ -182,27 +227,8 @@ public interface WriteBatchInterface { * native library. */ @Experimental("Performance optimization for a very specific workload") - void singleDelete(final ColumnFamilyHandle columnFamilyHandle, - final byte[] key) throws RocksDBException; - - /** - *

If column family contains a mapping for "key", erase it. Else do nothing.

- * - * @param key Key to delete within database. It is using position and limit. - * Supports direct buffer only. - * @throws RocksDBException - */ - void remove(ByteBuffer key) throws RocksDBException; - - /** - *

If column family contains a mapping for "key", erase it. Else do nothing.

- * - * @param columnFamilyHandle {@link ColumnFamilyHandle} instance - * @param key Key to delete within database. It is using position and limit. - * Supports direct buffer only. - * @throws RocksDBException - */ - void remove(ColumnFamilyHandle columnFamilyHandle, ByteBuffer key) throws RocksDBException; + void singleDelete(final ColumnFamilyHandle columnFamilyHandle, final byte[] key) + throws RocksDBException; /** * Removes the database entries in the range ["beginKey", "endKey"), i.e., @@ -237,8 +263,8 @@ public interface WriteBatchInterface { * Last key to delete within database (excluded) * @throws RocksDBException thrown if error happens in underlying native library. */ - void deleteRange(ColumnFamilyHandle columnFamilyHandle, byte[] beginKey, - byte[] endKey) throws RocksDBException; + void deleteRange(ColumnFamilyHandle columnFamilyHandle, byte[] beginKey, byte[] endKey) + throws RocksDBException; /** * Append a blob of arbitrary size to the records in this batch. The blob will diff --git a/java/src/main/java/org/rocksdb/WriteBatchWithIndex.java b/java/src/main/java/org/rocksdb/WriteBatchWithIndex.java index 495afd3fa..c73bd7dda 100644 --- a/java/src/main/java/org/rocksdb/WriteBatchWithIndex.java +++ b/java/src/main/java/org/rocksdb/WriteBatchWithIndex.java @@ -318,7 +318,7 @@ public class WriteBatchWithIndex extends AbstractWriteBatch { @Override final native void singleDelete(final long handle, final byte[] key, final int keyLen, final long cfHandle) throws RocksDBException; @Override - final native void removeDirect(final long handle, final ByteBuffer key, final int keyOffset, + final native void deleteDirect(final long handle, final ByteBuffer key, final int keyOffset, final int keyLength, final long cfHandle) throws RocksDBException; // DO NOT USE - `WriteBatchWithIndex::deleteRange` is not yet supported @Override diff --git a/java/src/test/java/org/rocksdb/WriteBatchTest.java b/java/src/test/java/org/rocksdb/WriteBatchTest.java index f915c7dcb..cc3ad26eb 100644 --- a/java/src/test/java/org/rocksdb/WriteBatchTest.java +++ b/java/src/test/java/org/rocksdb/WriteBatchTest.java @@ -100,7 +100,7 @@ public class WriteBatchTest { key.clear(); key.put("box".getBytes("US-ASCII")).flip(); - batch.remove(key); + batch.delete(key); assertThat(key.position()).isEqualTo(3); assertThat(key.limit()).isEqualTo(3);