HBASE-19497 Fix findbugs and error-prone warnings in hbase-common (branch-2)
[hbase.git] / hbase-common / src / test / java / org / apache / hadoop / hbase / TestByteBufferKeyValue.java
blob26459f927ee05de1448a8c0ef707143c38d2c80c
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with this
4 * work for additional information regarding copyright ownership. The ASF
5 * licenses this file to you under the Apache License, Version 2.0 (the
6 * "License"); you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
15 * under the License.
17 package org.apache.hadoop.hbase;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
22 import java.nio.ByteBuffer;
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.apache.hadoop.hbase.KeyValue.Type;
27 import org.apache.hadoop.hbase.testclassification.MiscTests;
28 import org.apache.hadoop.hbase.testclassification.SmallTests;
29 import org.apache.hadoop.hbase.util.ByteBufferUtils;
30 import org.apache.hadoop.hbase.util.Bytes;
31 import org.junit.Test;
32 import org.junit.experimental.categories.Category;
34 @Category({ MiscTests.class, SmallTests.class })
35 public class TestByteBufferKeyValue {
36 private static final String QUAL2 = "qual2";
37 private static final String FAM2 = "fam2";
38 private static final String QUAL1 = "qual1";
39 private static final String FAM1 = "fam1";
40 private static final String ROW1 = "row1";
41 private static final byte[] row1 = Bytes.toBytes(ROW1);
42 private static final byte[] fam1 = Bytes.toBytes(FAM1);
43 private static final byte[] fam2 = Bytes.toBytes(FAM2);
44 private static final byte[] qual1 = Bytes.toBytes(QUAL1);
45 private static final byte[] qual2 = Bytes.toBytes(QUAL2);
46 private static final Tag t1 = new ArrayBackedTag((byte) 1, Bytes.toBytes("TAG1"));
47 private static final Tag t2 = new ArrayBackedTag((byte) 2, Bytes.toBytes("TAG2"));
48 private static final ArrayList<Tag> tags = new ArrayList<Tag>();
49 static {
50 tags.add(t1);
51 tags.add(t2);
54 @Test
55 public void testByteBufferBackedKeyValue() throws Exception {
56 KeyValue kvCell = new KeyValue(row1, fam1, qual1, 0L, Type.Put, row1);
57 ByteBuffer buf = ByteBuffer.allocateDirect(kvCell.getBuffer().length);
58 ByteBufferUtils.copyFromArrayToBuffer(buf, kvCell.getBuffer(), 0, kvCell.getBuffer().length);
59 ByteBufferCell offheapKV = new ByteBufferKeyValue(buf, 0, buf.capacity(), 0L);
60 assertEquals(
61 ROW1,
62 ByteBufferUtils.toStringBinary(offheapKV.getRowByteBuffer(),
63 offheapKV.getRowPosition(), offheapKV.getRowLength()));
64 assertEquals(
65 FAM1,
66 ByteBufferUtils.toStringBinary(offheapKV.getFamilyByteBuffer(),
67 offheapKV.getFamilyPosition(), offheapKV.getFamilyLength()));
68 assertEquals(
69 QUAL1,
70 ByteBufferUtils.toStringBinary(offheapKV.getQualifierByteBuffer(),
71 offheapKV.getQualifierPosition(), offheapKV.getQualifierLength()));
72 assertEquals(
73 ROW1,
74 ByteBufferUtils.toStringBinary(offheapKV.getValueByteBuffer(),
75 offheapKV.getValuePosition(), offheapKV.getValueLength()));
76 assertEquals(0L, offheapKV.getTimestamp());
77 assertEquals(Type.Put.getCode(), offheapKV.getTypeByte());
79 // Use the array() APIs
80 assertEquals(
81 ROW1,
82 Bytes.toStringBinary(offheapKV.getRowArray(),
83 offheapKV.getRowOffset(), offheapKV.getRowLength()));
84 assertEquals(
85 FAM1,
86 Bytes.toStringBinary(offheapKV.getFamilyArray(),
87 offheapKV.getFamilyOffset(), offheapKV.getFamilyLength()));
88 assertEquals(
89 QUAL1,
90 Bytes.toStringBinary(offheapKV.getQualifierArray(),
91 offheapKV.getQualifierOffset(), offheapKV.getQualifierLength()));
92 assertEquals(
93 ROW1,
94 Bytes.toStringBinary(offheapKV.getValueArray(),
95 offheapKV.getValueOffset(), offheapKV.getValueLength()));
96 assertEquals(0L, offheapKV.getTimestamp());
97 assertEquals(Type.Put.getCode(), offheapKV.getTypeByte());
99 kvCell = new KeyValue(row1, fam2, qual2, 0L, Type.Put, row1);
100 buf = ByteBuffer.allocateDirect(kvCell.getBuffer().length);
101 ByteBufferUtils.copyFromArrayToBuffer(buf, kvCell.getBuffer(), 0, kvCell.getBuffer().length);
102 offheapKV = new ByteBufferKeyValue(buf, 0, buf.capacity(), 0L);
103 assertEquals(
104 FAM2,
105 ByteBufferUtils.toStringBinary(offheapKV.getFamilyByteBuffer(),
106 offheapKV.getFamilyPosition(), offheapKV.getFamilyLength()));
107 assertEquals(
108 QUAL2,
109 ByteBufferUtils.toStringBinary(offheapKV.getQualifierByteBuffer(),
110 offheapKV.getQualifierPosition(), offheapKV.getQualifierLength()));
111 byte[] nullQualifier = new byte[0];
112 kvCell = new KeyValue(row1, fam1, nullQualifier, 0L, Type.Put, row1);
113 buf = ByteBuffer.allocateDirect(kvCell.getBuffer().length);
114 ByteBufferUtils.copyFromArrayToBuffer(buf, kvCell.getBuffer(), 0, kvCell.getBuffer().length);
115 offheapKV = new ByteBufferKeyValue(buf, 0, buf.capacity(), 0L);
116 assertEquals(
117 ROW1,
118 ByteBufferUtils.toStringBinary(offheapKV.getRowByteBuffer(),
119 offheapKV.getRowPosition(), offheapKV.getRowLength()));
120 assertEquals(
121 FAM1,
122 ByteBufferUtils.toStringBinary(offheapKV.getFamilyByteBuffer(),
123 offheapKV.getFamilyPosition(), offheapKV.getFamilyLength()));
124 assertEquals(
126 ByteBufferUtils.toStringBinary(offheapKV.getQualifierByteBuffer(),
127 offheapKV.getQualifierPosition(), offheapKV.getQualifierLength()));
128 assertEquals(
129 ROW1,
130 ByteBufferUtils.toStringBinary(offheapKV.getValueByteBuffer(),
131 offheapKV.getValuePosition(), offheapKV.getValueLength()));
132 assertEquals(0L, offheapKV.getTimestamp());
133 assertEquals(Type.Put.getCode(), offheapKV.getTypeByte());
136 @Test
137 public void testByteBufferBackedKeyValueWithTags() throws Exception {
138 KeyValue kvCell = new KeyValue(row1, fam1, qual1, 0L, Type.Put, row1, tags);
139 ByteBuffer buf = ByteBuffer.allocateDirect(kvCell.getBuffer().length);
140 ByteBufferUtils.copyFromArrayToBuffer(buf, kvCell.getBuffer(), 0, kvCell.getBuffer().length);
141 ByteBufferKeyValue offheapKV = new ByteBufferKeyValue(buf, 0, buf.capacity(), 0L);
142 assertEquals(
143 ROW1,
144 ByteBufferUtils.toStringBinary(offheapKV.getRowByteBuffer(),
145 offheapKV.getRowPosition(), offheapKV.getRowLength()));
146 assertEquals(
147 FAM1,
148 ByteBufferUtils.toStringBinary(offheapKV.getFamilyByteBuffer(),
149 offheapKV.getFamilyPosition(), offheapKV.getFamilyLength()));
150 assertEquals(
151 QUAL1,
152 ByteBufferUtils.toStringBinary(offheapKV.getQualifierByteBuffer(),
153 offheapKV.getQualifierPosition(), offheapKV.getQualifierLength()));
154 assertEquals(
155 ROW1,
156 ByteBufferUtils.toStringBinary(offheapKV.getValueByteBuffer(),
157 offheapKV.getValuePosition(), offheapKV.getValueLength()));
158 assertEquals(0L, offheapKV.getTimestamp());
159 assertEquals(Type.Put.getCode(), offheapKV.getTypeByte());
160 // change tags to handle both onheap and offheap stuff
161 List<Tag> resTags = offheapKV.getTags();
162 Tag tag1 = resTags.get(0);
163 assertEquals(t1.getType(), tag1.getType());
164 assertEquals(Tag.getValueAsString(t1),
165 Tag.getValueAsString(tag1));
166 Tag tag2 = resTags.get(1);
167 assertEquals(tag2.getType(), tag2.getType());
168 assertEquals(Tag.getValueAsString(t2),
169 Tag.getValueAsString(tag2));
170 Tag res = PrivateCellUtil.getTag(offheapKV, (byte) 2).get();
171 assertEquals(Tag.getValueAsString(t2),
172 Tag.getValueAsString(tag2));
173 assertFalse(PrivateCellUtil.getTag(offheapKV, (byte) 3).isPresent());
176 @Test
177 public void testGetKeyMethods() throws Exception {
178 KeyValue kvCell = new KeyValue(row1, fam1, qual1, 0L, Type.Put, row1, tags);
179 ByteBuffer buf = ByteBuffer.allocateDirect(kvCell.getKeyLength());
180 ByteBufferUtils.copyFromArrayToBuffer(buf, kvCell.getBuffer(), kvCell.getKeyOffset(),
181 kvCell.getKeyLength());
182 ByteBufferCell offheapKeyOnlyKV = new ByteBufferKeyOnlyKeyValue(buf, 0, buf.capacity());
183 assertEquals(
184 ROW1,
185 ByteBufferUtils.toStringBinary(offheapKeyOnlyKV.getRowByteBuffer(),
186 offheapKeyOnlyKV.getRowPosition(), offheapKeyOnlyKV.getRowLength()));
187 assertEquals(
188 FAM1,
189 ByteBufferUtils.toStringBinary(offheapKeyOnlyKV.getFamilyByteBuffer(),
190 offheapKeyOnlyKV.getFamilyPosition(), offheapKeyOnlyKV.getFamilyLength()));
191 assertEquals(
192 QUAL1,
193 ByteBufferUtils.toStringBinary(offheapKeyOnlyKV.getQualifierByteBuffer(),
194 offheapKeyOnlyKV.getQualifierPosition(),
195 offheapKeyOnlyKV.getQualifierLength()));
196 assertEquals(0L, offheapKeyOnlyKV.getTimestamp());
197 assertEquals(Type.Put.getCode(), offheapKeyOnlyKV.getTypeByte());