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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org
.apache
.hadoop
.hbase
;
21 import java
.io
.IOException
;
22 import java
.nio
.ByteBuffer
;
23 import java
.util
.Collections
;
24 import java
.util
.Iterator
;
25 import java
.util
.Optional
;
26 import org
.apache
.hadoop
.hbase
.util
.ByteBufferUtils
;
27 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
28 import org
.apache
.hadoop
.hbase
.util
.ClassSize
;
29 import org
.apache
.yetus
.audience
.InterfaceAudience
;
32 * This is a key only Cell implementation which is identical to {@link KeyValue.KeyOnlyKeyValue}
33 * with respect to key serialization but have its data in the form of Byte buffer
34 * (onheap and offheap).
36 @InterfaceAudience.Private
37 public class ByteBufferKeyOnlyKeyValue
extends ByteBufferExtendedCell
{
38 public static final int FIXED_OVERHEAD
= ClassSize
.OBJECT
+ ClassSize
.REFERENCE
39 + (2 * Bytes
.SIZEOF_INT
) + Bytes
.SIZEOF_SHORT
;
40 private ByteBuffer buf
;
41 private int offset
= 0; // offset into buffer where key starts at
42 private int length
= 0; // length of this.
46 * Used in cases where we want to avoid lot of garbage by allocating new objects with different
47 * keys. Use the emtpy construtor and set the keys using {@link #setKey(ByteBuffer, int, int)}
49 public ByteBufferKeyOnlyKeyValue() {
52 public ByteBufferKeyOnlyKeyValue(ByteBuffer buf
, int offset
, int length
) {
53 setKey(buf
, offset
, length
);
57 * A setter that helps to avoid object creation every time and whenever
58 * there is a need to create new OffheapKeyOnlyKeyValue.
63 public void setKey(ByteBuffer key
, int offset
, int length
) {
67 this.rowLen
= ByteBufferUtils
.toShort(this.buf
, this.offset
);
71 public byte[] getRowArray() {
72 if (this.buf
.hasArray()) {
73 return this.buf
.array();
75 return CellUtil
.cloneRow(this);
79 public int getRowOffset() {
80 if (this.buf
.hasArray()) {
81 return getRowPosition() + this.buf
.arrayOffset();
87 public short getRowLength() {
92 public byte[] getFamilyArray() {
93 if (this.buf
.hasArray()) {
94 return this.buf
.array();
96 return CellUtil
.cloneFamily(this);
100 public int getFamilyOffset() {
101 if (this.buf
.hasArray()) {
102 return getFamilyPosition() + this.buf
.arrayOffset();
108 public byte getFamilyLength() {
109 return getFamilyLength(getFamilyLengthPosition());
112 private byte getFamilyLength(int famLenPos
) {
113 return ByteBufferUtils
.toByte(this.buf
, famLenPos
);
117 public byte[] getQualifierArray() {
118 if (this.buf
.hasArray()) {
119 return this.buf
.array();
121 return CellUtil
.cloneQualifier(this);
125 public int getQualifierOffset() {
126 if (this.buf
.hasArray()) {
127 return getQualifierPosition() + this.buf
.arrayOffset();
133 public int getQualifierLength() {
134 return getQualifierLength(getRowLength(), getFamilyLength());
137 private int getQualifierLength(int rlength
, int flength
) {
138 return this.length
- (int) KeyValue
.getKeyDataStructureSize(rlength
, flength
, 0);
142 public long getTimestamp() {
143 return ByteBufferUtils
.toLong(this.buf
, getTimestampOffset());
146 private int getTimestampOffset() {
147 return this.offset
+ this.length
- KeyValue
.TIMESTAMP_TYPE_SIZE
;
151 public byte getTypeByte() {
152 return ByteBufferUtils
.toByte(this.buf
, this.offset
+ this.length
- 1);
156 public void setSequenceId(long seqId
) throws IOException
{
157 throw new IllegalArgumentException("This is a key only Cell");
161 public void setTimestamp(long ts
) throws IOException
{
162 throw new IllegalArgumentException("This is a key only Cell");
166 public void setTimestamp(byte[] ts
) throws IOException
{
167 throw new IllegalArgumentException("This is a key only Cell");
171 public long getSequenceId() {
176 public byte[] getValueArray() {
177 throw new IllegalArgumentException("This is a key only Cell");
181 public int getValueOffset() {
186 public int getValueLength() {
191 public byte[] getTagsArray() {
192 throw new IllegalArgumentException("This is a key only Cell");
196 public int getTagsOffset() {
201 public int getTagsLength() {
206 public ByteBuffer
getRowByteBuffer() {
211 public int getRowPosition() {
212 return this.offset
+ Bytes
.SIZEOF_SHORT
;
216 public ByteBuffer
getFamilyByteBuffer() {
221 public int getFamilyPosition() {
222 return getFamilyLengthPosition() + Bytes
.SIZEOF_BYTE
;
225 // The position in BB where the family length is added.
226 private int getFamilyLengthPosition() {
227 return this.offset
+ Bytes
.SIZEOF_SHORT
+ getRowLength();
231 public ByteBuffer
getQualifierByteBuffer() {
236 public int getQualifierPosition() {
237 int famLenPos
= getFamilyLengthPosition();
238 return famLenPos
+ Bytes
.SIZEOF_BYTE
+ getFamilyLength(famLenPos
);
242 public ByteBuffer
getValueByteBuffer() {
243 throw new IllegalArgumentException("This is a key only Cell");
247 public int getValuePosition() {
252 public ByteBuffer
getTagsByteBuffer() {
253 throw new IllegalArgumentException("This is a key only Cell");
257 public int getTagsPosition() {
262 public String
toString() {
263 return CellUtil
.toString(this, false);
267 public Iterator
<Tag
> getTags() {
268 return Collections
.emptyIterator();
272 public Optional
<Tag
> getTag(byte type
) {
273 return Optional
.empty();
277 public long heapSize() {
278 if (this.buf
.hasArray()) {
279 return ClassSize
.align(FIXED_OVERHEAD
+ length
);
281 return ClassSize
.align(FIXED_OVERHEAD
);