Skip to content

Commit

Permalink
Merge pull request XRPLF#316 from fyrz/ReverseBytewiseComparator
Browse files Browse the repository at this point in the history
Built-in comparator(s) in RocksJava
  • Loading branch information
igorcanadi committed Sep 29, 2014
2 parents 2dc6f62 + 8b8011a commit abd70c5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
4 changes: 4 additions & 0 deletions include/rocksdb/comparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class Comparator {
// must not be deleted.
extern const Comparator* BytewiseComparator();

// Return a builtin comparator that uses reverse lexicographic byte-wise
// ordering.
extern const Comparator* ReverseBytewiseComparator();

} // namespace rocksdb

#endif // STORAGE_ROCKSDB_INCLUDE_COMPARATOR_H_
23 changes: 23 additions & 0 deletions java/org/rocksdb/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public class Options extends RocksObject {
}
static final long DEFAULT_CACHE_SIZE = 8 << 20;
static final int DEFAULT_NUM_SHARD_BITS = -1;

/**
* Builtin RocksDB comparators
*/
public enum BuiltinComparator {
BYTEWISE_COMPARATOR, REVERSE_BYTEWISE_COMPARATOR;
}

/**
* Construct options for opening a RocksDB.
*
Expand Down Expand Up @@ -78,6 +86,21 @@ public boolean createIfMissing() {
return createIfMissing(nativeHandle_);
}

/**
* Set BuiltinComparator to be used with RocksDB.
*
* Note: Comparator can be set once upon database creation.
*
* Default: BytewiseComparator.
* @param builtinComparator a BuiltinComparator type.
*/
public void setBuiltinComparator(BuiltinComparator builtinComparator) {
assert(isInitialized());
setBuiltinComparator(nativeHandle_, builtinComparator.ordinal());
}

private native void setBuiltinComparator(long handle, int builtinComparator);

/**
* Amount of data to build up in memory (backed by an unsorted log
* on disk) before converting to a sorted on-disk file.
Expand Down
18 changes: 18 additions & 0 deletions java/rocksjni/options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "rocksdb/table.h"
#include "rocksdb/slice_transform.h"
#include "rocksdb/rate_limiter.h"
#include "rocksdb/comparator.h"

/*
* Class: org_rocksdb_Options
Expand Down Expand Up @@ -63,6 +64,23 @@ jboolean Java_org_rocksdb_Options_createIfMissing(
return reinterpret_cast<rocksdb::Options*>(jhandle)->create_if_missing;
}

/*
* Class: org_rocksdb_Options
* Method: useReverseBytewiseComparator
* Signature: (JI)V
*/
void Java_org_rocksdb_Options_setBuiltinComparator(
JNIEnv* env, jobject jobj, jlong jhandle, jint builtinComparator) {
switch (builtinComparator){
case 1:
reinterpret_cast<rocksdb::Options*>(jhandle)->comparator = rocksdb::ReverseBytewiseComparator();
break;
default:
reinterpret_cast<rocksdb::Options*>(jhandle)->comparator = rocksdb::BytewiseComparator();
break;
}
}

/*
* Class: org_rocksdb_Options
* Method: setWriteBufferSize
Expand Down
23 changes: 22 additions & 1 deletion util/comparator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,39 @@ class BytewiseComparatorImpl : public Comparator {
// *key is a run of 0xffs. Leave it alone.
}
};
} // namespace

class ReverseBytewiseComparatorImpl : public BytewiseComparatorImpl {
public:
ReverseBytewiseComparatorImpl() { }

virtual const char* Name() const {
return "rocksdb.ReverseBytewiseComparator";
}

virtual int Compare(const Slice& a, const Slice& b) const {
return -a.compare(b);
}
};

}// namespace

static port::OnceType once = LEVELDB_ONCE_INIT;
static const Comparator* bytewise;
static const Comparator* rbytewise;

static void InitModule() {
bytewise = new BytewiseComparatorImpl;
rbytewise= new ReverseBytewiseComparatorImpl;
}

const Comparator* BytewiseComparator() {
port::InitOnce(&once, InitModule);
return bytewise;
}

const Comparator* ReverseBytewiseComparator() {
port::InitOnce(&once, InitModule);
return rbytewise;
}

} // namespace rocksdb

0 comments on commit abd70c5

Please sign in to comment.