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 25 import javax.xml.bind.annotation.XmlAccessType; 26 import javax.xml.bind.annotation.XmlAccessorType; 27 import javax.xml.bind.annotation.XmlAttribute; 28 import javax.xml.bind.annotation.XmlRootElement; 29 import javax.xml.bind.annotation.XmlValue; 30 31 import org.apache.hadoop.hbase.util.ByteStringer; 32 import org.apache.hadoop.hbase.classification.InterfaceAudience; 33 import org.apache.hadoop.hbase.CellUtil; 34 import org.apache.hadoop.hbase.HConstants; 35 import org.apache.hadoop.hbase.KeyValue; 36 import org.apache.hadoop.hbase.protobuf.ProtobufUtil; 37 import org.apache.hadoop.hbase.rest.ProtobufMessageHandler; 38 import org.apache.hadoop.hbase.rest.protobuf.generated.CellMessage.Cell; 39 import org.codehaus.jackson.annotate.JsonProperty; 40 41 /** 42 * Representation of a cell. A cell is a single value associated a column and 43 * optional qualifier, and either the timestamp when it was stored or the user- 44 * provided timestamp if one was explicitly supplied. 45 * 46 * <pre> 47 * <complexType name="Cell"> 48 * <sequence> 49 * <element name="value" maxOccurs="1" minOccurs="1"> 50 * <simpleType> 51 * <restriction base="base64Binary"/> 52 * </simpleType> 53 * </element> 54 * </sequence> 55 * <attribute name="column" type="base64Binary" /> 56 * <attribute name="timestamp" type="int" /> 57 * </complexType> 58 * </pre> 59 */ 60 @XmlRootElement(name="Cell") 61 @XmlAccessorType(XmlAccessType.FIELD) 62 @InterfaceAudience.Private 63 public class CellModel implements ProtobufMessageHandler, Serializable { 64 private static final long serialVersionUID = 1L; 65 66 @JsonProperty("column") 67 @XmlAttribute 68 private byte[] column; 69 70 @JsonProperty("timestamp") 71 @XmlAttribute 72 private long timestamp = HConstants.LATEST_TIMESTAMP; 73 74 @JsonProperty("$") 75 @XmlValue 76 private byte[] value; 77 78 /** 79 * Default constructor 80 */ CellModel()81 public CellModel() {} 82 83 /** 84 * Constructor 85 * @param column 86 * @param value 87 */ CellModel(byte[] column, byte[] value)88 public CellModel(byte[] column, byte[] value) { 89 this(column, HConstants.LATEST_TIMESTAMP, value); 90 } 91 92 /** 93 * Constructor 94 * @param column 95 * @param qualifier 96 * @param value 97 */ CellModel(byte[] column, byte[] qualifier, byte[] value)98 public CellModel(byte[] column, byte[] qualifier, byte[] value) { 99 this(column, qualifier, HConstants.LATEST_TIMESTAMP, value); 100 } 101 102 /** 103 * Constructor from KeyValue 104 * @param cell 105 */ CellModel(org.apache.hadoop.hbase.Cell cell)106 public CellModel(org.apache.hadoop.hbase.Cell cell) { 107 this(CellUtil.cloneFamily(cell), CellUtil.cloneQualifier(cell), cell.getTimestamp(), CellUtil 108 .cloneValue(cell)); 109 } 110 111 /** 112 * Constructor 113 * @param column 114 * @param timestamp 115 * @param value 116 */ CellModel(byte[] column, long timestamp, byte[] value)117 public CellModel(byte[] column, long timestamp, byte[] value) { 118 this.column = column; 119 this.timestamp = timestamp; 120 this.value = value; 121 } 122 123 /** 124 * Constructor 125 * @param column 126 * @param qualifier 127 * @param timestamp 128 * @param value 129 */ CellModel(byte[] column, byte[] qualifier, long timestamp, byte[] value)130 public CellModel(byte[] column, byte[] qualifier, long timestamp, 131 byte[] value) { 132 this.column = KeyValue.makeColumn(column, qualifier); 133 this.timestamp = timestamp; 134 this.value = value; 135 } 136 137 /** 138 * @return the column 139 */ getColumn()140 public byte[] getColumn() { 141 return column; 142 } 143 144 /** 145 * @param column the column to set 146 */ setColumn(byte[] column)147 public void setColumn(byte[] column) { 148 this.column = column; 149 } 150 151 /** 152 * @return true if the timestamp property has been specified by the 153 * user 154 */ hasUserTimestamp()155 public boolean hasUserTimestamp() { 156 return timestamp != HConstants.LATEST_TIMESTAMP; 157 } 158 159 /** 160 * @return the timestamp 161 */ getTimestamp()162 public long getTimestamp() { 163 return timestamp; 164 } 165 166 /** 167 * @param timestamp the timestamp to set 168 */ setTimestamp(long timestamp)169 public void setTimestamp(long timestamp) { 170 this.timestamp = timestamp; 171 } 172 173 /** 174 * @return the value 175 */ getValue()176 public byte[] getValue() { 177 return value; 178 } 179 180 /** 181 * @param value the value to set 182 */ setValue(byte[] value)183 public void setValue(byte[] value) { 184 this.value = value; 185 } 186 187 @Override createProtobufOutput()188 public byte[] createProtobufOutput() { 189 Cell.Builder builder = Cell.newBuilder(); 190 builder.setColumn(ByteStringer.wrap(getColumn())); 191 builder.setData(ByteStringer.wrap(getValue())); 192 if (hasUserTimestamp()) { 193 builder.setTimestamp(getTimestamp()); 194 } 195 return builder.build().toByteArray(); 196 } 197 198 @Override getObjectFromMessage(byte[] message)199 public ProtobufMessageHandler getObjectFromMessage(byte[] message) 200 throws IOException { 201 Cell.Builder builder = Cell.newBuilder(); 202 ProtobufUtil.mergeFrom(builder, message); 203 setColumn(builder.getColumn().toByteArray()); 204 setValue(builder.getData().toByteArray()); 205 if (builder.hasTimestamp()) { 206 setTimestamp(builder.getTimestamp()); 207 } 208 return this; 209 } 210 } 211