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.XmlAttribute;
30  import javax.xml.bind.annotation.XmlElement;
31  import javax.xml.bind.annotation.XmlRootElement;
32  
33  import org.apache.hadoop.hbase.classification.InterfaceAudience;
34  import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
35  import org.codehaus.jackson.annotate.JsonProperty;
36  
37  /**
38   * Representation of a row. A row is a related set of cells, grouped by common
39   * row key. RowModels do not appear in results by themselves. They are always
40   * encapsulated within CellSetModels.
41   * 
42   * <pre>
43   * &lt;complexType name="Row"&gt;
44   *   &lt;sequence&gt;
45   *     &lt;element name="key" type="base64Binary"&gt;&lt;/element&gt;
46   *     &lt;element name="cell" type="tns:Cell" 
47   *       maxOccurs="unbounded" minOccurs="1"&gt;&lt;/element&gt;
48   *   &lt;/sequence&gt;
49   * &lt;/complexType&gt;
50   * </pre>
51   */
52  @XmlRootElement(name="Row")
53  @XmlAccessorType(XmlAccessType.FIELD)
54  @InterfaceAudience.Private
55  public class RowModel implements ProtobufMessageHandler, Serializable {
56    private static final long serialVersionUID = 1L;
57  
58    @JsonProperty("key")
59    @XmlAttribute
60    private byte[] key;
61  
62    @JsonProperty("Cell")
63    @XmlElement(name="Cell")
64    private List<CellModel> cells = new ArrayList<CellModel>();
65  
66  
67    /**
68     * Default constructor
69     */
70    public RowModel() { }
71  
72    /**
73     * Constructor
74     * @param key the row key
75     */
76    public RowModel(final String key) {
77      this(key.getBytes());
78    }
79    
80    /**
81     * Constructor
82     * @param key the row key
83     */
84    public RowModel(final byte[] key) {
85      this.key = key;
86      cells = new ArrayList<CellModel>();
87    }
88  
89    /**
90     * Constructor
91     * @param key the row key
92     * @param cells the cells
93     */
94    public RowModel(final String key, final List<CellModel> cells) {
95      this(key.getBytes(), cells);
96    }
97    
98    /**
99     * Constructor
100    * @param key the row key
101    * @param cells the cells
102    */
103   public RowModel(final byte[] key, final List<CellModel> cells) {
104     this.key = key;
105     this.cells = cells;
106   }
107   
108   /**
109    * Adds a cell to the list of cells for this row
110    * @param cell the cell
111    */
112   public void addCell(CellModel cell) {
113     cells.add(cell);
114   }
115 
116   /**
117    * @return the row key
118    */
119   public byte[] getKey() {
120     return key;
121   }
122 
123   /**
124    * @param key the row key
125    */
126   public void setKey(byte[] key) {
127     this.key = key;
128   }
129 
130   /**
131    * @return the cells
132    */
133   public List<CellModel> getCells() {
134     return cells;
135   }
136 
137   @Override
138   public byte[] createProtobufOutput() {
139     // there is no standalone row protobuf message
140     throw new UnsupportedOperationException(
141         "no protobuf equivalent to RowModel");
142   }
143 
144   @Override
145   public ProtobufMessageHandler getObjectFromMessage(byte[] message)
146       throws IOException {
147     // there is no standalone row protobuf message
148     throw new UnsupportedOperationException(
149         "no protobuf equivalent to RowModel");
150   }
151 }