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  
19  package org.apache.hadoop.hbase.thrift;
20  
21  import java.nio.ByteBuffer;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.TreeMap;
25  
26  import org.apache.hadoop.hbase.HColumnDescriptor;
27  import org.apache.hadoop.hbase.KeyValue;
28  import org.apache.hadoop.hbase.client.Result;
29  import org.apache.hadoop.hbase.io.hfile.Compression;
30  import org.apache.hadoop.hbase.regionserver.StoreFile;
31  import org.apache.hadoop.hbase.regionserver.StoreFile.BloomType;
32  import org.apache.hadoop.hbase.thrift.generated.ColumnDescriptor;
33  import org.apache.hadoop.hbase.thrift.generated.IllegalArgument;
34  import org.apache.hadoop.hbase.thrift.generated.TCell;
35  import org.apache.hadoop.hbase.thrift.generated.TRowResult;
36  import org.apache.hadoop.hbase.util.Bytes;
37  
38  public class ThriftUtilities {
39  
40    /**
41     * This utility method creates a new Hbase HColumnDescriptor object based on a
42     * Thrift ColumnDescriptor "struct".
43     *
44     * @param in
45     *          Thrift ColumnDescriptor object
46     * @return HColumnDescriptor
47     * @throws IllegalArgument
48     */
49    static public HColumnDescriptor colDescFromThrift(ColumnDescriptor in)
50        throws IllegalArgument {
51      Compression.Algorithm comp =
52        Compression.getCompressionAlgorithmByName(in.compression.toLowerCase());
53      StoreFile.BloomType bt =
54        BloomType.valueOf(in.bloomFilterType);
55  
56      if (in.name == null || !in.name.hasRemaining()) {
57        throw new IllegalArgument("column name is empty");
58      }
59      byte [] parsedName = KeyValue.parseColumn(Bytes.getBytes(in.name))[0];
60      HColumnDescriptor col = new HColumnDescriptor(parsedName,
61          in.maxVersions, comp.getName(), in.inMemory, in.blockCacheEnabled,
62          in.timeToLive, bt.toString());
63      return col;
64    }
65  
66    /**
67     * This utility method creates a new Thrift ColumnDescriptor "struct" based on
68     * an Hbase HColumnDescriptor object.
69     *
70     * @param in
71     *          Hbase HColumnDescriptor object
72     * @return Thrift ColumnDescriptor
73     */
74    static public ColumnDescriptor colDescFromHbase(HColumnDescriptor in) {
75      ColumnDescriptor col = new ColumnDescriptor();
76      col.name = ByteBuffer.wrap(Bytes.add(in.getName(), KeyValue.COLUMN_FAMILY_DELIM_ARRAY));
77      col.maxVersions = in.getMaxVersions();
78      col.compression = in.getCompression().toString();
79      col.inMemory = in.isInMemory();
80      col.blockCacheEnabled = in.isBlockCacheEnabled();
81      col.bloomFilterType = in.getBloomFilterType().toString();
82      return col;
83    }
84  
85    /**
86     * This utility method creates a list of Thrift TCell "struct" based on
87     * an Hbase Cell object. The empty list is returned if the input is null.
88     *
89     * @param in
90     *          Hbase Cell object
91     * @return Thrift TCell array
92     */
93    static public List<TCell> cellFromHBase(KeyValue in) {
94      List<TCell> list = new ArrayList<TCell>(1);
95      if (in != null) {
96        list.add(new TCell(ByteBuffer.wrap(in.getValue()), in.getTimestamp()));
97      }
98      return list;
99    }
100 
101   /**
102    * This utility method creates a list of Thrift TCell "struct" based on
103    * an Hbase Cell array. The empty list is returned if the input is null.
104    * @param in Hbase Cell array
105    * @return Thrift TCell array
106    */
107   static public List<TCell> cellFromHBase(KeyValue[] in) {
108     List<TCell> list = null;
109     if (in != null) {
110       list = new ArrayList<TCell>(in.length);
111       for (int i = 0; i < in.length; i++) {
112         list.add(new TCell(ByteBuffer.wrap(in[i].getValue()), in[i].getTimestamp()));
113       }
114     } else {
115       list = new ArrayList<TCell>(0);
116     }
117     return list;
118   }
119 
120   /**
121    * This utility method creates a list of Thrift TRowResult "struct" based on
122    * an Hbase RowResult object. The empty list is returned if the input is
123    * null.
124    *
125    * @param in
126    *          Hbase RowResult object
127    * @return Thrift TRowResult array
128    */
129   static public List<TRowResult> rowResultFromHBase(Result[] in) {
130     List<TRowResult> results = new ArrayList<TRowResult>();
131     for ( Result result_ : in) {
132         if(result_ == null || result_.isEmpty()) {
133             continue;
134         }
135         TRowResult result = new TRowResult();
136         result.row = ByteBuffer.wrap(result_.getRow());
137         result.columns = new TreeMap<ByteBuffer, TCell>();
138         for(KeyValue kv : result_.raw()) {
139           result.columns.put(
140               ByteBuffer.wrap(KeyValue.makeColumn(kv.getFamily(),
141                   kv.getQualifier())),
142               new TCell(ByteBuffer.wrap(kv.getValue()), kv.getTimestamp()));
143         }
144       results.add(result);
145     }
146     return results;
147   }
148 
149   static public List<TRowResult> rowResultFromHBase(Result in) {
150     Result [] result = { in };
151     return rowResultFromHBase(result);
152   }
153 }