Abstractions for common write batch behaviour

This commit is contained in:
Adam Retter 2015-01-03 18:36:59 +00:00
parent be905491bf
commit c6e5545612
3 changed files with 110 additions and 72 deletions

View File

@ -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();
}

View File

@ -22,7 +22,7 @@ package org.rocksdb;
* non-const method, all threads accessing the same WriteBatch must use * non-const method, all threads accessing the same WriteBatch must use
* external synchronization. * external synchronization.
*/ */
public class WriteBatch extends RocksObject implements WriteBatchInterface { public class WriteBatch extends AbstractWriteBatch {
/** /**
* Constructs a WriteBatch instance. * Constructs a WriteBatch instance.
*/ */
@ -41,48 +41,6 @@ public class WriteBatch extends RocksObject implements WriteBatchInterface {
newWriteBatch(reserved_bytes); 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. * Support for iterating over the contents of a batch.
* *
@ -95,34 +53,22 @@ public class WriteBatch extends RocksObject implements WriteBatchInterface {
iterate(handler.nativeHandle_); iterate(handler.nativeHandle_);
} }
@Override @Override final native void disposeInternal(long handle);
public native void clear(); @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,
* Delete the c++ side pointer. long cfHandle);
*/ @Override final native void merge(byte[] key, int keyLen, byte[] value, int valueLen);
@Override protected void disposeInternal() { @Override final native void merge(byte[] key, int keyLen, byte[] value, int valueLen,
assert(isInitialized()); long cfHandle);
disposeInternal(nativeHandle_); @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 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 iterate(long handlerHandle) throws RocksDBException;
private native void disposeInternal(long handle);
/** /**
* Handler callback for iterating over the contents of a batch. * Handler callback for iterating over the contents of a batch.

View File

@ -41,10 +41,10 @@ void Java_org_rocksdb_WriteBatch_newWriteBatch(
/* /*
* Class: org_rocksdb_WriteBatch * Class: org_rocksdb_WriteBatch
* Method: count * Method: count0
* Signature: ()I * 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); rocksdb::WriteBatch* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
assert(wb != nullptr); assert(wb != nullptr);
@ -53,10 +53,10 @@ jint Java_org_rocksdb_WriteBatch_count(JNIEnv* env, jobject jobj) {
/* /*
* Class: org_rocksdb_WriteBatch * Class: org_rocksdb_WriteBatch
* Method: clear * Method: clear0
* Signature: ()V * 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); rocksdb::WriteBatch* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
assert(wb != nullptr); assert(wb != nullptr);