From c6e55456128a26481c6d7a374fb2228c6e3ee165 Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Sat, 3 Jan 2015 18:36:59 +0000 Subject: [PATCH] Abstractions for common write batch behaviour --- java/org/rocksdb/AbstractWriteBatch.java | 92 ++++++++++++++++++++++++ java/org/rocksdb/WriteBatch.java | 82 ++++----------------- java/rocksjni/write_batch.cc | 8 +-- 3 files changed, 110 insertions(+), 72 deletions(-) create mode 100644 java/org/rocksdb/AbstractWriteBatch.java diff --git a/java/org/rocksdb/AbstractWriteBatch.java b/java/org/rocksdb/AbstractWriteBatch.java new file mode 100644 index 000000000..b380c5d8a --- /dev/null +++ b/java/org/rocksdb/AbstractWriteBatch.java @@ -0,0 +1,92 @@ +// 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; + +public abstract class AbstractWriteBatch extends RocksObject implements WriteBatchInterface { + + @Override + public int count() { + assert (isInitialized()); + return count0(); + } + + @Override + public void put(byte[] key, byte[] value) { + assert (isInitialized()); + put(key, key.length, value, value.length); + } + + @Override + public void put(ColumnFamilyHandle columnFamilyHandle, byte[] key, byte[] value) { + assert (isInitialized()); + put(key, key.length, value, value.length, columnFamilyHandle.nativeHandle_); + } + + @Override + public void merge(byte[] key, byte[] value) { + assert (isInitialized()); + merge(key, key.length, value, value.length); + } + + @Override + public void merge(ColumnFamilyHandle columnFamilyHandle, byte[] key, byte[] value) { + assert (isInitialized()); + merge(key, key.length, value, value.length, columnFamilyHandle.nativeHandle_); + } + + @Override + public void remove(byte[] key) { + assert (isInitialized()); + remove(key, key.length); + } + + @Override + public void remove(ColumnFamilyHandle columnFamilyHandle, byte[] key) { + assert (isInitialized()); + remove(key, key.length, columnFamilyHandle.nativeHandle_); + } + + @Override + public void putLogData(byte[] blob) { + assert (isInitialized()); + putLogData(blob, blob.length); + } + + @Override + public void clear() { + assert (isInitialized()); + clear0(); + } + + /** + * Delete the c++ side pointer. + */ + @Override + protected void disposeInternal() { + assert (isInitialized()); + disposeInternal(nativeHandle_); + } + + abstract void disposeInternal(long handle); + + abstract int count0(); + + abstract void put(byte[] key, int keyLen, byte[] value, int valueLen); + + abstract void put(byte[] key, int keyLen, byte[] value, int valueLen, long cfHandle); + + abstract void merge(byte[] key, int keyLen, byte[] value, int valueLen); + + abstract void merge(byte[] key, int keyLen, byte[] value, int valueLen, long cfHandle); + + abstract void remove(byte[] key, int keyLen); + + abstract void remove(byte[] key, int keyLen, long cfHandle); + + abstract void putLogData(byte[] blob, int blobLen); + + abstract void clear0(); +} diff --git a/java/org/rocksdb/WriteBatch.java b/java/org/rocksdb/WriteBatch.java index 64f472c89..24133ec39 100644 --- a/java/org/rocksdb/WriteBatch.java +++ b/java/org/rocksdb/WriteBatch.java @@ -22,7 +22,7 @@ package org.rocksdb; * non-const method, all threads accessing the same WriteBatch must use * external synchronization. */ -public class WriteBatch extends RocksObject implements WriteBatchInterface { +public class WriteBatch extends AbstractWriteBatch { /** * Constructs a WriteBatch instance. */ @@ -41,48 +41,6 @@ public class WriteBatch extends RocksObject implements WriteBatchInterface { newWriteBatch(reserved_bytes); } - @Override - public native int count(); - - @Override - public void put(byte[] key, byte[] value) { - put(key, key.length, value, value.length); - } - - @Override - public void put(ColumnFamilyHandle columnFamilyHandle, - byte[] key, byte[] value) { - put(key, key.length, value, value.length, - columnFamilyHandle.nativeHandle_); - } - - @Override - public void merge(byte[] key, byte[] value) { - merge(key, key.length, value, value.length); - } - - @Override - public void merge(ColumnFamilyHandle columnFamilyHandle, - byte[] key, byte[] value) { - merge(key, key.length, value, value.length, - columnFamilyHandle.nativeHandle_); - } - - @Override - public void remove(byte[] key) { - remove(key, key.length); - } - - @Override - public void remove(ColumnFamilyHandle columnFamilyHandle, byte[] key) { - remove(key, key.length, columnFamilyHandle.nativeHandle_); - } - - @Override - public void putLogData(byte[] blob) { - putLogData(blob, blob.length); - } - /** * Support for iterating over the contents of a batch. * @@ -95,34 +53,22 @@ public class WriteBatch extends RocksObject implements WriteBatchInterface { iterate(handler.nativeHandle_); } - @Override - public native void clear(); - - /** - * Delete the c++ side pointer. - */ - @Override protected void disposeInternal() { - assert(isInitialized()); - disposeInternal(nativeHandle_); - } + @Override final native void disposeInternal(long handle); + @Override final native int count0(); + @Override final native void put(byte[] key, int keyLen, byte[] value, int valueLen); + @Override final native void put(byte[] key, int keyLen, byte[] value, int valueLen, + long cfHandle); + @Override final native void merge(byte[] key, int keyLen, byte[] value, int valueLen); + @Override final native void merge(byte[] key, int keyLen, byte[] value, int valueLen, + long cfHandle); + @Override final native void remove(byte[] key, int keyLen); + @Override final native void remove(byte[] key, int keyLen, long cfHandle); + @Override final native void putLogData(byte[] blob, int blobLen); + @Override final native void clear0(); private native void newWriteBatch(int reserved_bytes); - private native void put(byte[] key, int keyLen, - byte[] value, int valueLen); - private native void put(byte[] key, int keyLen, - byte[] value, int valueLen, - long cfHandle); - private native void merge(byte[] key, int keyLen, - byte[] value, int valueLen); - private native void merge(byte[] key, int keyLen, - byte[] value, int valueLen, - long cfHandle); - private native void remove(byte[] key, int keyLen); - private native void remove(byte[] key, int keyLen, - long cfHandle); - private native void putLogData(byte[] blob, int blobLen); private native void iterate(long handlerHandle) throws RocksDBException; - private native void disposeInternal(long handle); + /** * Handler callback for iterating over the contents of a batch. diff --git a/java/rocksjni/write_batch.cc b/java/rocksjni/write_batch.cc index dbf2e25e2..02f3989a9 100644 --- a/java/rocksjni/write_batch.cc +++ b/java/rocksjni/write_batch.cc @@ -41,10 +41,10 @@ void Java_org_rocksdb_WriteBatch_newWriteBatch( /* * Class: org_rocksdb_WriteBatch - * Method: count + * Method: count0 * Signature: ()I */ -jint Java_org_rocksdb_WriteBatch_count(JNIEnv* env, jobject jobj) { +jint Java_org_rocksdb_WriteBatch_count0(JNIEnv* env, jobject jobj) { rocksdb::WriteBatch* wb = rocksdb::WriteBatchJni::getHandle(env, jobj); assert(wb != nullptr); @@ -53,10 +53,10 @@ jint Java_org_rocksdb_WriteBatch_count(JNIEnv* env, jobject jobj) { /* * Class: org_rocksdb_WriteBatch - * Method: clear + * Method: clear0 * Signature: ()V */ -void Java_org_rocksdb_WriteBatch_clear(JNIEnv* env, jobject jobj) { +void Java_org_rocksdb_WriteBatch_clear0(JNIEnv* env, jobject jobj) { rocksdb::WriteBatch* wb = rocksdb::WriteBatchJni::getHandle(env, jobj); assert(wb != nullptr);