View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.client;
20  
21  import static org.junit.Assert.assertArrayEquals;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  
25  import java.io.IOException;
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.TableName;
28  import org.apache.hadoop.hbase.filter.CompareFilter;
29  import org.apache.hadoop.hbase.regionserver.NoSuchColumnFamilyException;
30  import org.apache.hadoop.hbase.testclassification.MediumTests;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.junit.AfterClass;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  @Category(MediumTests.class)
38  public class TestCheckAndMutate {
39    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
40  
41    /**
42     * @throws java.lang.Exception
43     */
44    @BeforeClass
45    public static void setUpBeforeClass() throws Exception {
46      TEST_UTIL.startMiniCluster();
47    }
48  
49    /**
50     * @throws java.lang.Exception
51     */
52    @AfterClass
53    public static void tearDownAfterClass() throws Exception {
54      TEST_UTIL.shutdownMiniCluster();
55    }
56  
57    @Test
58    public void testCheckAndMutate() throws Throwable {
59      final TableName tableName = TableName.valueOf("TestPutWithDelete");
60      final byte[] rowKey = Bytes.toBytes("12345");
61      final byte[] family = Bytes.toBytes("cf");
62      HTable table = TEST_UTIL.createTable(tableName, family);
63      TEST_UTIL.waitTableAvailable(tableName.getName(), 5000);
64      try {
65        // put one row
66        Put put = new Put(rowKey);
67        put.add(family, Bytes.toBytes("A"), Bytes.toBytes("a"));
68        put.add(family, Bytes.toBytes("B"), Bytes.toBytes("b"));
69        put.add(family, Bytes.toBytes("C"), Bytes.toBytes("c"));
70        table.put(put);
71        // get row back and assert the values
72        Get get = new Get(rowKey);
73        Result result = table.get(get);
74        assertTrue("Column A value should be a",
75            Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a"));
76        assertTrue("Column B value should be b",
77            Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b"));
78        assertTrue("Column C value should be c",
79            Bytes.toString(result.getValue(family, Bytes.toBytes("C"))).equals("c"));
80  
81        // put the same row again with C column deleted
82        RowMutations rm = new RowMutations(rowKey);
83        put = new Put(rowKey);
84        put.add(family, Bytes.toBytes("A"), Bytes.toBytes("a"));
85        put.add(family, Bytes.toBytes("B"), Bytes.toBytes("b"));
86        rm.add(put);
87        Delete del = new Delete(rowKey);
88        del.deleteColumn(family, Bytes.toBytes("C"));
89        rm.add(del);
90        boolean res = table.checkAndMutate(rowKey, family, Bytes.toBytes("A"), CompareFilter.CompareOp.EQUAL,
91            Bytes.toBytes("a"), rm);
92        assertTrue(res);
93  
94        // get row back and assert the values
95        get = new Get(rowKey);
96        result = table.get(get);
97        assertTrue("Column A value should be a",
98            Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a"));
99        assertTrue("Column B value should be b",
100           Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b"));
101       assertTrue("Column C should not exist",
102           result.getValue(family, Bytes.toBytes("C")) == null);
103 
104       //Test that we get a region level exception
105       try {
106         Put p = new Put(rowKey);
107         p.add(new byte[]{'b', 'o', 'g', 'u', 's'}, new byte[]{'A'},  new byte[0]);
108         rm = new RowMutations(rowKey);
109         rm.add(p);
110         table.checkAndMutate(rowKey, family, Bytes.toBytes("A"), CompareFilter.CompareOp.EQUAL,
111             Bytes.toBytes("a"), rm);
112         fail("Expected NoSuchColumnFamilyException");
113       } catch (RetriesExhaustedWithDetailsException e) {
114         try {
115           throw e.getCause(0);
116         } catch (NoSuchColumnFamilyException e1) {
117           // expected
118         }
119       }
120     } finally {
121       table.close();
122     }
123   }
124 
125   @Test
126   public void testCheckRowAndMutateDifferentRow() throws IOException {
127     TableName tableName = TableName.valueOf("TestCheckRowAndMutateDifferentRow");
128     byte[] family = Bytes.toBytes("f");
129     TEST_UTIL.createTable(tableName, family);
130     try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
131       byte[] row1 = Bytes.toBytes("row1");
132       byte[] qualifier = Bytes.toBytes("q1");
133       byte[] value1 = Bytes.toBytes("value1");
134 
135       Put put = new Put(row1);
136       put.addColumn(family, qualifier, value1);
137       table.put(put);
138 
139       Result result = table.get(new Get(row1));
140       assertArrayEquals("the value of column q in row1 should be value1",
141           value1, result.getValue(family, qualifier));
142 
143       byte[] row2 = Bytes.toBytes("row2");
144       byte[] value2 = Bytes.toBytes("value2");
145       RowMutations mutations = new RowMutations(row2);
146       put = new Put(row2);
147       put.addColumn(family, qualifier, value2);
148       mutations.add(put);
149 
150       // check row1 and put row2
151       assertTrue(table.checkAndMutate(row1, family, qualifier,
152           CompareFilter.CompareOp.GREATER, value2, mutations));
153 
154       result = table.get(new Get(row2));
155       assertArrayEquals("the value of column q in row2 should be value2",
156           value2, result.getValue(family, qualifier));
157     }
158   }
159 }