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.rest;
019
020import java.io.IOException;
021import org.apache.hadoop.hbase.Cell;
022import org.apache.hadoop.hbase.client.Result;
023import org.apache.hadoop.hbase.rest.model.CellModel;
024import org.apache.hadoop.hbase.rest.model.RowModel;
025import org.apache.yetus.audience.InterfaceAudience;
026
027import org.apache.hbase.thirdparty.com.google.protobuf.CodedInputStream;
028import org.apache.hbase.thirdparty.com.google.protobuf.Message;
029
030@InterfaceAudience.Private
031public final class RestUtil {
032
033  private RestUtil() {
034    // Do not instantiate
035  }
036
037  /**
038   * Speed-optimized method to convert an HBase result to a RowModel. Avoids iterators and uses the
039   * non-cloning constructors to minimize overhead, especially when using protobuf marshalling.
040   * @param r non-empty Result object
041   */
042  public static RowModel createRowModelFromResult(Result r) {
043    Cell firstCell = r.rawCells()[0];
044    RowModel rowModel =
045      new RowModel(firstCell.getRowArray(), firstCell.getRowOffset(), firstCell.getRowLength());
046    int cellsLength = r.rawCells().length;
047    for (int i = 0; i < cellsLength; i++) {
048      rowModel.addCell(new CellModel(r.rawCells()[i]));
049    }
050    return rowModel;
051  }
052
053  /**
054   * Merges the object from codedInput, then calls checkLastTagWas. This is based on
055   * ProtobufUtil.mergeFrom, but we have already taken care of setSizeLimit() before calling, so
056   * only the checkLastTagWas() call is retained.
057   * @param builder    protobuf object builder
058   * @param codedInput encoded object data
059   */
060  public static void mergeFrom(Message.Builder builder, CodedInputStream codedInput)
061    throws IOException {
062    builder.mergeFrom(codedInput);
063    codedInput.checkLastTagWas(0);
064  }
065}