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  
20  package org.apache.hadoop.hbase.rest.model;
21  
22  import java.io.IOException;
23  import java.io.Serializable;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import javax.xml.bind.annotation.XmlRootElement;
28  import javax.xml.bind.annotation.XmlElement;
29  
30  import org.apache.hadoop.classification.InterfaceAudience;
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
33  import org.apache.hadoop.hbase.rest.protobuf.generated.CellMessage.Cell;
34  import org.apache.hadoop.hbase.rest.protobuf.generated.CellSetMessage.CellSet;
35  
36  import com.google.protobuf.ByteString;
37  
38  /**
39   * Representation of a grouping of cells. May contain cells from more than
40   * one row. Encapsulates RowModel and CellModel models.
41   * 
42   * <pre>
43   * &lt;complexType name="CellSet"&gt;
44   *   &lt;sequence&gt;
45   *     &lt;element name="row" type="tns:Row" maxOccurs="unbounded" 
46   *       minOccurs="1"&gt;&lt;/element&gt;
47   *   &lt;/sequence&gt;
48   * &lt;/complexType&gt;
49   * 
50   * &lt;complexType name="Row"&gt;
51   *   &lt;sequence&gt;
52   *     &lt;element name="key" type="base64Binary"&gt;&lt;/element&gt;
53   *     &lt;element name="cell" type="tns:Cell" 
54   *       maxOccurs="unbounded" minOccurs="1"&gt;&lt;/element&gt;
55   *   &lt;/sequence&gt;
56   * &lt;/complexType&gt;
57   *
58   * &lt;complexType name="Cell"&gt;
59   *   &lt;sequence&gt;
60   *     &lt;element name="value" maxOccurs="1" minOccurs="1"&gt;
61   *       &lt;simpleType&gt;
62   *         &lt;restriction base="base64Binary"/&gt;
63   *       &lt;/simpleType&gt;
64   *     &lt;/element&gt;
65   *   &lt;/sequence&gt;
66   *   &lt;attribute name="column" type="base64Binary" /&gt;
67   *   &lt;attribute name="timestamp" type="int" /&gt;
68   * &lt;/complexType&gt;
69   * </pre>
70   */
71  @XmlRootElement(name="CellSet")
72  @InterfaceAudience.Private
73  public class CellSetModel implements Serializable, ProtobufMessageHandler {
74  
75    private static final long serialVersionUID = 1L;
76    
77    private List<RowModel> rows;
78  
79    /**  
80     * Constructor
81     */
82    public CellSetModel() {
83      this.rows = new ArrayList<RowModel>();
84    }
85    
86    /**
87     * @param rows the rows
88     */
89    public CellSetModel(List<RowModel> rows) {
90      super();
91      this.rows = rows;
92    }
93    
94    /**
95     * Add a row to this cell set
96     * @param row the row
97     */
98    public void addRow(RowModel row) {
99      rows.add(row);
100   }
101 
102   /**
103    * @return the rows
104    */
105   @XmlElement(name="Row")
106   public List<RowModel> getRows() {
107     return rows;
108   }
109 
110   @Override
111   public byte[] createProtobufOutput() {
112     CellSet.Builder builder = CellSet.newBuilder();
113     for (RowModel row: getRows()) {
114       CellSet.Row.Builder rowBuilder = CellSet.Row.newBuilder();
115       rowBuilder.setKey(ByteString.copyFrom(row.getKey()));
116       for (CellModel cell: row.getCells()) {
117         Cell.Builder cellBuilder = Cell.newBuilder();
118         cellBuilder.setColumn(ByteString.copyFrom(cell.getColumn()));
119         cellBuilder.setData(ByteString.copyFrom(cell.getValue()));
120         if (cell.hasUserTimestamp()) {
121           cellBuilder.setTimestamp(cell.getTimestamp());
122         }
123         rowBuilder.addValues(cellBuilder);
124       }
125       builder.addRows(rowBuilder);
126     }
127     return builder.build().toByteArray();
128   }
129 
130   @Override
131   public ProtobufMessageHandler getObjectFromMessage(byte[] message)
132       throws IOException {
133     CellSet.Builder builder = CellSet.newBuilder();
134     builder.mergeFrom(message);
135     for (CellSet.Row row: builder.getRowsList()) {
136       RowModel rowModel = new RowModel(row.getKey().toByteArray());
137       for (Cell cell: row.getValuesList()) {
138         long timestamp = HConstants.LATEST_TIMESTAMP;
139         if (cell.hasTimestamp()) {
140           timestamp = cell.getTimestamp();
141         }
142         rowModel.addCell(
143             new CellModel(cell.getColumn().toByteArray(), timestamp,
144                   cell.getData().toByteArray()));
145       }
146       addRow(rowModel);
147     }
148     return this;
149   }
150 }