HBASE-23892 SecureTestCluster should allow its subclasses to pass their Class referen...
[hbase.git] / hbase-common / src / main / java / org / apache / hadoop / hbase / ByteBufferKeyOnlyKeyValue.java
blob31f71f98c500dfd4f382fc5ec0ecc56a2c891727
1 /**
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;
31 /**
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.
43 private short rowLen;
45 /**
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);
56 /**
57 * A setter that helps to avoid object creation every time and whenever
58 * there is a need to create new OffheapKeyOnlyKeyValue.
59 * @param key
60 * @param offset
61 * @param length
63 public void setKey(ByteBuffer key, int offset, int length) {
64 this.buf = key;
65 this.offset = offset;
66 this.length = length;
67 this.rowLen = ByteBufferUtils.toShort(this.buf, this.offset);
70 @Override
71 public byte[] getRowArray() {
72 if (this.buf.hasArray()) {
73 return this.buf.array();
75 return CellUtil.cloneRow(this);
78 @Override
79 public int getRowOffset() {
80 if (this.buf.hasArray()) {
81 return getRowPosition() + this.buf.arrayOffset();
83 return 0;
86 @Override
87 public short getRowLength() {
88 return this.rowLen;
91 @Override
92 public byte[] getFamilyArray() {
93 if (this.buf.hasArray()) {
94 return this.buf.array();
96 return CellUtil.cloneFamily(this);
99 @Override
100 public int getFamilyOffset() {
101 if (this.buf.hasArray()) {
102 return getFamilyPosition() + this.buf.arrayOffset();
104 return 0;
107 @Override
108 public byte getFamilyLength() {
109 return getFamilyLength(getFamilyLengthPosition());
112 private byte getFamilyLength(int famLenPos) {
113 return ByteBufferUtils.toByte(this.buf, famLenPos);
116 @Override
117 public byte[] getQualifierArray() {
118 if (this.buf.hasArray()) {
119 return this.buf.array();
121 return CellUtil.cloneQualifier(this);
124 @Override
125 public int getQualifierOffset() {
126 if (this.buf.hasArray()) {
127 return getQualifierPosition() + this.buf.arrayOffset();
129 return 0;
132 @Override
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);
141 @Override
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;
150 @Override
151 public byte getTypeByte() {
152 return ByteBufferUtils.toByte(this.buf, this.offset + this.length - 1);
155 @Override
156 public void setSequenceId(long seqId) throws IOException {
157 throw new IllegalArgumentException("This is a key only Cell");
160 @Override
161 public void setTimestamp(long ts) throws IOException {
162 throw new IllegalArgumentException("This is a key only Cell");
165 @Override
166 public void setTimestamp(byte[] ts) throws IOException {
167 throw new IllegalArgumentException("This is a key only Cell");
170 @Override
171 public long getSequenceId() {
172 return 0;
175 @Override
176 public byte[] getValueArray() {
177 throw new IllegalArgumentException("This is a key only Cell");
180 @Override
181 public int getValueOffset() {
182 return 0;
185 @Override
186 public int getValueLength() {
187 return 0;
190 @Override
191 public byte[] getTagsArray() {
192 throw new IllegalArgumentException("This is a key only Cell");
195 @Override
196 public int getTagsOffset() {
197 return 0;
200 @Override
201 public int getTagsLength() {
202 return 0;
205 @Override
206 public ByteBuffer getRowByteBuffer() {
207 return this.buf;
210 @Override
211 public int getRowPosition() {
212 return this.offset + Bytes.SIZEOF_SHORT;
215 @Override
216 public ByteBuffer getFamilyByteBuffer() {
217 return this.buf;
220 @Override
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();
230 @Override
231 public ByteBuffer getQualifierByteBuffer() {
232 return this.buf;
235 @Override
236 public int getQualifierPosition() {
237 int famLenPos = getFamilyLengthPosition();
238 return famLenPos + Bytes.SIZEOF_BYTE + getFamilyLength(famLenPos);
241 @Override
242 public ByteBuffer getValueByteBuffer() {
243 throw new IllegalArgumentException("This is a key only Cell");
246 @Override
247 public int getValuePosition() {
248 return 0;
251 @Override
252 public ByteBuffer getTagsByteBuffer() {
253 throw new IllegalArgumentException("This is a key only Cell");
256 @Override
257 public int getTagsPosition() {
258 return 0;
261 @Override
262 public String toString() {
263 return CellUtil.toString(this, false);
266 @Override
267 public Iterator<Tag> getTags() {
268 return Collections.emptyIterator();
271 @Override
272 public Optional<Tag> getTag(byte type) {
273 return Optional.empty();
276 @Override
277 public long heapSize() {
278 if (this.buf.hasArray()) {
279 return ClassSize.align(FIXED_OVERHEAD + length);
281 return ClassSize.align(FIXED_OVERHEAD);