1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
9 * WriteBatch holds a collection of updates to apply atomically to a DB.
11 * The updates are applied in the order in which they are added
12 * to the WriteBatch. For example, the value of "key" will be "v3"
13 * after the following batch is written:
15 * batch.put("key", "v1");
16 * batch.remove("key");
17 * batch.put("key", "v2");
18 * batch.put("key", "v3");
20 * Multiple threads can invoke const methods on a WriteBatch without
21 * external synchronization, but if any of the threads may call a
22 * non-const method, all threads accessing the same WriteBatch must use
23 * external synchronization.
25 public class WriteBatch
extends AbstractWriteBatch
{
27 * Constructs a WriteBatch instance.
34 * Constructs a WriteBatch instance with a given size.
36 * @param reserved_bytes reserved size for WriteBatch
38 public WriteBatch(final int reserved_bytes
) {
39 super(newWriteBatch(reserved_bytes
));
43 * Support for iterating over the contents of a batch.
45 * @param handler A handler that is called back for each
46 * update present in the batch
48 * @throws RocksDBException If we cannot iterate over the batch
50 public void iterate(final Handler handler
) throws RocksDBException
{
51 iterate(nativeHandle_
, handler
.nativeHandle_
);
55 * <p>Private WriteBatch constructor which is used to construct
56 * WriteBatch instances from C++ side. As the reference to this
57 * object is also managed from C++ side the handle will be disowned.</p>
59 * @param nativeHandle address of native instance.
61 WriteBatch(final long nativeHandle
) {
62 this(nativeHandle
, false);
66 * <p>Private WriteBatch constructor which is used to construct
67 * WriteBatch instances. </p>
69 * @param nativeHandle address of native instance.
70 * @param owningNativeHandle whether to own this reference from the C++ side or not
72 WriteBatch(final long nativeHandle
, final boolean owningNativeHandle
) {
74 if(!owningNativeHandle
)
78 @Override protected final native void disposeInternal(final long handle
);
79 @Override final native int count0(final long handle
);
80 @Override final native void put(final long handle
, final byte[] key
,
81 final int keyLen
, final byte[] value
, final int valueLen
);
82 @Override final native void put(final long handle
, final byte[] key
,
83 final int keyLen
, final byte[] value
, final int valueLen
,
85 @Override final native void merge(final long handle
, final byte[] key
,
86 final int keyLen
, final byte[] value
, final int valueLen
);
87 @Override final native void merge(final long handle
, final byte[] key
,
88 final int keyLen
, final byte[] value
, final int valueLen
,
90 @Override final native void remove(final long handle
, final byte[] key
,
92 @Override final native void remove(final long handle
, final byte[] key
,
93 final int keyLen
, final long cfHandle
);
95 final native void deleteRange(final long handle
, final byte[] beginKey
, final int beginKeyLen
,
96 final byte[] endKey
, final int endKeyLen
);
98 final native void deleteRange(final long handle
, final byte[] beginKey
, final int beginKeyLen
,
99 final byte[] endKey
, final int endKeyLen
, final long cfHandle
);
100 @Override final native void putLogData(final long handle
,
101 final byte[] blob
, final int blobLen
);
102 @Override final native void clear0(final long handle
);
103 @Override final native void setSavePoint0(final long handle
);
104 @Override final native void rollbackToSavePoint0(final long handle
);
106 private native static long newWriteBatch(final int reserved_bytes
);
107 private native void iterate(final long handle
, final long handlerHandle
)
108 throws RocksDBException
;
112 * Handler callback for iterating over the contents of a batch.
114 public static abstract class Handler
115 extends AbstractImmutableNativeReference
{
116 private final long nativeHandle_
;
119 this.nativeHandle_
= createNewHandler0();
122 public abstract void put(byte[] key
, byte[] value
);
123 public abstract void merge(byte[] key
, byte[] value
);
124 public abstract void delete(byte[] key
);
125 public abstract void deleteRange(byte[] beginKey
, byte[] endKey
);
126 public abstract void logData(byte[] blob
);
129 * shouldContinue is called by the underlying iterator
130 * WriteBatch::Iterate. If it returns false,
131 * iteration is halted. Otherwise, it continues
132 * iterating. The default implementation always
135 * @return boolean value indicating if the
136 * iteration is halted.
138 public boolean shouldContinue() {
143 * Deletes underlying C++ handler pointer.
146 protected void disposeInternal() {
147 disposeInternal(nativeHandle_
);
150 private native long createNewHandler0();
151 private native void disposeInternal(final long handle
);