001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.util;
019
020import java.io.IOException;
021import java.io.OutputStream;
022import java.nio.ByteBuffer;
023import org.apache.hadoop.hbase.ByteBufferExtendedCell;
024import org.apache.hadoop.hbase.Cell;
025import org.apache.hadoop.hbase.CellUtil;
026import org.apache.hadoop.hbase.ExtendedCell;
027import org.apache.hadoop.hbase.PrivateCellUtil;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * A wrapper for a cell to be used with mapreduce, as the output value class for mappers/reducers.
032 */
033@InterfaceAudience.Private
034public class MapReduceExtendedCell extends ByteBufferExtendedCell {
035
036  private final Cell cell;
037
038  public MapReduceExtendedCell(Cell cell) {
039    this.cell = cell;
040  }
041
042  @Override
043  public byte[] getRowArray() {
044    return this.cell.getRowArray();
045  }
046
047  @Override
048  public int getRowOffset() {
049    return this.cell.getRowOffset();
050  }
051
052  @Override
053  public short getRowLength() {
054    return this.cell.getRowLength();
055  }
056
057  @Override
058  public byte[] getFamilyArray() {
059    return this.cell.getFamilyArray();
060  }
061
062  @Override
063  public int getFamilyOffset() {
064    return this.cell.getFamilyOffset();
065  }
066
067  @Override
068  public byte getFamilyLength() {
069    return this.cell.getFamilyLength();
070  }
071
072  @Override
073  public byte[] getQualifierArray() {
074    return this.cell.getQualifierArray();
075  }
076
077  @Override
078  public int getQualifierOffset() {
079    return this.cell.getQualifierOffset();
080  }
081
082  @Override
083  public int getQualifierLength() {
084    return this.cell.getQualifierLength();
085  }
086
087  @Override
088  public long getTimestamp() {
089    return this.cell.getTimestamp();
090  }
091
092  @Override
093  public byte getTypeByte() {
094    return this.cell.getTypeByte();
095  }
096
097  @Override
098  public long getSequenceId() {
099    return this.cell.getSequenceId();
100  }
101
102  @Override
103  public byte[] getValueArray() {
104    return this.cell.getValueArray();
105  }
106
107  @Override
108  public int getValueOffset() {
109    return this.cell.getValueOffset();
110  }
111
112  @Override
113  public int getValueLength() {
114    return this.cell.getValueLength();
115  }
116
117  @Override
118  public byte[] getTagsArray() {
119    return this.cell.getTagsArray();
120  }
121
122  @Override
123  public int getTagsOffset() {
124    return this.cell.getTagsOffset();
125  }
126
127  @Override
128  public int getTagsLength() {
129    return this.cell.getTagsLength();
130  }
131
132  @Override
133  public ByteBuffer getRowByteBuffer() {
134    if (cell instanceof ByteBufferExtendedCell) {
135      return ((ByteBufferExtendedCell) this.cell).getRowByteBuffer();
136    } else {
137      return ByteBuffer.wrap(CellUtil.cloneRow(this.cell));
138    }
139  }
140
141  @Override
142  public int getRowPosition() {
143    if (cell instanceof ByteBufferExtendedCell) {
144      return ((ByteBufferExtendedCell) this.cell).getRowPosition();
145    } else {
146      return 0;
147    }
148  }
149
150  @Override
151  public ByteBuffer getFamilyByteBuffer() {
152    if (cell instanceof ByteBufferExtendedCell) {
153      return ((ByteBufferExtendedCell) this.cell).getFamilyByteBuffer();
154    } else {
155      return ByteBuffer.wrap(CellUtil.cloneFamily(this.cell));
156    }
157  }
158
159  @Override
160  public int getFamilyPosition() {
161    if (cell instanceof ByteBufferExtendedCell) {
162      return ((ByteBufferExtendedCell) this.cell).getFamilyPosition();
163    } else {
164      return 0;
165    }
166  }
167
168  @Override
169  public ByteBuffer getQualifierByteBuffer() {
170    if (cell instanceof ByteBufferExtendedCell) {
171      return ((ByteBufferExtendedCell) this.cell).getQualifierByteBuffer();
172    } else {
173      return ByteBuffer.wrap(CellUtil.cloneQualifier(this.cell));
174    }
175  }
176
177  @Override
178  public int getQualifierPosition() {
179    if (cell instanceof ByteBufferExtendedCell) {
180      return ((ByteBufferExtendedCell) this.cell).getQualifierPosition();
181    } else {
182      return 0;
183    }
184  }
185
186  @Override
187  public ByteBuffer getValueByteBuffer() {
188    if (cell instanceof ByteBufferExtendedCell) {
189      return ((ByteBufferExtendedCell) this.cell).getValueByteBuffer();
190    } else {
191      return ByteBuffer.wrap(CellUtil.cloneValue(this.cell));
192    }
193  }
194
195  @Override
196  public int getValuePosition() {
197    if (cell instanceof ByteBufferExtendedCell) {
198      return ((ByteBufferExtendedCell) this.cell).getValuePosition();
199    } else {
200      return 0;
201    }
202  }
203
204  @Override
205  public ByteBuffer getTagsByteBuffer() {
206    if (cell instanceof ByteBufferExtendedCell) {
207      return ((ByteBufferExtendedCell) this.cell).getTagsByteBuffer();
208    } else {
209      return ByteBuffer.wrap(PrivateCellUtil.cloneTags(this.cell));
210    }
211  }
212
213  @Override
214  public int getTagsPosition() {
215    if (cell instanceof ByteBufferExtendedCell) {
216      return ((ByteBufferExtendedCell) this.cell).getTagsPosition();
217    } else {
218      return 0;
219    }
220  }
221
222  @Override
223  public String toString() {
224    return this.cell.toString();
225  }
226
227  @Override
228  public void setSequenceId(long seqId) throws IOException {
229    PrivateCellUtil.setSequenceId(cell, seqId);
230  }
231
232  @Override
233  public void setTimestamp(long ts) throws IOException {
234    PrivateCellUtil.setTimestamp(cell, ts);
235  }
236
237  @Override
238  public void setTimestamp(byte[] ts) throws IOException {
239    PrivateCellUtil.setTimestamp(cell, ts);
240  }
241
242  @Override
243  public long heapSize() {
244    return cell.heapSize();
245  }
246
247  @Override
248  public int write(OutputStream out, boolean withTags) throws IOException {
249    return PrivateCellUtil.writeCell(cell, out, withTags);
250  }
251
252  @Override
253  public int getSerializedSize(boolean withTags) {
254    return PrivateCellUtil.estimatedSerializedSizeOf(cell) - Bytes.SIZEOF_INT;
255  }
256
257  @Override
258  public void write(ByteBuffer buf, int offset) {
259    PrivateCellUtil.writeCellToBuffer(cell, buf, offset);
260  }
261
262  @Override
263  public ExtendedCell deepClone() {
264    try {
265      return (ExtendedCell) PrivateCellUtil.deepClone(cell);
266    } catch (CloneNotSupportedException e) {
267      throw new RuntimeException(e);
268    }
269  }
270}