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