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