~ubuntu-branches/ubuntu/precise/hbase/precise

« back to all changes in this revision

Viewing changes to src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/model/CellModel.java

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Koch
  • Date: 2010-05-06 14:20:42 UTC
  • Revision ID: james.westby@ubuntu.com-20100506142042-r3hlvgxdcpb8tynl
Tags: upstream-0.20.4+dfsg1
ImportĀ upstreamĀ versionĀ 0.20.4+dfsg1

Show diffs side-by-side

added added

removed removed

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