HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / client / TestCheckAndMutate.java
blobe4509affb766c18431b94713705aafebe4ac094b
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.client;
20 import static org.hamcrest.CoreMatchers.instanceOf;
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27 import java.io.IOException;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.List;
31 import org.apache.hadoop.hbase.CompareOperator;
32 import org.apache.hadoop.hbase.HBaseClassTestRule;
33 import org.apache.hadoop.hbase.HBaseTestingUtil;
34 import org.apache.hadoop.hbase.TableName;
35 import org.apache.hadoop.hbase.filter.BinaryComparator;
36 import org.apache.hadoop.hbase.filter.FamilyFilter;
37 import org.apache.hadoop.hbase.filter.FilterList;
38 import org.apache.hadoop.hbase.filter.QualifierFilter;
39 import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
40 import org.apache.hadoop.hbase.filter.TimestampsFilter;
41 import org.apache.hadoop.hbase.io.TimeRange;
42 import org.apache.hadoop.hbase.regionserver.NoSuchColumnFamilyException;
43 import org.apache.hadoop.hbase.testclassification.MediumTests;
44 import org.apache.hadoop.hbase.util.Bytes;
45 import org.junit.AfterClass;
46 import org.junit.BeforeClass;
47 import org.junit.ClassRule;
48 import org.junit.Rule;
49 import org.junit.Test;
50 import org.junit.experimental.categories.Category;
51 import org.junit.rules.TestName;
53 @Category(MediumTests.class)
54 public class TestCheckAndMutate {
56 @ClassRule
57 public static final HBaseClassTestRule CLASS_RULE =
58 HBaseClassTestRule.forClass(TestCheckAndMutate.class);
60 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
61 private static final byte[] ROWKEY = Bytes.toBytes("12345");
62 private static final byte[] ROWKEY2 = Bytes.toBytes("67890");
63 private static final byte[] ROWKEY3 = Bytes.toBytes("abcde");
64 private static final byte[] ROWKEY4 = Bytes.toBytes("fghij");
65 private static final byte[] FAMILY = Bytes.toBytes("cf");
67 @Rule
68 public TestName name = new TestName();
70 @BeforeClass
71 public static void setUpBeforeClass() throws Exception {
72 TEST_UTIL.startMiniCluster();
75 @AfterClass
76 public static void tearDownAfterClass() throws Exception {
77 TEST_UTIL.shutdownMiniCluster();
80 private Table createTable()
81 throws IOException, InterruptedException {
82 final TableName tableName = TableName.valueOf(name.getMethodName());
83 Table table = TEST_UTIL.createTable(tableName, FAMILY);
84 TEST_UTIL.waitTableAvailable(tableName.getName(), 5000);
85 return table;
88 private void putOneRow(Table table) throws IOException {
89 Put put = new Put(ROWKEY);
90 put.addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a"));
91 put.addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"));
92 put.addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c"));
93 table.put(put);
96 private void getOneRowAndAssertAllExist(final Table table) throws IOException {
97 Get get = new Get(ROWKEY);
98 Result result = table.get(get);
99 assertTrue("Column A value should be a",
100 Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("A"))).equals("a"));
101 assertTrue("Column B value should be b",
102 Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))).equals("b"));
103 assertTrue("Column C value should be c",
104 Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("C"))).equals("c"));
107 private void getOneRowAndAssertAllButCExist(final Table table) throws IOException {
108 Get get = new Get(ROWKEY);
109 Result result = table.get(get);
110 assertTrue("Column A value should be a",
111 Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("A"))).equals("a"));
112 assertTrue("Column B value should be b",
113 Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))).equals("b"));
114 assertTrue("Column C should not exist",
115 result.getValue(FAMILY, Bytes.toBytes("C")) == null);
118 private RowMutations makeRowMutationsWithColumnCDeleted() throws IOException {
119 RowMutations rm = new RowMutations(ROWKEY, 2);
120 Put put = new Put(ROWKEY);
121 put.addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a"));
122 put.addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"));
123 rm.add(put);
124 Delete del = new Delete(ROWKEY);
125 del.addColumn(FAMILY, Bytes.toBytes("C"));
126 rm.add(del);
127 return rm;
130 private RowMutations getBogusRowMutations() throws IOException {
131 Put p = new Put(ROWKEY);
132 byte[] value = new byte[0];
133 p.addColumn(new byte[]{'b', 'o', 'g', 'u', 's'}, new byte[]{'A'}, value);
134 RowMutations rm = new RowMutations(ROWKEY);
135 rm.add(p);
136 return rm;
139 // Tests for old checkAndMutate API
141 @Test
142 @Deprecated
143 public void testCheckAndMutateForOldApi() throws Throwable {
144 try (Table table = createTable()) {
145 // put one row
146 putOneRow(table);
147 // get row back and assert the values
148 getOneRowAndAssertAllExist(table);
150 // put the same row again with C column deleted
151 RowMutations rm = makeRowMutationsWithColumnCDeleted();
152 boolean res = table.checkAndMutate(ROWKEY, FAMILY).qualifier(Bytes.toBytes("A"))
153 .ifEquals(Bytes.toBytes("a")).thenMutate(rm);
154 assertTrue(res);
156 // get row back and assert the values
157 getOneRowAndAssertAllButCExist(table);
159 // Test that we get a region level exception
160 try {
161 rm = getBogusRowMutations();
162 table.checkAndMutate(ROWKEY, FAMILY).qualifier(Bytes.toBytes("A"))
163 .ifEquals(Bytes.toBytes("a")).thenMutate(rm);
164 fail("Expected NoSuchColumnFamilyException");
165 } catch (NoSuchColumnFamilyException e) {
166 // expected
167 } catch (RetriesExhaustedException e) {
168 assertThat(e.getCause(), instanceOf(NoSuchColumnFamilyException.class));
173 @Test
174 @Deprecated
175 public void testCheckAndMutateWithSingleFilterForOldApi() throws Throwable {
176 try (Table table = createTable()) {
177 // put one row
178 putOneRow(table);
179 // get row back and assert the values
180 getOneRowAndAssertAllExist(table);
182 // Put with success
183 boolean ok = table.checkAndMutate(ROWKEY, new SingleColumnValueFilter(FAMILY,
184 Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")))
185 .thenPut(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d")));
186 assertTrue(ok);
188 Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D")));
189 assertEquals("d", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("D"))));
191 // Put with failure
192 ok = table.checkAndMutate(ROWKEY, new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
193 CompareOperator.EQUAL, Bytes.toBytes("b")))
194 .thenPut(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("E"), Bytes.toBytes("e")));
195 assertFalse(ok);
197 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("E"))));
199 // Delete with success
200 ok = table.checkAndMutate(ROWKEY, new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
201 CompareOperator.EQUAL, Bytes.toBytes("a")))
202 .thenDelete(new Delete(ROWKEY).addColumns(FAMILY, Bytes.toBytes("D")));
203 assertTrue(ok);
205 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D"))));
207 // Mutate with success
208 ok = table.checkAndMutate(ROWKEY, new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"),
209 CompareOperator.EQUAL, Bytes.toBytes("b")))
210 .thenMutate(new RowMutations(ROWKEY)
211 .add((Mutation) new Put(ROWKEY)
212 .addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d")))
213 .add((Mutation) new Delete(ROWKEY).addColumns(FAMILY, Bytes.toBytes("A"))));
214 assertTrue(ok);
216 result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D")));
217 assertEquals("d", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("D"))));
219 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"))));
223 @Test
224 @Deprecated
225 public void testCheckAndMutateWithMultipleFiltersForOldApi() throws Throwable {
226 try (Table table = createTable()) {
227 // put one row
228 putOneRow(table);
229 // get row back and assert the values
230 getOneRowAndAssertAllExist(table);
232 // Put with success
233 boolean ok = table.checkAndMutate(ROWKEY, new FilterList(
234 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL,
235 Bytes.toBytes("a")),
236 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL,
237 Bytes.toBytes("b"))
239 .thenPut(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d")));
240 assertTrue(ok);
242 Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D")));
243 assertEquals("d", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("D"))));
245 // Put with failure
246 ok = table.checkAndMutate(ROWKEY, new FilterList(
247 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL,
248 Bytes.toBytes("a")),
249 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL,
250 Bytes.toBytes("c"))
252 .thenPut(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("E"), Bytes.toBytes("e")));
253 assertFalse(ok);
255 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("E"))));
257 // Delete with success
258 ok = table.checkAndMutate(ROWKEY, new FilterList(
259 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL,
260 Bytes.toBytes("a")),
261 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL,
262 Bytes.toBytes("b"))
264 .thenDelete(new Delete(ROWKEY).addColumns(FAMILY, Bytes.toBytes("D")));
265 assertTrue(ok);
267 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D"))));
269 // Mutate with success
270 ok = table.checkAndMutate(ROWKEY, new FilterList(
271 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL,
272 Bytes.toBytes("a")),
273 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL,
274 Bytes.toBytes("b"))
276 .thenMutate(new RowMutations(ROWKEY)
277 .add((Mutation) new Put(ROWKEY)
278 .addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d")))
279 .add((Mutation) new Delete(ROWKEY).addColumns(FAMILY, Bytes.toBytes("A"))));
280 assertTrue(ok);
282 result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D")));
283 assertEquals("d", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("D"))));
285 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"))));
289 @Test
290 @Deprecated
291 public void testCheckAndMutateWithTimestampFilterForOldApi() throws Throwable {
292 try (Table table = createTable()) {
293 // Put with specifying the timestamp
294 table.put(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a")));
296 // Put with success
297 boolean ok = table.checkAndMutate(ROWKEY, new FilterList(
298 new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
299 new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
300 new TimestampsFilter(Collections.singletonList(100L))
302 .thenPut(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")));
303 assertTrue(ok);
305 Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
306 assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
308 // Put with failure
309 ok = table.checkAndMutate(ROWKEY, new FilterList(
310 new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
311 new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
312 new TimestampsFilter(Collections.singletonList(101L))
314 .thenPut(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")));
315 assertFalse(ok);
317 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"))));
321 @Test
322 @Deprecated
323 public void testCheckAndMutateWithFilterAndTimeRangeForOldApi() throws Throwable {
324 try (Table table = createTable()) {
325 // Put with specifying the timestamp
326 table.put(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a")));
328 // Put with success
329 boolean ok = table.checkAndMutate(ROWKEY, new SingleColumnValueFilter(FAMILY,
330 Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")))
331 .timeRange(TimeRange.between(0, 101))
332 .thenPut(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")));
333 assertTrue(ok);
335 Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
336 assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
338 // Put with failure
339 ok = table.checkAndMutate(ROWKEY, new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
340 CompareOperator.EQUAL, Bytes.toBytes("a")))
341 .timeRange(TimeRange.between(0, 100))
342 .thenPut(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")));
343 assertFalse(ok);
345 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"))));
349 @Test(expected = NullPointerException.class)
350 @Deprecated
351 public void testCheckAndMutateWithoutConditionForOldApi() throws Throwable {
352 try (Table table = createTable()) {
353 table.checkAndMutate(ROWKEY, FAMILY)
354 .thenPut(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d")));
358 // Tests for new CheckAndMutate API
360 @Test
361 public void testCheckAndMutate() throws Throwable {
362 try (Table table = createTable()) {
363 // put one row
364 putOneRow(table);
365 // get row back and assert the values
366 getOneRowAndAssertAllExist(table);
368 // put the same row again with C column deleted
369 RowMutations rm = makeRowMutationsWithColumnCDeleted();
370 CheckAndMutateResult res = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
371 .ifEquals(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a"))
372 .build(rm));
373 assertTrue(res.isSuccess());
374 assertNull(res.getResult());
376 // get row back and assert the values
377 getOneRowAndAssertAllButCExist(table);
379 // Test that we get a region level exception
380 try {
381 rm = getBogusRowMutations();
382 table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
383 .ifEquals(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a"))
384 .build(rm));
385 fail("Expected NoSuchColumnFamilyException");
386 } catch (NoSuchColumnFamilyException e) {
387 // expected
388 } catch (RetriesExhaustedException e) {
389 assertThat(e.getCause(), instanceOf(NoSuchColumnFamilyException.class));
394 @Test
395 public void testCheckAndMutateWithSingleFilter() throws Throwable {
396 try (Table table = createTable()) {
397 // put one row
398 putOneRow(table);
399 // get row back and assert the values
400 getOneRowAndAssertAllExist(table);
402 // Put with success
403 CheckAndMutateResult result = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
404 .ifMatches(new SingleColumnValueFilter(FAMILY,
405 Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")))
406 .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d"))));
407 assertTrue(result.isSuccess());
408 assertNull(result.getResult());
410 Result r = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D")));
411 assertEquals("d", Bytes.toString(r.getValue(FAMILY, Bytes.toBytes("D"))));
413 // Put with failure
414 result = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
415 .ifMatches(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
416 CompareOperator.EQUAL, Bytes.toBytes("b")))
417 .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("E"), Bytes.toBytes("e"))));
418 assertFalse(result.isSuccess());
419 assertNull(result.getResult());
421 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("E"))));
423 // Delete with success
424 result = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
425 .ifMatches(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
426 CompareOperator.EQUAL, Bytes.toBytes("a")))
427 .build(new Delete(ROWKEY).addColumns(FAMILY, Bytes.toBytes("D"))));
428 assertTrue(result.isSuccess());
429 assertNull(result.getResult());
431 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D"))));
433 // Mutate with success
434 result = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
435 .ifMatches(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"),
436 CompareOperator.EQUAL, Bytes.toBytes("b")))
437 .build(new RowMutations(ROWKEY)
438 .add((Mutation) new Put(ROWKEY)
439 .addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d")))
440 .add((Mutation) new Delete(ROWKEY).addColumns(FAMILY, Bytes.toBytes("A")))));
441 assertTrue(result.isSuccess());
442 assertNull(result.getResult());
444 r = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D")));
445 assertEquals("d", Bytes.toString(r.getValue(FAMILY, Bytes.toBytes("D"))));
447 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"))));
451 @Test
452 public void testCheckAndMutateWithMultipleFilters() throws Throwable {
453 try (Table table = createTable()) {
454 // put one row
455 putOneRow(table);
456 // get row back and assert the values
457 getOneRowAndAssertAllExist(table);
459 // Put with success
460 CheckAndMutateResult result = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
461 .ifMatches(new FilterList(
462 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL,
463 Bytes.toBytes("a")),
464 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL,
465 Bytes.toBytes("b"))))
466 .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d"))));
467 assertTrue(result.isSuccess());
468 assertNull(result.getResult());
470 Result r = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D")));
471 assertEquals("d", Bytes.toString(r.getValue(FAMILY, Bytes.toBytes("D"))));
473 // Put with failure
474 result = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
475 .ifMatches(new FilterList(
476 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL,
477 Bytes.toBytes("a")),
478 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL,
479 Bytes.toBytes("c"))))
480 .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("E"), Bytes.toBytes("e"))));
481 assertFalse(result.isSuccess());
482 assertNull(result.getResult());
484 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("E"))));
486 // Delete with success
487 result = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
488 .ifMatches(new FilterList(
489 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL,
490 Bytes.toBytes("a")),
491 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL,
492 Bytes.toBytes("b"))))
493 .build(new Delete(ROWKEY).addColumns(FAMILY, Bytes.toBytes("D"))));
494 assertTrue(result.isSuccess());
495 assertNull(result.getResult());
497 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D"))));
499 // Mutate with success
500 result = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
501 .ifMatches(new FilterList(
502 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL,
503 Bytes.toBytes("a")),
504 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL,
505 Bytes.toBytes("b"))))
506 .build(new RowMutations(ROWKEY)
507 .add((Mutation) new Put(ROWKEY)
508 .addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d")))
509 .add((Mutation) new Delete(ROWKEY).addColumns(FAMILY, Bytes.toBytes("A")))));
510 assertTrue(result.isSuccess());
511 assertNull(result.getResult());
513 r = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D")));
514 assertEquals("d", Bytes.toString(r.getValue(FAMILY, Bytes.toBytes("D"))));
516 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"))));
520 @Test
521 public void testCheckAndMutateWithTimestampFilter() throws Throwable {
522 try (Table table = createTable()) {
523 // Put with specifying the timestamp
524 table.put(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a")));
526 // Put with success
527 CheckAndMutateResult result = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
528 .ifMatches(new FilterList(
529 new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
530 new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
531 new TimestampsFilter(Collections.singletonList(100L))))
532 .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"))));
533 assertTrue(result.isSuccess());
534 assertNull(result.getResult());
536 Result r = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
537 assertEquals("b", Bytes.toString(r.getValue(FAMILY, Bytes.toBytes("B"))));
539 // Put with failure
540 result = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
541 .ifMatches(new FilterList(
542 new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
543 new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
544 new TimestampsFilter(Collections.singletonList(101L))))
545 .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c"))));
546 assertFalse(result.isSuccess());
547 assertNull(result.getResult());
549 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"))));
553 @Test
554 public void testCheckAndMutateWithFilterAndTimeRange() throws Throwable {
555 try (Table table = createTable()) {
556 // Put with specifying the timestamp
557 table.put(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a")));
559 // Put with success
560 CheckAndMutateResult result = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
561 .ifMatches(new SingleColumnValueFilter(FAMILY,
562 Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")))
563 .timeRange(TimeRange.between(0, 101))
564 .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"))));
565 assertTrue(result.isSuccess());
566 assertNull(result.getResult());
568 Result r = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
569 assertEquals("b", Bytes.toString(r.getValue(FAMILY, Bytes.toBytes("B"))));
571 // Put with failure
572 result = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
573 .ifMatches(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
574 CompareOperator.EQUAL, Bytes.toBytes("a")))
575 .timeRange(TimeRange.between(0, 100))
576 .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c"))));
577 assertFalse(result.isSuccess());
578 assertNull(result.getResult());
580 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"))));
584 @Test(expected = IllegalStateException.class)
585 public void testCheckAndMutateBuilderWithoutCondition() {
586 CheckAndMutate.newBuilder(ROWKEY)
587 .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d")));
590 @Test
591 public void testCheckAndIncrement() throws Throwable {
592 try (Table table = createTable()) {
593 table.put(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a")));
595 // CheckAndIncrement with correct value
596 CheckAndMutateResult res = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
597 .ifEquals(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a"))
598 .build(new Increment(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), 1)));
599 assertTrue(res.isSuccess());
600 assertEquals(1, Bytes.toLong(res.getResult().getValue(FAMILY, Bytes.toBytes("B"))));
602 Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
603 assertEquals(1, Bytes.toLong(result.getValue(FAMILY, Bytes.toBytes("B"))));
605 // CheckAndIncrement with wrong value
606 res = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
607 .ifEquals(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("b"))
608 .build(new Increment(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), 1)));
609 assertFalse(res.isSuccess());
610 assertNull(res.getResult());
612 result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
613 assertEquals(1, Bytes.toLong(result.getValue(FAMILY, Bytes.toBytes("B"))));
615 table.put(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")));
617 // CheckAndIncrement with a filter and correct value
618 res = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
619 .ifMatches(new FilterList(
620 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL,
621 Bytes.toBytes("a")),
622 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("C"), CompareOperator.EQUAL,
623 Bytes.toBytes("c"))))
624 .build(new Increment(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), 2)));
625 assertTrue(res.isSuccess());
626 assertEquals(3, Bytes.toLong(res.getResult().getValue(FAMILY, Bytes.toBytes("B"))));
628 result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
629 assertEquals(3, Bytes.toLong(result.getValue(FAMILY, Bytes.toBytes("B"))));
631 // CheckAndIncrement with a filter and correct value
632 res = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
633 .ifMatches(new FilterList(
634 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL,
635 Bytes.toBytes("b")),
636 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("C"), CompareOperator.EQUAL,
637 Bytes.toBytes("d"))))
638 .build(new Increment(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), 2)));
639 assertFalse(res.isSuccess());
640 assertNull(res.getResult());
642 result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
643 assertEquals(3, Bytes.toLong(result.getValue(FAMILY, Bytes.toBytes("B"))));
647 @Test
648 public void testCheckAndAppend() throws Throwable {
649 try (Table table = createTable()) {
650 table.put(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a")));
652 // CheckAndAppend with correct value
653 CheckAndMutateResult res = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
654 .ifEquals(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a"))
655 .build(new Append(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"))));
656 assertTrue(res.isSuccess());
657 assertEquals("b", Bytes.toString(res.getResult().getValue(FAMILY, Bytes.toBytes("B"))));
659 Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
660 assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
662 // CheckAndAppend with correct value
663 res = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
664 .ifEquals(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("b"))
665 .build(new Append(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"))));
666 assertFalse(res.isSuccess());
667 assertNull(res.getResult());
669 result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
670 assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
672 table.put(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")));
674 // CheckAndAppend with a filter and correct value
675 res = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
676 .ifMatches(new FilterList(
677 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL,
678 Bytes.toBytes("a")),
679 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("C"), CompareOperator.EQUAL,
680 Bytes.toBytes("c"))))
681 .build(new Append(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("bb"))));
682 assertTrue(res.isSuccess());
683 assertEquals("bbb", Bytes.toString(res.getResult().getValue(FAMILY, Bytes.toBytes("B"))));
685 result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
686 assertEquals("bbb", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
688 // CheckAndAppend with a filter and wrong value
689 res = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
690 .ifMatches(new FilterList(
691 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL,
692 Bytes.toBytes("b")),
693 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("C"), CompareOperator.EQUAL,
694 Bytes.toBytes("d"))))
695 .build(new Append(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("bb"))));
696 assertFalse(res.isSuccess());
697 assertNull(res.getResult());
699 result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
700 assertEquals("bbb", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
704 @Test
705 public void testCheckAndRowMutations() throws Throwable {
706 final byte[] q1 = Bytes.toBytes("q1");
707 final byte[] q2 = Bytes.toBytes("q2");
708 final byte[] q3 = Bytes.toBytes("q3");
709 final byte[] q4 = Bytes.toBytes("q4");
710 final String v1 = "v1";
712 try (Table table = createTable()) {
713 // Initial values
714 table.put(Arrays.asList(
715 new Put(ROWKEY).addColumn(FAMILY, q2, Bytes.toBytes("toBeDeleted")),
716 new Put(ROWKEY).addColumn(FAMILY, q3, Bytes.toBytes(5L)),
717 new Put(ROWKEY).addColumn(FAMILY, q4, Bytes.toBytes("a"))));
719 // Do CheckAndRowMutations
720 CheckAndMutate checkAndMutate = CheckAndMutate.newBuilder(ROWKEY)
721 .ifNotExists(FAMILY, q1)
722 .build(new RowMutations(ROWKEY).add(Arrays.asList(
723 new Put(ROWKEY).addColumn(FAMILY, q1, Bytes.toBytes(v1)),
724 new Delete(ROWKEY).addColumns(FAMILY, q2),
725 new Increment(ROWKEY).addColumn(FAMILY, q3, 1),
726 new Append(ROWKEY).addColumn(FAMILY, q4, Bytes.toBytes("b"))))
729 CheckAndMutateResult result = table.checkAndMutate(checkAndMutate);
730 assertTrue(result.isSuccess());
731 assertEquals(6L, Bytes.toLong(result.getResult().getValue(FAMILY, q3)));
732 assertEquals("ab", Bytes.toString(result.getResult().getValue(FAMILY, q4)));
734 // Verify the value
735 Result r = table.get(new Get(ROWKEY));
736 assertEquals(v1, Bytes.toString(r.getValue(FAMILY, q1)));
737 assertNull(r.getValue(FAMILY, q2));
738 assertEquals(6L, Bytes.toLong(r.getValue(FAMILY, q3)));
739 assertEquals("ab", Bytes.toString(r.getValue(FAMILY, q4)));
741 // Do CheckAndRowMutations again
742 checkAndMutate = CheckAndMutate.newBuilder(ROWKEY)
743 .ifNotExists(FAMILY, q1)
744 .build(new RowMutations(ROWKEY).add(Arrays.asList(
745 new Delete(ROWKEY).addColumns(FAMILY, q1),
746 new Put(ROWKEY).addColumn(FAMILY, q2, Bytes.toBytes(v1)),
747 new Increment(ROWKEY).addColumn(FAMILY, q3, 1),
748 new Append(ROWKEY).addColumn(FAMILY, q4, Bytes.toBytes("b"))))
751 result = table.checkAndMutate(checkAndMutate);
752 assertFalse(result.isSuccess());
753 assertNull(result.getResult());
755 // Verify the value
756 r = table.get(new Get(ROWKEY));
757 assertEquals(v1, Bytes.toString(r.getValue(FAMILY, q1)));
758 assertNull(r.getValue(FAMILY, q2));
759 assertEquals(6L, Bytes.toLong(r.getValue(FAMILY, q3)));
760 assertEquals("ab", Bytes.toString(r.getValue(FAMILY, q4)));
764 // Tests for batch version of checkAndMutate
766 @Test
767 public void testCheckAndMutateBatch() throws Throwable {
768 try (Table table = createTable()) {
769 table.put(Arrays.asList(
770 new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a")),
771 new Put(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")),
772 new Put(ROWKEY3).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")),
773 new Put(ROWKEY4).addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d"))));
775 // Test for Put
776 CheckAndMutate checkAndMutate1 = CheckAndMutate.newBuilder(ROWKEY)
777 .ifEquals(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a"))
778 .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("e")));
780 CheckAndMutate checkAndMutate2 = CheckAndMutate.newBuilder(ROWKEY2)
781 .ifEquals(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("a"))
782 .build(new Put(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("f")));
784 List<CheckAndMutateResult> results =
785 table.checkAndMutate(Arrays.asList(checkAndMutate1, checkAndMutate2));
787 assertTrue(results.get(0).isSuccess());
788 assertNull(results.get(0).getResult());
789 assertFalse(results.get(1).isSuccess());
790 assertNull(results.get(1).getResult());
792 Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A")));
793 assertEquals("e", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("A"))));
795 result = table.get(new Get(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("B")));
796 assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
798 // Test for Delete
799 checkAndMutate1 = CheckAndMutate.newBuilder(ROWKEY)
800 .ifEquals(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("e"))
801 .build(new Delete(ROWKEY));
803 checkAndMutate2 = CheckAndMutate.newBuilder(ROWKEY2)
804 .ifEquals(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("a"))
805 .build(new Delete(ROWKEY2));
807 results = table.checkAndMutate(Arrays.asList(checkAndMutate1, checkAndMutate2));
809 assertTrue(results.get(0).isSuccess());
810 assertNull(results.get(0).getResult());
811 assertFalse(results.get(1).isSuccess());
812 assertNull(results.get(1).getResult());
814 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"))));
816 result = table.get(new Get(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("B")));
817 assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
819 // Test for RowMutations
820 checkAndMutate1 = CheckAndMutate.newBuilder(ROWKEY3)
821 .ifEquals(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c"))
822 .build(new RowMutations(ROWKEY3)
823 .add((Mutation) new Put(ROWKEY3)
824 .addColumn(FAMILY, Bytes.toBytes("F"), Bytes.toBytes("f")))
825 .add((Mutation) new Delete(ROWKEY3).addColumns(FAMILY, Bytes.toBytes("C"))));
827 checkAndMutate2 = CheckAndMutate.newBuilder(ROWKEY4)
828 .ifEquals(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("f"))
829 .build(new RowMutations(ROWKEY4)
830 .add((Mutation) new Put(ROWKEY4)
831 .addColumn(FAMILY, Bytes.toBytes("F"), Bytes.toBytes("f")))
832 .add((Mutation) new Delete(ROWKEY4).addColumns(FAMILY, Bytes.toBytes("D"))));
834 results = table.checkAndMutate(Arrays.asList(checkAndMutate1, checkAndMutate2));
836 assertTrue(results.get(0).isSuccess());
837 assertNull(results.get(0).getResult());
838 assertFalse(results.get(1).isSuccess());
839 assertNull(results.get(1).getResult());
841 result = table.get(new Get(ROWKEY3));
842 assertEquals("f", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("F"))));
843 assertNull(result.getValue(FAMILY, Bytes.toBytes("D")));
845 result = table.get(new Get(ROWKEY4));
846 assertNull(result.getValue(FAMILY, Bytes.toBytes("F")));
847 assertEquals("d", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("D"))));
851 @Test
852 public void testCheckAndMutateBatch2() throws Throwable {
853 try (Table table = createTable()) {
854 table.put(Arrays.asList(
855 new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a")),
856 new Put(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")),
857 new Put(ROWKEY3).addColumn(FAMILY, Bytes.toBytes("C"), 100, Bytes.toBytes("c")),
858 new Put(ROWKEY4).addColumn(FAMILY, Bytes.toBytes("D"), 100, Bytes.toBytes("d"))));
860 // Test for ifNotExists()
861 CheckAndMutate checkAndMutate1 = CheckAndMutate.newBuilder(ROWKEY)
862 .ifNotExists(FAMILY, Bytes.toBytes("B"))
863 .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("e")));
865 CheckAndMutate checkAndMutate2 = CheckAndMutate.newBuilder(ROWKEY2)
866 .ifNotExists(FAMILY, Bytes.toBytes("B"))
867 .build(new Put(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("f")));
869 List<CheckAndMutateResult> results =
870 table.checkAndMutate(Arrays.asList(checkAndMutate1, checkAndMutate2));
872 assertTrue(results.get(0).isSuccess());
873 assertNull(results.get(0).getResult());
874 assertFalse(results.get(1).isSuccess());
875 assertNull(results.get(1).getResult());
877 Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A")));
878 assertEquals("e", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("A"))));
880 result = table.get(new Get(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("B")));
881 assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
883 // Test for ifMatches()
884 checkAndMutate1 = CheckAndMutate.newBuilder(ROWKEY)
885 .ifMatches(FAMILY, Bytes.toBytes("A"), CompareOperator.NOT_EQUAL, Bytes.toBytes("a"))
886 .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a")));
888 checkAndMutate2 = CheckAndMutate.newBuilder(ROWKEY2)
889 .ifMatches(FAMILY, Bytes.toBytes("B"), CompareOperator.GREATER, Bytes.toBytes("b"))
890 .build(new Put(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("f")));
892 results = table.checkAndMutate(Arrays.asList(checkAndMutate1, checkAndMutate2));
894 assertTrue(results.get(0).isSuccess());
895 assertNull(results.get(0).getResult());
896 assertFalse(results.get(1).isSuccess());
897 assertNull(results.get(1).getResult());
899 result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A")));
900 assertEquals("a", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("A"))));
902 result = table.get(new Get(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("B")));
903 assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
905 // Test for timeRange()
906 checkAndMutate1 = CheckAndMutate.newBuilder(ROWKEY3)
907 .ifEquals(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c"))
908 .timeRange(TimeRange.between(0, 101))
909 .build(new Put(ROWKEY3).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("e")));
911 checkAndMutate2 = CheckAndMutate.newBuilder(ROWKEY4)
912 .ifEquals(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d"))
913 .timeRange(TimeRange.between(0, 100))
914 .build(new Put(ROWKEY4).addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("f")));
916 results = table.checkAndMutate(Arrays.asList(checkAndMutate1, checkAndMutate2));
918 assertTrue(results.get(0).isSuccess());
919 assertNull(results.get(0).getResult());
920 assertFalse(results.get(1).isSuccess());
921 assertNull(results.get(1).getResult());
923 result = table.get(new Get(ROWKEY3).addColumn(FAMILY, Bytes.toBytes("C")));
924 assertEquals("e", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("C"))));
926 result = table.get(new Get(ROWKEY4).addColumn(FAMILY, Bytes.toBytes("D")));
927 assertEquals("d", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("D"))));
931 @Test
932 public void testCheckAndMutateBatchWithFilter() throws Throwable {
933 try (Table table = createTable()) {
934 table.put(Arrays.asList(
935 new Put(ROWKEY)
936 .addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a"))
937 .addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"))
938 .addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")),
939 new Put(ROWKEY2)
940 .addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d"))
941 .addColumn(FAMILY, Bytes.toBytes("E"), Bytes.toBytes("e"))
942 .addColumn(FAMILY, Bytes.toBytes("F"), Bytes.toBytes("f"))));
944 // Test for Put
945 CheckAndMutate checkAndMutate1 = CheckAndMutate.newBuilder(ROWKEY)
946 .ifMatches(new FilterList(
947 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL,
948 Bytes.toBytes("a")),
949 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL,
950 Bytes.toBytes("b"))))
951 .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("g")));
953 CheckAndMutate checkAndMutate2 = CheckAndMutate.newBuilder(ROWKEY2)
954 .ifMatches(new FilterList(
955 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("D"), CompareOperator.EQUAL,
956 Bytes.toBytes("a")),
957 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("E"), CompareOperator.EQUAL,
958 Bytes.toBytes("b"))))
959 .build(new Put(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("F"), Bytes.toBytes("h")));
961 List<CheckAndMutateResult> results =
962 table.checkAndMutate(Arrays.asList(checkAndMutate1, checkAndMutate2));
964 assertTrue(results.get(0).isSuccess());
965 assertNull(results.get(0).getResult());
966 assertFalse(results.get(1).isSuccess());
967 assertNull(results.get(1).getResult());
969 Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C")));
970 assertEquals("g", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("C"))));
972 result = table.get(new Get(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("F")));
973 assertEquals("f", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("F"))));
975 // Test for Delete
976 checkAndMutate1 = CheckAndMutate.newBuilder(ROWKEY)
977 .ifMatches(new FilterList(
978 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL,
979 Bytes.toBytes("a")),
980 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL,
981 Bytes.toBytes("b"))))
982 .build(new Delete(ROWKEY).addColumns(FAMILY, Bytes.toBytes("C")));
984 checkAndMutate2 = CheckAndMutate.newBuilder(ROWKEY2)
985 .ifMatches(new FilterList(
986 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("D"), CompareOperator.EQUAL,
987 Bytes.toBytes("a")),
988 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("E"), CompareOperator.EQUAL,
989 Bytes.toBytes("b"))))
990 .build(new Delete(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("F")));
992 results = table.checkAndMutate(Arrays.asList(checkAndMutate1, checkAndMutate2));
994 assertTrue(results.get(0).isSuccess());
995 assertNull(results.get(0).getResult());
996 assertFalse(results.get(1).isSuccess());
997 assertNull(results.get(1).getResult());
999 assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"))));
1001 result = table.get(new Get(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("F")));
1002 assertEquals("f", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("F"))));
1004 // Test for RowMutations
1005 checkAndMutate1 = CheckAndMutate.newBuilder(ROWKEY)
1006 .ifMatches(new FilterList(
1007 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL,
1008 Bytes.toBytes("a")),
1009 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL,
1010 Bytes.toBytes("b"))))
1011 .build(new RowMutations(ROWKEY)
1012 .add((Mutation) new Put(ROWKEY)
1013 .addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")))
1014 .add((Mutation) new Delete(ROWKEY).addColumns(FAMILY, Bytes.toBytes("A"))));
1016 checkAndMutate2 = CheckAndMutate.newBuilder(ROWKEY2)
1017 .ifMatches(new FilterList(
1018 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("D"), CompareOperator.EQUAL,
1019 Bytes.toBytes("a")),
1020 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("E"), CompareOperator.EQUAL,
1021 Bytes.toBytes("b"))))
1022 .build(new RowMutations(ROWKEY2)
1023 .add((Mutation) new Put(ROWKEY2)
1024 .addColumn(FAMILY, Bytes.toBytes("F"), Bytes.toBytes("g")))
1025 .add((Mutation) new Delete(ROWKEY2).addColumns(FAMILY, Bytes.toBytes("D"))));
1027 results = table.checkAndMutate(Arrays.asList(checkAndMutate1, checkAndMutate2));
1029 assertTrue(results.get(0).isSuccess());
1030 assertNull(results.get(0).getResult());
1031 assertFalse(results.get(1).isSuccess());
1032 assertNull(results.get(1).getResult());
1034 result = table.get(new Get(ROWKEY));
1035 assertNull(result.getValue(FAMILY, Bytes.toBytes("A")));
1036 assertEquals("c", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("C"))));
1038 result = table.get(new Get(ROWKEY2));
1039 assertEquals("d", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("D"))));
1040 assertEquals("f", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("F"))));
1044 @Test
1045 public void testCheckAndMutateBatchWithFilterAndTimeRange() throws Throwable {
1046 try (Table table = createTable()) {
1047 table.put(Arrays.asList(
1048 new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a"))
1049 .addColumn(FAMILY, Bytes.toBytes("B"), 100, Bytes.toBytes("b"))
1050 .addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")),
1051 new Put(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("D"), 100, Bytes.toBytes("d"))
1052 .addColumn(FAMILY, Bytes.toBytes("E"), 100, Bytes.toBytes("e"))
1053 .addColumn(FAMILY, Bytes.toBytes("F"), Bytes.toBytes("f"))));
1055 CheckAndMutate checkAndMutate1 = CheckAndMutate.newBuilder(ROWKEY)
1056 .ifMatches(new FilterList(
1057 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL,
1058 Bytes.toBytes("a")),
1059 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL,
1060 Bytes.toBytes("b"))))
1061 .timeRange(TimeRange.between(0, 101))
1062 .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("g")));
1064 CheckAndMutate checkAndMutate2 = CheckAndMutate.newBuilder(ROWKEY2)
1065 .ifMatches(new FilterList(
1066 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("D"), CompareOperator.EQUAL,
1067 Bytes.toBytes("d")),
1068 new SingleColumnValueFilter(FAMILY, Bytes.toBytes("E"), CompareOperator.EQUAL,
1069 Bytes.toBytes("e"))))
1070 .timeRange(TimeRange.between(0, 100))
1071 .build(new Put(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("F"), Bytes.toBytes("h")));
1073 List<CheckAndMutateResult> results =
1074 table.checkAndMutate(Arrays.asList(checkAndMutate1, checkAndMutate2));
1076 assertTrue(results.get(0).isSuccess());
1077 assertNull(results.get(0).getResult());
1078 assertFalse(results.get(1).isSuccess());
1079 assertNull(results.get(1).getResult());
1081 Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C")));
1082 assertEquals("g", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("C"))));
1084 result = table.get(new Get(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("F")));
1085 assertEquals("f", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("F"))));
1089 @Test
1090 public void testCheckAndIncrementBatch() throws Throwable {
1091 try (Table table = createTable()) {
1092 table.put(Arrays.asList(
1093 new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a"))
1094 .addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes(0L)),
1095 new Put(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c"))
1096 .addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes(0L))));
1098 // CheckAndIncrement with correct value
1099 CheckAndMutate checkAndMutate1 = CheckAndMutate.newBuilder(ROWKEY)
1100 .ifEquals(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a"))
1101 .build(new Increment(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), 1));
1103 // CheckAndIncrement with wrong value
1104 CheckAndMutate checkAndMutate2 = CheckAndMutate.newBuilder(ROWKEY2)
1105 .ifEquals(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("d"))
1106 .build(new Increment(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("D"), 1));
1108 List<CheckAndMutateResult> results =
1109 table.checkAndMutate(Arrays.asList(checkAndMutate1, checkAndMutate2));
1111 assertTrue(results.get(0).isSuccess());
1112 assertEquals(1, Bytes.toLong(results.get(0).getResult()
1113 .getValue(FAMILY, Bytes.toBytes("B"))));
1114 assertFalse(results.get(1).isSuccess());
1115 assertNull(results.get(1).getResult());
1117 Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
1118 assertEquals(1, Bytes.toLong(result.getValue(FAMILY, Bytes.toBytes("B"))));
1120 result = table.get(new Get(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("D")));
1121 assertEquals(0, Bytes.toLong(result.getValue(FAMILY, Bytes.toBytes("D"))));
1125 @Test
1126 public void testCheckAndAppendBatch() throws Throwable {
1127 try (Table table = createTable()) {
1128 table.put(Arrays.asList(
1129 new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a"))
1130 .addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")),
1131 new Put(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c"))
1132 .addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d"))));
1134 // CheckAndAppend with correct value
1135 CheckAndMutate checkAndMutate1 = CheckAndMutate.newBuilder(ROWKEY)
1136 .ifEquals(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a"))
1137 .build(new Append(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")));
1139 // CheckAndAppend with wrong value
1140 CheckAndMutate checkAndMutate2 = CheckAndMutate.newBuilder(ROWKEY2)
1141 .ifEquals(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("d"))
1142 .build(new Append(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d")));
1144 List<CheckAndMutateResult> results =
1145 table.checkAndMutate(Arrays.asList(checkAndMutate1, checkAndMutate2));
1147 assertTrue(results.get(0).isSuccess());
1148 assertEquals("bb", Bytes.toString(results.get(0).getResult()
1149 .getValue(FAMILY, Bytes.toBytes("B"))));
1150 assertFalse(results.get(1).isSuccess());
1151 assertNull(results.get(1).getResult());
1153 Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
1154 assertEquals("bb", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
1156 result = table.get(new Get(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("D")));
1157 assertEquals("d", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("D"))));
1161 @Test
1162 public void testCheckAndRowMutationsBatch() throws Throwable {
1163 try (Table table = createTable()) {
1164 table.put(Arrays.asList(
1165 new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"))
1166 .addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes(1L))
1167 .addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d")),
1168 new Put(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("F"), Bytes.toBytes("f"))
1169 .addColumn(FAMILY, Bytes.toBytes("G"), Bytes.toBytes(1L))
1170 .addColumn(FAMILY, Bytes.toBytes("H"), Bytes.toBytes("h")))
1173 // CheckAndIncrement with correct value
1174 CheckAndMutate checkAndMutate1 = CheckAndMutate.newBuilder(ROWKEY)
1175 .ifEquals(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"))
1176 .build(new RowMutations(ROWKEY).add(Arrays.asList(
1177 new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a")),
1178 new Delete(ROWKEY).addColumns(FAMILY, Bytes.toBytes("B")),
1179 new Increment(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"), 1L),
1180 new Append(ROWKEY).addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d"))
1181 )));
1183 // CheckAndIncrement with wrong value
1184 CheckAndMutate checkAndMutate2 = CheckAndMutate.newBuilder(ROWKEY2)
1185 .ifEquals(FAMILY, Bytes.toBytes("F"), Bytes.toBytes("a"))
1186 .build(new RowMutations(ROWKEY2).add(Arrays.asList(
1187 new Put(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("E"), Bytes.toBytes("e")),
1188 new Delete(ROWKEY2).addColumns(FAMILY, Bytes.toBytes("F")),
1189 new Increment(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("G"), 1L),
1190 new Append(ROWKEY2).addColumn(FAMILY, Bytes.toBytes("H"), Bytes.toBytes("h"))
1191 )));
1193 List<CheckAndMutateResult> results =
1194 table.checkAndMutate(Arrays.asList(checkAndMutate1, checkAndMutate2));
1196 assertTrue(results.get(0).isSuccess());
1197 assertEquals(2, Bytes.toLong(results.get(0).getResult()
1198 .getValue(FAMILY, Bytes.toBytes("C"))));
1199 assertEquals("dd", Bytes.toString(results.get(0).getResult()
1200 .getValue(FAMILY, Bytes.toBytes("D"))));
1202 assertFalse(results.get(1).isSuccess());
1203 assertNull(results.get(1).getResult());
1205 Result result = table.get(new Get(ROWKEY));
1206 assertEquals("a", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("A"))));
1207 assertNull(result.getValue(FAMILY, Bytes.toBytes("B")));
1208 assertEquals(2, Bytes.toLong(result.getValue(FAMILY, Bytes.toBytes("C"))));
1209 assertEquals("dd", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("D"))));
1211 result = table.get(new Get(ROWKEY2));
1212 assertNull(result.getValue(FAMILY, Bytes.toBytes("E")));
1213 assertEquals("f", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("F"))));
1214 assertEquals(1, Bytes.toLong(result.getValue(FAMILY, Bytes.toBytes("G"))));
1215 assertEquals("h", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("H"))));
1219 @Test
1220 public void testCheckAndMutateForNull() throws Exception {
1221 byte[] qualifier = Bytes.toBytes("Q");
1222 try (Table table = createTable()) {
1223 byte [] row1 = Bytes.toBytes("testRow1");
1224 Put put = new Put(row1);
1225 put.addColumn(FAMILY, qualifier, Bytes.toBytes("v0"));
1226 table.put(put);
1227 assertEquals("v0", Bytes.toString(
1228 table.get(new Get(row1).addColumn(FAMILY, qualifier)).getValue(FAMILY, qualifier)));
1230 CheckAndMutate checkAndMutate1 = CheckAndMutate.newBuilder(row1)
1231 .ifMatches(FAMILY, qualifier, CompareOperator.NOT_EQUAL, new byte[] {})
1232 .build(new Put(row1).addColumn(FAMILY, qualifier, Bytes.toBytes("v1")));
1233 table.checkAndMutate(checkAndMutate1);
1234 assertEquals("v1", Bytes.toString(
1235 table.get(new Get(row1).addColumn(FAMILY, qualifier)).getValue(FAMILY, qualifier)));
1237 byte [] row2 = Bytes.toBytes("testRow2");
1238 put = new Put(row2);
1239 put.addColumn(FAMILY, qualifier, new byte[] {});
1240 table.put(put);
1241 assertEquals(0,
1242 table.get(new Get(row2).addColumn(FAMILY, qualifier)).getValue(FAMILY, qualifier).length);
1244 CheckAndMutate checkAndMutate2 = CheckAndMutate.newBuilder(row2)
1245 .ifMatches(FAMILY, qualifier, CompareOperator.EQUAL, new byte[] {})
1246 .build(new Put(row2).addColumn(FAMILY, qualifier, Bytes.toBytes("v2")));
1247 table.checkAndMutate(checkAndMutate2);
1248 assertEquals("v2", Bytes.toString(
1249 table.get(new Get(row2).addColumn(FAMILY, qualifier)).getValue(FAMILY, qualifier)));
1251 byte [] row3 = Bytes.toBytes("testRow3");
1252 put = new Put(row3).addColumn(FAMILY, qualifier, Bytes.toBytes("v0"));
1253 assertNull(table.get(new Get(row3).addColumn(FAMILY, qualifier)).getValue(FAMILY, qualifier));
1254 CheckAndMutate checkAndMutate3 = CheckAndMutate.newBuilder(row3)
1255 .ifMatches(FAMILY, qualifier, CompareOperator.NOT_EQUAL, new byte[] {})
1256 .build(put);
1257 table.checkAndMutate(checkAndMutate3);
1258 assertNull(table.get(new Get(row3).addColumn(FAMILY, qualifier)).getValue(FAMILY, qualifier));
1260 CheckAndMutate checkAndMutate4 = CheckAndMutate.newBuilder(row3)
1261 .ifMatches(FAMILY, qualifier, CompareOperator.EQUAL, new byte[] {})
1262 .build(put);
1263 table.checkAndMutate(checkAndMutate4);
1264 assertEquals("v0", Bytes.toString(
1265 table.get(new Get(row3).addColumn(FAMILY, qualifier)).getValue(FAMILY, qualifier)));