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.model;
019
020import java.io.IOException;
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.List;
024import javax.xml.bind.annotation.XmlAccessType;
025import javax.xml.bind.annotation.XmlAccessorType;
026import javax.xml.bind.annotation.XmlElement;
027import javax.xml.bind.annotation.XmlRootElement;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
030import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
031import org.apache.hadoop.hbase.rest.protobuf.generated.CellMessage.Cell;
032import org.apache.hadoop.hbase.rest.protobuf.generated.CellSetMessage.CellSet;
033import org.apache.hadoop.hbase.util.ByteStringer;
034import org.apache.yetus.audience.InterfaceAudience;
035
036/**
037 * Representation of a grouping of cells. May contain cells from more than one row. Encapsulates
038 * RowModel and CellModel models.
039 *
040 * <pre>
041 * &lt;complexType name="CellSet"&gt;
042 *   &lt;sequence&gt;
043 *     &lt;element name="row" type="tns:Row" maxOccurs="unbounded"
044 *       minOccurs="1"&gt;&lt;/element&gt;
045 *   &lt;/sequence&gt;
046 * &lt;/complexType&gt;
047 *
048 * &lt;complexType name="Row"&gt;
049 *   &lt;sequence&gt;
050 *     &lt;element name="key" type="base64Binary"&gt;&lt;/element&gt;
051 *     &lt;element name="cell" type="tns:Cell"
052 *       maxOccurs="unbounded" minOccurs="1"&gt;&lt;/element&gt;
053 *   &lt;/sequence&gt;
054 * &lt;/complexType&gt;
055 *
056 * &lt;complexType name="Cell"&gt;
057 *   &lt;sequence&gt;
058 *     &lt;element name="value" maxOccurs="1" minOccurs="1"&gt;
059 *       &lt;simpleType&gt;
060 *         &lt;restriction base="base64Binary"/&gt;
061 *       &lt;/simpleType&gt;
062 *     &lt;/element&gt;
063 *   &lt;/sequence&gt;
064 *   &lt;attribute name="column" type="base64Binary" /&gt;
065 *   &lt;attribute name="timestamp" type="int" /&gt;
066 * &lt;/complexType&gt;
067 * </pre>
068 */
069@XmlRootElement(name = "CellSet")
070@XmlAccessorType(XmlAccessType.FIELD)
071@InterfaceAudience.Private
072public class CellSetModel implements Serializable, ProtobufMessageHandler {
073  private static final long serialVersionUID = 1L;
074
075  @XmlElement(name = "Row")
076  private List<RowModel> rows;
077
078  /**
079   * Constructor
080   */
081  public CellSetModel() {
082    this.rows = new ArrayList<>();
083  }
084
085  /**
086   * @param rows the rows
087   */
088  public CellSetModel(List<RowModel> rows) {
089    super();
090    this.rows = rows;
091  }
092
093  /**
094   * Add a row to this cell set
095   * @param row the row
096   */
097  public void addRow(RowModel row) {
098    rows.add(row);
099  }
100
101  /** Returns the rows */
102  public List<RowModel> getRows() {
103    return rows;
104  }
105
106  @Override
107  public byte[] createProtobufOutput() {
108    CellSet.Builder builder = CellSet.newBuilder();
109    for (RowModel row : getRows()) {
110      CellSet.Row.Builder rowBuilder = CellSet.Row.newBuilder();
111      rowBuilder.setKey(ByteStringer.wrap(row.getKey()));
112      for (CellModel cell : row.getCells()) {
113        Cell.Builder cellBuilder = Cell.newBuilder();
114        cellBuilder.setColumn(ByteStringer.wrap(cell.getColumn()));
115        cellBuilder.setData(ByteStringer.wrap(cell.getValue()));
116        if (cell.hasUserTimestamp()) {
117          cellBuilder.setTimestamp(cell.getTimestamp());
118        }
119        rowBuilder.addValues(cellBuilder);
120      }
121      builder.addRows(rowBuilder);
122    }
123    return builder.build().toByteArray();
124  }
125
126  @Override
127  public ProtobufMessageHandler getObjectFromMessage(byte[] message) throws IOException {
128    CellSet.Builder builder = CellSet.newBuilder();
129    ProtobufUtil.mergeFrom(builder, message);
130    for (CellSet.Row row : builder.getRowsList()) {
131      RowModel rowModel = new RowModel(row.getKey().toByteArray());
132      for (Cell cell : row.getValuesList()) {
133        long timestamp = HConstants.LATEST_TIMESTAMP;
134        if (cell.hasTimestamp()) {
135          timestamp = cell.getTimestamp();
136        }
137        rowModel.addCell(
138          new CellModel(cell.getColumn().toByteArray(), timestamp, cell.getData().toByteArray()));
139      }
140      addRow(rowModel);
141    }
142    return this;
143  }
144}