Skip to content

Commit 5340484

Browse files
committed
Built-in comparator(s) in RocksJava
Extended Built-in comparators with ReverseBytewiseComparator. Reverse key handling is under certain conditions essential. E.g. while using timestamp versioned data. As native-comparators were not available using JAVA-API. Both built-in comparators were exposed via JNI to be set upon database creation time.
1 parent d439451 commit 5340484

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

include/rocksdb/comparator.h

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ class Comparator {
6262
// must not be deleted.
6363
extern const Comparator* BytewiseComparator();
6464

65+
// Return a builtin comparator that uses reverse lexicographic byte-wise
66+
// ordering.
67+
extern const Comparator* ReverseBytewiseComparator();
68+
6569
} // namespace rocksdb
6670

6771
#endif // STORAGE_ROCKSDB_INCLUDE_COMPARATOR_H_

java/org/rocksdb/Options.java

+23
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ public class Options extends RocksObject {
1818
}
1919
static final long DEFAULT_CACHE_SIZE = 8 << 20;
2020
static final int DEFAULT_NUM_SHARD_BITS = -1;
21+
22+
/**
23+
* Builtin RocksDB comparators
24+
*/
25+
public enum BuiltinComparator {
26+
BYTEWISE_COMPARATOR, REVERSE_BYTEWISE_COMPARATOR;
27+
}
28+
2129
/**
2230
* Construct options for opening a RocksDB.
2331
*
@@ -78,6 +86,21 @@ public boolean createIfMissing() {
7886
return createIfMissing(nativeHandle_);
7987
}
8088

89+
/**
90+
* Set BuiltinComparator to be used with RocksDB.
91+
*
92+
* Note: Comparator can be set once upon database creation.
93+
*
94+
* Default: BytewiseComparator.
95+
* @param builtinComparator a BuiltinComparator type.
96+
*/
97+
public void setBuiltinComparator(BuiltinComparator builtinComparator) {
98+
assert(isInitialized());
99+
setBuiltinComparator(nativeHandle_, builtinComparator.ordinal());
100+
}
101+
102+
private native void setBuiltinComparator(long handle, int builtinComparator);
103+
81104
/**
82105
* Amount of data to build up in memory (backed by an unsorted log
83106
* on disk) before converting to a sorted on-disk file.

java/rocksjni/options.cc

+18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "rocksdb/table.h"
2323
#include "rocksdb/slice_transform.h"
2424
#include "rocksdb/rate_limiter.h"
25+
#include "rocksdb/comparator.h"
2526

2627
/*
2728
* Class: org_rocksdb_Options
@@ -63,6 +64,23 @@ jboolean Java_org_rocksdb_Options_createIfMissing(
6364
return reinterpret_cast<rocksdb::Options*>(jhandle)->create_if_missing;
6465
}
6566

67+
/*
68+
* Class: org_rocksdb_Options
69+
* Method: useReverseBytewiseComparator
70+
* Signature: (JI)V
71+
*/
72+
void Java_org_rocksdb_Options_setBuiltinComparator(
73+
JNIEnv* env, jobject jobj, jlong jhandle, jint builtinComparator) {
74+
switch (builtinComparator){
75+
case 1:
76+
reinterpret_cast<rocksdb::Options*>(jhandle)->comparator = rocksdb::ReverseBytewiseComparator();
77+
break;
78+
default:
79+
reinterpret_cast<rocksdb::Options*>(jhandle)->comparator = rocksdb::BytewiseComparator();
80+
break;
81+
}
82+
}
83+
6684
/*
6785
* Class: org_rocksdb_Options
6886
* Method: setWriteBufferSize

util/comparator.cc

+22-1
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,39 @@ class BytewiseComparatorImpl : public Comparator {
6969
// *key is a run of 0xffs. Leave it alone.
7070
}
7171
};
72-
} // namespace
72+
73+
class ReverseBytewiseComparatorImpl : public BytewiseComparatorImpl {
74+
public:
75+
ReverseBytewiseComparatorImpl() { }
76+
77+
virtual const char* Name() const {
78+
return "leveldb.ReverseBytewiseComparator";
79+
}
80+
81+
virtual int Compare(const Slice& a, const Slice& b) const {
82+
return -a.compare(b);
83+
}
84+
};
85+
86+
}// namespace
7387

7488
static port::OnceType once = LEVELDB_ONCE_INIT;
7589
static const Comparator* bytewise;
90+
static const Comparator* rbytewise;
7691

7792
static void InitModule() {
7893
bytewise = new BytewiseComparatorImpl;
94+
rbytewise= new ReverseBytewiseComparatorImpl;
7995
}
8096

8197
const Comparator* BytewiseComparator() {
8298
port::InitOnce(&once, InitModule);
8399
return bytewise;
84100
}
85101

102+
const Comparator* ReverseBytewiseComparator() {
103+
port::InitOnce(&once, InitModule);
104+
return rbytewise;
105+
}
106+
86107
} // namespace rocksdb

0 commit comments

Comments
 (0)