001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.thrift;
019
020import static org.apache.hadoop.hbase.util.Bytes.getBytes;
021
022import java.nio.ByteBuffer;
023import java.util.ArrayList;
024import java.util.List;
025import java.util.Locale;
026import java.util.TreeMap;
027import org.apache.hadoop.hbase.Cell;
028import org.apache.hadoop.hbase.CellUtil;
029import org.apache.hadoop.hbase.HColumnDescriptor;
030import org.apache.hadoop.hbase.KeyValue;
031import org.apache.hadoop.hbase.client.Append;
032import org.apache.hadoop.hbase.client.Increment;
033import org.apache.hadoop.hbase.client.Result;
034import org.apache.hadoop.hbase.io.compress.Compression;
035import org.apache.hadoop.hbase.regionserver.BloomType;
036import org.apache.hadoop.hbase.thrift.generated.ColumnDescriptor;
037import org.apache.hadoop.hbase.thrift.generated.IllegalArgument;
038import org.apache.hadoop.hbase.thrift.generated.TAppend;
039import org.apache.hadoop.hbase.thrift.generated.TCell;
040import org.apache.hadoop.hbase.thrift.generated.TColumn;
041import org.apache.hadoop.hbase.thrift.generated.TIncrement;
042import org.apache.hadoop.hbase.thrift.generated.TRowResult;
043import org.apache.hadoop.hbase.util.Bytes;
044import org.apache.yetus.audience.InterfaceAudience;
045
046@InterfaceAudience.Private
047public class ThriftUtilities {
048
049  /**
050   * This utility method creates a new Hbase HColumnDescriptor object based on a Thrift
051   * ColumnDescriptor "struct". n * Thrift ColumnDescriptor object nn
052   */
053  static public HColumnDescriptor colDescFromThrift(ColumnDescriptor in) throws IllegalArgument {
054    Compression.Algorithm comp =
055      Compression.getCompressionAlgorithmByName(in.compression.toLowerCase(Locale.ROOT));
056    BloomType bt = BloomType.valueOf(in.bloomFilterType);
057
058    if (in.name == null || !in.name.hasRemaining()) {
059      throw new IllegalArgument("column name is empty");
060    }
061    byte[] parsedName = CellUtil.parseColumn(Bytes.getBytes(in.name))[0];
062    HColumnDescriptor col = new HColumnDescriptor(parsedName).setMaxVersions(in.maxVersions)
063      .setCompressionType(comp).setInMemory(in.inMemory).setBlockCacheEnabled(in.blockCacheEnabled)
064      .setTimeToLive(in.timeToLive > 0 ? in.timeToLive : Integer.MAX_VALUE).setBloomFilterType(bt);
065    return col;
066  }
067
068  /**
069   * This utility method creates a new Thrift ColumnDescriptor "struct" based on an Hbase
070   * HColumnDescriptor object. n * Hbase HColumnDescriptor object
071   * @return Thrift ColumnDescriptor
072   */
073  static public ColumnDescriptor colDescFromHbase(HColumnDescriptor in) {
074    ColumnDescriptor col = new ColumnDescriptor();
075    col.name = ByteBuffer.wrap(Bytes.add(in.getName(), KeyValue.COLUMN_FAMILY_DELIM_ARRAY));
076    col.maxVersions = in.getMaxVersions();
077    col.compression = in.getCompressionType().toString();
078    col.inMemory = in.isInMemory();
079    col.blockCacheEnabled = in.isBlockCacheEnabled();
080    col.bloomFilterType = in.getBloomFilterType().toString();
081    col.timeToLive = in.getTimeToLive();
082    return col;
083  }
084
085  /**
086   * This utility method creates a list of Thrift TCell "struct" based on an Hbase Cell object. The
087   * empty list is returned if the input is null. n * Hbase Cell object
088   * @return Thrift TCell array
089   */
090  static public List<TCell> cellFromHBase(Cell in) {
091    List<TCell> list = new ArrayList<>(1);
092    if (in != null) {
093      list.add(new TCell(ByteBuffer.wrap(CellUtil.cloneValue(in)), in.getTimestamp()));
094    }
095    return list;
096  }
097
098  /**
099   * This utility method creates a list of Thrift TCell "struct" based on an Hbase Cell array. The
100   * empty list is returned if the input is null.
101   * @param in Hbase Cell array
102   * @return Thrift TCell array
103   */
104  static public List<TCell> cellFromHBase(Cell[] in) {
105    List<TCell> list = null;
106    if (in != null) {
107      list = new ArrayList<>(in.length);
108      for (int i = 0; i < in.length; i++) {
109        list.add(new TCell(ByteBuffer.wrap(CellUtil.cloneValue(in[i])), in[i].getTimestamp()));
110      }
111    } else {
112      list = new ArrayList<>(0);
113    }
114    return list;
115  }
116
117  /**
118   * This utility method creates a list of Thrift TRowResult "struct" based on an Hbase RowResult
119   * object. The empty list is returned if the input is null. n * Hbase RowResult object n * This
120   * boolean dictates if row data is returned in a sorted order sortColumns = True will set
121   * TRowResult's sortedColumns member which is an ArrayList of TColumn struct sortColumns = False
122   * will set TRowResult's columns member which is a map of columnName and TCell struct
123   * @return Thrift TRowResult array
124   */
125  static public List<TRowResult> rowResultFromHBase(Result[] in, boolean sortColumns) {
126    List<TRowResult> results = new ArrayList<>(in.length);
127    for (Result result_ : in) {
128      if (result_ == null || result_.isEmpty()) {
129        continue;
130      }
131      TRowResult result = new TRowResult();
132      result.row = ByteBuffer.wrap(result_.getRow());
133      if (sortColumns) {
134        result.sortedColumns = new ArrayList<>();
135        for (Cell kv : result_.rawCells()) {
136          result.sortedColumns.add(new TColumn(
137            ByteBuffer
138              .wrap(CellUtil.makeColumn(CellUtil.cloneFamily(kv), CellUtil.cloneQualifier(kv))),
139            new TCell(ByteBuffer.wrap(CellUtil.cloneValue(kv)), kv.getTimestamp())));
140        }
141      } else {
142        result.columns = new TreeMap<>();
143        for (Cell kv : result_.rawCells()) {
144          result.columns.put(
145            ByteBuffer
146              .wrap(CellUtil.makeColumn(CellUtil.cloneFamily(kv), CellUtil.cloneQualifier(kv))),
147            new TCell(ByteBuffer.wrap(CellUtil.cloneValue(kv)), kv.getTimestamp()));
148        }
149      }
150      results.add(result);
151    }
152    return results;
153  }
154
155  /**
156   * This utility method creates a list of Thrift TRowResult "struct" based on an array of Hbase
157   * RowResult objects. The empty list is returned if the input is null. n * Array of Hbase
158   * RowResult objects
159   * @return Thrift TRowResult array
160   */
161  static public List<TRowResult> rowResultFromHBase(Result[] in) {
162    return rowResultFromHBase(in, false);
163  }
164
165  static public List<TRowResult> rowResultFromHBase(Result in) {
166    Result[] result = { in };
167    return rowResultFromHBase(result);
168  }
169
170  /**
171   * From a {@link TIncrement} create an {@link Increment}.
172   * @param tincrement the Thrift version of an increment
173   * @return an increment that the {@link TIncrement} represented.
174   */
175  public static Increment incrementFromThrift(TIncrement tincrement) {
176    Increment inc = new Increment(tincrement.getRow());
177    byte[][] famAndQf = CellUtil.parseColumn(tincrement.getColumn());
178    if (famAndQf.length != 2) return null;
179    inc.addColumn(famAndQf[0], famAndQf[1], tincrement.getAmmount());
180    return inc;
181  }
182
183  /**
184   * From a {@link TAppend} create an {@link Append}.
185   * @param tappend the Thrift version of an append.
186   * @return an increment that the {@link TAppend} represented.
187   */
188  public static Append appendFromThrift(TAppend tappend) {
189    Append append = new Append(tappend.getRow());
190    List<ByteBuffer> columns = tappend.getColumns();
191    List<ByteBuffer> values = tappend.getValues();
192
193    if (columns.size() != values.size()) {
194      throw new IllegalArgumentException(
195        "Sizes of columns and values in tappend object are not matching");
196    }
197
198    int length = columns.size();
199
200    for (int i = 0; i < length; i++) {
201      byte[][] famAndQf = CellUtil.parseColumn(getBytes(columns.get(i)));
202      append.addColumn(famAndQf[0], famAndQf[1], getBytes(values.get(i)));
203    }
204    return append;
205  }
206}