]>
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 | ||
8 | import java.nio.ByteBuffer; | |
9 | ||
10 | /** | |
11 | * Base class for slices which will receive direct | |
12 | * ByteBuffer based access to the underlying data. | |
13 | * | |
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 | |
17 | */ | |
18 | public class DirectSlice extends AbstractSlice<ByteBuffer> { | |
19 | public final static DirectSlice NONE = new DirectSlice(); | |
20 | ||
21 | /** | |
22 | * Indicates whether we have to free the memory pointed to by the Slice | |
23 | */ | |
24 | private final boolean internalBuffer; | |
25 | private volatile boolean cleared = false; | |
26 | private volatile long internalBufferOffset = 0; | |
27 | ||
28 | /** | |
29 | * Called from JNI to construct a new Java DirectSlice | |
30 | * without an underlying C++ object set | |
31 | * at creation time. | |
32 | * | |
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. | |
39 | */ | |
40 | DirectSlice() { | |
41 | super(); | |
42 | this.internalBuffer = false; | |
43 | } | |
44 | ||
45 | /** | |
46 | * Constructs a slice | |
47 | * where the data is taken from | |
48 | * a String. | |
49 | * | |
50 | * @param str The string | |
51 | */ | |
52 | public DirectSlice(final String str) { | |
53 | super(createNewSliceFromString(str)); | |
54 | this.internalBuffer = true; | |
55 | } | |
56 | ||
57 | /** | |
58 | * Constructs a slice where the data is | |
59 | * read from the provided | |
60 | * ByteBuffer up to a certain length | |
61 | * | |
62 | * @param data The buffer containing the data | |
63 | * @param length The length of the data to use for the slice | |
64 | */ | |
65 | public DirectSlice(final ByteBuffer data, final int length) { | |
66 | super(createNewDirectSlice0(ensureDirect(data), length)); | |
67 | this.internalBuffer = false; | |
68 | } | |
69 | ||
70 | /** | |
71 | * Constructs a slice where the data is | |
72 | * read from the provided | |
73 | * ByteBuffer | |
74 | * | |
75 | * @param data The bugger containing the data | |
76 | */ | |
77 | public DirectSlice(final ByteBuffer data) { | |
78 | super(createNewDirectSlice1(ensureDirect(data))); | |
79 | this.internalBuffer = false; | |
80 | } | |
81 | ||
82 | private static ByteBuffer ensureDirect(final ByteBuffer data) { | |
83 | if(!data.isDirect()) { | |
84 | throw new IllegalArgumentException("The ByteBuffer must be direct"); | |
85 | } | |
86 | return data; | |
87 | } | |
88 | ||
89 | /** | |
90 | * Retrieves the byte at a specific offset | |
91 | * from the underlying data | |
92 | * | |
93 | * @param offset The (zero-based) offset of the byte to retrieve | |
94 | * | |
95 | * @return the requested byte | |
96 | */ | |
97 | public byte get(final int offset) { | |
98 | return get0(getNativeHandle(), offset); | |
99 | } | |
100 | ||
101 | @Override | |
102 | public void clear() { | |
103 | clear0(getNativeHandle(), !cleared && internalBuffer, internalBufferOffset); | |
104 | cleared = true; | |
105 | } | |
106 | ||
107 | @Override | |
108 | public void removePrefix(final int n) { | |
109 | removePrefix0(getNativeHandle(), n); | |
110 | this.internalBufferOffset += n; | |
111 | } | |
112 | ||
1e59de90 TL |
113 | public void setLength(final int n) { |
114 | setLength0(getNativeHandle(), n); | |
115 | } | |
116 | ||
7c673cae FG |
117 | @Override |
118 | protected void disposeInternal() { | |
119 | final long nativeHandle = getNativeHandle(); | |
120 | if(!cleared && internalBuffer) { | |
121 | disposeInternalBuf(nativeHandle, internalBufferOffset); | |
122 | } | |
123 | disposeInternal(nativeHandle); | |
124 | } | |
125 | ||
126 | private native static long createNewDirectSlice0(final ByteBuffer data, | |
127 | final int length); | |
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); | |
1e59de90 | 134 | private native void setLength0(long handle, int length); |
7c673cae FG |
135 | private native void disposeInternalBuf(final long handle, |
136 | long internalBufferOffset); | |
137 | } |