]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/test/java/org/rocksdb/WriteBatchTest.java
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / java / src / test / java / org / rocksdb / WriteBatchTest.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 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
9 package org.rocksdb;
10
11 import org.junit.ClassRule;
12 import org.junit.Rule;
13 import org.junit.Test;
14 import org.junit.rules.TemporaryFolder;
15
16 import java.io.UnsupportedEncodingException;
17 import java.util.Arrays;
18
19 import static org.assertj.core.api.Assertions.assertThat;
20
21 /**
22 * This class mimics the db/write_batch_test.cc
23 * in the c++ rocksdb library.
24 * <p/>
25 * Not ported yet:
26 * <p/>
27 * Continue();
28 * PutGatherSlices();
29 */
30 public class WriteBatchTest {
31 @ClassRule
32 public static final RocksMemoryResource rocksMemoryResource =
33 new RocksMemoryResource();
34
35 @Rule
36 public TemporaryFolder dbFolder = new TemporaryFolder();
37
38 @Test
39 public void emptyWriteBatch() {
40 try (final WriteBatch batch = new WriteBatch()) {
41 assertThat(batch.count()).isEqualTo(0);
42 }
43 }
44
45 @Test
46 public void multipleBatchOperations()
47 throws UnsupportedEncodingException {
48 try (WriteBatch batch = new WriteBatch()) {
49 batch.put("foo".getBytes("US-ASCII"), "bar".getBytes("US-ASCII"));
50 batch.remove("box".getBytes("US-ASCII"));
51 batch.put("baz".getBytes("US-ASCII"), "boo".getBytes("US-ASCII"));
52
53 WriteBatchTestInternalHelper.setSequence(batch, 100);
54 assertThat(WriteBatchTestInternalHelper.sequence(batch)).
55 isNotNull().
56 isEqualTo(100);
57 assertThat(batch.count()).isEqualTo(3);
58 assertThat(new String(getContents(batch), "US-ASCII")).
59 isEqualTo("Put(baz, boo)@102" +
60 "Delete(box)@101" +
61 "Put(foo, bar)@100");
62 }
63 }
64
65 @Test
66 public void testAppendOperation()
67 throws UnsupportedEncodingException {
68 try (final WriteBatch b1 = new WriteBatch();
69 final WriteBatch b2 = new WriteBatch()) {
70 WriteBatchTestInternalHelper.setSequence(b1, 200);
71 WriteBatchTestInternalHelper.setSequence(b2, 300);
72 WriteBatchTestInternalHelper.append(b1, b2);
73 assertThat(getContents(b1).length).isEqualTo(0);
74 assertThat(b1.count()).isEqualTo(0);
75 b2.put("a".getBytes("US-ASCII"), "va".getBytes("US-ASCII"));
76 WriteBatchTestInternalHelper.append(b1, b2);
77 assertThat("Put(a, va)@200".equals(new String(getContents(b1),
78 "US-ASCII")));
79 assertThat(b1.count()).isEqualTo(1);
80 b2.clear();
81 b2.put("b".getBytes("US-ASCII"), "vb".getBytes("US-ASCII"));
82 WriteBatchTestInternalHelper.append(b1, b2);
83 assertThat(("Put(a, va)@200" +
84 "Put(b, vb)@201")
85 .equals(new String(getContents(b1), "US-ASCII")));
86 assertThat(b1.count()).isEqualTo(2);
87 b2.remove("foo".getBytes("US-ASCII"));
88 WriteBatchTestInternalHelper.append(b1, b2);
89 assertThat(("Put(a, va)@200" +
90 "Put(b, vb)@202" +
91 "Put(b, vb)@201" +
92 "Delete(foo)@203")
93 .equals(new String(getContents(b1), "US-ASCII")));
94 assertThat(b1.count()).isEqualTo(4);
95 }
96 }
97
98 @Test
99 public void blobOperation()
100 throws UnsupportedEncodingException {
101 try (final WriteBatch batch = new WriteBatch()) {
102 batch.put("k1".getBytes("US-ASCII"), "v1".getBytes("US-ASCII"));
103 batch.put("k2".getBytes("US-ASCII"), "v2".getBytes("US-ASCII"));
104 batch.put("k3".getBytes("US-ASCII"), "v3".getBytes("US-ASCII"));
105 batch.putLogData("blob1".getBytes("US-ASCII"));
106 batch.remove("k2".getBytes("US-ASCII"));
107 batch.putLogData("blob2".getBytes("US-ASCII"));
108 batch.merge("foo".getBytes("US-ASCII"), "bar".getBytes("US-ASCII"));
109 assertThat(batch.count()).isEqualTo(5);
110 assertThat(("Merge(foo, bar)@4" +
111 "Put(k1, v1)@0" +
112 "Delete(k2)@3" +
113 "Put(k2, v2)@1" +
114 "Put(k3, v3)@2")
115 .equals(new String(getContents(batch), "US-ASCII")));
116 }
117 }
118
119 @Test
120 public void savePoints()
121 throws UnsupportedEncodingException, RocksDBException {
122 try (final WriteBatch batch = new WriteBatch()) {
123 batch.put("k1".getBytes("US-ASCII"), "v1".getBytes("US-ASCII"));
124 batch.put("k2".getBytes("US-ASCII"), "v2".getBytes("US-ASCII"));
125 batch.put("k3".getBytes("US-ASCII"), "v3".getBytes("US-ASCII"));
126
127 assertThat(getFromWriteBatch(batch, "k1")).isEqualTo("v1");
128 assertThat(getFromWriteBatch(batch, "k2")).isEqualTo("v2");
129 assertThat(getFromWriteBatch(batch, "k3")).isEqualTo("v3");
130
131
132 batch.setSavePoint();
133
134 batch.remove("k2".getBytes("US-ASCII"));
135 batch.put("k3".getBytes("US-ASCII"), "v3-2".getBytes("US-ASCII"));
136
137 assertThat(getFromWriteBatch(batch, "k2")).isNull();
138 assertThat(getFromWriteBatch(batch, "k3")).isEqualTo("v3-2");
139
140
141 batch.setSavePoint();
142
143 batch.put("k3".getBytes("US-ASCII"), "v3-3".getBytes("US-ASCII"));
144 batch.put("k4".getBytes("US-ASCII"), "v4".getBytes("US-ASCII"));
145
146 assertThat(getFromWriteBatch(batch, "k3")).isEqualTo("v3-3");
147 assertThat(getFromWriteBatch(batch, "k4")).isEqualTo("v4");
148
149
150 batch.rollbackToSavePoint();
151
152 assertThat(getFromWriteBatch(batch, "k2")).isNull();
153 assertThat(getFromWriteBatch(batch, "k3")).isEqualTo("v3-2");
154 assertThat(getFromWriteBatch(batch, "k4")).isNull();
155
156
157 batch.rollbackToSavePoint();
158
159 assertThat(getFromWriteBatch(batch, "k1")).isEqualTo("v1");
160 assertThat(getFromWriteBatch(batch, "k2")).isEqualTo("v2");
161 assertThat(getFromWriteBatch(batch, "k3")).isEqualTo("v3");
162 assertThat(getFromWriteBatch(batch, "k4")).isNull();
163 }
164 }
165
166 @Test
167 public void deleteRange() throws RocksDBException {
168 try (final RocksDB db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
169 final WriteOptions wOpt = new WriteOptions()) {
170 db.put("key1".getBytes(), "value".getBytes());
171 db.put("key2".getBytes(), "12345678".getBytes());
172 db.put("key3".getBytes(), "abcdefg".getBytes());
173 db.put("key4".getBytes(), "xyz".getBytes());
174 assertThat(db.get("key1".getBytes())).isEqualTo("value".getBytes());
175 assertThat(db.get("key2".getBytes())).isEqualTo("12345678".getBytes());
176 assertThat(db.get("key3".getBytes())).isEqualTo("abcdefg".getBytes());
177 assertThat(db.get("key4".getBytes())).isEqualTo("xyz".getBytes());
178
179 WriteBatch batch = new WriteBatch();
180 batch.deleteRange("key2".getBytes(), "key4".getBytes());
181 db.write(new WriteOptions(), batch);
182
183 assertThat(db.get("key1".getBytes())).isEqualTo("value".getBytes());
184 assertThat(db.get("key2".getBytes())).isNull();
185 assertThat(db.get("key3".getBytes())).isNull();
186 assertThat(db.get("key4".getBytes())).isEqualTo("xyz".getBytes());
187 }
188 }
189
190 @Test(expected = RocksDBException.class)
191 public void restorePoints_withoutSavePoints() throws RocksDBException {
192 try (final WriteBatch batch = new WriteBatch()) {
193 batch.rollbackToSavePoint();
194 }
195 }
196
197 @Test(expected = RocksDBException.class)
198 public void restorePoints_withoutSavePoints_nested() throws RocksDBException {
199 try (final WriteBatch batch = new WriteBatch()) {
200
201 batch.setSavePoint();
202 batch.rollbackToSavePoint();
203
204 // without previous corresponding setSavePoint
205 batch.rollbackToSavePoint();
206 }
207 }
208
209 static byte[] getContents(final WriteBatch wb) {
210 return getContents(wb.nativeHandle_);
211 }
212
213 static String getFromWriteBatch(final WriteBatch wb, final String key)
214 throws RocksDBException, UnsupportedEncodingException {
215 final WriteBatchGetter getter =
216 new WriteBatchGetter(key.getBytes("US-ASCII"));
217 wb.iterate(getter);
218 if(getter.getValue() != null) {
219 return new String(getter.getValue(), "US-ASCII");
220 } else {
221 return null;
222 }
223 }
224
225 private static native byte[] getContents(final long writeBatchHandle);
226
227 private static class WriteBatchGetter extends WriteBatch.Handler {
228
229 private final byte[] key;
230 private byte[] value;
231
232 public WriteBatchGetter(final byte[] key) {
233 this.key = key;
234 }
235
236 public byte[] getValue() {
237 return value;
238 }
239
240 @Override
241 public void put(final byte[] key, final byte[] value) {
242 if(Arrays.equals(this.key, key)) {
243 this.value = value;
244 }
245 }
246
247 @Override
248 public void merge(final byte[] key, final byte[] value) {
249 if(Arrays.equals(this.key, key)) {
250 throw new UnsupportedOperationException();
251 }
252 }
253
254 @Override
255 public void delete(final byte[] key) {
256 if(Arrays.equals(this.key, key)) {
257 this.value = null;
258 }
259 }
260
261 @Override
262 public void deleteRange(final byte[] beginKey, final byte[] endKey) {
263 throw new UnsupportedOperationException();
264 }
265
266 @Override
267 public void logData(final byte[] blob) {
268 }
269 }
270 }
271
272 /**
273 * Package-private class which provides java api to access
274 * c++ WriteBatchInternal.
275 */
276 class WriteBatchTestInternalHelper {
277 static void setSequence(final WriteBatch wb, final long sn) {
278 setSequence(wb.nativeHandle_, sn);
279 }
280
281 static long sequence(final WriteBatch wb) {
282 return sequence(wb.nativeHandle_);
283 }
284
285 static void append(final WriteBatch wb1, final WriteBatch wb2) {
286 append(wb1.nativeHandle_, wb2.nativeHandle_);
287 }
288
289 private static native void setSequence(final long writeBatchHandle,
290 final long sn);
291
292 private static native long sequence(final long writeBatchHandle);
293
294 private static native void append(final long writeBatchHandle1,
295 final long writeBatchHandle2);
296 }