]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/main/java/org/rocksdb/DirectSlice.java
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 * Base class for slices which will receive direct
12 * ByteBuffer based access to the underlying data.
14 * ByteBuffer backed slices typically perform better with
15 * larger keys and values. When using smaller keys and
16 * values consider using @see org.rocksdb.Slice
18 public class DirectSlice
extends AbstractSlice
<ByteBuffer
> {
19 public final static DirectSlice NONE
= new DirectSlice();
22 * Indicates whether we have to free the memory pointed to by the Slice
24 private final boolean internalBuffer
;
25 private volatile boolean cleared
= false;
26 private volatile long internalBufferOffset
= 0;
29 * Called from JNI to construct a new Java DirectSlice
30 * without an underlying C++ object set
33 * Note: You should be aware that it is intentionally marked as
34 * package-private. This is so that developers cannot construct their own
35 * default DirectSlice objects (at present). As developers cannot construct
36 * their own DirectSlice objects through this, they are not creating
37 * underlying C++ DirectSlice objects, and so there is nothing to free
38 * (dispose) from Java.
42 this.internalBuffer
= false;
47 * where the data is taken from
50 * @param str The string
52 public DirectSlice(final String str
) {
53 super(createNewSliceFromString(str
));
54 this.internalBuffer
= true;
58 * Constructs a slice where the data is
59 * read from the provided
60 * ByteBuffer up to a certain length
62 * @param data The buffer containing the data
63 * @param length The length of the data to use for the slice
65 public DirectSlice(final ByteBuffer data
, final int length
) {
66 super(createNewDirectSlice0(ensureDirect(data
), length
));
67 this.internalBuffer
= false;
71 * Constructs a slice where the data is
72 * read from the provided
75 * @param data The bugger containing the data
77 public DirectSlice(final ByteBuffer data
) {
78 super(createNewDirectSlice1(ensureDirect(data
)));
79 this.internalBuffer
= false;
82 private static ByteBuffer
ensureDirect(final ByteBuffer data
) {
83 if(!data
.isDirect()) {
84 throw new IllegalArgumentException("The ByteBuffer must be direct");
90 * Retrieves the byte at a specific offset
91 * from the underlying data
93 * @param offset The (zero-based) offset of the byte to retrieve
95 * @return the requested byte
97 public byte get(final int offset
) {
98 return get0(getNativeHandle(), offset
);
102 public void clear() {
103 clear0(getNativeHandle(), !cleared
&& internalBuffer
, internalBufferOffset
);
108 public void removePrefix(final int n
) {
109 removePrefix0(getNativeHandle(), n
);
110 this.internalBufferOffset
+= n
;
113 public void setLength(final int n
) {
114 setLength0(getNativeHandle(), n
);
118 protected void disposeInternal() {
119 final long nativeHandle
= getNativeHandle();
120 if(!cleared
&& internalBuffer
) {
121 disposeInternalBuf(nativeHandle
, internalBufferOffset
);
123 disposeInternal(nativeHandle
);
126 private native static long createNewDirectSlice0(final ByteBuffer data
,
128 private native static long createNewDirectSlice1(final ByteBuffer data
);
129 @Override protected final native ByteBuffer
data0(long handle
);
130 private native byte get0(long handle
, int offset
);
131 private native void clear0(long handle
, boolean internalBuffer
,
132 long internalBufferOffset
);
133 private native void removePrefix0(long handle
, int length
);
134 private native void setLength0(long handle
, int length
);
135 private native void disposeInternalBuf(final long handle
,
136 long internalBufferOffset
);