001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.thrift2;
020
021import static org.apache.hadoop.hbase.util.Bytes.getBytes;
022
023import java.io.IOException;
024import java.nio.ByteBuffer;
025import java.util.ArrayList;
026import java.util.Collections;
027import java.util.List;
028import java.util.Map;
029import java.util.NavigableSet;
030import java.util.Set;
031import java.util.stream.Collectors;
032
033import org.apache.hadoop.hbase.Cell;
034import org.apache.hadoop.hbase.CellBuilderFactory;
035import org.apache.hadoop.hbase.CellBuilderType;
036import org.apache.hadoop.hbase.CellUtil;
037import org.apache.hadoop.hbase.CompareOperator;
038import org.apache.hadoop.hbase.ExtendedCellBuilder;
039import org.apache.hadoop.hbase.ExtendedCellBuilderFactory;
040import org.apache.hadoop.hbase.HConstants;
041import org.apache.hadoop.hbase.HRegionInfo;
042import org.apache.hadoop.hbase.HRegionLocation;
043import org.apache.hadoop.hbase.HTableDescriptor;
044import org.apache.hadoop.hbase.KeepDeletedCells;
045import org.apache.hadoop.hbase.NamespaceDescriptor;
046import org.apache.hadoop.hbase.PrivateCellUtil;
047import org.apache.hadoop.hbase.ServerName;
048import org.apache.hadoop.hbase.TableName;
049import org.apache.hadoop.hbase.client.Append;
050import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
051import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
052import org.apache.hadoop.hbase.client.Consistency;
053import org.apache.hadoop.hbase.client.Delete;
054import org.apache.hadoop.hbase.client.Durability;
055import org.apache.hadoop.hbase.client.Get;
056import org.apache.hadoop.hbase.client.Increment;
057import org.apache.hadoop.hbase.client.LogQueryFilter;
058import org.apache.hadoop.hbase.client.Mutation;
059import org.apache.hadoop.hbase.client.OnlineLogRecord;
060import org.apache.hadoop.hbase.client.OperationWithAttributes;
061import org.apache.hadoop.hbase.client.Put;
062import org.apache.hadoop.hbase.client.Result;
063import org.apache.hadoop.hbase.client.RowMutations;
064import org.apache.hadoop.hbase.client.Scan;
065import org.apache.hadoop.hbase.client.Scan.ReadType;
066import org.apache.hadoop.hbase.client.TableDescriptor;
067import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
068import org.apache.hadoop.hbase.exceptions.DeserializationException;
069import org.apache.hadoop.hbase.filter.Filter;
070import org.apache.hadoop.hbase.filter.ParseFilter;
071import org.apache.hadoop.hbase.io.TimeRange;
072import org.apache.hadoop.hbase.io.compress.Compression;
073import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
074import org.apache.hadoop.hbase.regionserver.BloomType;
075import org.apache.hadoop.hbase.security.visibility.Authorizations;
076import org.apache.hadoop.hbase.security.visibility.CellVisibility;
077import org.apache.hadoop.hbase.thrift2.generated.TAppend;
078import org.apache.hadoop.hbase.thrift2.generated.TAuthorization;
079import org.apache.hadoop.hbase.thrift2.generated.TBloomFilterType;
080import org.apache.hadoop.hbase.thrift2.generated.TCellVisibility;
081import org.apache.hadoop.hbase.thrift2.generated.TColumn;
082import org.apache.hadoop.hbase.thrift2.generated.TColumnFamilyDescriptor;
083import org.apache.hadoop.hbase.thrift2.generated.TColumnIncrement;
084import org.apache.hadoop.hbase.thrift2.generated.TColumnValue;
085import org.apache.hadoop.hbase.thrift2.generated.TCompareOp;
086import org.apache.hadoop.hbase.thrift2.generated.TCompressionAlgorithm;
087import org.apache.hadoop.hbase.thrift2.generated.TConsistency;
088import org.apache.hadoop.hbase.thrift2.generated.TDataBlockEncoding;
089import org.apache.hadoop.hbase.thrift2.generated.TDelete;
090import org.apache.hadoop.hbase.thrift2.generated.TDeleteType;
091import org.apache.hadoop.hbase.thrift2.generated.TDurability;
092import org.apache.hadoop.hbase.thrift2.generated.TFilterByOperator;
093import org.apache.hadoop.hbase.thrift2.generated.TGet;
094import org.apache.hadoop.hbase.thrift2.generated.THRegionInfo;
095import org.apache.hadoop.hbase.thrift2.generated.THRegionLocation;
096import org.apache.hadoop.hbase.thrift2.generated.TIncrement;
097import org.apache.hadoop.hbase.thrift2.generated.TKeepDeletedCells;
098import org.apache.hadoop.hbase.thrift2.generated.TLogQueryFilter;
099import org.apache.hadoop.hbase.thrift2.generated.TLogType;
100import org.apache.hadoop.hbase.thrift2.generated.TMutation;
101import org.apache.hadoop.hbase.thrift2.generated.TNamespaceDescriptor;
102import org.apache.hadoop.hbase.thrift2.generated.TOnlineLogRecord;
103import org.apache.hadoop.hbase.thrift2.generated.TPut;
104import org.apache.hadoop.hbase.thrift2.generated.TReadType;
105import org.apache.hadoop.hbase.thrift2.generated.TResult;
106import org.apache.hadoop.hbase.thrift2.generated.TRowMutations;
107import org.apache.hadoop.hbase.thrift2.generated.TScan;
108import org.apache.hadoop.hbase.thrift2.generated.TServerName;
109import org.apache.hadoop.hbase.thrift2.generated.TTableDescriptor;
110import org.apache.hadoop.hbase.thrift2.generated.TTableName;
111import org.apache.hadoop.hbase.thrift2.generated.TTimeRange;
112import org.apache.hadoop.hbase.util.Bytes;
113import org.apache.hbase.thirdparty.org.apache.commons.collections4.CollectionUtils;
114import org.apache.yetus.audience.InterfaceAudience;
115
116import org.apache.hbase.thirdparty.org.apache.commons.collections4.MapUtils;
117
118import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
119import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos;
120
121@InterfaceAudience.Private
122public class ThriftUtilities {
123
124  private final static Cell[] EMPTY_CELL_ARRAY = new Cell[]{};
125  private final static Result EMPTY_RESULT = Result.create(EMPTY_CELL_ARRAY);
126  private final static Result EMPTY_RESULT_STALE = Result.create(EMPTY_CELL_ARRAY, null, true);
127
128
129
130  private ThriftUtilities() {
131    throw new UnsupportedOperationException("Can't initialize class");
132  }
133
134  /**
135   * Creates a {@link Get} (HBase) from a {@link TGet} (Thrift).
136   *
137   * This ignores any timestamps set on {@link TColumn} objects.
138   *
139   * @param in the <code>TGet</code> to convert
140   *
141   * @return <code>Get</code> object
142   *
143   * @throws IOException if an invalid time range or max version parameter is given
144   */
145  public static Get getFromThrift(TGet in) throws IOException {
146    Get out = new Get(in.getRow());
147
148    // Timestamp overwrites time range if both are set
149    if (in.isSetTimestamp()) {
150      out.setTimestamp(in.getTimestamp());
151    } else if (in.isSetTimeRange()) {
152      out.setTimeRange(in.getTimeRange().getMinStamp(), in.getTimeRange().getMaxStamp());
153    }
154
155    if (in.isSetMaxVersions()) {
156      out.setMaxVersions(in.getMaxVersions());
157    }
158
159    if (in.isSetFilterString()) {
160      ParseFilter parseFilter = new ParseFilter();
161      out.setFilter(parseFilter.parseFilterString(in.getFilterString()));
162    }
163
164    if (in.isSetAttributes()) {
165      addAttributes(out,in.getAttributes());
166    }
167
168    if (in.isSetAuthorizations()) {
169      out.setAuthorizations(new Authorizations(in.getAuthorizations().getLabels()));
170    }
171
172    if (in.isSetConsistency()) {
173      out.setConsistency(consistencyFromThrift(in.getConsistency()));
174    }
175
176    if (in.isSetTargetReplicaId()) {
177      out.setReplicaId(in.getTargetReplicaId());
178    }
179
180    if (in.isSetCacheBlocks()) {
181      out.setCacheBlocks(in.isCacheBlocks());
182    }
183    if (in.isSetStoreLimit()) {
184      out.setMaxResultsPerColumnFamily(in.getStoreLimit());
185    }
186    if (in.isSetStoreOffset()) {
187      out.setRowOffsetPerColumnFamily(in.getStoreOffset());
188    }
189    if (in.isSetExistence_only()) {
190      out.setCheckExistenceOnly(in.isExistence_only());
191    }
192
193    if (in.isSetColumns()) {
194      for (TColumn column : in.getColumns()) {
195        if (column.isSetQualifier()) {
196          out.addColumn(column.getFamily(), column.getQualifier());
197        } else {
198          out.addFamily(column.getFamily());
199        }
200      }
201    }
202
203    if (in.isSetFilterBytes()) {
204      out.setFilter(filterFromThrift(in.getFilterBytes()));
205    }
206    return out;
207  }
208
209  /**
210   * Converts multiple {@link TGet}s (Thrift) into a list of {@link Get}s (HBase).
211   *
212   * @param in list of <code>TGet</code>s to convert
213   *
214   * @return list of <code>Get</code> objects
215   *
216   * @throws IOException if an invalid time range or max version parameter is given
217   * @see #getFromThrift(TGet)
218   */
219  public static List<Get> getsFromThrift(List<TGet> in) throws IOException {
220    List<Get> out = new ArrayList<>(in.size());
221    for (TGet get : in) {
222      out.add(getFromThrift(get));
223    }
224    return out;
225  }
226
227  /**
228   * Creates a {@link TResult} (Thrift) from a {@link Result} (HBase).
229   *
230   * @param in the <code>Result</code> to convert
231   *
232   * @return converted result, returns an empty result if the input is <code>null</code>
233   */
234  public static TResult resultFromHBase(Result in) {
235    Cell[] raw = in.rawCells();
236    TResult out = new TResult();
237    byte[] row = in.getRow();
238    if (row != null) {
239      out.setRow(in.getRow());
240    }
241    List<TColumnValue> columnValues = new ArrayList<>(raw.length);
242    for (Cell kv : raw) {
243      TColumnValue col = new TColumnValue();
244      col.setFamily(CellUtil.cloneFamily(kv));
245      col.setQualifier(CellUtil.cloneQualifier(kv));
246      col.setTimestamp(kv.getTimestamp());
247      col.setValue(CellUtil.cloneValue(kv));
248      col.setType(kv.getType().getCode());
249      if (kv.getTagsLength() > 0) {
250        col.setTags(PrivateCellUtil.cloneTags(kv));
251      }
252      columnValues.add(col);
253    }
254    out.setColumnValues(columnValues);
255
256    out.setStale(in.isStale());
257
258    out.setPartial(in.mayHaveMoreCellsInRow());
259    return out;
260  }
261
262  /**
263   * Converts multiple {@link Result}s (HBase) into a list of {@link TResult}s (Thrift).
264   *
265   * @param in array of <code>Result</code>s to convert
266   *
267   * @return list of converted <code>TResult</code>s
268   *
269   * @see #resultFromHBase(Result)
270   */
271  public static List<TResult> resultsFromHBase(Result[] in) {
272    List<TResult> out = new ArrayList<>(in.length);
273    for (Result result : in) {
274      out.add(resultFromHBase(result));
275    }
276    return out;
277  }
278
279  /**
280   * Creates a {@link Put} (HBase) from a {@link TPut} (Thrift)
281   *
282   * @param in the <code>TPut</code> to convert
283   *
284   * @return converted <code>Put</code>
285   */
286  public static Put putFromThrift(TPut in) {
287    Put out;
288
289    if (in.isSetTimestamp()) {
290      out = new Put(in.getRow(), in.getTimestamp());
291    } else {
292      out = new Put(in.getRow());
293    }
294
295    if (in.isSetDurability()) {
296      out.setDurability(durabilityFromThrift(in.getDurability()));
297    }
298
299    for (TColumnValue columnValue : in.getColumnValues()) {
300      try {
301        if (columnValue.isSetTimestamp()) {
302          out.add(CellBuilderFactory.create(CellBuilderType.DEEP_COPY)
303              .setRow(out.getRow())
304              .setFamily(columnValue.getFamily())
305              .setQualifier(columnValue.getQualifier())
306              .setTimestamp(columnValue.getTimestamp())
307              .setType(Cell.Type.Put)
308              .setValue(columnValue.getValue())
309              .build());
310        } else {
311          out.add(CellBuilderFactory.create(CellBuilderType.DEEP_COPY)
312              .setRow(out.getRow())
313              .setFamily(columnValue.getFamily())
314              .setQualifier(columnValue.getQualifier())
315              .setTimestamp(out.getTimestamp())
316              .setType(Cell.Type.Put)
317              .setValue(columnValue.getValue())
318              .build());
319        }
320      } catch (IOException e) {
321        throw new IllegalArgumentException((e));
322      }
323    }
324
325    if (in.isSetAttributes()) {
326      addAttributes(out,in.getAttributes());
327    }
328
329    if (in.getCellVisibility() != null) {
330      out.setCellVisibility(new CellVisibility(in.getCellVisibility().getExpression()));
331    }
332
333    return out;
334  }
335
336  /**
337   * Converts multiple {@link TPut}s (Thrift) into a list of {@link Put}s (HBase).
338   *
339   * @param in list of <code>TPut</code>s to convert
340   *
341   * @return list of converted <code>Put</code>s
342   *
343   * @see #putFromThrift(TPut)
344   */
345  public static List<Put> putsFromThrift(List<TPut> in) {
346    List<Put> out = new ArrayList<>(in.size());
347    for (TPut put : in) {
348      out.add(putFromThrift(put));
349    }
350    return out;
351  }
352
353  /**
354   * Creates a {@link Delete} (HBase) from a {@link TDelete} (Thrift).
355   *
356   * @param in the <code>TDelete</code> to convert
357   *
358   * @return converted <code>Delete</code>
359   */
360  public static Delete deleteFromThrift(TDelete in) {
361    Delete out;
362
363    if (in.isSetColumns()) {
364      out = new Delete(in.getRow());
365      for (TColumn column : in.getColumns()) {
366        if (in.isSetDeleteType()) {
367          switch (in.getDeleteType()) {
368          case DELETE_COLUMN:
369            if (column.isSetTimestamp()) {
370              out.addColumn(column.getFamily(), column.getQualifier(), column.getTimestamp());
371            } else {
372              out.addColumn(column.getFamily(), column.getQualifier());
373            }
374            break;
375          case DELETE_COLUMNS:
376            if (column.isSetTimestamp()) {
377              out.addColumns(column.getFamily(), column.getQualifier(), column.getTimestamp());
378            } else {
379              out.addColumns(column.getFamily(), column.getQualifier());
380            }
381            break;
382          case DELETE_FAMILY:
383            if (column.isSetTimestamp()) {
384              out.addFamily(column.getFamily(), column.getTimestamp());
385            } else {
386              out.addFamily(column.getFamily());
387            }
388            break;
389          case DELETE_FAMILY_VERSION:
390            if (column.isSetTimestamp()) {
391              out.addFamilyVersion(column.getFamily(), column.getTimestamp());
392            } else {
393              throw new IllegalArgumentException(
394                  "Timestamp is required for TDelete with DeleteFamilyVersion type");
395            }
396            break;
397          }
398        } else {
399          throw new IllegalArgumentException("DeleteType is required for TDelete");
400        }
401      }
402    } else {
403      if (in.isSetTimestamp()) {
404        out = new Delete(in.getRow(), in.getTimestamp());
405      } else {
406        out = new Delete(in.getRow());
407      }
408    }
409
410    if (in.isSetAttributes()) {
411      addAttributes(out,in.getAttributes());
412    }
413
414    if (in.isSetDurability()) {
415      out.setDurability(durabilityFromThrift(in.getDurability()));
416    }
417
418    return out;
419  }
420
421  /**
422   * Converts multiple {@link TDelete}s (Thrift) into a list of {@link Delete}s (HBase).
423   *
424   * @param in list of <code>TDelete</code>s to convert
425   *
426   * @return list of converted <code>Delete</code>s
427   *
428   * @see #deleteFromThrift(TDelete)
429   */
430
431  public static List<Delete> deletesFromThrift(List<TDelete> in) {
432    List<Delete> out = new ArrayList<>(in.size());
433    for (TDelete delete : in) {
434      out.add(deleteFromThrift(delete));
435    }
436    return out;
437  }
438
439  public static TDeleteType deleteTypeFromHBase(Cell.Type type) {
440    switch (type) {
441      case Delete: return TDeleteType.DELETE_COLUMN;
442      case DeleteColumn: return TDeleteType.DELETE_COLUMNS;
443      case DeleteFamily: return TDeleteType.DELETE_FAMILY;
444      case DeleteFamilyVersion: return TDeleteType.DELETE_FAMILY_VERSION;
445      default: throw new IllegalArgumentException("Unknow delete type " + type);
446    }  }
447
448  public static TDelete deleteFromHBase(Delete in) {
449    TDelete out = new TDelete(ByteBuffer.wrap(in.getRow()));
450
451    List<TColumn> columns = new ArrayList<>(in.getFamilyCellMap().entrySet().size());
452    long rowTimestamp = in.getTimestamp();
453    if (rowTimestamp != HConstants.LATEST_TIMESTAMP) {
454      out.setTimestamp(rowTimestamp);
455    }
456
457    for (Map.Entry<String, byte[]> attribute : in.getAttributesMap().entrySet()) {
458      out.putToAttributes(ByteBuffer.wrap(Bytes.toBytes(attribute.getKey())),
459          ByteBuffer.wrap(attribute.getValue()));
460    }
461    if (in.getDurability() != Durability.USE_DEFAULT)  {
462      out.setDurability(durabilityFromHBase(in.getDurability()));
463    }
464    // Delete the whole row
465    if (in.getFamilyCellMap().size() == 0) {
466      return out;
467    }
468    TDeleteType type = null;
469    for (Map.Entry<byte[], List<Cell>> familyEntry:
470        in.getFamilyCellMap().entrySet()) {
471      byte[] family = familyEntry.getKey();
472      TColumn column = new TColumn(ByteBuffer.wrap(familyEntry.getKey()));
473      for (Cell cell: familyEntry.getValue()) {
474        TDeleteType cellDeleteType = deleteTypeFromHBase(cell.getType());
475        if (type == null) {
476          type = cellDeleteType;
477        } else if (type != cellDeleteType){
478          throw new RuntimeException("Only the same delete type is supported, but two delete type "
479              + "is founded, one is " + type + " the other one is " + cellDeleteType);
480        }
481        byte[] qualifier = CellUtil.cloneQualifier(cell);
482        long timestamp = cell.getTimestamp();
483        column.setFamily(family);
484        if (qualifier != null) {
485          column.setQualifier(qualifier);
486        }
487        if (timestamp != HConstants.LATEST_TIMESTAMP) {
488          column.setTimestamp(timestamp);
489        }
490      }
491      columns.add(column);
492    }
493    out.setColumns(columns);
494    out.setDeleteType(type);
495
496    return out;
497  }
498
499  /**
500   * Creates a {@link RowMutations} (HBase) from a {@link TRowMutations} (Thrift)
501   *
502   * @param in the <code>TRowMutations</code> to convert
503   *
504   * @return converted <code>RowMutations</code>
505   */
506  public static RowMutations rowMutationsFromThrift(TRowMutations in) throws IOException {
507    List<TMutation> mutations = in.getMutations();
508    RowMutations out = new RowMutations(in.getRow(), mutations.size());
509    for (TMutation mutation : mutations) {
510      if (mutation.isSetPut()) {
511        out.add(putFromThrift(mutation.getPut()));
512      }
513      if (mutation.isSetDeleteSingle()) {
514        out.add(deleteFromThrift(mutation.getDeleteSingle()));
515      }
516    }
517    return out;
518  }
519
520  public static Scan scanFromThrift(TScan in) throws IOException {
521    Scan out = new Scan();
522
523    if (in.isSetStartRow())
524      out.setStartRow(in.getStartRow());
525    if (in.isSetStopRow())
526      out.setStopRow(in.getStopRow());
527    if (in.isSetCaching())
528      out.setCaching(in.getCaching());
529    if (in.isSetMaxVersions()) {
530      out.setMaxVersions(in.getMaxVersions());
531    }
532
533    if (in.isSetColumns()) {
534      for (TColumn column : in.getColumns()) {
535        if (column.isSetQualifier()) {
536          out.addColumn(column.getFamily(), column.getQualifier());
537        } else {
538          out.addFamily(column.getFamily());
539        }
540      }
541    }
542
543    TTimeRange timeRange = in.getTimeRange();
544    if (timeRange != null &&
545        timeRange.isSetMinStamp() && timeRange.isSetMaxStamp()) {
546      out.setTimeRange(timeRange.getMinStamp(), timeRange.getMaxStamp());
547    }
548
549    if (in.isSetBatchSize()) {
550      out.setBatch(in.getBatchSize());
551    }
552
553    if (in.isSetFilterString()) {
554      ParseFilter parseFilter = new ParseFilter();
555      out.setFilter(parseFilter.parseFilterString(in.getFilterString()));
556    }
557
558    if (in.isSetAttributes()) {
559      addAttributes(out,in.getAttributes());
560    }
561
562    if (in.isSetAuthorizations()) {
563      out.setAuthorizations(new Authorizations(in.getAuthorizations().getLabels()));
564    }
565
566    if (in.isSetReversed()) {
567      out.setReversed(in.isReversed());
568    }
569
570    if (in.isSetCacheBlocks()) {
571      out.setCacheBlocks(in.isCacheBlocks());
572    }
573
574    if (in.isSetColFamTimeRangeMap()) {
575      Map<ByteBuffer, TTimeRange> colFamTimeRangeMap = in.getColFamTimeRangeMap();
576      if (MapUtils.isNotEmpty(colFamTimeRangeMap)) {
577        for (Map.Entry<ByteBuffer, TTimeRange> entry : colFamTimeRangeMap.entrySet()) {
578          out.setColumnFamilyTimeRange(Bytes.toBytes(entry.getKey()),
579              entry.getValue().getMinStamp(), entry.getValue().getMaxStamp());
580        }
581      }
582    }
583
584    if (in.isSetReadType()) {
585      out.setReadType(readTypeFromThrift(in.getReadType()));
586    }
587
588    if (in.isSetLimit()) {
589      out.setLimit(in.getLimit());
590    }
591
592    if (in.isSetConsistency()) {
593      out.setConsistency(consistencyFromThrift(in.getConsistency()));
594    }
595
596    if (in.isSetTargetReplicaId()) {
597      out.setReplicaId(in.getTargetReplicaId());
598    }
599
600    if (in.isSetFilterBytes()) {
601      out.setFilter(filterFromThrift(in.getFilterBytes()));
602    }
603
604    return out;
605  }
606
607  public static byte[] filterFromHBase(Filter filter) throws IOException {
608    FilterProtos.Filter filterPB = ProtobufUtil.toFilter(filter);
609    return filterPB.toByteArray();
610  }
611
612  public static Filter filterFromThrift(byte[] filterBytes) throws IOException {
613    FilterProtos.Filter filterPB  = FilterProtos.Filter.parseFrom(filterBytes);
614    return ProtobufUtil.toFilter(filterPB);
615  }
616
617  public static TScan scanFromHBase(Scan in) throws IOException {
618    TScan out = new TScan();
619    out.setStartRow(in.getStartRow());
620    out.setStopRow(in.getStopRow());
621    out.setCaching(in.getCaching());
622    out.setMaxVersions(in.getMaxVersions());
623    for (Map.Entry<byte[], NavigableSet<byte[]>> family : in.getFamilyMap().entrySet()) {
624
625      if (family.getValue() != null && !family.getValue().isEmpty()) {
626        for (byte[] qualifier : family.getValue()) {
627          TColumn column = new TColumn();
628          column.setFamily(family.getKey());
629          column.setQualifier(qualifier);
630          out.addToColumns(column);
631        }
632      } else {
633        TColumn column = new TColumn();
634        column.setFamily(family.getKey());
635        out.addToColumns(column);
636      }
637    }
638    TTimeRange tTimeRange = new TTimeRange();
639    tTimeRange.setMinStamp(in.getTimeRange().getMin()).setMaxStamp(in.getTimeRange().getMax());
640    out.setTimeRange(tTimeRange);
641    out.setBatchSize(in.getBatch());
642
643    for (Map.Entry<String, byte[]> attribute : in.getAttributesMap().entrySet()) {
644      out.putToAttributes(ByteBuffer.wrap(Bytes.toBytes(attribute.getKey())),
645          ByteBuffer.wrap(attribute.getValue()));
646    }
647
648    try {
649      Authorizations authorizations = in.getAuthorizations();
650      if (authorizations != null) {
651        TAuthorization tAuthorization = new TAuthorization();
652        tAuthorization.setLabels(authorizations.getLabels());
653        out.setAuthorizations(tAuthorization);
654      }
655    } catch (DeserializationException e) {
656      throw new RuntimeException(e);
657    }
658
659    out.setReversed(in.isReversed());
660    out.setCacheBlocks(in.getCacheBlocks());
661    out.setReadType(readTypeFromHBase(in.getReadType()));
662    out.setLimit(in.getLimit());
663    out.setConsistency(consistencyFromHBase(in.getConsistency()));
664    out.setTargetReplicaId(in.getReplicaId());
665    for (Map.Entry<byte[], TimeRange> entry : in.getColumnFamilyTimeRange().entrySet()) {
666      if (entry.getValue() != null) {
667        TTimeRange timeRange = new TTimeRange();
668        timeRange.setMinStamp(entry.getValue().getMin()).setMaxStamp(entry.getValue().getMax());
669        out.putToColFamTimeRangeMap(ByteBuffer.wrap(entry.getKey()), timeRange);
670      }
671    }
672    if (in.getFilter() != null) {
673      try {
674        out.setFilterBytes(filterFromHBase(in.getFilter()));
675      } catch (IOException ioE) {
676        throw new RuntimeException(ioE);
677      }
678    }
679    return out;
680  }
681
682  public static Increment incrementFromThrift(TIncrement in) throws IOException {
683    Increment out = new Increment(in.getRow());
684    for (TColumnIncrement column : in.getColumns()) {
685      out.addColumn(column.getFamily(), column.getQualifier(), column.getAmount());
686    }
687
688    if (in.isSetAttributes()) {
689      addAttributes(out,in.getAttributes());
690    }
691
692    if (in.isSetDurability()) {
693      out.setDurability(durabilityFromThrift(in.getDurability()));
694    }
695
696    if(in.getCellVisibility() != null) {
697      out.setCellVisibility(new CellVisibility(in.getCellVisibility().getExpression()));
698    }
699
700    if (in.isSetReturnResults()) {
701      out.setReturnResults(in.isReturnResults());
702    }
703
704    return out;
705  }
706
707  public static Append appendFromThrift(TAppend append) throws IOException {
708    Append out = new Append(append.getRow());
709    for (TColumnValue column : append.getColumns()) {
710      out.addColumn(column.getFamily(), column.getQualifier(), column.getValue());
711    }
712
713    if (append.isSetAttributes()) {
714      addAttributes(out, append.getAttributes());
715    }
716
717    if (append.isSetDurability()) {
718      out.setDurability(durabilityFromThrift(append.getDurability()));
719    }
720
721    if(append.getCellVisibility() != null) {
722      out.setCellVisibility(new CellVisibility(append.getCellVisibility().getExpression()));
723    }
724
725    if (append.isSetReturnResults()) {
726      out.setReturnResults(append.isReturnResults());
727    }
728
729    return out;
730  }
731
732  public static THRegionLocation regionLocationFromHBase(HRegionLocation hrl) {
733    HRegionInfo hri = hrl.getRegionInfo();
734    ServerName serverName = hrl.getServerName();
735
736    THRegionInfo thRegionInfo = new THRegionInfo();
737    THRegionLocation thRegionLocation = new THRegionLocation();
738    TServerName tServerName = new TServerName();
739
740    tServerName.setHostName(serverName.getHostname());
741    tServerName.setPort(serverName.getPort());
742    tServerName.setStartCode(serverName.getStartcode());
743
744    thRegionInfo.setTableName(hri.getTable().getName());
745    thRegionInfo.setEndKey(hri.getEndKey());
746    thRegionInfo.setStartKey(hri.getStartKey());
747    thRegionInfo.setOffline(hri.isOffline());
748    thRegionInfo.setSplit(hri.isSplit());
749    thRegionInfo.setReplicaId(hri.getReplicaId());
750
751    thRegionLocation.setRegionInfo(thRegionInfo);
752    thRegionLocation.setServerName(tServerName);
753
754    return thRegionLocation;
755  }
756
757  public static List<THRegionLocation> regionLocationsFromHBase(List<HRegionLocation> locations) {
758    List<THRegionLocation> tlocations = new ArrayList<>(locations.size());
759    for (HRegionLocation hrl:locations) {
760      tlocations.add(regionLocationFromHBase(hrl));
761    }
762    return tlocations;
763  }
764
765  /**
766   * Adds all the attributes into the Operation object
767   */
768  private static void addAttributes(OperationWithAttributes op,
769                                    Map<ByteBuffer, ByteBuffer> attributes) {
770    if (attributes == null || attributes.isEmpty()) {
771      return;
772    }
773    for (Map.Entry<ByteBuffer, ByteBuffer> entry : attributes.entrySet()) {
774      String name = Bytes.toStringBinary(getBytes(entry.getKey()));
775      byte[] value =  getBytes(entry.getValue());
776      op.setAttribute(name, value);
777    }
778  }
779
780  private static Durability durabilityFromThrift(TDurability tDurability) {
781    switch (tDurability.getValue()) {
782      case 0: return Durability.USE_DEFAULT;
783      case 1: return Durability.SKIP_WAL;
784      case 2: return Durability.ASYNC_WAL;
785      case 3: return Durability.SYNC_WAL;
786      case 4: return Durability.FSYNC_WAL;
787      default: return Durability.USE_DEFAULT;
788    }
789  }
790
791  public static CompareOperator compareOpFromThrift(TCompareOp tCompareOp) {
792    switch (tCompareOp.getValue()) {
793      case 0: return CompareOperator.LESS;
794      case 1: return CompareOperator.LESS_OR_EQUAL;
795      case 2: return CompareOperator.EQUAL;
796      case 3: return CompareOperator.NOT_EQUAL;
797      case 4: return CompareOperator.GREATER_OR_EQUAL;
798      case 5: return CompareOperator.GREATER;
799      case 6: return CompareOperator.NO_OP;
800      default: return null;
801    }
802  }
803
804  private static ReadType readTypeFromThrift(TReadType tReadType) {
805    switch (tReadType.getValue()) {
806      case 1: return ReadType.DEFAULT;
807      case 2: return ReadType.STREAM;
808      case 3: return ReadType.PREAD;
809      default: return null;
810    }
811  }
812
813  private static TReadType readTypeFromHBase(ReadType readType) {
814    switch (readType) {
815      case DEFAULT: return TReadType.DEFAULT;
816      case STREAM: return TReadType.STREAM;
817      case PREAD: return TReadType.PREAD;
818      default: return TReadType.DEFAULT;
819    }
820  }
821
822  private static Consistency consistencyFromThrift(TConsistency tConsistency) {
823    switch (tConsistency.getValue()) {
824      case 1: return Consistency.STRONG;
825      case 2: return Consistency.TIMELINE;
826      default: return Consistency.STRONG;
827    }
828  }
829
830  public static TableName tableNameFromThrift(TTableName tableName) {
831    return TableName.valueOf(tableName.getNs(), tableName.getQualifier());
832  }
833
834  public static TableName[] tableNamesArrayFromThrift(List<TTableName> tableNames) {
835    TableName[] out = new TableName[tableNames.size()];
836    int index = 0;
837    for (TTableName tableName : tableNames) {
838      out[index++] = tableNameFromThrift(tableName);
839    }
840    return out;
841  }
842
843  public static List<TableName> tableNamesFromThrift(List<TTableName> tableNames) {
844    List<TableName> out = new ArrayList<>(tableNames.size());
845    for (TTableName tableName : tableNames) {
846      out.add(tableNameFromThrift(tableName));
847    }
848    return out;
849  }
850
851  public static TTableName tableNameFromHBase(TableName table) {
852    TTableName tableName = new TTableName();
853    tableName.setNs(table.getNamespace());
854    tableName.setQualifier(table.getQualifier());
855    return tableName;
856  }
857
858  public static List<TTableName> tableNamesFromHBase(List<TableName> in) {
859    List<TTableName> out = new ArrayList<>(in.size());
860    for (TableName tableName : in) {
861      out.add(tableNameFromHBase(tableName));
862    }
863    return out;
864  }
865
866  public static List<TTableName> tableNamesFromHBase(TableName[] in) {
867    List<TTableName> out = new ArrayList<>(in.length);
868    for (TableName tableName : in) {
869      out.add(tableNameFromHBase(tableName));
870    }
871    return out;
872  }
873
874  public static byte[][] splitKeyFromThrift(List<ByteBuffer> in) {
875    if (in == null || in.size() == 0) {
876      return null;
877    }
878    byte[][] out = new byte[in.size()][];
879    int index = 0;
880    for (ByteBuffer key : in) {
881      out[index++] = key.array();
882    }
883    return out;
884  }
885
886  public static BloomType bloomFilterFromThrift(TBloomFilterType in) {
887    switch (in.getValue()) {
888      case 0: return BloomType.NONE;
889      case 1: return BloomType.ROW;
890      case 2: return BloomType.ROWCOL;
891      case 3: return BloomType.ROWPREFIX_FIXED_LENGTH;
892      default: return BloomType.ROW;
893    }
894  }
895
896  public static Compression.Algorithm compressionAlgorithmFromThrift(TCompressionAlgorithm in) {
897    switch (in.getValue()) {
898      case 0: return Compression.Algorithm.LZO;
899      case 1: return Compression.Algorithm.GZ;
900      case 2: return Compression.Algorithm.NONE;
901      case 3: return Compression.Algorithm.SNAPPY;
902      case 4: return Compression.Algorithm.LZ4;
903      case 5: return Compression.Algorithm.BZIP2;
904      case 6: return Compression.Algorithm.ZSTD;
905      default: return Compression.Algorithm.NONE;
906    }
907  }
908
909  public static DataBlockEncoding dataBlockEncodingFromThrift(TDataBlockEncoding in) {
910    switch (in.getValue()) {
911      case 0: return DataBlockEncoding.NONE;
912      case 2: return DataBlockEncoding.PREFIX;
913      case 3: return DataBlockEncoding.DIFF;
914      case 4: return DataBlockEncoding.FAST_DIFF;
915      case 7: return DataBlockEncoding.ROW_INDEX_V1;
916      default: return DataBlockEncoding.NONE;
917    }
918  }
919
920  public static KeepDeletedCells keepDeletedCellsFromThrift(TKeepDeletedCells in) {
921    switch (in.getValue()) {
922      case 0: return KeepDeletedCells.FALSE;
923      case 1: return KeepDeletedCells.TRUE;
924      case 2: return KeepDeletedCells.TTL;
925      default: return KeepDeletedCells.FALSE;
926    }
927  }
928
929  public static ColumnFamilyDescriptor columnFamilyDescriptorFromThrift(
930      TColumnFamilyDescriptor in) {
931    ColumnFamilyDescriptorBuilder builder = ColumnFamilyDescriptorBuilder
932        .newBuilder(in.getName());
933
934    if (in.isSetAttributes()) {
935      for (Map.Entry<ByteBuffer, ByteBuffer> attribute : in.getAttributes().entrySet()) {
936        builder.setValue(attribute.getKey().array(), attribute.getValue().array());
937      }
938    }
939    if (in.isSetConfiguration()) {
940      for (Map.Entry<String, String> conf : in.getConfiguration().entrySet()) {
941        builder.setConfiguration(conf.getKey(), conf.getValue());
942      }
943    }
944    if (in.isSetBlockSize()) {
945      builder.setBlocksize(in.getBlockSize());
946    }
947    if (in.isSetBloomnFilterType()) {
948      builder.setBloomFilterType(bloomFilterFromThrift(in.getBloomnFilterType()));
949    }
950    if (in.isSetCompressionType()) {
951      builder.setCompressionType(compressionAlgorithmFromThrift(in.getCompressionType()));
952    }
953    if (in.isSetDfsReplication()) {
954      builder.setDFSReplication(in.getDfsReplication());
955    }
956    if (in.isSetDataBlockEncoding()) {
957      builder.setDataBlockEncoding(dataBlockEncodingFromThrift(in.getDataBlockEncoding()));
958    }
959    if (in.isSetKeepDeletedCells()) {
960      builder.setKeepDeletedCells(keepDeletedCellsFromThrift(in.getKeepDeletedCells()));
961    }
962    if (in.isSetMaxVersions()) {
963      builder.setMaxVersions(in.getMaxVersions());
964    }
965    if (in.isSetMinVersions()) {
966      builder.setMinVersions(in.getMinVersions());
967    }
968    if (in.isSetScope()) {
969      builder.setScope(in.getScope());
970    }
971    if (in.isSetTimeToLive()) {
972      builder.setTimeToLive(in.getTimeToLive());
973    }
974    if (in.isSetBlockCacheEnabled()) {
975      builder.setBlockCacheEnabled(in.isBlockCacheEnabled());
976    }
977    if (in.isSetCacheBloomsOnWrite()) {
978      builder.setCacheBloomsOnWrite(in.isCacheBloomsOnWrite());
979    }
980    if (in.isSetCacheDataOnWrite()) {
981      builder.setCacheDataOnWrite(in.isCacheDataOnWrite());
982    }
983    if (in.isSetCacheIndexesOnWrite()) {
984      builder.setCacheIndexesOnWrite(in.isCacheIndexesOnWrite());
985    }
986    if (in.isSetCompressTags()) {
987      builder.setCompressTags(in.isCompressTags());
988    }
989    if (in.isSetEvictBlocksOnClose()) {
990      builder.setEvictBlocksOnClose(in.isEvictBlocksOnClose());
991    }
992    if (in.isSetInMemory()) {
993      builder.setInMemory(in.isInMemory());
994    }
995
996
997    return builder.build();
998  }
999
1000  public static NamespaceDescriptor namespaceDescriptorFromThrift(TNamespaceDescriptor in) {
1001    NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(in.getName());
1002    if (in.isSetConfiguration()) {
1003      for (Map.Entry<String, String> conf : in.getConfiguration().entrySet()) {
1004        builder.addConfiguration(conf.getKey(), conf.getValue());
1005      }
1006    }
1007    return builder.build();
1008  }
1009
1010  public static TNamespaceDescriptor namespaceDescriptorFromHBase(NamespaceDescriptor in) {
1011    TNamespaceDescriptor out = new TNamespaceDescriptor();
1012    out.setName(in.getName());
1013    for (Map.Entry<String, String> conf : in.getConfiguration().entrySet()) {
1014      out.putToConfiguration(conf.getKey(), conf.getValue());
1015    }
1016    return out;
1017  }
1018
1019  public static List<TNamespaceDescriptor> namespaceDescriptorsFromHBase(
1020      NamespaceDescriptor[] in) {
1021    List<TNamespaceDescriptor> out = new ArrayList<>(in.length);
1022    for (NamespaceDescriptor descriptor : in) {
1023      out.add(namespaceDescriptorFromHBase(descriptor));
1024    }
1025    return out;
1026  }
1027
1028  public static TableDescriptor tableDescriptorFromThrift(TTableDescriptor in) {
1029    TableDescriptorBuilder builder = TableDescriptorBuilder
1030        .newBuilder(tableNameFromThrift(in.getTableName()));
1031    for (TColumnFamilyDescriptor column : in.getColumns()) {
1032      builder.setColumnFamily(columnFamilyDescriptorFromThrift(column));
1033    }
1034    if (in.isSetAttributes()) {
1035      for (Map.Entry<ByteBuffer, ByteBuffer> attribute : in.getAttributes().entrySet()) {
1036        builder.setValue(attribute.getKey().array(), attribute.getValue().array());
1037      }
1038    }
1039    if (in.isSetDurability()) {
1040      builder.setDurability(durabilityFromThrift(in.getDurability()));
1041    }
1042    return builder.build();
1043  }
1044
1045  public static HTableDescriptor hTableDescriptorFromThrift(TTableDescriptor in) {
1046    return new HTableDescriptor(tableDescriptorFromThrift(in));
1047  }
1048
1049  public static HTableDescriptor[] hTableDescriptorsFromThrift(List<TTableDescriptor> in) {
1050    HTableDescriptor[] out = new HTableDescriptor[in.size()];
1051    int index = 0;
1052    for (TTableDescriptor tTableDescriptor : in) {
1053      out[index++] = hTableDescriptorFromThrift(tTableDescriptor);
1054    }
1055    return out;
1056  }
1057
1058
1059  public static List<TableDescriptor> tableDescriptorsFromThrift(List<TTableDescriptor> in) {
1060    List<TableDescriptor> out = new ArrayList<>();
1061    for (TTableDescriptor tableDescriptor : in) {
1062      out.add(tableDescriptorFromThrift(tableDescriptor));
1063    }
1064    return out;
1065  }
1066
1067  private static TDurability durabilityFromHBase(Durability durability) {
1068    switch (durability) {
1069      case USE_DEFAULT: return TDurability.USE_DEFAULT;
1070      case SKIP_WAL: return TDurability.SKIP_WAL;
1071      case ASYNC_WAL: return TDurability.ASYNC_WAL;
1072      case SYNC_WAL: return TDurability.SYNC_WAL;
1073      case FSYNC_WAL: return TDurability.FSYNC_WAL;
1074      default: return null;
1075    }
1076  }
1077
1078  public static TTableDescriptor tableDescriptorFromHBase(TableDescriptor in) {
1079    TTableDescriptor out = new TTableDescriptor();
1080    out.setTableName(tableNameFromHBase(in.getTableName()));
1081    Map<Bytes, Bytes> attributes = in.getValues();
1082    for (Map.Entry<Bytes, Bytes> attribute : attributes.entrySet()) {
1083      out.putToAttributes(ByteBuffer.wrap(attribute.getKey().get()),
1084          ByteBuffer.wrap(attribute.getValue().get()));
1085    }
1086    for (ColumnFamilyDescriptor column : in.getColumnFamilies()) {
1087      out.addToColumns(columnFamilyDescriptorFromHBase(column));
1088    }
1089    out.setDurability(durabilityFromHBase(in.getDurability()));
1090    return out;
1091  }
1092
1093  public static List<TTableDescriptor> tableDescriptorsFromHBase(List<TableDescriptor> in) {
1094    List<TTableDescriptor> out = new ArrayList<>(in.size());
1095    for (TableDescriptor descriptor : in) {
1096      out.add(tableDescriptorFromHBase(descriptor));
1097    }
1098    return out;
1099  }
1100
1101  public static List<TTableDescriptor> tableDescriptorsFromHBase(TableDescriptor[] in) {
1102    List<TTableDescriptor> out = new ArrayList<>(in.length);
1103    for (TableDescriptor descriptor : in) {
1104      out.add(tableDescriptorFromHBase(descriptor));
1105    }
1106    return out;
1107  }
1108
1109
1110  public static TBloomFilterType bloomFilterFromHBase(BloomType in) {
1111    switch (in) {
1112      case NONE: return TBloomFilterType.NONE;
1113      case ROW: return TBloomFilterType.ROW;
1114      case ROWCOL: return TBloomFilterType.ROWCOL;
1115      case ROWPREFIX_FIXED_LENGTH: return TBloomFilterType.ROWPREFIX_FIXED_LENGTH;
1116      default: return TBloomFilterType.ROW;
1117    }
1118  }
1119
1120  public static TCompressionAlgorithm compressionAlgorithmFromHBase(Compression.Algorithm in) {
1121    switch (in) {
1122      case LZO: return TCompressionAlgorithm.LZO;
1123      case GZ: return TCompressionAlgorithm.GZ;
1124      case NONE: return TCompressionAlgorithm.NONE;
1125      case SNAPPY: return TCompressionAlgorithm.SNAPPY;
1126      case LZ4: return TCompressionAlgorithm.LZ4;
1127      case BZIP2: return TCompressionAlgorithm.BZIP2;
1128      case ZSTD: return TCompressionAlgorithm.ZSTD;
1129      default: return TCompressionAlgorithm.NONE;
1130    }
1131  }
1132
1133  public static TDataBlockEncoding dataBlockEncodingFromHBase(DataBlockEncoding in) {
1134    switch (in) {
1135      case NONE: return TDataBlockEncoding.NONE;
1136      case PREFIX: return TDataBlockEncoding.PREFIX;
1137      case DIFF: return TDataBlockEncoding.DIFF;
1138      case FAST_DIFF: return TDataBlockEncoding.FAST_DIFF;
1139      case ROW_INDEX_V1: return TDataBlockEncoding.ROW_INDEX_V1;
1140      default: return TDataBlockEncoding.NONE;
1141    }
1142  }
1143
1144  public static TKeepDeletedCells keepDeletedCellsFromHBase(KeepDeletedCells in) {
1145    switch (in) {
1146      case FALSE: return TKeepDeletedCells.FALSE;
1147      case TRUE: return TKeepDeletedCells.TRUE;
1148      case TTL: return TKeepDeletedCells.TTL;
1149      default: return TKeepDeletedCells.FALSE;
1150    }
1151  }
1152
1153  public static TColumnFamilyDescriptor columnFamilyDescriptorFromHBase(
1154      ColumnFamilyDescriptor in) {
1155    TColumnFamilyDescriptor out = new TColumnFamilyDescriptor();
1156    out.setName(in.getName());
1157    for (Map.Entry<Bytes, Bytes> attribute : in.getValues().entrySet()) {
1158      out.putToAttributes(ByteBuffer.wrap(attribute.getKey().get()),
1159          ByteBuffer.wrap(attribute.getValue().get()));
1160    }
1161    for (Map.Entry<String, String> conf : in.getConfiguration().entrySet()) {
1162      out.putToConfiguration(conf.getKey(), conf.getValue());
1163    }
1164    out.setBlockSize(in.getBlocksize());
1165    out.setBloomnFilterType(bloomFilterFromHBase(in.getBloomFilterType()));
1166    out.setCompressionType(compressionAlgorithmFromHBase(in.getCompressionType()));
1167    out.setDfsReplication(in.getDFSReplication());
1168    out.setDataBlockEncoding(dataBlockEncodingFromHBase(in.getDataBlockEncoding()));
1169    out.setKeepDeletedCells(keepDeletedCellsFromHBase(in.getKeepDeletedCells()));
1170    out.setMaxVersions(in.getMaxVersions());
1171    out.setMinVersions(in.getMinVersions());
1172    out.setScope(in.getScope());
1173    out.setTimeToLive(in.getTimeToLive());
1174    out.setBlockCacheEnabled(in.isBlockCacheEnabled());
1175    out.setCacheBloomsOnWrite(in.isCacheBloomsOnWrite());
1176    out.setCacheDataOnWrite(in.isCacheDataOnWrite());
1177    out.setCacheIndexesOnWrite(in.isCacheIndexesOnWrite());
1178    out.setCompressTags(in.isCompressTags());
1179    out.setEvictBlocksOnClose(in.isEvictBlocksOnClose());
1180    out.setInMemory(in.isInMemory());
1181    return out;
1182  }
1183
1184
1185  private static TConsistency consistencyFromHBase(Consistency consistency) {
1186    switch (consistency) {
1187      case STRONG: return TConsistency.STRONG;
1188      case TIMELINE: return TConsistency.TIMELINE;
1189      default: return TConsistency.STRONG;
1190    }
1191  }
1192
1193  public static TGet getFromHBase(Get in) {
1194    TGet out = new TGet();
1195    out.setRow(in.getRow());
1196
1197    TTimeRange tTimeRange = new TTimeRange();
1198    tTimeRange.setMaxStamp(in.getTimeRange().getMax()).setMinStamp(in.getTimeRange().getMin());
1199    out.setTimeRange(tTimeRange);
1200    out.setMaxVersions(in.getMaxVersions());
1201
1202    for (Map.Entry<String, byte[]> attribute : in.getAttributesMap().entrySet()) {
1203      out.putToAttributes(ByteBuffer.wrap(Bytes.toBytes(attribute.getKey())),
1204          ByteBuffer.wrap(attribute.getValue()));
1205    }
1206    try {
1207      Authorizations authorizations = in.getAuthorizations();
1208      if (authorizations != null) {
1209        TAuthorization tAuthorization = new TAuthorization();
1210        tAuthorization.setLabels(authorizations.getLabels());
1211        out.setAuthorizations(tAuthorization);
1212      }
1213    } catch (DeserializationException e) {
1214      throw new RuntimeException(e);
1215    }
1216    out.setConsistency(consistencyFromHBase(in.getConsistency()));
1217    out.setTargetReplicaId(in.getReplicaId());
1218    out.setCacheBlocks(in.getCacheBlocks());
1219    out.setStoreLimit(in.getMaxResultsPerColumnFamily());
1220    out.setStoreOffset(in.getRowOffsetPerColumnFamily());
1221    out.setExistence_only(in.isCheckExistenceOnly());
1222    for (Map.Entry<byte[], NavigableSet<byte[]>> family : in.getFamilyMap().entrySet()) {
1223
1224      if (family.getValue() != null && !family.getValue().isEmpty()) {
1225        for (byte[] qualifier : family.getValue()) {
1226          TColumn column = new TColumn();
1227          column.setFamily(family.getKey());
1228          column.setQualifier(qualifier);
1229          out.addToColumns(column);
1230        }
1231      } else {
1232        TColumn column = new TColumn();
1233        column.setFamily(family.getKey());
1234        out.addToColumns(column);
1235      }
1236    }
1237    if (in.getFilter() != null) {
1238      try {
1239        out.setFilterBytes(filterFromHBase(in.getFilter()));
1240      } catch (IOException ioE) {
1241        throw new RuntimeException(ioE);
1242      }
1243    }
1244    return out;
1245  }
1246
1247  public static Cell toCell(ExtendedCellBuilder cellBuilder, byte[] row, TColumnValue columnValue) {
1248    return cellBuilder.clear()
1249        .setRow(row)
1250        .setFamily(columnValue.getFamily())
1251        .setQualifier(columnValue.getQualifier())
1252        .setTimestamp(columnValue.getTimestamp())
1253        .setType(columnValue.getType())
1254        .setValue(columnValue.getValue())
1255        .setTags(columnValue.getTags())
1256        .build();
1257  }
1258
1259
1260
1261
1262
1263
1264
1265  public static Result resultFromThrift(TResult in) {
1266    if (in == null) {
1267      return null;
1268    }
1269    if (!in.isSetColumnValues() || in.getColumnValues().isEmpty()){
1270      return in.isStale() ? EMPTY_RESULT_STALE : EMPTY_RESULT;
1271    }
1272    List<Cell> cells = new ArrayList<>(in.getColumnValues().size());
1273    ExtendedCellBuilder builder = ExtendedCellBuilderFactory.create(CellBuilderType.SHALLOW_COPY);
1274    for (TColumnValue columnValue : in.getColumnValues()) {
1275      cells.add(toCell(builder, in.getRow(), columnValue));
1276    }
1277    return Result.create(cells, null, in.isStale(), in.isPartial());
1278  }
1279
1280  public static TPut putFromHBase(Put in) {
1281    TPut out = new TPut();
1282    out.setRow(in.getRow());
1283    if (in.getTimestamp() != HConstants.LATEST_TIMESTAMP) {
1284      out.setTimestamp(in.getTimestamp());
1285    }
1286    if (in.getDurability() != Durability.USE_DEFAULT) {
1287      out.setDurability(durabilityFromHBase(in.getDurability()));
1288    }
1289    for (Map.Entry<byte [], List<Cell>> entry : in.getFamilyCellMap().entrySet()) {
1290      byte[] family = entry.getKey();
1291      for (Cell cell : entry.getValue()) {
1292        TColumnValue columnValue = new TColumnValue();
1293        columnValue.setFamily(family)
1294            .setQualifier(CellUtil.cloneQualifier(cell))
1295            .setType(cell.getType().getCode())
1296            .setTimestamp(cell.getTimestamp())
1297            .setValue(CellUtil.cloneValue(cell));
1298        if (cell.getTagsLength() != 0) {
1299          columnValue.setTags(CellUtil.cloneTags(cell));
1300        }
1301        out.addToColumnValues(columnValue);
1302      }
1303    }
1304    for (Map.Entry<String, byte[]> attribute : in.getAttributesMap().entrySet()) {
1305      out.putToAttributes(ByteBuffer.wrap(Bytes.toBytes(attribute.getKey())),
1306          ByteBuffer.wrap(attribute.getValue()));
1307    }
1308    try {
1309      CellVisibility cellVisibility = in.getCellVisibility();
1310      if (cellVisibility != null) {
1311        TCellVisibility tCellVisibility = new TCellVisibility();
1312        tCellVisibility.setExpression(cellVisibility.getExpression());
1313        out.setCellVisibility(tCellVisibility);
1314      }
1315    } catch (DeserializationException e) {
1316      throw new RuntimeException(e);
1317    }
1318    return out;
1319  }
1320
1321  public static List<TPut> putsFromHBase(List<Put> in) {
1322    List<TPut> out = new ArrayList<>(in.size());
1323    for (Put put : in) {
1324      out.add(putFromHBase(put));
1325    }
1326    return out;
1327  }
1328
1329  public static NamespaceDescriptor[] namespaceDescriptorsFromThrift(
1330      List<TNamespaceDescriptor> in) {
1331    NamespaceDescriptor[] out = new NamespaceDescriptor[in.size()];
1332    int index = 0;
1333    for (TNamespaceDescriptor descriptor : in) {
1334      out[index++] = namespaceDescriptorFromThrift(descriptor);
1335    }
1336    return out;
1337  }
1338
1339  public static List<TDelete> deletesFromHBase(List<Delete> in) {
1340    List<TDelete> out = new ArrayList<>(in.size());
1341    for (Delete delete : in) {
1342      out.add(deleteFromHBase(delete));
1343    }
1344    return out;
1345  }
1346
1347  public static TAppend appendFromHBase(Append in) throws IOException {
1348    TAppend out = new TAppend();
1349    out.setRow(in.getRow());
1350
1351    if (in.getDurability() != Durability.USE_DEFAULT) {
1352      out.setDurability(durabilityFromHBase(in.getDurability()));
1353    }
1354    for (Map.Entry<byte [], List<Cell>> entry : in.getFamilyCellMap().entrySet()) {
1355      byte[] family = entry.getKey();
1356      for (Cell cell : entry.getValue()) {
1357        TColumnValue columnValue = new TColumnValue();
1358        columnValue.setFamily(family)
1359            .setQualifier(CellUtil.cloneQualifier(cell))
1360            .setType(cell.getType().getCode())
1361            .setTimestamp(cell.getTimestamp())
1362            .setValue(CellUtil.cloneValue(cell));
1363        if (cell.getTagsLength() != 0) {
1364          columnValue.setTags(CellUtil.cloneTags(cell));
1365        }
1366        out.addToColumns(columnValue);
1367      }
1368    }
1369    for (Map.Entry<String, byte[]> attribute : in.getAttributesMap().entrySet()) {
1370      out.putToAttributes(ByteBuffer.wrap(Bytes.toBytes(attribute.getKey())),
1371          ByteBuffer.wrap(attribute.getValue()));
1372    }
1373    try {
1374      CellVisibility cellVisibility = in.getCellVisibility();
1375      if (cellVisibility != null) {
1376        TCellVisibility tCellVisibility = new TCellVisibility();
1377        tCellVisibility.setExpression(cellVisibility.getExpression());
1378        out.setCellVisibility(tCellVisibility);
1379      }
1380    } catch (DeserializationException e) {
1381      throw new RuntimeException(e);
1382    }
1383    out.setReturnResults(in.isReturnResults());
1384    return out;
1385  }
1386
1387  public static TIncrement incrementFromHBase(Increment in) throws IOException {
1388    TIncrement out = new TIncrement();
1389    out.setRow(in.getRow());
1390
1391    if (in.getDurability() != Durability.USE_DEFAULT) {
1392      out.setDurability(durabilityFromHBase(in.getDurability()));
1393    }
1394    for (Map.Entry<byte [], List<Cell>> entry : in.getFamilyCellMap().entrySet()) {
1395      byte[] family = entry.getKey();
1396      for (Cell cell : entry.getValue()) {
1397        TColumnIncrement columnValue = new TColumnIncrement();
1398        columnValue.setFamily(family).setQualifier(CellUtil.cloneQualifier(cell));
1399        columnValue.setAmount(
1400            Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
1401        out.addToColumns(columnValue);
1402      }
1403    }
1404    for (Map.Entry<String, byte[]> attribute : in.getAttributesMap().entrySet()) {
1405      out.putToAttributes(ByteBuffer.wrap(Bytes.toBytes(attribute.getKey())),
1406          ByteBuffer.wrap(attribute.getValue()));
1407    }
1408    try {
1409      CellVisibility cellVisibility = in.getCellVisibility();
1410      if (cellVisibility != null) {
1411        TCellVisibility tCellVisibility = new TCellVisibility();
1412        tCellVisibility.setExpression(cellVisibility.getExpression());
1413        out.setCellVisibility(tCellVisibility);
1414      }
1415    } catch (DeserializationException e) {
1416      throw new RuntimeException(e);
1417    }
1418    out.setReturnResults(in.isReturnResults());
1419    return out;
1420  }
1421
1422  public static TRowMutations rowMutationsFromHBase(RowMutations in) {
1423    TRowMutations tRowMutations = new TRowMutations();
1424    tRowMutations.setRow(in.getRow());
1425    for (Mutation mutation : in.getMutations()) {
1426      TMutation tMutation = new TMutation();
1427      if (mutation instanceof Put) {
1428        tMutation.setPut(ThriftUtilities.putFromHBase((Put)mutation));
1429      } else if (mutation instanceof Delete) {
1430        tMutation.setDeleteSingle(ThriftUtilities.deleteFromHBase((Delete)mutation));
1431      } else {
1432        throw new IllegalArgumentException(
1433            "Only Put and Delete is supported in mutateRow, but muation=" + mutation);
1434      }
1435      tRowMutations.addToMutations(tMutation);
1436    }
1437    return tRowMutations;
1438  }
1439
1440  public static TCompareOp compareOpFromHBase(CompareOperator compareOp) {
1441    switch (compareOp) {
1442      case LESS: return TCompareOp.LESS;
1443      case LESS_OR_EQUAL: return TCompareOp.LESS_OR_EQUAL;
1444      case EQUAL: return TCompareOp.EQUAL;
1445      case NOT_EQUAL: return TCompareOp.NOT_EQUAL;
1446      case GREATER_OR_EQUAL: return TCompareOp.GREATER_OR_EQUAL;
1447      case GREATER: return TCompareOp.GREATER;
1448      case NO_OP: return TCompareOp.NO_OP;
1449      default: return null;
1450    }
1451  }
1452  public static List<ByteBuffer> splitKeyFromHBase(byte[][] in) {
1453    if (in == null || in.length == 0) {
1454      return null;
1455    }
1456    List<ByteBuffer> out = new ArrayList<>(in.length);
1457    for (byte[] key : in) {
1458      out.add(ByteBuffer.wrap(key));
1459    }
1460    return out;
1461  }
1462
1463  public static Result[] resultsFromThrift(List<TResult> in) {
1464    Result[] out = new Result[in.size()];
1465    int index = 0;
1466    for (TResult tResult : in) {
1467      out[index++] = resultFromThrift(tResult);
1468    }
1469    return out;
1470  }
1471
1472  public static List<TGet> getsFromHBase(List<Get> in) {
1473    List<TGet> out = new ArrayList<>(in.size());
1474    for (Get get : in) {
1475      out.add(getFromHBase(get));
1476    }
1477    return out;
1478  }
1479
1480  public static Set<TServerName> getServerNamesFromHBase(Set<ServerName> serverNames) {
1481    if (CollectionUtils.isEmpty(serverNames)) {
1482      return Collections.emptySet();
1483    }
1484    return serverNames.stream().map(serverName -> {
1485      TServerName tServerName = new TServerName();
1486      tServerName.setHostName(serverName.getHostname());
1487      tServerName.setPort(serverName.getPort());
1488      tServerName.setStartCode(serverName.getStartcode());
1489      return tServerName;
1490    }).collect(Collectors.toSet());
1491  }
1492
1493  public static Set<ServerName> getServerNamesFromThrift(Set<TServerName> tServerNames) {
1494    if (CollectionUtils.isEmpty(tServerNames)) {
1495      return Collections.emptySet();
1496    }
1497    return tServerNames.stream().map(tServerName ->
1498      ServerName.valueOf(tServerName.getHostName(),
1499        tServerName.getPort(),
1500        tServerName.getStartCode()))
1501      .collect(Collectors.toSet());
1502  }
1503
1504  public static TLogQueryFilter getSlowLogQueryFromHBase(
1505      LogQueryFilter logQueryFilter) {
1506    TLogQueryFilter tLogQueryFilter = new TLogQueryFilter();
1507    tLogQueryFilter.setRegionName(logQueryFilter.getRegionName());
1508    tLogQueryFilter.setClientAddress(logQueryFilter.getClientAddress());
1509    tLogQueryFilter.setTableName(logQueryFilter.getTableName());
1510    tLogQueryFilter.setUserName(logQueryFilter.getUserName());
1511    tLogQueryFilter.setLimit(logQueryFilter.getLimit());
1512    TLogType tLogType = gettLogTypeFromHBase(logQueryFilter);
1513    tLogQueryFilter.setLogType(tLogType);
1514    TFilterByOperator tFilterByOperator = getTFilterByFromHBase(logQueryFilter);
1515    tLogQueryFilter.setFilterByOperator(tFilterByOperator);
1516    return tLogQueryFilter;
1517  }
1518
1519  private static TLogType gettLogTypeFromHBase(final LogQueryFilter logQueryFilter) {
1520    TLogType tLogType;
1521    switch (logQueryFilter.getType()) {
1522      case SLOW_LOG: {
1523        tLogType = TLogType.SLOW_LOG;
1524        break;
1525      }
1526      case LARGE_LOG: {
1527        tLogType = TLogType.LARGE_LOG;
1528        break;
1529      }
1530      default: {
1531        tLogType = TLogType.SLOW_LOG;
1532      }
1533    }
1534    return tLogType;
1535  }
1536
1537  private static TFilterByOperator getTFilterByFromHBase(final LogQueryFilter logQueryFilter) {
1538    TFilterByOperator tFilterByOperator;
1539    switch (logQueryFilter.getFilterByOperator()) {
1540      case AND: {
1541        tFilterByOperator = TFilterByOperator.AND;
1542        break;
1543      }
1544      case OR: {
1545        tFilterByOperator = TFilterByOperator.OR;
1546        break;
1547      }
1548      default: {
1549        tFilterByOperator = TFilterByOperator.OR;
1550      }
1551    }
1552    return tFilterByOperator;
1553  }
1554
1555  public static LogQueryFilter getSlowLogQueryFromThrift(
1556      TLogQueryFilter tLogQueryFilter) {
1557    LogQueryFilter logQueryFilter = new LogQueryFilter();
1558    logQueryFilter.setRegionName(tLogQueryFilter.getRegionName());
1559    logQueryFilter.setClientAddress(tLogQueryFilter.getClientAddress());
1560    logQueryFilter.setTableName(tLogQueryFilter.getTableName());
1561    logQueryFilter.setUserName(tLogQueryFilter.getUserName());
1562    logQueryFilter.setLimit(tLogQueryFilter.getLimit());
1563    LogQueryFilter.Type type = getLogTypeFromThrift(tLogQueryFilter);
1564    logQueryFilter.setType(type);
1565    LogQueryFilter.FilterByOperator filterByOperator = getFilterByFromThrift(tLogQueryFilter);
1566    logQueryFilter.setFilterByOperator(filterByOperator);
1567    return logQueryFilter;
1568  }
1569
1570  private static LogQueryFilter.Type getLogTypeFromThrift(
1571      final TLogQueryFilter tSlowLogQueryFilter) {
1572    LogQueryFilter.Type type;
1573    switch (tSlowLogQueryFilter.getLogType()) {
1574      case SLOW_LOG: {
1575        type = LogQueryFilter.Type.SLOW_LOG;
1576        break;
1577      }
1578      case LARGE_LOG: {
1579        type = LogQueryFilter.Type.LARGE_LOG;
1580        break;
1581      }
1582      default: {
1583        type = LogQueryFilter.Type.SLOW_LOG;
1584      }
1585    }
1586    return type;
1587  }
1588
1589  private static LogQueryFilter.FilterByOperator getFilterByFromThrift(
1590      final TLogQueryFilter tLogQueryFilter) {
1591    LogQueryFilter.FilterByOperator filterByOperator;
1592    switch (tLogQueryFilter.getFilterByOperator()) {
1593      case AND: {
1594        filterByOperator = LogQueryFilter.FilterByOperator.AND;
1595        break;
1596      }
1597      case OR: {
1598        filterByOperator = LogQueryFilter.FilterByOperator.OR;
1599        break;
1600      }
1601      default: {
1602        filterByOperator = LogQueryFilter.FilterByOperator.OR;
1603      }
1604    }
1605    return filterByOperator;
1606  }
1607
1608  public static List<TOnlineLogRecord> getSlowLogRecordsFromHBase(
1609      List<OnlineLogRecord> onlineLogRecords) {
1610    if (CollectionUtils.isEmpty(onlineLogRecords)) {
1611      return Collections.emptyList();
1612    }
1613    return onlineLogRecords.stream()
1614      .map(slowLogRecord -> {
1615        TOnlineLogRecord tOnlineLogRecord = new TOnlineLogRecord();
1616        tOnlineLogRecord.setCallDetails(slowLogRecord.getCallDetails());
1617        tOnlineLogRecord.setClientAddress(slowLogRecord.getClientAddress());
1618        tOnlineLogRecord.setMethodName(slowLogRecord.getMethodName());
1619        tOnlineLogRecord.setMultiGetsCount(slowLogRecord.getMultiGetsCount());
1620        tOnlineLogRecord.setMultiMutationsCount(slowLogRecord.getMultiMutationsCount());
1621        tOnlineLogRecord.setMultiServiceCalls(slowLogRecord.getMultiServiceCalls());
1622        tOnlineLogRecord.setParam(slowLogRecord.getParam());
1623        tOnlineLogRecord.setProcessingTime(slowLogRecord.getProcessingTime());
1624        tOnlineLogRecord.setQueueTime(slowLogRecord.getQueueTime());
1625        tOnlineLogRecord.setRegionName(slowLogRecord.getRegionName());
1626        tOnlineLogRecord.setResponseSize(slowLogRecord.getResponseSize());
1627        tOnlineLogRecord.setServerClass(slowLogRecord.getServerClass());
1628        tOnlineLogRecord.setStartTime(slowLogRecord.getStartTime());
1629        tOnlineLogRecord.setUserName(slowLogRecord.getUserName());
1630        return tOnlineLogRecord;
1631      }).collect(Collectors.toList());
1632  }
1633
1634  public static List<OnlineLogRecord> getSlowLogRecordsFromThrift(
1635    List<TOnlineLogRecord> tOnlineLogRecords) {
1636    if (CollectionUtils.isEmpty(tOnlineLogRecords)) {
1637      return Collections.emptyList();
1638    }
1639    return tOnlineLogRecords.stream()
1640      .map(tSlowLogRecord -> new OnlineLogRecord.OnlineLogRecordBuilder()
1641        .setCallDetails(tSlowLogRecord.getCallDetails())
1642        .setClientAddress(tSlowLogRecord.getClientAddress())
1643        .setMethodName(tSlowLogRecord.getMethodName())
1644        .setMultiGetsCount(tSlowLogRecord.getMultiGetsCount())
1645        .setMultiMutationsCount(tSlowLogRecord.getMultiMutationsCount())
1646        .setMultiServiceCalls(tSlowLogRecord.getMultiServiceCalls())
1647        .setParam(tSlowLogRecord.getParam())
1648        .setProcessingTime(tSlowLogRecord.getProcessingTime())
1649        .setQueueTime(tSlowLogRecord.getQueueTime())
1650        .setRegionName(tSlowLogRecord.getRegionName())
1651        .setResponseSize(tSlowLogRecord.getResponseSize())
1652        .setServerClass(tSlowLogRecord.getServerClass())
1653        .setStartTime(tSlowLogRecord.getStartTime())
1654        .setUserName(tSlowLogRecord.getUserName())
1655        .build())
1656      .collect(Collectors.toList());
1657  }
1658
1659}