View Javadoc

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
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  package org.apache.omid;
19  
20  import org.apache.hadoop.conf.Configuration;
21  import org.apache.hadoop.hbase.CellComparator;
22  import org.apache.hadoop.hbase.HColumnDescriptor;
23  import org.apache.hadoop.hbase.HTableDescriptor;
24  import org.apache.hadoop.hbase.KeyValue;
25  import org.apache.hadoop.hbase.TableName;
26  import org.apache.hadoop.hbase.client.Admin;
27  import org.apache.hadoop.hbase.client.Connection;
28  import org.apache.hadoop.hbase.client.CoprocessorHConnection;
29  import org.apache.hadoop.hbase.coprocessor.ObserverContext;
30  import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
31  import org.apache.hadoop.hbase.regionserver.HRegionServer;
32  import org.apache.hadoop.hbase.regionserver.Region;
33  import org.apache.hadoop.hbase.regionserver.Store;
34  import org.apache.hadoop.hbase.util.Bytes;
35  
36  import java.io.IOException;
37  
38  public class HBaseShims {
39  
40      static public void setKeyValueSequenceId(KeyValue kv, int sequenceId) {
41  
42          kv.setSequenceId(sequenceId);
43  
44      }
45  
46      static public Region getRegionCoprocessorRegion(RegionCoprocessorEnvironment env) {
47  
48          return env.getRegion();
49  
50      }
51  
52      static public void flushAllOnlineRegions(HRegionServer regionServer, TableName tableName) throws IOException {
53  
54          for (Region r : regionServer.getOnlineRegions(tableName)) {
55              r.flush(true);
56          }
57  
58      }
59  
60      static public void addFamilyToHTableDescriptor(HTableDescriptor tableDesc, HColumnDescriptor columnDesc) {
61  
62          tableDesc.addFamily(columnDesc);
63  
64      }
65  
66      public static CellComparator cellComparatorInstance() {
67          return new CellComparator();
68      }
69  
70      public static boolean OmidCompactionEnabled(ObserverContext<RegionCoprocessorEnvironment> env,
71                                    Store store,
72                                    String cfFlagValue) {
73          HTableDescriptor desc = env.getEnvironment().getRegion().getTableDesc();
74          HColumnDescriptor famDesc
75                  = desc.getFamily(Bytes.toBytes(store.getColumnFamilyName()));
76          return Boolean.valueOf(famDesc.getValue(cfFlagValue));
77      }
78  
79  
80      public static void setCompaction(Connection conn, TableName table, byte[] columnFamily, String key, String value)
81              throws IOException {
82          try(Admin admin = conn.getAdmin()) {
83              HTableDescriptor desc = admin.getTableDescriptor(table);
84              HColumnDescriptor cfDesc = desc.getFamily(columnFamily);
85              cfDesc.setValue(key, value);
86              admin.modifyColumn(table, cfDesc);
87          }
88      }
89      
90      /**
91       * For HBase 1.x, an HConstants.HBASE_CLIENT_RETRIES_NUMBER value of 0
92       * means no retries, while for 2.x a value of 1 means no retries. 
93       * @return
94       */
95      public static int getNoRetriesNumber() {
96          return 0;
97      }
98      
99      /**
100      * Create an HBase Connection from the region server
101      */
102     public static Connection newServerConnection(Configuration config, RegionCoprocessorEnvironment env) throws IOException {
103         return new CoprocessorHConnection(config, (HRegionServer)env.getRegionServerServices());
104     }
105 
106 }