]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/main/java/org/rocksdb/util/DirectBytewiseComparator.java
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
6 package org
.rocksdb
.util
;
8 import org
.rocksdb
.ComparatorOptions
;
9 import org
.rocksdb
.DirectComparator
;
10 import org
.rocksdb
.DirectSlice
;
12 import java
.nio
.ByteBuffer
;
15 * This is a Java Native implementation of the C++
16 * equivalent BytewiseComparatorImpl using {@link DirectSlice}
18 * The performance of Comparators implemented in Java is always
19 * less than their C++ counterparts due to the bridging overhead,
20 * as such you likely don't want to use this apart from benchmarking
21 * and you most likely instead wanted
22 * {@link org.rocksdb.BuiltinComparator#BYTEWISE_COMPARATOR}
24 public class DirectBytewiseComparator
extends DirectComparator
{
26 public DirectBytewiseComparator(final ComparatorOptions copt
) {
31 public String
name() {
32 return "rocksdb.java.DirectBytewiseComparator";
36 public int compare(final DirectSlice a
, final DirectSlice b
) {
37 return a
.data().compareTo(b
.data());
41 public String
findShortestSeparator(final String start
,
42 final DirectSlice limit
) {
43 final byte[] startBytes
= start
.getBytes();
45 // Find length of common prefix
46 final int min_length
= Math
.min(startBytes
.length
, limit
.size());
48 while ((diff_index
< min_length
) &&
49 (startBytes
[diff_index
] == limit
.get(diff_index
))) {
53 if (diff_index
>= min_length
) {
54 // Do not shorten if one string is a prefix of the other
56 final byte diff_byte
= startBytes
[diff_index
];
57 if(diff_byte
< 0xff && diff_byte
+ 1 < limit
.get(diff_index
)) {
58 final byte shortest
[] = new byte[diff_index
+ 1];
59 System
.arraycopy(startBytes
, 0, shortest
, 0, diff_index
+ 1);
60 shortest
[diff_index
]++;
61 assert(ByteBuffer
.wrap(shortest
).compareTo(limit
.data()) < 0);
62 return new String(shortest
);
70 public String
findShortSuccessor(final String key
) {
71 final byte[] keyBytes
= key
.getBytes();
73 // Find first character that can be incremented
74 final int n
= keyBytes
.length
;
75 for (int i
= 0; i
< n
; i
++) {
76 final byte byt
= keyBytes
[i
];
78 final byte shortSuccessor
[] = new byte[i
+ 1];
79 System
.arraycopy(keyBytes
, 0, shortSuccessor
, 0, i
+ 1);
81 return new String(shortSuccessor
);
84 // *key is a run of 0xffs. Leave it alone.