]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/main/java/org/rocksdb/WriteBatch.java
add subtree-ish sources for 12.0.3
[ceph.git] / 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 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.
5
6 package org.rocksdb;
7
8 /**
9 * WriteBatch holds a collection of updates to apply atomically to a DB.
10 *
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:
14 *
15 * batch.put("key", "v1");
16 * batch.remove("key");
17 * batch.put("key", "v2");
18 * batch.put("key", "v3");
19 *
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.
24 */
25 public class WriteBatch extends AbstractWriteBatch {
26 /**
27 * Constructs a WriteBatch instance.
28 */
29 public WriteBatch() {
30 this(0);
31 }
32
33 /**
34 * Constructs a WriteBatch instance with a given size.
35 *
36 * @param reserved_bytes reserved size for WriteBatch
37 */
38 public WriteBatch(final int reserved_bytes) {
39 super(newWriteBatch(reserved_bytes));
40 }
41
42 /**
43 * Support for iterating over the contents of a batch.
44 *
45 * @param handler A handler that is called back for each
46 * update present in the batch
47 *
48 * @throws RocksDBException If we cannot iterate over the batch
49 */
50 public void iterate(final Handler handler) throws RocksDBException {
51 iterate(nativeHandle_, handler.nativeHandle_);
52 }
53
54 /**
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>
58 *
59 * @param nativeHandle address of native instance.
60 */
61 WriteBatch(final long nativeHandle) {
62 this(nativeHandle, false);
63 }
64
65 /**
66 * <p>Private WriteBatch constructor which is used to construct
67 * WriteBatch instances. </p>
68 *
69 * @param nativeHandle address of native instance.
70 * @param owningNativeHandle whether to own this reference from the C++ side or not
71 */
72 WriteBatch(final long nativeHandle, final boolean owningNativeHandle) {
73 super(nativeHandle);
74 if(!owningNativeHandle)
75 disOwnNativeHandle();
76 }
77
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,
84 final long cfHandle);
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,
89 final long cfHandle);
90 @Override final native void remove(final long handle, final byte[] key,
91 final int keyLen);
92 @Override final native void remove(final long handle, final byte[] key,
93 final int keyLen, final long cfHandle);
94 @Override
95 final native void deleteRange(final long handle, final byte[] beginKey, final int beginKeyLen,
96 final byte[] endKey, final int endKeyLen);
97 @Override
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);
105
106 private native static long newWriteBatch(final int reserved_bytes);
107 private native void iterate(final long handle, final long handlerHandle)
108 throws RocksDBException;
109
110
111 /**
112 * Handler callback for iterating over the contents of a batch.
113 */
114 public static abstract class Handler
115 extends AbstractImmutableNativeReference {
116 private final long nativeHandle_;
117 public Handler() {
118 super(true);
119 this.nativeHandle_ = createNewHandler0();
120 }
121
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);
127
128 /**
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
133 * returns true.
134 *
135 * @return boolean value indicating if the
136 * iteration is halted.
137 */
138 public boolean shouldContinue() {
139 return true;
140 }
141
142 /**
143 * Deletes underlying C++ handler pointer.
144 */
145 @Override
146 protected void disposeInternal() {
147 disposeInternal(nativeHandle_);
148 }
149
150 private native long createNewHandler0();
151 private native void disposeInternal(final long handle);
152 }
153 }