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
;
28 import java
.io
.IOException
;
29 import java
.util
.Arrays
;
30 import java
.util
.Collections
;
31 import java
.util
.List
;
32 import org
.apache
.hadoop
.hbase
.CompareOperator
;
33 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
34 import org
.apache
.hadoop
.hbase
.HBaseTestingUtil
;
35 import org
.apache
.hadoop
.hbase
.TableName
;
36 import org
.apache
.hadoop
.hbase
.filter
.BinaryComparator
;
37 import org
.apache
.hadoop
.hbase
.filter
.FamilyFilter
;
38 import org
.apache
.hadoop
.hbase
.filter
.FilterList
;
39 import org
.apache
.hadoop
.hbase
.filter
.QualifierFilter
;
40 import org
.apache
.hadoop
.hbase
.filter
.SingleColumnValueFilter
;
41 import org
.apache
.hadoop
.hbase
.filter
.TimestampsFilter
;
42 import org
.apache
.hadoop
.hbase
.io
.TimeRange
;
43 import org
.apache
.hadoop
.hbase
.regionserver
.NoSuchColumnFamilyException
;
44 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
45 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
46 import org
.junit
.AfterClass
;
47 import org
.junit
.BeforeClass
;
48 import org
.junit
.ClassRule
;
49 import org
.junit
.Rule
;
50 import org
.junit
.Test
;
51 import org
.junit
.experimental
.categories
.Category
;
52 import org
.junit
.rules
.TestName
;
54 @Category(MediumTests
.class)
55 public class TestCheckAndMutate
{
58 public static final HBaseClassTestRule CLASS_RULE
=
59 HBaseClassTestRule
.forClass(TestCheckAndMutate
.class);
61 private static final HBaseTestingUtil TEST_UTIL
= new HBaseTestingUtil();
62 private static final byte[] ROWKEY
= Bytes
.toBytes("12345");
63 private static final byte[] ROWKEY2
= Bytes
.toBytes("67890");
64 private static final byte[] ROWKEY3
= Bytes
.toBytes("abcde");
65 private static final byte[] ROWKEY4
= Bytes
.toBytes("fghij");
66 private static final byte[] FAMILY
= Bytes
.toBytes("cf");
69 public TestName name
= new TestName();
72 public static void setUpBeforeClass() throws Exception
{
73 TEST_UTIL
.startMiniCluster();
77 public static void tearDownAfterClass() throws Exception
{
78 TEST_UTIL
.shutdownMiniCluster();
81 private Table
createTable()
82 throws IOException
, InterruptedException
{
83 final TableName tableName
= TableName
.valueOf(name
.getMethodName());
84 Table table
= TEST_UTIL
.createTable(tableName
, FAMILY
);
85 TEST_UTIL
.waitTableAvailable(tableName
.getName(), 5000);
89 private void putOneRow(Table table
) throws IOException
{
90 Put put
= new Put(ROWKEY
);
91 put
.addColumn(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a"));
92 put
.addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("b"));
93 put
.addColumn(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("c"));
97 private void getOneRowAndAssertAllExist(final Table table
) throws IOException
{
98 Get get
= new Get(ROWKEY
);
99 Result result
= table
.get(get
);
100 assertTrue("Column A value should be a",
101 Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("A"))).equals("a"));
102 assertTrue("Column B value should be b",
103 Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("B"))).equals("b"));
104 assertTrue("Column C value should be c",
105 Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("C"))).equals("c"));
108 private void getOneRowAndAssertAllButCExist(final Table table
) throws IOException
{
109 Get get
= new Get(ROWKEY
);
110 Result result
= table
.get(get
);
111 assertTrue("Column A value should be a",
112 Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("A"))).equals("a"));
113 assertTrue("Column B value should be b",
114 Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("B"))).equals("b"));
115 assertTrue("Column C should not exist",
116 result
.getValue(FAMILY
, Bytes
.toBytes("C")) == null);
119 private RowMutations
makeRowMutationsWithColumnCDeleted() throws IOException
{
120 RowMutations rm
= new RowMutations(ROWKEY
, 2);
121 Put put
= new Put(ROWKEY
);
122 put
.addColumn(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a"));
123 put
.addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("b"));
125 Delete del
= new Delete(ROWKEY
);
126 del
.addColumn(FAMILY
, Bytes
.toBytes("C"));
131 private RowMutations
getBogusRowMutations() throws IOException
{
132 Put p
= new Put(ROWKEY
);
133 byte[] value
= new byte[0];
134 p
.addColumn(new byte[]{'b', 'o', 'g', 'u', 's'}, new byte[]{'A'}, value
);
135 RowMutations rm
= new RowMutations(ROWKEY
);
140 // Tests for old checkAndMutate API
144 public void testCheckAndMutateForOldApi() throws Throwable
{
145 try (Table table
= createTable()) {
148 // get row back and assert the values
149 getOneRowAndAssertAllExist(table
);
151 // put the same row again with C column deleted
152 RowMutations rm
= makeRowMutationsWithColumnCDeleted();
153 boolean res
= table
.checkAndMutate(ROWKEY
, FAMILY
).qualifier(Bytes
.toBytes("A"))
154 .ifEquals(Bytes
.toBytes("a")).thenMutate(rm
);
157 // get row back and assert the values
158 getOneRowAndAssertAllButCExist(table
);
160 // Test that we get a region level exception
162 rm
= getBogusRowMutations();
163 table
.checkAndMutate(ROWKEY
, FAMILY
).qualifier(Bytes
.toBytes("A"))
164 .ifEquals(Bytes
.toBytes("a")).thenMutate(rm
);
165 fail("Expected NoSuchColumnFamilyException");
166 } catch (NoSuchColumnFamilyException e
) {
168 } catch (RetriesExhaustedException e
) {
169 assertThat(e
.getCause(), instanceOf(NoSuchColumnFamilyException
.class));
176 public void testCheckAndMutateWithSingleFilterForOldApi() throws Throwable
{
177 try (Table table
= createTable()) {
180 // get row back and assert the values
181 getOneRowAndAssertAllExist(table
);
184 boolean ok
= table
.checkAndMutate(ROWKEY
, new SingleColumnValueFilter(FAMILY
,
185 Bytes
.toBytes("A"), CompareOperator
.EQUAL
, Bytes
.toBytes("a")))
186 .thenPut(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("d")));
189 Result result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D")));
190 assertEquals("d", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("D"))));
193 ok
= table
.checkAndMutate(ROWKEY
, new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"),
194 CompareOperator
.EQUAL
, Bytes
.toBytes("b")))
195 .thenPut(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("E"), Bytes
.toBytes("e")));
198 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("E"))));
200 // Delete with success
201 ok
= table
.checkAndMutate(ROWKEY
, new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"),
202 CompareOperator
.EQUAL
, Bytes
.toBytes("a")))
203 .thenDelete(new Delete(ROWKEY
).addColumns(FAMILY
, Bytes
.toBytes("D")));
206 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D"))));
208 // Mutate with success
209 ok
= table
.checkAndMutate(ROWKEY
, new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("B"),
210 CompareOperator
.EQUAL
, Bytes
.toBytes("b")))
211 .thenMutate(new RowMutations(ROWKEY
)
212 .add((Mutation
) new Put(ROWKEY
)
213 .addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("d")))
214 .add((Mutation
) new Delete(ROWKEY
).addColumns(FAMILY
, Bytes
.toBytes("A"))));
217 result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D")));
218 assertEquals("d", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("D"))));
220 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"))));
226 public void testCheckAndMutateWithMultipleFiltersForOldApi() throws Throwable
{
227 try (Table table
= createTable()) {
230 // get row back and assert the values
231 getOneRowAndAssertAllExist(table
);
234 boolean ok
= table
.checkAndMutate(ROWKEY
, new FilterList(
235 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"), CompareOperator
.EQUAL
,
237 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("B"), CompareOperator
.EQUAL
,
240 .thenPut(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("d")));
243 Result result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D")));
244 assertEquals("d", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("D"))));
247 ok
= table
.checkAndMutate(ROWKEY
, new FilterList(
248 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"), CompareOperator
.EQUAL
,
250 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("B"), CompareOperator
.EQUAL
,
253 .thenPut(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("E"), Bytes
.toBytes("e")));
256 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("E"))));
258 // Delete with success
259 ok
= table
.checkAndMutate(ROWKEY
, new FilterList(
260 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"), CompareOperator
.EQUAL
,
262 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("B"), CompareOperator
.EQUAL
,
265 .thenDelete(new Delete(ROWKEY
).addColumns(FAMILY
, Bytes
.toBytes("D")));
268 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D"))));
270 // Mutate with success
271 ok
= table
.checkAndMutate(ROWKEY
, new FilterList(
272 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"), CompareOperator
.EQUAL
,
274 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("B"), CompareOperator
.EQUAL
,
277 .thenMutate(new RowMutations(ROWKEY
)
278 .add((Mutation
) new Put(ROWKEY
)
279 .addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("d")))
280 .add((Mutation
) new Delete(ROWKEY
).addColumns(FAMILY
, Bytes
.toBytes("A"))));
283 result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D")));
284 assertEquals("d", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("D"))));
286 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"))));
292 public void testCheckAndMutateWithTimestampFilterForOldApi() throws Throwable
{
293 try (Table table
= createTable()) {
294 // Put with specifying the timestamp
295 table
.put(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"), 100, Bytes
.toBytes("a")));
298 boolean ok
= table
.checkAndMutate(ROWKEY
, new FilterList(
299 new FamilyFilter(CompareOperator
.EQUAL
, new BinaryComparator(FAMILY
)),
300 new QualifierFilter(CompareOperator
.EQUAL
, new BinaryComparator(Bytes
.toBytes("A"))),
301 new TimestampsFilter(Collections
.singletonList(100L))
303 .thenPut(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("b")));
306 Result result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B")));
307 assertEquals("b", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("B"))));
310 ok
= table
.checkAndMutate(ROWKEY
, new FilterList(
311 new FamilyFilter(CompareOperator
.EQUAL
, new BinaryComparator(FAMILY
)),
312 new QualifierFilter(CompareOperator
.EQUAL
, new BinaryComparator(Bytes
.toBytes("A"))),
313 new TimestampsFilter(Collections
.singletonList(101L))
315 .thenPut(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("c")));
318 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("C"))));
324 public void testCheckAndMutateWithFilterAndTimeRangeForOldApi() throws Throwable
{
325 try (Table table
= createTable()) {
326 // Put with specifying the timestamp
327 table
.put(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"), 100, Bytes
.toBytes("a")));
330 boolean ok
= table
.checkAndMutate(ROWKEY
, new SingleColumnValueFilter(FAMILY
,
331 Bytes
.toBytes("A"), CompareOperator
.EQUAL
, Bytes
.toBytes("a")))
332 .timeRange(TimeRange
.between(0, 101))
333 .thenPut(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("b")));
336 Result result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B")));
337 assertEquals("b", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("B"))));
340 ok
= table
.checkAndMutate(ROWKEY
, new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"),
341 CompareOperator
.EQUAL
, Bytes
.toBytes("a")))
342 .timeRange(TimeRange
.between(0, 100))
343 .thenPut(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("c")));
346 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("C"))));
350 @Test(expected
= NullPointerException
.class)
352 public void testCheckAndMutateWithoutConditionForOldApi() throws Throwable
{
353 try (Table table
= createTable()) {
354 table
.checkAndMutate(ROWKEY
, FAMILY
)
355 .thenPut(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("d")));
359 // Tests for new CheckAndMutate API
362 public void testCheckAndMutate() throws Throwable
{
363 try (Table table
= createTable()) {
366 // get row back and assert the values
367 getOneRowAndAssertAllExist(table
);
369 // put the same row again with C column deleted
370 RowMutations rm
= makeRowMutationsWithColumnCDeleted();
371 CheckAndMutateResult res
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
372 .ifEquals(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a"))
374 assertTrue(res
.isSuccess());
375 assertNull(res
.getResult());
377 // get row back and assert the values
378 getOneRowAndAssertAllButCExist(table
);
380 // Test that we get a region level exception
382 rm
= getBogusRowMutations();
383 table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
384 .ifEquals(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a"))
386 fail("Expected NoSuchColumnFamilyException");
387 } catch (NoSuchColumnFamilyException e
) {
389 } catch (RetriesExhaustedException e
) {
390 assertThat(e
.getCause(), instanceOf(NoSuchColumnFamilyException
.class));
396 public void testCheckAndMutateWithSingleFilter() throws Throwable
{
397 try (Table table
= createTable()) {
400 // get row back and assert the values
401 getOneRowAndAssertAllExist(table
);
404 CheckAndMutateResult result
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
405 .ifMatches(new SingleColumnValueFilter(FAMILY
,
406 Bytes
.toBytes("A"), CompareOperator
.EQUAL
, Bytes
.toBytes("a")))
407 .build(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("d"))));
408 assertTrue(result
.isSuccess());
409 assertNull(result
.getResult());
411 Result r
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D")));
412 assertEquals("d", Bytes
.toString(r
.getValue(FAMILY
, Bytes
.toBytes("D"))));
415 result
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
416 .ifMatches(new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"),
417 CompareOperator
.EQUAL
, Bytes
.toBytes("b")))
418 .build(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("E"), Bytes
.toBytes("e"))));
419 assertFalse(result
.isSuccess());
420 assertNull(result
.getResult());
422 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("E"))));
424 // Delete with success
425 result
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
426 .ifMatches(new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"),
427 CompareOperator
.EQUAL
, Bytes
.toBytes("a")))
428 .build(new Delete(ROWKEY
).addColumns(FAMILY
, Bytes
.toBytes("D"))));
429 assertTrue(result
.isSuccess());
430 assertNull(result
.getResult());
432 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D"))));
434 // Mutate with success
435 result
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
436 .ifMatches(new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("B"),
437 CompareOperator
.EQUAL
, Bytes
.toBytes("b")))
438 .build(new RowMutations(ROWKEY
)
439 .add((Mutation
) new Put(ROWKEY
)
440 .addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("d")))
441 .add((Mutation
) new Delete(ROWKEY
).addColumns(FAMILY
, Bytes
.toBytes("A")))));
442 assertTrue(result
.isSuccess());
443 assertNull(result
.getResult());
445 r
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D")));
446 assertEquals("d", Bytes
.toString(r
.getValue(FAMILY
, Bytes
.toBytes("D"))));
448 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"))));
453 public void testCheckAndMutateWithMultipleFilters() throws Throwable
{
454 try (Table table
= createTable()) {
457 // get row back and assert the values
458 getOneRowAndAssertAllExist(table
);
461 CheckAndMutateResult result
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
462 .ifMatches(new FilterList(
463 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"), CompareOperator
.EQUAL
,
465 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("B"), CompareOperator
.EQUAL
,
466 Bytes
.toBytes("b"))))
467 .build(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("d"))));
468 assertTrue(result
.isSuccess());
469 assertNull(result
.getResult());
471 Result r
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D")));
472 assertEquals("d", Bytes
.toString(r
.getValue(FAMILY
, Bytes
.toBytes("D"))));
475 result
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
476 .ifMatches(new FilterList(
477 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"), CompareOperator
.EQUAL
,
479 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("B"), CompareOperator
.EQUAL
,
480 Bytes
.toBytes("c"))))
481 .build(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("E"), Bytes
.toBytes("e"))));
482 assertFalse(result
.isSuccess());
483 assertNull(result
.getResult());
485 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("E"))));
487 // Delete with success
488 result
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
489 .ifMatches(new FilterList(
490 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"), CompareOperator
.EQUAL
,
492 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("B"), CompareOperator
.EQUAL
,
493 Bytes
.toBytes("b"))))
494 .build(new Delete(ROWKEY
).addColumns(FAMILY
, Bytes
.toBytes("D"))));
495 assertTrue(result
.isSuccess());
496 assertNull(result
.getResult());
498 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D"))));
500 // Mutate with success
501 result
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
502 .ifMatches(new FilterList(
503 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"), CompareOperator
.EQUAL
,
505 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("B"), CompareOperator
.EQUAL
,
506 Bytes
.toBytes("b"))))
507 .build(new RowMutations(ROWKEY
)
508 .add((Mutation
) new Put(ROWKEY
)
509 .addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("d")))
510 .add((Mutation
) new Delete(ROWKEY
).addColumns(FAMILY
, Bytes
.toBytes("A")))));
511 assertTrue(result
.isSuccess());
512 assertNull(result
.getResult());
514 r
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D")));
515 assertEquals("d", Bytes
.toString(r
.getValue(FAMILY
, Bytes
.toBytes("D"))));
517 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"))));
522 public void testCheckAndMutateWithTimestampFilter() throws Throwable
{
523 try (Table table
= createTable()) {
524 // Put with specifying the timestamp
525 table
.put(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"), 100, Bytes
.toBytes("a")));
528 CheckAndMutateResult result
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
529 .ifMatches(new FilterList(
530 new FamilyFilter(CompareOperator
.EQUAL
, new BinaryComparator(FAMILY
)),
531 new QualifierFilter(CompareOperator
.EQUAL
, new BinaryComparator(Bytes
.toBytes("A"))),
532 new TimestampsFilter(Collections
.singletonList(100L))))
533 .build(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("b"))));
534 assertTrue(result
.isSuccess());
535 assertNull(result
.getResult());
537 Result r
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B")));
538 assertEquals("b", Bytes
.toString(r
.getValue(FAMILY
, Bytes
.toBytes("B"))));
541 result
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
542 .ifMatches(new FilterList(
543 new FamilyFilter(CompareOperator
.EQUAL
, new BinaryComparator(FAMILY
)),
544 new QualifierFilter(CompareOperator
.EQUAL
, new BinaryComparator(Bytes
.toBytes("A"))),
545 new TimestampsFilter(Collections
.singletonList(101L))))
546 .build(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("c"))));
547 assertFalse(result
.isSuccess());
548 assertNull(result
.getResult());
550 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("C"))));
555 public void testCheckAndMutateWithFilterAndTimeRange() throws Throwable
{
556 try (Table table
= createTable()) {
557 // Put with specifying the timestamp
558 table
.put(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"), 100, Bytes
.toBytes("a")));
561 CheckAndMutateResult result
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
562 .ifMatches(new SingleColumnValueFilter(FAMILY
,
563 Bytes
.toBytes("A"), CompareOperator
.EQUAL
, Bytes
.toBytes("a")))
564 .timeRange(TimeRange
.between(0, 101))
565 .build(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("b"))));
566 assertTrue(result
.isSuccess());
567 assertNull(result
.getResult());
569 Result r
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B")));
570 assertEquals("b", Bytes
.toString(r
.getValue(FAMILY
, Bytes
.toBytes("B"))));
573 result
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
574 .ifMatches(new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"),
575 CompareOperator
.EQUAL
, Bytes
.toBytes("a")))
576 .timeRange(TimeRange
.between(0, 100))
577 .build(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("c"))));
578 assertFalse(result
.isSuccess());
579 assertNull(result
.getResult());
581 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("C"))));
585 @Test(expected
= IllegalStateException
.class)
586 public void testCheckAndMutateBuilderWithoutCondition() {
587 CheckAndMutate
.newBuilder(ROWKEY
)
588 .build(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("d")));
592 public void testCheckAndIncrement() throws Throwable
{
593 try (Table table
= createTable()) {
594 table
.put(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a")));
596 // CheckAndIncrement with correct value
597 CheckAndMutateResult res
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
598 .ifEquals(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a"))
599 .build(new Increment(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B"), 1)));
600 assertTrue(res
.isSuccess());
601 assertEquals(1, Bytes
.toLong(res
.getResult().getValue(FAMILY
, Bytes
.toBytes("B"))));
603 Result result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B")));
604 assertEquals(1, Bytes
.toLong(result
.getValue(FAMILY
, Bytes
.toBytes("B"))));
606 // CheckAndIncrement with wrong value
607 res
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
608 .ifEquals(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("b"))
609 .build(new Increment(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B"), 1)));
610 assertFalse(res
.isSuccess());
611 assertNull(res
.getResult());
613 result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B")));
614 assertEquals(1, Bytes
.toLong(result
.getValue(FAMILY
, Bytes
.toBytes("B"))));
616 table
.put(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("c")));
618 // CheckAndIncrement with a filter and correct value
619 res
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
620 .ifMatches(new FilterList(
621 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"), CompareOperator
.EQUAL
,
623 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("C"), CompareOperator
.EQUAL
,
624 Bytes
.toBytes("c"))))
625 .build(new Increment(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B"), 2)));
626 assertTrue(res
.isSuccess());
627 assertEquals(3, Bytes
.toLong(res
.getResult().getValue(FAMILY
, Bytes
.toBytes("B"))));
629 result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B")));
630 assertEquals(3, Bytes
.toLong(result
.getValue(FAMILY
, Bytes
.toBytes("B"))));
632 // CheckAndIncrement with a filter and correct value
633 res
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
634 .ifMatches(new FilterList(
635 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"), CompareOperator
.EQUAL
,
637 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("C"), CompareOperator
.EQUAL
,
638 Bytes
.toBytes("d"))))
639 .build(new Increment(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B"), 2)));
640 assertFalse(res
.isSuccess());
641 assertNull(res
.getResult());
643 result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B")));
644 assertEquals(3, Bytes
.toLong(result
.getValue(FAMILY
, Bytes
.toBytes("B"))));
649 public void testCheckAndAppend() throws Throwable
{
650 try (Table table
= createTable()) {
651 table
.put(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a")));
653 // CheckAndAppend with correct value
654 CheckAndMutateResult res
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
655 .ifEquals(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a"))
656 .build(new Append(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("b"))));
657 assertTrue(res
.isSuccess());
658 assertEquals("b", Bytes
.toString(res
.getResult().getValue(FAMILY
, Bytes
.toBytes("B"))));
660 Result result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B")));
661 assertEquals("b", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("B"))));
663 // CheckAndAppend with correct value
664 res
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
665 .ifEquals(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("b"))
666 .build(new Append(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("b"))));
667 assertFalse(res
.isSuccess());
668 assertNull(res
.getResult());
670 result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B")));
671 assertEquals("b", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("B"))));
673 table
.put(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("c")));
675 // CheckAndAppend with a filter and correct value
676 res
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
677 .ifMatches(new FilterList(
678 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"), CompareOperator
.EQUAL
,
680 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("C"), CompareOperator
.EQUAL
,
681 Bytes
.toBytes("c"))))
682 .build(new Append(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("bb"))));
683 assertTrue(res
.isSuccess());
684 assertEquals("bbb", Bytes
.toString(res
.getResult().getValue(FAMILY
, Bytes
.toBytes("B"))));
686 result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B")));
687 assertEquals("bbb", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("B"))));
689 // CheckAndAppend with a filter and wrong value
690 res
= table
.checkAndMutate(CheckAndMutate
.newBuilder(ROWKEY
)
691 .ifMatches(new FilterList(
692 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"), CompareOperator
.EQUAL
,
694 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("C"), CompareOperator
.EQUAL
,
695 Bytes
.toBytes("d"))))
696 .build(new Append(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("bb"))));
697 assertFalse(res
.isSuccess());
698 assertNull(res
.getResult());
700 result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B")));
701 assertEquals("bbb", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("B"))));
706 public void testCheckAndRowMutations() throws Throwable
{
707 final byte[] q1
= Bytes
.toBytes("q1");
708 final byte[] q2
= Bytes
.toBytes("q2");
709 final byte[] q3
= Bytes
.toBytes("q3");
710 final byte[] q4
= Bytes
.toBytes("q4");
711 final String v1
= "v1";
713 try (Table table
= createTable()) {
715 table
.put(Arrays
.asList(
716 new Put(ROWKEY
).addColumn(FAMILY
, q2
, Bytes
.toBytes("toBeDeleted")),
717 new Put(ROWKEY
).addColumn(FAMILY
, q3
, Bytes
.toBytes(5L)),
718 new Put(ROWKEY
).addColumn(FAMILY
, q4
, Bytes
.toBytes("a"))));
720 // Do CheckAndRowMutations
721 CheckAndMutate checkAndMutate
= CheckAndMutate
.newBuilder(ROWKEY
)
722 .ifNotExists(FAMILY
, q1
)
723 .build(new RowMutations(ROWKEY
).add(Arrays
.asList(
724 new Put(ROWKEY
).addColumn(FAMILY
, q1
, Bytes
.toBytes(v1
)),
725 new Delete(ROWKEY
).addColumns(FAMILY
, q2
),
726 new Increment(ROWKEY
).addColumn(FAMILY
, q3
, 1),
727 new Append(ROWKEY
).addColumn(FAMILY
, q4
, Bytes
.toBytes("b"))))
730 CheckAndMutateResult result
= table
.checkAndMutate(checkAndMutate
);
731 assertTrue(result
.isSuccess());
732 assertEquals(6L, Bytes
.toLong(result
.getResult().getValue(FAMILY
, q3
)));
733 assertEquals("ab", Bytes
.toString(result
.getResult().getValue(FAMILY
, q4
)));
736 Result r
= table
.get(new Get(ROWKEY
));
737 assertEquals(v1
, Bytes
.toString(r
.getValue(FAMILY
, q1
)));
738 assertNull(r
.getValue(FAMILY
, q2
));
739 assertEquals(6L, Bytes
.toLong(r
.getValue(FAMILY
, q3
)));
740 assertEquals("ab", Bytes
.toString(r
.getValue(FAMILY
, q4
)));
742 // Do CheckAndRowMutations again
743 checkAndMutate
= CheckAndMutate
.newBuilder(ROWKEY
)
744 .ifNotExists(FAMILY
, q1
)
745 .build(new RowMutations(ROWKEY
).add(Arrays
.asList(
746 new Delete(ROWKEY
).addColumns(FAMILY
, q1
),
747 new Put(ROWKEY
).addColumn(FAMILY
, q2
, Bytes
.toBytes(v1
)),
748 new Increment(ROWKEY
).addColumn(FAMILY
, q3
, 1),
749 new Append(ROWKEY
).addColumn(FAMILY
, q4
, Bytes
.toBytes("b"))))
752 result
= table
.checkAndMutate(checkAndMutate
);
753 assertFalse(result
.isSuccess());
754 assertNull(result
.getResult());
757 r
= table
.get(new Get(ROWKEY
));
758 assertEquals(v1
, Bytes
.toString(r
.getValue(FAMILY
, q1
)));
759 assertNull(r
.getValue(FAMILY
, q2
));
760 assertEquals(6L, Bytes
.toLong(r
.getValue(FAMILY
, q3
)));
761 assertEquals("ab", Bytes
.toString(r
.getValue(FAMILY
, q4
)));
765 // Tests for batch version of checkAndMutate
768 public void testCheckAndMutateBatch() throws Throwable
{
769 try (Table table
= createTable()) {
770 table
.put(Arrays
.asList(
771 new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a")),
772 new Put(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("b")),
773 new Put(ROWKEY3
).addColumn(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("c")),
774 new Put(ROWKEY4
).addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("d"))));
777 CheckAndMutate checkAndMutate1
= CheckAndMutate
.newBuilder(ROWKEY
)
778 .ifEquals(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a"))
779 .build(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("e")));
781 CheckAndMutate checkAndMutate2
= CheckAndMutate
.newBuilder(ROWKEY2
)
782 .ifEquals(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("a"))
783 .build(new Put(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("f")));
785 List
<CheckAndMutateResult
> results
=
786 table
.checkAndMutate(Arrays
.asList(checkAndMutate1
, checkAndMutate2
));
788 assertTrue(results
.get(0).isSuccess());
789 assertNull(results
.get(0).getResult());
790 assertFalse(results
.get(1).isSuccess());
791 assertNull(results
.get(1).getResult());
793 Result result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A")));
794 assertEquals("e", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("A"))));
796 result
= table
.get(new Get(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("B")));
797 assertEquals("b", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("B"))));
800 checkAndMutate1
= CheckAndMutate
.newBuilder(ROWKEY
)
801 .ifEquals(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("e"))
802 .build(new Delete(ROWKEY
));
804 checkAndMutate2
= CheckAndMutate
.newBuilder(ROWKEY2
)
805 .ifEquals(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("a"))
806 .build(new Delete(ROWKEY2
));
808 results
= table
.checkAndMutate(Arrays
.asList(checkAndMutate1
, checkAndMutate2
));
810 assertTrue(results
.get(0).isSuccess());
811 assertNull(results
.get(0).getResult());
812 assertFalse(results
.get(1).isSuccess());
813 assertNull(results
.get(1).getResult());
815 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"))));
817 result
= table
.get(new Get(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("B")));
818 assertEquals("b", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("B"))));
820 // Test for RowMutations
821 checkAndMutate1
= CheckAndMutate
.newBuilder(ROWKEY3
)
822 .ifEquals(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("c"))
823 .build(new RowMutations(ROWKEY3
)
824 .add((Mutation
) new Put(ROWKEY3
)
825 .addColumn(FAMILY
, Bytes
.toBytes("F"), Bytes
.toBytes("f")))
826 .add((Mutation
) new Delete(ROWKEY3
).addColumns(FAMILY
, Bytes
.toBytes("C"))));
828 checkAndMutate2
= CheckAndMutate
.newBuilder(ROWKEY4
)
829 .ifEquals(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("f"))
830 .build(new RowMutations(ROWKEY4
)
831 .add((Mutation
) new Put(ROWKEY4
)
832 .addColumn(FAMILY
, Bytes
.toBytes("F"), Bytes
.toBytes("f")))
833 .add((Mutation
) new Delete(ROWKEY4
).addColumns(FAMILY
, Bytes
.toBytes("D"))));
835 results
= table
.checkAndMutate(Arrays
.asList(checkAndMutate1
, checkAndMutate2
));
837 assertTrue(results
.get(0).isSuccess());
838 assertNull(results
.get(0).getResult());
839 assertFalse(results
.get(1).isSuccess());
840 assertNull(results
.get(1).getResult());
842 result
= table
.get(new Get(ROWKEY3
));
843 assertEquals("f", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("F"))));
844 assertNull(result
.getValue(FAMILY
, Bytes
.toBytes("D")));
846 result
= table
.get(new Get(ROWKEY4
));
847 assertNull(result
.getValue(FAMILY
, Bytes
.toBytes("F")));
848 assertEquals("d", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("D"))));
853 public void testCheckAndMutateBatch2() throws Throwable
{
854 try (Table table
= createTable()) {
855 table
.put(Arrays
.asList(
856 new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a")),
857 new Put(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("b")),
858 new Put(ROWKEY3
).addColumn(FAMILY
, Bytes
.toBytes("C"), 100, Bytes
.toBytes("c")),
859 new Put(ROWKEY4
).addColumn(FAMILY
, Bytes
.toBytes("D"), 100, Bytes
.toBytes("d"))));
861 // Test for ifNotExists()
862 CheckAndMutate checkAndMutate1
= CheckAndMutate
.newBuilder(ROWKEY
)
863 .ifNotExists(FAMILY
, Bytes
.toBytes("B"))
864 .build(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("e")));
866 CheckAndMutate checkAndMutate2
= CheckAndMutate
.newBuilder(ROWKEY2
)
867 .ifNotExists(FAMILY
, Bytes
.toBytes("B"))
868 .build(new Put(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("f")));
870 List
<CheckAndMutateResult
> results
=
871 table
.checkAndMutate(Arrays
.asList(checkAndMutate1
, checkAndMutate2
));
873 assertTrue(results
.get(0).isSuccess());
874 assertNull(results
.get(0).getResult());
875 assertFalse(results
.get(1).isSuccess());
876 assertNull(results
.get(1).getResult());
878 Result result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A")));
879 assertEquals("e", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("A"))));
881 result
= table
.get(new Get(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("B")));
882 assertEquals("b", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("B"))));
884 // Test for ifMatches()
885 checkAndMutate1
= CheckAndMutate
.newBuilder(ROWKEY
)
886 .ifMatches(FAMILY
, Bytes
.toBytes("A"), CompareOperator
.NOT_EQUAL
, Bytes
.toBytes("a"))
887 .build(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a")));
889 checkAndMutate2
= CheckAndMutate
.newBuilder(ROWKEY2
)
890 .ifMatches(FAMILY
, Bytes
.toBytes("B"), CompareOperator
.GREATER
, Bytes
.toBytes("b"))
891 .build(new Put(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("f")));
893 results
= table
.checkAndMutate(Arrays
.asList(checkAndMutate1
, checkAndMutate2
));
895 assertTrue(results
.get(0).isSuccess());
896 assertNull(results
.get(0).getResult());
897 assertFalse(results
.get(1).isSuccess());
898 assertNull(results
.get(1).getResult());
900 result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A")));
901 assertEquals("a", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("A"))));
903 result
= table
.get(new Get(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("B")));
904 assertEquals("b", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("B"))));
906 // Test for timeRange()
907 checkAndMutate1
= CheckAndMutate
.newBuilder(ROWKEY3
)
908 .ifEquals(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("c"))
909 .timeRange(TimeRange
.between(0, 101))
910 .build(new Put(ROWKEY3
).addColumn(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("e")));
912 checkAndMutate2
= CheckAndMutate
.newBuilder(ROWKEY4
)
913 .ifEquals(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("d"))
914 .timeRange(TimeRange
.between(0, 100))
915 .build(new Put(ROWKEY4
).addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("f")));
917 results
= table
.checkAndMutate(Arrays
.asList(checkAndMutate1
, checkAndMutate2
));
919 assertTrue(results
.get(0).isSuccess());
920 assertNull(results
.get(0).getResult());
921 assertFalse(results
.get(1).isSuccess());
922 assertNull(results
.get(1).getResult());
924 result
= table
.get(new Get(ROWKEY3
).addColumn(FAMILY
, Bytes
.toBytes("C")));
925 assertEquals("e", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("C"))));
927 result
= table
.get(new Get(ROWKEY4
).addColumn(FAMILY
, Bytes
.toBytes("D")));
928 assertEquals("d", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("D"))));
933 public void testCheckAndMutateBatchWithFilter() throws Throwable
{
934 try (Table table
= createTable()) {
935 table
.put(Arrays
.asList(
937 .addColumn(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a"))
938 .addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("b"))
939 .addColumn(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("c")),
941 .addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("d"))
942 .addColumn(FAMILY
, Bytes
.toBytes("E"), Bytes
.toBytes("e"))
943 .addColumn(FAMILY
, Bytes
.toBytes("F"), Bytes
.toBytes("f"))));
946 CheckAndMutate checkAndMutate1
= CheckAndMutate
.newBuilder(ROWKEY
)
947 .ifMatches(new FilterList(
948 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"), CompareOperator
.EQUAL
,
950 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("B"), CompareOperator
.EQUAL
,
951 Bytes
.toBytes("b"))))
952 .build(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("g")));
954 CheckAndMutate checkAndMutate2
= CheckAndMutate
.newBuilder(ROWKEY2
)
955 .ifMatches(new FilterList(
956 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("D"), CompareOperator
.EQUAL
,
958 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("E"), CompareOperator
.EQUAL
,
959 Bytes
.toBytes("b"))))
960 .build(new Put(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("F"), Bytes
.toBytes("h")));
962 List
<CheckAndMutateResult
> results
=
963 table
.checkAndMutate(Arrays
.asList(checkAndMutate1
, checkAndMutate2
));
965 assertTrue(results
.get(0).isSuccess());
966 assertNull(results
.get(0).getResult());
967 assertFalse(results
.get(1).isSuccess());
968 assertNull(results
.get(1).getResult());
970 Result result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("C")));
971 assertEquals("g", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("C"))));
973 result
= table
.get(new Get(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("F")));
974 assertEquals("f", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("F"))));
977 checkAndMutate1
= CheckAndMutate
.newBuilder(ROWKEY
)
978 .ifMatches(new FilterList(
979 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"), CompareOperator
.EQUAL
,
981 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("B"), CompareOperator
.EQUAL
,
982 Bytes
.toBytes("b"))))
983 .build(new Delete(ROWKEY
).addColumns(FAMILY
, Bytes
.toBytes("C")));
985 checkAndMutate2
= CheckAndMutate
.newBuilder(ROWKEY2
)
986 .ifMatches(new FilterList(
987 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("D"), CompareOperator
.EQUAL
,
989 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("E"), CompareOperator
.EQUAL
,
990 Bytes
.toBytes("b"))))
991 .build(new Delete(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("F")));
993 results
= table
.checkAndMutate(Arrays
.asList(checkAndMutate1
, checkAndMutate2
));
995 assertTrue(results
.get(0).isSuccess());
996 assertNull(results
.get(0).getResult());
997 assertFalse(results
.get(1).isSuccess());
998 assertNull(results
.get(1).getResult());
1000 assertFalse(table
.exists(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("C"))));
1002 result
= table
.get(new Get(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("F")));
1003 assertEquals("f", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("F"))));
1005 // Test for RowMutations
1006 checkAndMutate1
= CheckAndMutate
.newBuilder(ROWKEY
)
1007 .ifMatches(new FilterList(
1008 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"), CompareOperator
.EQUAL
,
1009 Bytes
.toBytes("a")),
1010 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("B"), CompareOperator
.EQUAL
,
1011 Bytes
.toBytes("b"))))
1012 .build(new RowMutations(ROWKEY
)
1013 .add((Mutation
) new Put(ROWKEY
)
1014 .addColumn(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("c")))
1015 .add((Mutation
) new Delete(ROWKEY
).addColumns(FAMILY
, Bytes
.toBytes("A"))));
1017 checkAndMutate2
= CheckAndMutate
.newBuilder(ROWKEY2
)
1018 .ifMatches(new FilterList(
1019 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("D"), CompareOperator
.EQUAL
,
1020 Bytes
.toBytes("a")),
1021 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("E"), CompareOperator
.EQUAL
,
1022 Bytes
.toBytes("b"))))
1023 .build(new RowMutations(ROWKEY2
)
1024 .add((Mutation
) new Put(ROWKEY2
)
1025 .addColumn(FAMILY
, Bytes
.toBytes("F"), Bytes
.toBytes("g")))
1026 .add((Mutation
) new Delete(ROWKEY2
).addColumns(FAMILY
, Bytes
.toBytes("D"))));
1028 results
= table
.checkAndMutate(Arrays
.asList(checkAndMutate1
, checkAndMutate2
));
1030 assertTrue(results
.get(0).isSuccess());
1031 assertNull(results
.get(0).getResult());
1032 assertFalse(results
.get(1).isSuccess());
1033 assertNull(results
.get(1).getResult());
1035 result
= table
.get(new Get(ROWKEY
));
1036 assertNull(result
.getValue(FAMILY
, Bytes
.toBytes("A")));
1037 assertEquals("c", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("C"))));
1039 result
= table
.get(new Get(ROWKEY2
));
1040 assertEquals("d", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("D"))));
1041 assertEquals("f", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("F"))));
1046 public void testCheckAndMutateBatchWithFilterAndTimeRange() throws Throwable
{
1047 try (Table table
= createTable()) {
1048 table
.put(Arrays
.asList(
1049 new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"), 100, Bytes
.toBytes("a"))
1050 .addColumn(FAMILY
, Bytes
.toBytes("B"), 100, Bytes
.toBytes("b"))
1051 .addColumn(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("c")),
1052 new Put(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("D"), 100, Bytes
.toBytes("d"))
1053 .addColumn(FAMILY
, Bytes
.toBytes("E"), 100, Bytes
.toBytes("e"))
1054 .addColumn(FAMILY
, Bytes
.toBytes("F"), Bytes
.toBytes("f"))));
1056 CheckAndMutate checkAndMutate1
= CheckAndMutate
.newBuilder(ROWKEY
)
1057 .ifMatches(new FilterList(
1058 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("A"), CompareOperator
.EQUAL
,
1059 Bytes
.toBytes("a")),
1060 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("B"), CompareOperator
.EQUAL
,
1061 Bytes
.toBytes("b"))))
1062 .timeRange(TimeRange
.between(0, 101))
1063 .build(new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("g")));
1065 CheckAndMutate checkAndMutate2
= CheckAndMutate
.newBuilder(ROWKEY2
)
1066 .ifMatches(new FilterList(
1067 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("D"), CompareOperator
.EQUAL
,
1068 Bytes
.toBytes("d")),
1069 new SingleColumnValueFilter(FAMILY
, Bytes
.toBytes("E"), CompareOperator
.EQUAL
,
1070 Bytes
.toBytes("e"))))
1071 .timeRange(TimeRange
.between(0, 100))
1072 .build(new Put(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("F"), Bytes
.toBytes("h")));
1074 List
<CheckAndMutateResult
> results
=
1075 table
.checkAndMutate(Arrays
.asList(checkAndMutate1
, checkAndMutate2
));
1077 assertTrue(results
.get(0).isSuccess());
1078 assertNull(results
.get(0).getResult());
1079 assertFalse(results
.get(1).isSuccess());
1080 assertNull(results
.get(1).getResult());
1082 Result result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("C")));
1083 assertEquals("g", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("C"))));
1085 result
= table
.get(new Get(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("F")));
1086 assertEquals("f", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("F"))));
1091 public void testCheckAndIncrementBatch() throws Throwable
{
1092 try (Table table
= createTable()) {
1093 table
.put(Arrays
.asList(
1094 new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a"))
1095 .addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes(0L)),
1096 new Put(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("c"))
1097 .addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes(0L))));
1099 // CheckAndIncrement with correct value
1100 CheckAndMutate checkAndMutate1
= CheckAndMutate
.newBuilder(ROWKEY
)
1101 .ifEquals(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a"))
1102 .build(new Increment(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B"), 1));
1104 // CheckAndIncrement with wrong value
1105 CheckAndMutate checkAndMutate2
= CheckAndMutate
.newBuilder(ROWKEY2
)
1106 .ifEquals(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("d"))
1107 .build(new Increment(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("D"), 1));
1109 List
<CheckAndMutateResult
> results
=
1110 table
.checkAndMutate(Arrays
.asList(checkAndMutate1
, checkAndMutate2
));
1112 assertTrue(results
.get(0).isSuccess());
1113 assertEquals(1, Bytes
.toLong(results
.get(0).getResult()
1114 .getValue(FAMILY
, Bytes
.toBytes("B"))));
1115 assertFalse(results
.get(1).isSuccess());
1116 assertNull(results
.get(1).getResult());
1118 Result result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B")));
1119 assertEquals(1, Bytes
.toLong(result
.getValue(FAMILY
, Bytes
.toBytes("B"))));
1121 result
= table
.get(new Get(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("D")));
1122 assertEquals(0, Bytes
.toLong(result
.getValue(FAMILY
, Bytes
.toBytes("D"))));
1127 public void testCheckAndAppendBatch() throws Throwable
{
1128 try (Table table
= createTable()) {
1129 table
.put(Arrays
.asList(
1130 new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a"))
1131 .addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("b")),
1132 new Put(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("c"))
1133 .addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("d"))));
1135 // CheckAndAppend with correct value
1136 CheckAndMutate checkAndMutate1
= CheckAndMutate
.newBuilder(ROWKEY
)
1137 .ifEquals(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a"))
1138 .build(new Append(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("b")));
1140 // CheckAndAppend with wrong value
1141 CheckAndMutate checkAndMutate2
= CheckAndMutate
.newBuilder(ROWKEY2
)
1142 .ifEquals(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes("d"))
1143 .build(new Append(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("d")));
1145 List
<CheckAndMutateResult
> results
=
1146 table
.checkAndMutate(Arrays
.asList(checkAndMutate1
, checkAndMutate2
));
1148 assertTrue(results
.get(0).isSuccess());
1149 assertEquals("bb", Bytes
.toString(results
.get(0).getResult()
1150 .getValue(FAMILY
, Bytes
.toBytes("B"))));
1151 assertFalse(results
.get(1).isSuccess());
1152 assertNull(results
.get(1).getResult());
1154 Result result
= table
.get(new Get(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B")));
1155 assertEquals("bb", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("B"))));
1157 result
= table
.get(new Get(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("D")));
1158 assertEquals("d", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("D"))));
1163 public void testCheckAndRowMutationsBatch() throws Throwable
{
1164 try (Table table
= createTable()) {
1165 table
.put(Arrays
.asList(
1166 new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("b"))
1167 .addColumn(FAMILY
, Bytes
.toBytes("C"), Bytes
.toBytes(1L))
1168 .addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("d")),
1169 new Put(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("F"), Bytes
.toBytes("f"))
1170 .addColumn(FAMILY
, Bytes
.toBytes("G"), Bytes
.toBytes(1L))
1171 .addColumn(FAMILY
, Bytes
.toBytes("H"), Bytes
.toBytes("h")))
1174 // CheckAndIncrement with correct value
1175 CheckAndMutate checkAndMutate1
= CheckAndMutate
.newBuilder(ROWKEY
)
1176 .ifEquals(FAMILY
, Bytes
.toBytes("B"), Bytes
.toBytes("b"))
1177 .build(new RowMutations(ROWKEY
).add(Arrays
.asList(
1178 new Put(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("A"), Bytes
.toBytes("a")),
1179 new Delete(ROWKEY
).addColumns(FAMILY
, Bytes
.toBytes("B")),
1180 new Increment(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("C"), 1L),
1181 new Append(ROWKEY
).addColumn(FAMILY
, Bytes
.toBytes("D"), Bytes
.toBytes("d"))
1184 // CheckAndIncrement with wrong value
1185 CheckAndMutate checkAndMutate2
= CheckAndMutate
.newBuilder(ROWKEY2
)
1186 .ifEquals(FAMILY
, Bytes
.toBytes("F"), Bytes
.toBytes("a"))
1187 .build(new RowMutations(ROWKEY2
).add(Arrays
.asList(
1188 new Put(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("E"), Bytes
.toBytes("e")),
1189 new Delete(ROWKEY2
).addColumns(FAMILY
, Bytes
.toBytes("F")),
1190 new Increment(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("G"), 1L),
1191 new Append(ROWKEY2
).addColumn(FAMILY
, Bytes
.toBytes("H"), Bytes
.toBytes("h"))
1194 List
<CheckAndMutateResult
> results
=
1195 table
.checkAndMutate(Arrays
.asList(checkAndMutate1
, checkAndMutate2
));
1197 assertTrue(results
.get(0).isSuccess());
1198 assertEquals(2, Bytes
.toLong(results
.get(0).getResult()
1199 .getValue(FAMILY
, Bytes
.toBytes("C"))));
1200 assertEquals("dd", Bytes
.toString(results
.get(0).getResult()
1201 .getValue(FAMILY
, Bytes
.toBytes("D"))));
1203 assertFalse(results
.get(1).isSuccess());
1204 assertNull(results
.get(1).getResult());
1206 Result result
= table
.get(new Get(ROWKEY
));
1207 assertEquals("a", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("A"))));
1208 assertNull(result
.getValue(FAMILY
, Bytes
.toBytes("B")));
1209 assertEquals(2, Bytes
.toLong(result
.getValue(FAMILY
, Bytes
.toBytes("C"))));
1210 assertEquals("dd", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("D"))));
1212 result
= table
.get(new Get(ROWKEY2
));
1213 assertNull(result
.getValue(FAMILY
, Bytes
.toBytes("E")));
1214 assertEquals("f", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("F"))));
1215 assertEquals(1, Bytes
.toLong(result
.getValue(FAMILY
, Bytes
.toBytes("G"))));
1216 assertEquals("h", Bytes
.toString(result
.getValue(FAMILY
, Bytes
.toBytes("H"))));