]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
5 | |
6 | package org.rocksdb; | |
7 | ||
f67539c2 TL |
8 | import java.nio.ByteBuffer; |
9 | ||
7c673cae FG |
10 | /** |
11 | * Comparators are used by RocksDB to determine | |
12 | * the ordering of keys. | |
13 | * | |
f67539c2 | 14 | * Implementations of Comparators in Java should extend this class. |
7c673cae | 15 | */ |
f67539c2 | 16 | public abstract class AbstractComparator |
11fdf7f2 | 17 | extends RocksCallbackObject { |
7c673cae | 18 | |
f67539c2 | 19 | AbstractComparator() { |
11fdf7f2 | 20 | super(); |
7c673cae FG |
21 | } |
22 | ||
11fdf7f2 TL |
23 | protected AbstractComparator(final ComparatorOptions copt) { |
24 | super(copt.nativeHandle_); | |
25 | } | |
26 | ||
f67539c2 TL |
27 | @Override |
28 | protected long initializeNative(final long... nativeParameterHandles) { | |
29 | return createNewComparator(nativeParameterHandles[0]); | |
30 | } | |
31 | ||
11fdf7f2 TL |
32 | /** |
33 | * Get the type of this comparator. | |
34 | * | |
35 | * Used for determining the correct C++ cast in native code. | |
36 | * | |
37 | * @return The type of the comparator. | |
38 | */ | |
f67539c2 TL |
39 | ComparatorType getComparatorType() { |
40 | return ComparatorType.JAVA_COMPARATOR; | |
41 | } | |
11fdf7f2 | 42 | |
7c673cae FG |
43 | /** |
44 | * The name of the comparator. Used to check for comparator | |
45 | * mismatches (i.e., a DB created with one comparator is | |
46 | * accessed using a different comparator). | |
47 | * | |
48 | * A new name should be used whenever | |
49 | * the comparator implementation changes in a way that will cause | |
50 | * the relative ordering of any two keys to change. | |
51 | * | |
52 | * Names starting with "rocksdb." are reserved and should not be used. | |
53 | * | |
54 | * @return The name of this comparator implementation | |
55 | */ | |
56 | public abstract String name(); | |
57 | ||
58 | /** | |
f67539c2 TL |
59 | * Three-way key comparison. Implementations should provide a |
60 | * <a href="https://en.wikipedia.org/wiki/Total_order">total order</a> | |
61 | * on keys that might be passed to it. | |
62 | * | |
63 | * The implementation may modify the {@code ByteBuffer}s passed in, though | |
64 | * it would be unconventional to modify the "limit" or any of the | |
65 | * underlying bytes. As a callback, RocksJava will ensure that {@code a} | |
66 | * is a different instance from {@code b}. | |
7c673cae | 67 | * |
f67539c2 TL |
68 | * @param a buffer containing the first key in its "remaining" elements |
69 | * @param b buffer containing the second key in its "remaining" elements | |
7c673cae | 70 | * |
f67539c2 | 71 | * @return Should return either: |
7c673cae FG |
72 | * 1) < 0 if "a" < "b" |
73 | * 2) == 0 if "a" == "b" | |
74 | * 3) > 0 if "a" > "b" | |
75 | */ | |
f67539c2 | 76 | public abstract int compare(final ByteBuffer a, final ByteBuffer b); |
7c673cae FG |
77 | |
78 | /** | |
79 | * <p>Used to reduce the space requirements | |
80 | * for internal data structures like index blocks.</p> | |
81 | * | |
f67539c2 | 82 | * <p>If start < limit, you may modify start which is a |
7c673cae FG |
83 | * shorter string in [start, limit).</p> |
84 | * | |
f67539c2 TL |
85 | * If you modify start, it is expected that you set the byte buffer so that |
86 | * a subsequent read of start.remaining() bytes from start.position() | |
87 | * to start.limit() will obtain the new start value. | |
7c673cae | 88 | * |
f67539c2 TL |
89 | * <p>Simple comparator implementations may return with start unchanged. |
90 | * i.e., an implementation of this method that does nothing is correct.</p> | |
7c673cae | 91 | * |
f67539c2 TL |
92 | * @param start the start |
93 | * @param limit the limit | |
7c673cae | 94 | */ |
f67539c2 TL |
95 | public void findShortestSeparator(final ByteBuffer start, |
96 | final ByteBuffer limit) { | |
97 | // no-op | |
7c673cae FG |
98 | } |
99 | ||
100 | /** | |
101 | * <p>Used to reduce the space requirements | |
102 | * for internal data structures like index blocks.</p> | |
103 | * | |
f67539c2 | 104 | * <p>You may change key to a shorter key (key1) where |
7c673cae FG |
105 | * key1 ≥ key.</p> |
106 | * | |
f67539c2 TL |
107 | * <p>Simple comparator implementations may return the key unchanged. |
108 | * i.e., an implementation of | |
7c673cae FG |
109 | * this method that does nothing is correct.</p> |
110 | * | |
f67539c2 | 111 | * @param key the key |
7c673cae | 112 | */ |
f67539c2 TL |
113 | public void findShortSuccessor(final ByteBuffer key) { |
114 | // no-op | |
115 | } | |
116 | ||
117 | public final boolean usingDirectBuffers() { | |
118 | return usingDirectBuffers(nativeHandle_); | |
7c673cae | 119 | } |
f67539c2 TL |
120 | |
121 | private native boolean usingDirectBuffers(final long nativeHandle); | |
122 | ||
123 | private native long createNewComparator(final long comparatorOptionsHandle); | |
7c673cae | 124 | } |