View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.client;
21  
22  import java.io.IOException;
23  import java.util.Arrays;
24  import static junit.framework.TestCase.assertTrue;
25  import org.apache.hadoop.hbase.Cell;
26  import org.apache.hadoop.hbase.CellUtil;
27  import org.apache.hadoop.hbase.CoprocessorEnvironment;
28  import org.apache.hadoop.hbase.HBaseTestingUtility;
29  import org.apache.hadoop.hbase.HColumnDescriptor;
30  import org.apache.hadoop.hbase.HTableDescriptor;
31  import org.apache.hadoop.hbase.KeyValue;
32  import org.apache.hadoop.hbase.TableName;
33  import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
34  import org.apache.hadoop.hbase.coprocessor.ObserverContext;
35  import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
36  import org.apache.hadoop.hbase.testclassification.ClientTests;
37  import org.apache.hadoop.hbase.testclassification.MediumTests;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.junit.AfterClass;
40  import org.junit.BeforeClass;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  @Category({MediumTests.class, ClientTests.class})
45  public class TestResultFromCoprocessor {
46    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
47    private static final byte[] ROW = Bytes.toBytes("normal_row");
48    private static final byte[] FAMILY = Bytes.toBytes("fm");
49    private static final byte[] QUAL = Bytes.toBytes("qual");
50    private static final byte[] VALUE = Bytes.toBytes(100L);
51    private static final byte[] FIXED_VALUE = Bytes.toBytes("fixed_value");
52    private static final Cell FIXED_CELL = CellUtil.createCell(ROW, FAMILY,
53            QUAL, 0, KeyValue.Type.Put.getCode(), FIXED_VALUE);
54    private static final Result FIXED_RESULT = Result.create(Arrays.asList(FIXED_CELL));
55    private static final TableName TABLE_NAME = TableName.valueOf("TestResultFromCoprocessor");
56    @BeforeClass
57    public static void setUpBeforeClass() throws Exception {
58      TEST_UTIL.startMiniCluster(3);
59      HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);
60      desc.addCoprocessor(MyObserver.class.getName())
61          .addFamily(new HColumnDescriptor(FAMILY));
62      TEST_UTIL.getHBaseAdmin().createTable(desc);
63    }
64  
65    @AfterClass
66    public static void tearDownAfterClass() throws Exception {
67      TEST_UTIL.shutdownMiniCluster();
68    }
69  
70    @Test
71    public void testAppend() throws IOException {
72      try (Table t = TEST_UTIL.getConnection().getTable(TABLE_NAME)) {
73        Put put = new Put(ROW);
74        put.addColumn(FAMILY, QUAL, VALUE);
75        t.put(put);
76        assertRowAndValue(t.get(new Get(ROW)), ROW, VALUE);
77        Append append = new Append(ROW);
78        append.add(FAMILY, QUAL, FIXED_VALUE);
79        assertRowAndValue(t.append(append), ROW, FIXED_VALUE);
80        assertRowAndValue(t.get(new Get(ROW)), ROW, Bytes.add(VALUE, FIXED_VALUE));
81      }
82    }
83  
84    @Test
85    public void testIncrement() throws IOException {
86      try (Table t = TEST_UTIL.getConnection().getTable(TABLE_NAME)) {
87        Put put = new Put(ROW);
88        put.addColumn(FAMILY, QUAL, VALUE);
89        t.put(put);
90        assertRowAndValue(t.get(new Get(ROW)), ROW, VALUE);
91        Increment inc = new Increment(ROW);
92        inc.addColumn(FAMILY, QUAL, 99);
93        assertRowAndValue(t.increment(inc), ROW, FIXED_VALUE);
94        assertRowAndValue(t.get(new Get(ROW)), ROW, Bytes.toBytes(199L));
95      }
96    }
97  
98    private static void assertRowAndValue(Result r, byte[] row, byte[] value) {
99      for (Cell c : r.rawCells()) {
100       assertTrue(Bytes.equals(CellUtil.cloneRow(c), row));
101       assertTrue(Bytes.equals(CellUtil.cloneValue(c), value));
102     }
103   }
104 
105   public static class MyObserver extends BaseRegionObserver {
106 
107     @Override
108     public Result postAppend(final ObserverContext<RegionCoprocessorEnvironment> c,
109       final Append append, final Result result) {
110       return FIXED_RESULT;
111     }
112 
113     @Override
114     public Result postIncrement(final ObserverContext<RegionCoprocessorEnvironment> c,
115       final Increment increment, final Result result) {
116       return FIXED_RESULT;
117     }
118 
119     @Override
120     public void start(CoprocessorEnvironment env) throws IOException {
121     }
122 
123     @Override
124     public void stop(CoprocessorEnvironment env) throws IOException {
125     }
126   }
127 
128 }