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 package org.rocksdb;
6 
7 /**
8  * This class controls the behaviour
9  * of Java implementations of
10  * AbstractComparator
11  *
12  * Note that dispose() must be called before a ComparatorOptions
13  * instance becomes out-of-scope to release the allocated memory in C++.
14  */
15 public class ComparatorOptions extends RocksObject {
ComparatorOptions()16   public ComparatorOptions() {
17     super(newComparatorOptions());
18   }
19 
20   /**
21    * Get the synchronisation type used to guard the reused buffers.
22    * Only used if {@link #maxReusedBufferSize()} > 0
23    * Default: {@link ReusedSynchronisationType#ADAPTIVE_MUTEX}
24    *
25    * @return the synchronisation type
26    */
reusedSynchronisationType()27   public ReusedSynchronisationType reusedSynchronisationType() {
28     assert(isOwningHandle());
29     return ReusedSynchronisationType.getReusedSynchronisationType(
30         reusedSynchronisationType(nativeHandle_));
31   }
32 
33   /**
34    * Set the synchronisation type used to guard the reused buffers.
35    * Only used if {@link #maxReusedBufferSize()} > 0
36    * Default: {@link ReusedSynchronisationType#ADAPTIVE_MUTEX}
37    *
38    * @param reusedSynchronisationType the synchronisation type
39    *
40    * @return the reference to the current comparator options.
41    */
setReusedSynchronisationType( final ReusedSynchronisationType reusedSynchronisationType)42   public ComparatorOptions setReusedSynchronisationType(
43       final ReusedSynchronisationType reusedSynchronisationType) {
44     assert (isOwningHandle());
45     setReusedSynchronisationType(nativeHandle_,
46         reusedSynchronisationType.getValue());
47     return this;
48   }
49 
50   /**
51    * Indicates if a direct byte buffer (i.e. outside of the normal
52    * garbage-collected heap) is used, as opposed to a non-direct byte buffer
53    * which is a wrapper around an on-heap byte[].
54    *
55    * Default: true
56    *
57    * @return true if a direct byte buffer will be used, false otherwise
58    */
useDirectBuffer()59   public boolean useDirectBuffer() {
60     assert(isOwningHandle());
61     return useDirectBuffer(nativeHandle_);
62   }
63 
64   /**
65    * Controls whether a direct byte buffer (i.e. outside of the normal
66    * garbage-collected heap) is used, as opposed to a non-direct byte buffer
67    * which is a wrapper around an on-heap byte[].
68    *
69    * Default: true
70    *
71    * @param useDirectBuffer true if a direct byte buffer should be used,
72    *     false otherwise
73    * @return the reference to the current comparator options.
74    */
setUseDirectBuffer(final boolean useDirectBuffer)75   public ComparatorOptions setUseDirectBuffer(final boolean useDirectBuffer) {
76     assert(isOwningHandle());
77     setUseDirectBuffer(nativeHandle_, useDirectBuffer);
78     return this;
79   }
80 
81   /**
82    * Maximum size of a buffer (in bytes) that will be reused.
83    * Comparators will use 5 of these buffers,
84    * so the retained memory size will be 5 * max_reused_buffer_size.
85    * When a buffer is needed for transferring data to a callback,
86    * if it requires less than {@code maxReuseBufferSize}, then an
87    * existing buffer will be reused, else a new buffer will be
88    * allocated just for that callback.
89    *
90    * Default: 64 bytes
91    *
92    * @return the maximum size of a buffer which is reused,
93    *     or 0 if reuse is disabled
94    */
maxReusedBufferSize()95   public int maxReusedBufferSize() {
96     assert(isOwningHandle());
97     return maxReusedBufferSize(nativeHandle_);
98   }
99 
100   /**
101    * Sets the maximum size of a buffer (in bytes) that will be reused.
102    * Comparators will use 5 of these buffers,
103    * so the retained memory size will be 5 * max_reused_buffer_size.
104    * When a buffer is needed for transferring data to a callback,
105    * if it requires less than {@code maxReuseBufferSize}, then an
106    * existing buffer will be reused, else a new buffer will be
107    * allocated just for that callback.
108    *
109    * Default: 64 bytes
110    *
111    * @param maxReusedBufferSize the maximum size for a buffer to reuse, or 0 to
112    *     disable reuse
113    *
114    * @return the maximum size of a buffer which is reused
115    */
setMaxReusedBufferSize(final int maxReusedBufferSize)116   public ComparatorOptions setMaxReusedBufferSize(final int maxReusedBufferSize) {
117     assert(isOwningHandle());
118     setMaxReusedBufferSize(nativeHandle_, maxReusedBufferSize);
119     return this;
120   }
121 
newComparatorOptions()122   private native static long newComparatorOptions();
reusedSynchronisationType(final long handle)123   private native byte reusedSynchronisationType(final long handle);
setReusedSynchronisationType(final long handle, final byte reusedSynchronisationType)124   private native void setReusedSynchronisationType(final long handle,
125       final byte reusedSynchronisationType);
useDirectBuffer(final long handle)126   private native boolean useDirectBuffer(final long handle);
setUseDirectBuffer(final long handle, final boolean useDirectBuffer)127   private native void setUseDirectBuffer(final long handle,
128       final boolean useDirectBuffer);
maxReusedBufferSize(final long handle)129   private native int maxReusedBufferSize(final long handle);
setMaxReusedBufferSize(final long handle, final int maxReuseBufferSize)130   private native void setMaxReusedBufferSize(final long handle,
131       final int maxReuseBufferSize);
disposeInternal(final long handle)132   @Override protected final native void disposeInternal(final long handle);
133 }
134