View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.rest.model;
22  
23  import java.io.IOException;
24  import java.io.Serializable;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import javax.xml.bind.annotation.XmlRootElement;
29  import javax.xml.bind.annotation.XmlElement;
30  
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  public class CellSetModel implements Serializable, ProtobufMessageHandler {
73  
74    private static final long serialVersionUID = 1L;
75    
76    private List<RowModel> rows;
77  
78    /**  
79     * Constructor
80     */
81    public CellSetModel() {
82      this.rows = new ArrayList<RowModel>();
83    }
84    
85    /**
86     * @param rows the rows
87     */
88    public CellSetModel(List<RowModel> rows) {
89      super();
90      this.rows = rows;
91    }
92    
93    /**
94     * Add a row to this cell set
95     * @param row the row
96     */
97    public void addRow(RowModel row) {
98      rows.add(row);
99    }
100 
101   /**
102    * @return the rows
103    */
104   @XmlElement(name="Row")
105   public List<RowModel> getRows() {
106     return rows;
107   }
108 
109   @Override
110   public byte[] createProtobufOutput() {
111     CellSet.Builder builder = CellSet.newBuilder();
112     for (RowModel row: getRows()) {
113       CellSet.Row.Builder rowBuilder = CellSet.Row.newBuilder();
114       rowBuilder.setKey(ByteString.copyFrom(row.getKey()));
115       for (CellModel cell: row.getCells()) {
116         Cell.Builder cellBuilder = Cell.newBuilder();
117         cellBuilder.setColumn(ByteString.copyFrom(cell.getColumn()));
118         cellBuilder.setData(ByteString.copyFrom(cell.getValue()));
119         if (cell.hasUserTimestamp()) {
120           cellBuilder.setTimestamp(cell.getTimestamp());
121         }
122         rowBuilder.addValues(cellBuilder);
123       }
124       builder.addRows(rowBuilder);
125     }
126     return builder.build().toByteArray();
127   }
128 
129   @Override
130   public ProtobufMessageHandler getObjectFromMessage(byte[] message)
131       throws IOException {
132     CellSet.Builder builder = CellSet.newBuilder();
133     builder.mergeFrom(message);
134     for (CellSet.Row row: builder.getRowsList()) {
135       RowModel rowModel = new RowModel(row.getKey().toByteArray());
136       for (Cell cell: row.getValuesList()) {
137         long timestamp = HConstants.LATEST_TIMESTAMP;
138         if (cell.hasTimestamp()) {
139           timestamp = cell.getTimestamp();
140         }
141         rowModel.addCell(
142             new CellModel(cell.getColumn().toByteArray(), timestamp,
143                   cell.getData().toByteArray()));
144       }
145       addRow(rowModel);
146     }
147     return this;
148   }
149 }