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).
5 
6 package org.rocksdb;
7 
8 import java.nio.ByteBuffer;
9 
10 /**
11  * This class is intentionally private,
12  * it holds methods which are called
13  * from C++ to interact with a Comparator
14  * written in Java.
15  *
16  * Placing these bridge methods in this
17  * class keeps the API of the
18  * {@link org.rocksdb.AbstractComparator} clean.
19  */
20 class AbstractComparatorJniBridge {
21 
22     /**
23      * Only called from JNI.
24      *
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.
29      *
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}
38      *
39      * @return the result of the comparison
40      */
compareInternal( final AbstractComparator comparator, final ByteBuffer a, final int aLen, final ByteBuffer b, final int bLen)41     private static int compareInternal(
42             final AbstractComparator comparator,
43             final ByteBuffer a, final int aLen,
44             final ByteBuffer b, final int bLen) {
45         if (aLen != -1) {
46             a.mark();
47             a.limit(aLen);
48         }
49         if (bLen != -1) {
50             b.mark();
51             b.limit(bLen);
52         }
53 
54         final int c = comparator.compare(a, b);
55 
56         if (aLen != -1) {
57             a.reset();
58         }
59         if (bLen != -1) {
60             b.reset();
61         }
62 
63         return c;
64     }
65 
66     /**
67      * Only called from JNI.
68      *
69      * Simply a bridge to calling
70      * {@link AbstractComparator#findShortestSeparator(ByteBuffer, ByteBuffer)},
71      * which ensures that the byte buffer lengths are correct
72      * before the call.
73      *
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}
82      *
83      * @return either {@code startLen} if the start key is unchanged, otherwise
84      *     the new length of the start key
85      */
findShortestSeparatorInternal( final AbstractComparator comparator, final ByteBuffer start, final int startLen, final ByteBuffer limit, final int limitLen)86     private static int findShortestSeparatorInternal(
87             final AbstractComparator comparator,
88             final ByteBuffer start, final int startLen,
89             final ByteBuffer limit, final int limitLen) {
90         if (startLen != -1) {
91             start.limit(startLen);
92         }
93         if (limitLen != -1) {
94             limit.limit(limitLen);
95         }
96         comparator.findShortestSeparator(start, limit);
97         return start.remaining();
98     }
99 
100     /**
101      * Only called from JNI.
102      *
103      * Simply a bridge to calling
104      * {@link AbstractComparator#findShortestSeparator(ByteBuffer, ByteBuffer)},
105      * which ensures that the byte buffer length is correct
106      * before the call.
107      *
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}
113      *
114      * @return either keyLen if the key is unchanged, otherwise the new length of the key
115      */
findShortSuccessorInternal( final AbstractComparator comparator, final ByteBuffer key, final int keyLen)116     private static int findShortSuccessorInternal(
117             final AbstractComparator comparator,
118             final ByteBuffer key, final int keyLen) {
119         if (keyLen != -1) {
120             key.limit(keyLen);
121         }
122         comparator.findShortSuccessor(key);
123         return key.remaining();
124     }
125 }
126