]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/main/java/org/rocksdb/WriteBatch.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 * WriteBatch holds a collection of updates to apply atomically to a DB.
13 * The updates are applied in the order in which they are added
14 * to the WriteBatch. For example, the value of "key" will be "v3"
15 * after the following batch is written:
17 * batch.put("key", "v1");
18 * batch.remove("key");
19 * batch.put("key", "v2");
20 * batch.put("key", "v3");
22 * Multiple threads can invoke const methods on a WriteBatch without
23 * external synchronization, but if any of the threads may call a
24 * non-const method, all threads accessing the same WriteBatch must use
25 * external synchronization.
27 public class WriteBatch
extends AbstractWriteBatch
{
29 * Constructs a WriteBatch instance.
36 * Constructs a WriteBatch instance with a given size.
38 * @param reserved_bytes reserved size for WriteBatch
40 public WriteBatch(final int reserved_bytes
) {
41 super(newWriteBatch(reserved_bytes
));
45 * Constructs a WriteBatch instance from a serialized representation
46 * as returned by {@link #data()}.
48 * @param serialized the serialized representation.
50 public WriteBatch(final byte[] serialized
) {
51 super(newWriteBatch(serialized
, serialized
.length
));
55 * Support for iterating over the contents of a batch.
57 * @param handler A handler that is called back for each
58 * update present in the batch
60 * @throws RocksDBException If we cannot iterate over the batch
62 public void iterate(final Handler handler
) throws RocksDBException
{
63 iterate(nativeHandle_
, handler
.nativeHandle_
);
67 * Retrieve the serialized version of this batch.
69 * @return the serialized representation of this write batch.
71 * @throws RocksDBException if an error occurs whilst retrieving
72 * the serialized batch data.
74 public byte[] data() throws RocksDBException
{
75 return data(nativeHandle_
);
79 * Retrieve data size of the batch.
81 * @return the serialized data size of the batch.
83 public long getDataSize() {
84 return getDataSize(nativeHandle_
);
88 * Returns true if Put will be called during Iterate.
90 * @return true if Put will be called during Iterate.
92 public boolean hasPut() {
93 return hasPut(nativeHandle_
);
97 * Returns true if Delete will be called during Iterate.
99 * @return true if Delete will be called during Iterate.
101 public boolean hasDelete() {
102 return hasDelete(nativeHandle_
);
106 * Returns true if SingleDelete will be called during Iterate.
108 * @return true if SingleDelete will be called during Iterate.
110 public boolean hasSingleDelete() {
111 return hasSingleDelete(nativeHandle_
);
115 * Returns true if DeleteRange will be called during Iterate.
117 * @return true if DeleteRange will be called during Iterate.
119 public boolean hasDeleteRange() {
120 return hasDeleteRange(nativeHandle_
);
124 * Returns true if Merge will be called during Iterate.
126 * @return true if Merge will be called during Iterate.
128 public boolean hasMerge() {
129 return hasMerge(nativeHandle_
);
133 * Returns true if MarkBeginPrepare will be called during Iterate.
135 * @return true if MarkBeginPrepare will be called during Iterate.
137 public boolean hasBeginPrepare() {
138 return hasBeginPrepare(nativeHandle_
);
142 * Returns true if MarkEndPrepare will be called during Iterate.
144 * @return true if MarkEndPrepare will be called during Iterate.
146 public boolean hasEndPrepare() {
147 return hasEndPrepare(nativeHandle_
);
151 * Returns true if MarkCommit will be called during Iterate.
153 * @return true if MarkCommit will be called during Iterate.
155 public boolean hasCommit() {
156 return hasCommit(nativeHandle_
);
160 * Returns true if MarkRollback will be called during Iterate.
162 * @return true if MarkRollback will be called during Iterate.
164 public boolean hasRollback() {
165 return hasRollback(nativeHandle_
);
169 public WriteBatch
getWriteBatch() {
174 * Marks this point in the WriteBatch as the last record to
175 * be inserted into the WAL, provided the WAL is enabled.
177 public void markWalTerminationPoint() {
178 markWalTerminationPoint(nativeHandle_
);
182 * Gets the WAL termination point.
184 * See {@link #markWalTerminationPoint()}
186 * @return the WAL termination point
188 public SavePoint
getWalTerminationPoint() {
189 return getWalTerminationPoint(nativeHandle_
);
193 WriteBatch
getWriteBatch(final long handle
) {
198 * <p>Private WriteBatch constructor which is used to construct
199 * WriteBatch instances from C++ side. As the reference to this
200 * object is also managed from C++ side the handle will be disowned.</p>
202 * @param nativeHandle address of native instance.
204 WriteBatch(final long nativeHandle
) {
205 this(nativeHandle
, false);
209 * <p>Private WriteBatch constructor which is used to construct
210 * WriteBatch instances. </p>
212 * @param nativeHandle address of native instance.
213 * @param owningNativeHandle whether to own this reference from the C++ side or not
215 WriteBatch(final long nativeHandle
, final boolean owningNativeHandle
) {
217 if(!owningNativeHandle
)
218 disOwnNativeHandle();
221 @Override protected final native void disposeInternal(final long handle
);
222 @Override final native int count0(final long handle
);
223 @Override final native void put(final long handle
, final byte[] key
,
224 final int keyLen
, final byte[] value
, final int valueLen
);
225 @Override final native void put(final long handle
, final byte[] key
,
226 final int keyLen
, final byte[] value
, final int valueLen
,
227 final long cfHandle
);
229 final native void putDirect(final long handle
, final ByteBuffer key
, final int keyOffset
,
230 final int keyLength
, final ByteBuffer value
, final int valueOffset
, final int valueLength
,
231 final long cfHandle
);
232 @Override final native void merge(final long handle
, final byte[] key
,
233 final int keyLen
, final byte[] value
, final int valueLen
);
234 @Override final native void merge(final long handle
, final byte[] key
,
235 final int keyLen
, final byte[] value
, final int valueLen
,
236 final long cfHandle
);
237 @Override final native void delete(final long handle
, final byte[] key
,
238 final int keyLen
) throws RocksDBException
;
239 @Override final native void delete(final long handle
, final byte[] key
,
240 final int keyLen
, final long cfHandle
) throws RocksDBException
;
241 @Override final native void singleDelete(final long handle
, final byte[] key
,
242 final int keyLen
) throws RocksDBException
;
243 @Override final native void singleDelete(final long handle
, final byte[] key
,
244 final int keyLen
, final long cfHandle
) throws RocksDBException
;
246 final native void deleteDirect(final long handle
, final ByteBuffer key
, final int keyOffset
,
247 final int keyLength
, final long cfHandle
) throws RocksDBException
;
249 final native void deleteRange(final long handle
, final byte[] beginKey
, final int beginKeyLen
,
250 final byte[] endKey
, final int endKeyLen
);
252 final native void deleteRange(final long handle
, final byte[] beginKey
, final int beginKeyLen
,
253 final byte[] endKey
, final int endKeyLen
, final long cfHandle
);
254 @Override final native void putLogData(final long handle
,
255 final byte[] blob
, final int blobLen
) throws RocksDBException
;
256 @Override final native void clear0(final long handle
);
257 @Override final native void setSavePoint0(final long handle
);
258 @Override final native void rollbackToSavePoint0(final long handle
);
259 @Override final native void popSavePoint(final long handle
) throws RocksDBException
;
260 @Override final native void setMaxBytes(final long nativeHandle
,
261 final long maxBytes
);
263 private native static long newWriteBatch(final int reserved_bytes
);
264 private native static long newWriteBatch(final byte[] serialized
,
265 final int serializedLength
);
266 private native void iterate(final long handle
, final long handlerHandle
)
267 throws RocksDBException
;
268 private native byte[] data(final long nativeHandle
) throws RocksDBException
;
269 private native long getDataSize(final long nativeHandle
);
270 private native boolean hasPut(final long nativeHandle
);
271 private native boolean hasDelete(final long nativeHandle
);
272 private native boolean hasSingleDelete(final long nativeHandle
);
273 private native boolean hasDeleteRange(final long nativeHandle
);
274 private native boolean hasMerge(final long nativeHandle
);
275 private native boolean hasBeginPrepare(final long nativeHandle
);
276 private native boolean hasEndPrepare(final long nativeHandle
);
277 private native boolean hasCommit(final long nativeHandle
);
278 private native boolean hasRollback(final long nativeHandle
);
279 private native void markWalTerminationPoint(final long nativeHandle
);
280 private native SavePoint
getWalTerminationPoint(final long nativeHandle
);
283 * Handler callback for iterating over the contents of a batch.
285 public static abstract class Handler
286 extends RocksCallbackObject
{
292 protected long initializeNative(final long... nativeParameterHandles
) {
293 return createNewHandler0();
296 public abstract void put(final int columnFamilyId
, final byte[] key
,
297 final byte[] value
) throws RocksDBException
;
298 public abstract void put(final byte[] key
, final byte[] value
);
299 public abstract void merge(final int columnFamilyId
, final byte[] key
,
300 final byte[] value
) throws RocksDBException
;
301 public abstract void merge(final byte[] key
, final byte[] value
);
302 public abstract void delete(final int columnFamilyId
, final byte[] key
)
303 throws RocksDBException
;
304 public abstract void delete(final byte[] key
);
305 public abstract void singleDelete(final int columnFamilyId
,
306 final byte[] key
) throws RocksDBException
;
307 public abstract void singleDelete(final byte[] key
);
308 public abstract void deleteRange(final int columnFamilyId
,
309 final byte[] beginKey
, final byte[] endKey
) throws RocksDBException
;
310 public abstract void deleteRange(final byte[] beginKey
,
311 final byte[] endKey
);
312 public abstract void logData(final byte[] blob
);
313 public abstract void putBlobIndex(final int columnFamilyId
,
314 final byte[] key
, final byte[] value
) throws RocksDBException
;
315 public abstract void markBeginPrepare() throws RocksDBException
;
316 public abstract void markEndPrepare(final byte[] xid
)
317 throws RocksDBException
;
318 public abstract void markNoop(final boolean emptyBatch
)
319 throws RocksDBException
;
320 public abstract void markRollback(final byte[] xid
)
321 throws RocksDBException
;
322 public abstract void markCommit(final byte[] xid
)
323 throws RocksDBException
;
324 public abstract void markCommitWithTimestamp(final byte[] xid
, final byte[] ts
)
325 throws RocksDBException
;
328 * shouldContinue is called by the underlying iterator
329 * {@link WriteBatch#iterate(Handler)}. If it returns false,
330 * iteration is halted. Otherwise, it continues
331 * iterating. The default implementation always
334 * @return boolean value indicating if the
335 * iteration is halted.
337 public boolean shouldContinue() {
341 private native long createNewHandler0();
345 * A structure for describing the save point in the Write Batch.
347 public static class SavePoint
{
350 private long contentFlags
;
352 public SavePoint(final long size
, final long count
,
353 final long contentFlags
) {
356 this.contentFlags
= contentFlags
;
359 public void clear() {
362 this.contentFlags
= 0;
366 * Get the size of the serialized representation.
368 * @return the size of the serialized representation.
370 public long getSize() {
375 * Get the number of elements.
377 * @return the number of elements.
379 public long getCount() {
384 * Get the content flags.
386 * @return the content flags.
388 public long getContentFlags() {
392 public boolean isCleared() {
393 return (size
| count
| contentFlags
) == 0;