View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.rest.model;
20  
21  import java.io.IOException;
22  import java.io.Serializable;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import javax.xml.bind.annotation.XmlAccessType;
27  import javax.xml.bind.annotation.XmlAccessorType;
28  import javax.xml.bind.annotation.XmlElement;
29  import javax.xml.bind.annotation.XmlRootElement;
30  
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.classification.InterfaceAudience;
33  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
34  import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
35  import org.apache.hadoop.hbase.rest.protobuf.generated.CellMessage.Cell;
36  import org.apache.hadoop.hbase.rest.protobuf.generated.CellSetMessage.CellSet;
37  import org.apache.hadoop.hbase.util.ByteStringer;
38  
39  /**
40   * Representation of a grouping of cells. May contain cells from more than
41   * one row. Encapsulates RowModel and CellModel models.
42   *
43   * <pre>
44   * &lt;complexType name="CellSet"&gt;
45   *   &lt;sequence&gt;
46   *     &lt;element name="row" type="tns:Row" maxOccurs="unbounded"
47   *       minOccurs="1"&gt;&lt;/element&gt;
48   *   &lt;/sequence&gt;
49   * &lt;/complexType&gt;
50   *
51   * &lt;complexType name="Row"&gt;
52   *   &lt;sequence&gt;
53   *     &lt;element name="key" type="base64Binary"&gt;&lt;/element&gt;
54   *     &lt;element name="cell" type="tns:Cell"
55   *       maxOccurs="unbounded" minOccurs="1"&gt;&lt;/element&gt;
56   *   &lt;/sequence&gt;
57   * &lt;/complexType&gt;
58   *
59   * &lt;complexType name="Cell"&gt;
60   *   &lt;sequence&gt;
61   *     &lt;element name="value" maxOccurs="1" minOccurs="1"&gt;
62   *       &lt;simpleType&gt;
63   *         &lt;restriction base="base64Binary"/&gt;
64   *       &lt;/simpleType&gt;
65   *     &lt;/element&gt;
66   *   &lt;/sequence&gt;
67   *   &lt;attribute name="column" type="base64Binary" /&gt;
68   *   &lt;attribute name="timestamp" type="int" /&gt;
69   * &lt;/complexType&gt;
70   * </pre>
71   */
72  @XmlRootElement(name="CellSet")
73  @XmlAccessorType(XmlAccessType.FIELD)
74  @InterfaceAudience.Private
75  public class CellSetModel implements Serializable, ProtobufMessageHandler {
76    private static final long serialVersionUID = 1L;
77  
78    @XmlElement(name="Row")
79    private List<RowModel> rows;
80  
81    /**
82     * Constructor
83     */
84    public CellSetModel() {
85      this.rows = new ArrayList<>();
86    }
87  
88    /**
89     * @param rows the rows
90     */
91    public CellSetModel(List<RowModel> rows) {
92      super();
93      this.rows = rows;
94    }
95  
96    /**
97     * Add a row to this cell set
98     * @param row the row
99     */
100   public void addRow(RowModel row) {
101     rows.add(row);
102   }
103 
104   /**
105    * @return the rows
106    */
107   public List<RowModel> getRows() {
108     return rows;
109   }
110 
111   @Override
112   public byte[] createProtobufOutput() {
113     CellSet.Builder builder = CellSet.newBuilder();
114     for (RowModel row : getRows()) {
115       CellSet.Row.Builder rowBuilder = CellSet.Row.newBuilder();
116       rowBuilder.setKey(ByteStringer.wrap(row.getKey()));
117       for (CellModel cell : row.getCells()) {
118         Cell.Builder cellBuilder = Cell.newBuilder();
119         cellBuilder.setColumn(ByteStringer.wrap(cell.getColumn()));
120         cellBuilder.setData(ByteStringer.wrap(cell.getValue()));
121         if (cell.hasUserTimestamp()) {
122           cellBuilder.setTimestamp(cell.getTimestamp());
123         }
124         rowBuilder.addValues(cellBuilder);
125       }
126       builder.addRows(rowBuilder);
127     }
128     return builder.build().toByteArray();
129   }
130 
131   @Override
132   public ProtobufMessageHandler getObjectFromMessage(byte[] message)
133       throws IOException {
134     CellSet.Builder builder = CellSet.newBuilder();
135     ProtobufUtil.mergeFrom(builder, message);
136     for (CellSet.Row row : builder.getRowsList()) {
137       RowModel rowModel = new RowModel(row.getKey().toByteArray());
138       for (Cell cell : row.getValuesList()) {
139         long timestamp = HConstants.LATEST_TIMESTAMP;
140         if (cell.hasTimestamp()) {
141           timestamp = cell.getTimestamp();
142         }
143         rowModel.addCell(
144             new CellModel(cell.getColumn().toByteArray(), timestamp,
145                   cell.getData().toByteArray()));
146       }
147       addRow(rowModel);
148     }
149     return this;
150   }
151 }