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 static org.apache.hadoop.hbase.util.Bytes.getBytes;
22  
23  import java.nio.ByteBuffer;
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Locale;
27  import java.util.TreeMap;
28
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.Cell;
31  import org.apache.hadoop.hbase.CellUtil;
32  import org.apache.hadoop.hbase.HColumnDescriptor;
33  import org.apache.hadoop.hbase.KeyValue;
34  import org.apache.hadoop.hbase.client.Append;
35  import org.apache.hadoop.hbase.client.Increment;
36  import org.apache.hadoop.hbase.client.Result;
37  import org.apache.hadoop.hbase.io.compress.Compression;
38  import org.apache.hadoop.hbase.regionserver.BloomType;
39  import org.apache.hadoop.hbase.thrift.generated.ColumnDescriptor;
40  import org.apache.hadoop.hbase.thrift.generated.IllegalArgument;
41  import org.apache.hadoop.hbase.thrift.generated.TAppend;
42  import org.apache.hadoop.hbase.thrift.generated.TCell;
43  import org.apache.hadoop.hbase.thrift.generated.TColumn;
44  import org.apache.hadoop.hbase.thrift.generated.TIncrement;
45  import org.apache.hadoop.hbase.thrift.generated.TRowResult;
46  import org.apache.hadoop.hbase.util.Bytes;
47
48  @InterfaceAudience.Private
49  public class ThriftUtilities {
50
51    /**
52     * This utility method creates a new Hbase HColumnDescriptor object based on a
53     * Thrift ColumnDescriptor "struct".
54     *
55     * @param in
56     *          Thrift ColumnDescriptor object
57     * @return HColumnDescriptor
58     * @throws IllegalArgument
59     */
60    static public HColumnDescriptor colDescFromThrift(ColumnDescriptor in)
61        throws IllegalArgument {
62      Compression.Algorithm comp =
63        Compression.getCompressionAlgorithmByName(in.compression.toLowerCase(Locale.ROOT));
64      BloomType bt =
65        BloomType.valueOf(in.bloomFilterType);
66
67      if (in.name == null || !in.name.hasRemaining()) {
68        throw new IllegalArgument("column name is empty");
69      }
70      byte [] parsedName = KeyValue.parseColumn(Bytes.getBytes(in.name))[0];
71      HColumnDescriptor col = new HColumnDescriptor(parsedName)
72          .setMaxVersions(in.maxVersions)
73          .setCompressionType(comp)
74          .setInMemory(in.inMemory)
75          .setBlockCacheEnabled(in.blockCacheEnabled)
76          .setTimeToLive(in.timeToLive > 0 ? in.timeToLive : Integer.MAX_VALUE)
77          .setBloomFilterType(bt);
78      return col;
79    }
80
81    /**
82     * This utility method creates a new Thrift ColumnDescriptor "struct" based on
83     * an Hbase HColumnDescriptor object.
84     *
85     * @param in
86     *          Hbase HColumnDescriptor object
87     * @return Thrift ColumnDescriptor
88     */
89    static public ColumnDescriptor colDescFromHbase(HColumnDescriptor in) {
90      ColumnDescriptor col = new ColumnDescriptor();
91      col.name = ByteBuffer.wrap(Bytes.add(in.getName(), KeyValue.COLUMN_FAMILY_DELIM_ARRAY));
92      col.maxVersions = in.getMaxVersions();
93      col.compression = in.getCompressionType().toString();
94      col.inMemory = in.isInMemory();
95      col.blockCacheEnabled = in.isBlockCacheEnabled();
96      col.bloomFilterType = in.getBloomFilterType().toString();
97      return col;
98    }
99
100   /**
101    * This utility method creates a list of Thrift TCell "struct" based on
102    * an Hbase Cell object. The empty list is returned if the input is null.
103    *
104    * @param in
105    *          Hbase Cell object
106    * @return Thrift TCell array
107    */
108   static public List<TCell> cellFromHBase(Cell in) {
109     List<TCell> list = new ArrayList<TCell>(1);
110     if (in != null) {
111       list.add(new TCell(ByteBuffer.wrap(CellUtil.cloneValue(in)), in.getTimestamp()));
112     }
113     return list;
114   }
115
116   /**
117    * This utility method creates a list of Thrift TCell "struct" based on
118    * an Hbase Cell array. The empty list is returned if the input is null.
119    * @param in Hbase Cell array
120    * @return Thrift TCell array
121    */
122   static public List<TCell> cellFromHBase(Cell[] in) {
123     List<TCell> list = null;
124     if (in != null) {
125       list = new ArrayList<TCell>(in.length);
126       for (int i = 0; i < in.length; i++) {
127         list.add(new TCell(ByteBuffer.wrap(CellUtil.cloneValue(in[i])), in[i].getTimestamp()));
128       }
129     } else {
130       list = new ArrayList<TCell>(0);
131     }
132     return list;
133   }
134
135   /**
136    * This utility method creates a list of Thrift TRowResult "struct" based on
137    * an Hbase RowResult object. The empty list is returned if the input is
138    * null.
139    *
140    * @param in
141    *          Hbase RowResult object
142    * @param sortColumns
143    *          This boolean dictates if row data is returned in a sorted order
144    *          sortColumns = True will set TRowResult's sortedColumns member
145    *                        which is an ArrayList of TColumn struct
146    *          sortColumns = False will set TRowResult's columns member which is
147    *                        a map of columnName and TCell struct
148    * @return Thrift TRowResult array
149    */
150   static public List<TRowResult> rowResultFromHBase(Result[] in, boolean sortColumns) {
151     List<TRowResult> results = new ArrayList<TRowResult>();
152     for ( Result result_ : in) {
153         if(result_ == null || result_.isEmpty()) {
154             continue;
155         }
156         TRowResult result = new TRowResult();
157         result.row = ByteBuffer.wrap(result_.getRow());
158         if (sortColumns) {
159           result.sortedColumns = new ArrayList<TColumn>();
160           for (Cell kv : result_.rawCells()) {
161             result.sortedColumns.add(new TColumn(
162                 ByteBuffer.wrap(KeyValue.makeColumn(CellUtil.cloneFamily(kv),
163                     CellUtil.cloneQualifier(kv))),
164                 new TCell(ByteBuffer.wrap(CellUtil.cloneValue(kv)), kv.getTimestamp())));
165           }
166         } else {
167           result.columns = new TreeMap<ByteBuffer, TCell>();
168           for (Cell kv : result_.rawCells()) {
169             result.columns.put(
170                 ByteBuffer.wrap(KeyValue.makeColumn(CellUtil.cloneFamily(kv),
171                     CellUtil.cloneQualifier(kv))),
172                 new TCell(ByteBuffer.wrap(CellUtil.cloneValue(kv)), kv.getTimestamp()));
173           }
174         }
175       results.add(result);
176     }
177     return results;
178   }
179
180   /**
181    * This utility method creates a list of Thrift TRowResult "struct" based on
182    * an array of Hbase RowResult objects. The empty list is returned if the input is
183    * null.
184    *
185    * @param in
186    *          Array of Hbase RowResult objects
187    * @return Thrift TRowResult array
188    */
189   static public List<TRowResult> rowResultFromHBase(Result[] in) {
190     return rowResultFromHBase(in, false);
191   }
192
193   static public List<TRowResult> rowResultFromHBase(Result in) {
194     Result [] result = { in };
195     return rowResultFromHBase(result);
196   }
197
198   /**
199    * From a {@link TIncrement} create an {@link Increment}.
200    * @param tincrement the Thrift version of an increment
201    * @return an increment that the {@link TIncrement} represented.
202    */
203   public static Increment incrementFromThrift(TIncrement tincrement) {
204     Increment inc = new Increment(tincrement.getRow());
205     byte[][] famAndQf = KeyValue.parseColumn(tincrement.getColumn());
206     if (famAndQf.length != 2) return null;
207     inc.addColumn(famAndQf[0], famAndQf[1], tincrement.getAmmount());
208     return inc;
209   }
210
211   /**
212    * From a {@link TAppend} create an {@link Append}.
213    * @param tappend the Thrift version of an append.
214    * @return an increment that the {@link TAppend} represented.
215    */
216   public static Append appendFromThrift(TAppend tappend) {
217     Append append = new Append(tappend.getRow());
218     List<ByteBuffer> columns = tappend.getColumns();
219     List<ByteBuffer> values = tappend.getValues();
220
221     if (columns.size() != values.size()) {
222       throw new IllegalArgumentException(
223           "Sizes of columns and values in tappend object are not matching");
224     }
225
226     int length = columns.size();
227
228     for (int i = 0; i < length; i++) {
229       byte[][] famAndQf = KeyValue.parseColumn(getBytes(columns.get(i)));
230       append.add(famAndQf[0], famAndQf[1], getBytes(values.get(i)));
231     }
232     return append;
233   }
234 }