Integrated feedback from ankgup87
Test Plan: tested using unit tests Reviewers: ankgup87 Differential Revision: https://reviews.facebook.net/D24573
This commit is contained in:
parent
a40ce219b9
commit
2ef3ed86f3
@ -9,11 +9,9 @@ import java.util.*;
|
||||
|
||||
/**
|
||||
* MergeOperator holds an operator to be applied when compacting
|
||||
* two values held under the same key in order to obtain a single
|
||||
* two merge operands held under the same key in order to obtain a single
|
||||
* value.
|
||||
*/
|
||||
public abstract class MergeOperator {
|
||||
|
||||
abstract protected long newMergeOperatorHandle();
|
||||
|
||||
public interface MergeOperator {
|
||||
public long newMergeOperatorHandle();
|
||||
}
|
||||
|
@ -2235,13 +2235,16 @@ public class Options extends RocksObject {
|
||||
long handle, int minPartialMergeOperands);
|
||||
|
||||
/**
|
||||
* Set the merge operator to be used for merging two different key/value
|
||||
* pairs that share the same key. The merge function is invoked during
|
||||
* Set the merge operator to be used for merging two merge operands
|
||||
* of the same key. The merge function is invoked during
|
||||
* compaction and at lookup time, if multiple key/value pairs belonging
|
||||
* to the same key are found in the database.
|
||||
*
|
||||
* @param name the name of the merge function, as defined by
|
||||
* the MergeOperators factory (see utilities/MergeOperators.h)
|
||||
* The merge function is specified by name and must be one of the
|
||||
* standard merge operators provided by RocksDB. The available
|
||||
* operators are "put", "uint64add", "stringappend" and "stringappendtest".
|
||||
* @return the reference to the current option.
|
||||
*/
|
||||
public Options setMergeOperatorName(String name) {
|
||||
@ -2257,8 +2260,7 @@ public class Options extends RocksObject {
|
||||
* compaction and at lookup time, if multiple key/value pairs belonging
|
||||
* to the same key are found in the database.
|
||||
*
|
||||
* @param name the name of the merge function, as defined by
|
||||
* the MergeOperators factory (see utilities/MergeOperators.h)
|
||||
* @param a {@link MergeOperator} object
|
||||
* @return the reference to the current option.
|
||||
*/
|
||||
public Options setMergeOperator(MergeOperator mergeOperator) {
|
||||
|
@ -473,7 +473,7 @@ public class RocksDB extends RocksObject {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the database entry for "key" to "value".
|
||||
* Add merge operand for key/value pair.
|
||||
*
|
||||
* @param key the specified key to be merged.
|
||||
* @param value the value to be nerged with the current value for
|
||||
@ -484,8 +484,9 @@ public class RocksDB extends RocksObject {
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge the database entry for "key" with "value".
|
||||
* Add merge operand for key/value pair.
|
||||
*
|
||||
* @param writeOpts {@link WriteOptions} for this write.
|
||||
* @param key the specified key to be merged.
|
||||
* @param value the value to be merged with the current value for
|
||||
* the specified key.
|
||||
|
@ -6,16 +6,12 @@
|
||||
package org.rocksdb;
|
||||
|
||||
/**
|
||||
* MergeOperator holds an operator to be applied when compacting
|
||||
* two values held under the same key in order to obtain a single
|
||||
* value.
|
||||
* StringAppendOperator is a merge operator that concatenates
|
||||
* two strings.
|
||||
*/
|
||||
public class StringAppendOperator extends MergeOperator {
|
||||
|
||||
@Override protected long newMergeOperatorHandle() {
|
||||
return newMergeOperatorHandleImpl();
|
||||
}
|
||||
|
||||
public class StringAppendOperator implements MergeOperator {
|
||||
@Override public long newMergeOperatorHandle() {
|
||||
return newMergeOperatorHandleImpl();
|
||||
}
|
||||
private native long newMergeOperatorHandleImpl();
|
||||
|
||||
}
|
||||
|
@ -9,80 +9,77 @@ import java.util.Collections;
|
||||
import org.rocksdb.*;
|
||||
|
||||
public class MergeTest {
|
||||
static final String db_path_string = "/tmp/mergestringjni_db";
|
||||
static final String db_path_function = "/tmp/mergefunctionjni_db";
|
||||
static {
|
||||
RocksDB.loadLibrary();
|
||||
}
|
||||
static final String db_path_string = "/tmp/mergestringjni_db";
|
||||
static final String db_path_function = "/tmp/mergefunctionjni_db";
|
||||
static {
|
||||
RocksDB.loadLibrary();
|
||||
}
|
||||
|
||||
public static void testStringOption()
|
||||
throws InterruptedException, RocksDBException {
|
||||
public static void testStringOption()
|
||||
throws InterruptedException, RocksDBException {
|
||||
|
||||
System.out.println("Testing merge function string option ===");
|
||||
System.out.println("Testing merge function string option ===");
|
||||
|
||||
Options opt = new Options();
|
||||
opt.setCreateIfMissing(true);
|
||||
opt.setMergeOperatorName("stringappend");
|
||||
Options opt = new Options();
|
||||
opt.setCreateIfMissing(true);
|
||||
opt.setMergeOperatorName("stringappend");
|
||||
|
||||
RocksDB db = RocksDB.open(opt, db_path_string);
|
||||
RocksDB db = RocksDB.open(opt, db_path_string);
|
||||
|
||||
System.out.println("Writing aa under key...");
|
||||
db.put("key".getBytes(), "aa".getBytes());
|
||||
System.out.println("Writing aa under key...");
|
||||
db.put("key".getBytes(), "aa".getBytes());
|
||||
|
||||
System.out.println("Writing bb under key...");
|
||||
db.merge("key".getBytes(), "bb".getBytes());
|
||||
System.out.println("Writing bb under key...");
|
||||
db.merge("key".getBytes(), "bb".getBytes());
|
||||
|
||||
byte[] value = db.get("key".getBytes());
|
||||
String strValue = new String(value);
|
||||
byte[] value = db.get("key".getBytes());
|
||||
String strValue = new String(value);
|
||||
|
||||
System.out.println("Retrieved value: " + strValue);
|
||||
System.out.println("Retrieved value: " + strValue);
|
||||
|
||||
db.close();
|
||||
opt.dispose();
|
||||
db.close();
|
||||
opt.dispose();
|
||||
|
||||
assert(strValue.equals("aa,bb"));
|
||||
assert(strValue.equals("aa,bb"));
|
||||
|
||||
System.out.println("Merge function string option passed!");
|
||||
System.out.println("Merge function string option passed!");
|
||||
}
|
||||
|
||||
}
|
||||
public static void testOperatorOption()
|
||||
throws InterruptedException, RocksDBException {
|
||||
|
||||
public static void testOperatorOption()
|
||||
throws InterruptedException, RocksDBException {
|
||||
System.out.println("Testing merge function operator option ===");
|
||||
|
||||
System.out.println("Testing merge function operator option ===");
|
||||
Options opt = new Options();
|
||||
opt.setCreateIfMissing(true);
|
||||
|
||||
Options opt = new Options();
|
||||
opt.setCreateIfMissing(true);
|
||||
StringAppendOperator stringAppendOperator = new StringAppendOperator();
|
||||
opt.setMergeOperator(stringAppendOperator);
|
||||
|
||||
StringAppendOperator stringAppendOperator = new StringAppendOperator();
|
||||
opt.setMergeOperator(stringAppendOperator);
|
||||
RocksDB db = RocksDB.open(opt, db_path_string);
|
||||
|
||||
RocksDB db = RocksDB.open(opt, db_path_string);
|
||||
System.out.println("Writing aa under key...");
|
||||
db.put("key".getBytes(), "aa".getBytes());
|
||||
|
||||
System.out.println("Writing aa under key...");
|
||||
db.put("key".getBytes(), "aa".getBytes());
|
||||
System.out.println("Writing bb under key...");
|
||||
db.merge("key".getBytes(), "bb".getBytes());
|
||||
|
||||
System.out.println("Writing bb under key...");
|
||||
db.merge("key".getBytes(), "bb".getBytes());
|
||||
byte[] value = db.get("key".getBytes());
|
||||
String strValue = new String(value);
|
||||
|
||||
byte[] value = db.get("key".getBytes());
|
||||
String strValue = new String(value);
|
||||
System.out.println("Retrieved value: " + strValue);
|
||||
|
||||
System.out.println("Retrieved value: " + strValue);
|
||||
db.close();
|
||||
opt.dispose();
|
||||
|
||||
db.close();
|
||||
opt.dispose();
|
||||
assert(strValue.equals("aa,bb"));
|
||||
|
||||
assert(strValue.equals("aa,bb"));
|
||||
System.out.println("Merge function operator option passed!");
|
||||
}
|
||||
|
||||
System.out.println("Merge function operator option passed!");
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
throws InterruptedException, RocksDBException {
|
||||
testStringOption();
|
||||
testOperatorOption();
|
||||
|
||||
}
|
||||
public static void main(String[] args)
|
||||
throws InterruptedException, RocksDBException {
|
||||
testStringOption();
|
||||
testOperatorOption();
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,8 @@
|
||||
// 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.
|
||||
//
|
||||
// This file implements the "bridge" between Java and C++ for rocksdb::MergeOperator.
|
||||
// This file implements the "bridge" between Java and C++
|
||||
// for rocksdb::MergeOperator.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -27,9 +28,10 @@
|
||||
* Method: newMergeOperatorHandle
|
||||
* Signature: ()J
|
||||
*/
|
||||
jlong Java_org_rocksdb_StringAppendOperator_newMergeOperatorHandleImpl(JNIEnv* env, jobject jobj) {
|
||||
std::shared_ptr<rocksdb::MergeOperator> *op = new std::shared_ptr<rocksdb::MergeOperator>();
|
||||
jlong Java_org_rocksdb_StringAppendOperator_newMergeOperatorHandleImpl
|
||||
(JNIEnv* env, jobject jobj) {
|
||||
std::shared_ptr<rocksdb::MergeOperator> *op =
|
||||
new std::shared_ptr<rocksdb::MergeOperator>();
|
||||
*op = rocksdb::MergeOperators::CreateFromStringId("stringappend");
|
||||
return reinterpret_cast<jlong>(op);
|
||||
}
|
||||
|
||||
|
@ -1623,9 +1623,10 @@ void Java_org_rocksdb_Options_setMergeOperatorName(
|
||||
* Signature: (JJjava/lang/String)V
|
||||
*/
|
||||
void Java_org_rocksdb_Options_setMergeOperator(
|
||||
JNIEnv* env, jobject jobj, jlong jhandle, jlong mergeOperatorHandle) {
|
||||
JNIEnv* env, jobject jobj, jlong jhandle, jlong mergeOperatorHandle) {
|
||||
reinterpret_cast<rocksdb::Options*>(jhandle)->merge_operator =
|
||||
*(reinterpret_cast<std::shared_ptr<rocksdb::MergeOperator>*> (mergeOperatorHandle));
|
||||
*(reinterpret_cast<std::shared_ptr<rocksdb::MergeOperator>*>
|
||||
(mergeOperatorHandle));
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
@ -1784,4 +1785,3 @@ void Java_org_rocksdb_ReadOptions_setTailing(
|
||||
reinterpret_cast<rocksdb::ReadOptions*>(jhandle)->tailing =
|
||||
static_cast<bool>(jtailing);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user