1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
8 import java
.nio
.ByteBuffer
;
11 * This class is intentionally private,
12 * it holds methods which are called
13 * from C++ to interact with a Comparator
16 * Placing these bridge methods in this
17 * class keeps the API of the
18 * {@link org.rocksdb.AbstractComparator} clean.
20 class AbstractComparatorJniBridge
{
23 * Only called from JNI.
25 * Simply a bridge to calling
26 * {@link AbstractComparator#compare(ByteBuffer, ByteBuffer)},
27 * which ensures that the byte buffer lengths are correct
28 * before and after the call.
30 * @param comparator the comparator object on which to
31 * call {@link AbstractComparator#compare(ByteBuffer, ByteBuffer)}
32 * @param a buffer access to first key
33 * @param aLen the length of the a key,
34 * may be smaller than the buffer {@code a}
35 * @param b buffer access to second key
36 * @param bLen the length of the b key,
37 * may be smaller than the buffer {@code b}
39 * @return the result of the comparison
41 private static int compareInternal(
42 final AbstractComparator comparator
,
43 final ByteBuffer a
, final int aLen
,
44 final ByteBuffer b
, final int bLen
) {
54 final int c
= comparator
.compare(a
, b
);
67 * Only called from JNI.
69 * Simply a bridge to calling
70 * {@link AbstractComparator#findShortestSeparator(ByteBuffer, ByteBuffer)},
71 * which ensures that the byte buffer lengths are correct
74 * @param comparator the comparator object on which to
75 * call {@link AbstractComparator#findShortestSeparator(ByteBuffer, ByteBuffer)}
76 * @param start buffer access to the start key
77 * @param startLen the length of the start key,
78 * may be smaller than the buffer {@code start}
79 * @param limit buffer access to the limit key
80 * @param limitLen the length of the limit key,
81 * may be smaller than the buffer {@code limit}
83 * @return either {@code startLen} if the start key is unchanged, otherwise
84 * the new length of the start key
86 private static int findShortestSeparatorInternal(
87 final AbstractComparator comparator
,
88 final ByteBuffer start
, final int startLen
,
89 final ByteBuffer limit
, final int limitLen
) {
91 start
.limit(startLen
);
94 limit
.limit(limitLen
);
96 comparator
.findShortestSeparator(start
, limit
);
97 return start
.remaining();
101 * Only called from JNI.
103 * Simply a bridge to calling
104 * {@link AbstractComparator#findShortestSeparator(ByteBuffer, ByteBuffer)},
105 * which ensures that the byte buffer length is correct
108 * @param comparator the comparator object on which to
109 * call {@link AbstractComparator#findShortSuccessor(ByteBuffer)}
110 * @param key buffer access to the key
111 * @param keyLen the length of the key,
112 * may be smaller than the buffer {@code key}
114 * @return either keyLen if the key is unchanged, otherwise the new length of the key
116 private static int findShortSuccessorInternal(
117 final AbstractComparator comparator
,
118 final ByteBuffer key
, final int keyLen
) {
122 comparator
.findShortSuccessor(key
);
123 return key
.remaining();