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 org.apache.hadoop.hbase.HBaseTestingUtility;
22  import org.apache.hadoop.hbase.testclassification.MediumTests;
23  import org.apache.hadoop.hbase.TableName;
24  import org.apache.hadoop.hbase.filter.CompareFilter;
25  import org.apache.hadoop.hbase.regionserver.NoSuchColumnFamilyException;
26  import org.apache.hadoop.hbase.util.Bytes;
27  import org.junit.AfterClass;
28  import org.junit.BeforeClass;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  import static org.junit.Assert.assertTrue;
33  import static org.junit.Assert.fail;
34  
35  @Category(MediumTests.class)
36  public class TestCheckAndMutate {
37    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
38  
39    /**
40     * @throws java.lang.Exception
41     */
42    @BeforeClass
43    public static void setUpBeforeClass() throws Exception {
44      TEST_UTIL.startMiniCluster();
45    }
46  
47    /**
48     * @throws java.lang.Exception
49     */
50    @AfterClass
51    public static void tearDownAfterClass() throws Exception {
52      TEST_UTIL.shutdownMiniCluster();
53    }
54  
55    @Test
56    public void testCheckAndMutate() throws Exception {
57      final TableName tableName = TableName.valueOf("TestPutWithDelete");
58      final byte[] rowKey = Bytes.toBytes("12345");
59      final byte[] family = Bytes.toBytes("cf");
60      HTable table = TEST_UTIL.createTable(tableName, family);
61      TEST_UTIL.waitTableAvailable(tableName.getName(), 5000);
62      try {
63        // put one row
64        Put put = new Put(rowKey);
65        put.add(family, Bytes.toBytes("A"), Bytes.toBytes("a"));
66        put.add(family, Bytes.toBytes("B"), Bytes.toBytes("b"));
67        put.add(family, Bytes.toBytes("C"), Bytes.toBytes("c"));
68        table.put(put);
69        // get row back and assert the values
70        Get get = new Get(rowKey);
71        Result result = table.get(get);
72        assertTrue("Column A value should be a",
73            Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a"));
74        assertTrue("Column B value should be b",
75            Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b"));
76        assertTrue("Column C value should be c",
77            Bytes.toString(result.getValue(family, Bytes.toBytes("C"))).equals("c"));
78  
79        // put the same row again with C column deleted
80        RowMutations rm = new RowMutations(rowKey);
81        put = new Put(rowKey);
82        put.add(family, Bytes.toBytes("A"), Bytes.toBytes("a"));
83        put.add(family, Bytes.toBytes("B"), Bytes.toBytes("b"));
84        rm.add(put);
85        Delete del = new Delete(rowKey);
86        del.deleteColumn(family, Bytes.toBytes("C"));
87        rm.add(del);
88        boolean res = table.checkAndMutate(rowKey, family, Bytes.toBytes("A"), CompareFilter.CompareOp.EQUAL,
89            Bytes.toBytes("a"), rm);
90        assertTrue(res);
91  
92        // get row back and assert the values
93        get = new Get(rowKey);
94        result = table.get(get);
95        assertTrue("Column A value should be a",
96            Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a"));
97        assertTrue("Column B value should be b",
98            Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b"));
99        assertTrue("Column C should not exist",
100           result.getValue(family, Bytes.toBytes("C")) == null);
101 
102       //Test that we get a region level exception
103       try {
104         Put p = new Put(rowKey);
105         p.add(new byte[]{'b', 'o', 'g', 'u', 's'}, new byte[]{'A'},  new byte[0]);
106         rm = new RowMutations(rowKey);
107         rm.add(p);
108         table.checkAndMutate(rowKey, family, Bytes.toBytes("A"), CompareFilter.CompareOp.EQUAL,
109             Bytes.toBytes("a"), rm);
110         fail("Expected NoSuchColumnFamilyException");
111       } catch(NoSuchColumnFamilyException e) {
112       }
113     } finally {
114       table.close();
115     }
116   }
117 }