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; 019 020import static org.junit.jupiter.api.Assertions.assertArrayEquals; 021import static org.junit.jupiter.api.Assertions.assertEquals; 022import static org.junit.jupiter.api.Assertions.assertThrows; 023import static org.junit.jupiter.api.Assertions.assertTrue; 024 025import java.io.IOException; 026import java.nio.ByteBuffer; 027import org.apache.hadoop.hbase.io.ByteArrayOutputStream; 028import org.apache.hadoop.hbase.testclassification.MiscTests; 029import org.apache.hadoop.hbase.testclassification.SmallTests; 030import org.apache.hadoop.hbase.util.Bytes; 031import org.junit.jupiter.api.BeforeAll; 032import org.junit.jupiter.api.Tag; 033import org.junit.jupiter.api.Test; 034 035@Tag(MiscTests.TAG) 036@Tag(SmallTests.TAG) 037public class TestIndividualBytesFieldCell { 038 039 private static IndividualBytesFieldCell ic0 = null; 040 private static KeyValue kv0 = null; 041 042 @BeforeAll 043 public static void testConstructorAndVerify() { 044 // Immutable inputs 045 byte[] row = Bytes.toBytes("immutable-row"); 046 byte[] family = Bytes.toBytes("immutable-family"); 047 byte[] qualifier = Bytes.toBytes("immutable-qualifier"); 048 byte[] value = Bytes.toBytes("immutable-value"); 049 byte[] tags = Bytes.toBytes("immutable-tags"); 050 051 // Other inputs 052 long timestamp = 5000L; 053 long seqId = 0L; 054 KeyValue.Type type = KeyValue.Type.Put; 055 056 ic0 = new IndividualBytesFieldCell(row, family, qualifier, timestamp, type, seqId, value, tags); 057 kv0 = new KeyValue(row, family, qualifier, timestamp, type, value, tags); 058 059 // Verify if no local copy is made for row, family, qualifier, value or tags. 060 assertTrue(ic0.getRowArray() == row); 061 assertTrue(ic0.getFamilyArray() == family); 062 assertTrue(ic0.getQualifierArray() == qualifier); 063 assertTrue(ic0.getValueArray() == value); 064 assertTrue(ic0.getTagsArray() == tags); 065 066 // Verify others. 067 assertEquals(timestamp, ic0.getTimestamp()); 068 assertEquals(seqId, ic0.getSequenceId()); 069 assertEquals(type.getCode(), ic0.getTypeByte()); 070 071 // Verify offsets of backing byte arrays are always 0. 072 assertEquals(0, ic0.getRowOffset()); 073 assertEquals(0, ic0.getFamilyOffset()); 074 assertEquals(0, ic0.getQualifierOffset()); 075 assertEquals(0, ic0.getValueOffset()); 076 assertEquals(0, ic0.getTagsOffset()); 077 } 078 079 // Verify clone() and deepClone() 080 @Test 081 public void testClone() throws CloneNotSupportedException { 082 // Verify clone. Only shadow copies are made for backing byte arrays. 083 IndividualBytesFieldCell cloned = (IndividualBytesFieldCell) ic0.clone(); 084 assertTrue(cloned.getRowArray() == ic0.getRowArray()); 085 assertTrue(cloned.getFamilyArray() == ic0.getFamilyArray()); 086 assertTrue(cloned.getQualifierArray() == ic0.getQualifierArray()); 087 assertTrue(cloned.getValueArray() == ic0.getValueArray()); 088 assertTrue(cloned.getTagsArray() == ic0.getTagsArray()); 089 090 // Verify if deep clone returns a KeyValue object 091 assertTrue(ic0.deepClone() instanceof KeyValue); 092 } 093 094 /** 095 * Verify KeyValue format related functions: write() and getSerializedSize(). Should have the same 096 * behaviors as {@link KeyValue}. 097 */ 098 @Test 099 public void testWriteIntoKeyValueFormat() throws IOException { 100 // Verify getSerializedSize(). 101 assertEquals(kv0.getSerializedSize(true), ic0.getSerializedSize(true)); // with tags 102 assertEquals(kv0.getSerializedSize(false), ic0.getSerializedSize(false)); // without tags 103 104 // Verify writing into ByteBuffer. 105 ByteBuffer bbufIC = ByteBuffer.allocate(ic0.getSerializedSize(true)); 106 ic0.write(bbufIC, 0); 107 108 ByteBuffer bbufKV = ByteBuffer.allocate(kv0.getSerializedSize(true)); 109 kv0.write(bbufKV, 0); 110 111 assertTrue(bbufIC.equals(bbufKV)); 112 113 // Verify writing into OutputStream. 114 testWriteIntoOutputStream(ic0, kv0, true); // with tags 115 testWriteIntoOutputStream(ic0, kv0, false); // without tags 116 } 117 118 /** 119 * @param ic An instance of IndividualBytesFieldCell to compare. 120 * @param kv An instance of KeyValue to compare. 121 * @param withTags Whether to write tags. 122 */ 123 private void testWriteIntoOutputStream(IndividualBytesFieldCell ic, KeyValue kv, boolean withTags) 124 throws IOException { 125 ByteArrayOutputStream outIC = new ByteArrayOutputStream(ic.getSerializedSize(withTags)); 126 ByteArrayOutputStream outKV = new ByteArrayOutputStream(kv.getSerializedSize(withTags)); 127 128 // compare the number of bytes written 129 assertEquals(kv.write(outKV, withTags), ic.write(outIC, withTags)); 130 // compare the underlying byte array 131 assertArrayEquals(outKV.getBuffer(), outIC.getBuffer()); 132 } 133 134 /** 135 * Verify getXXXArray() and getXXXLength() when family/qualifier/value/tags are null. Should have 136 * the same behaviors as {@link KeyValue}. 137 */ 138 @Test 139 public void testNullFamilyQualifierValueTags() { 140 byte[] row = Bytes.toBytes("row1"); 141 142 long timestamp = 5000L; 143 long seqId = 0L; 144 KeyValue.Type type = KeyValue.Type.Put; 145 146 // Test when following fields are null. 147 byte[] family = null; 148 byte[] qualifier = null; 149 byte[] value = null; 150 byte[] tags = null; 151 152 ExtendedCell ic1 = 153 new IndividualBytesFieldCell(row, family, qualifier, timestamp, type, seqId, value, tags); 154 155 ExtendedCell kv1 = new KeyValue(row, family, qualifier, timestamp, type, value, tags); 156 byte[] familyArrayInKV = 157 Bytes.copy(kv1.getFamilyArray(), kv1.getFamilyOffset(), kv1.getFamilyLength()); 158 byte[] qualifierArrayInKV = 159 Bytes.copy(kv1.getQualifierArray(), kv1.getQualifierOffset(), kv1.getQualifierLength()); 160 byte[] valueArrayInKV = 161 Bytes.copy(kv1.getValueArray(), kv1.getValueOffset(), kv1.getValueLength()); 162 byte[] tagsArrayInKV = Bytes.copy(kv1.getTagsArray(), kv1.getTagsOffset(), kv1.getTagsLength()); 163 164 // getXXXArray() for family, qualifier, value and tags are supposed to return empty byte array, 165 // rather than null. 166 assertArrayEquals(familyArrayInKV, ic1.getFamilyArray()); 167 assertArrayEquals(qualifierArrayInKV, ic1.getQualifierArray()); 168 assertArrayEquals(valueArrayInKV, ic1.getValueArray()); 169 assertArrayEquals(tagsArrayInKV, ic1.getTagsArray()); 170 171 // getXXXLength() for family, qualifier, value and tags are supposed to return 0. 172 assertEquals(kv1.getFamilyLength(), ic1.getFamilyLength()); 173 assertEquals(kv1.getQualifierLength(), ic1.getQualifierLength()); 174 assertEquals(kv1.getValueLength(), ic1.getValueLength()); 175 assertEquals(kv1.getTagsLength(), ic1.getTagsLength()); 176 } 177 178 @Test 179 public void testIfExtendedCellImplemented() { 180 // Verify if ExtendedCell interface is implemented 181 ExtendedCell ec = (ExtendedCell) ic0; 182 ec.deepClone(); // Do something with ec 183 } 184 185 @Test 186 public void testIllegalRow() { 187 assertThrows(IllegalArgumentException.class, () -> { 188 new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 100, HConstants.EMPTY_BYTE_ARRAY, 0, 0, 189 HConstants.EMPTY_BYTE_ARRAY, 0, 0, 0L, KeyValue.Type.Put, 0, HConstants.EMPTY_BYTE_ARRAY, 0, 190 0, HConstants.EMPTY_BYTE_ARRAY, 0, 0); 191 }); 192 } 193 194 @Test 195 public void testIllegalFamily() { 196 assertThrows(IllegalArgumentException.class, () -> { 197 new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3, Bytes.toBytes("family"), 0, 100, 198 HConstants.EMPTY_BYTE_ARRAY, 0, 0, 0L, KeyValue.Type.Put, 0, HConstants.EMPTY_BYTE_ARRAY, 0, 199 0, HConstants.EMPTY_BYTE_ARRAY, 0, 0); 200 }); 201 } 202 203 @Test 204 public void testIllegalQualifier() { 205 assertThrows(IllegalArgumentException.class, () -> { 206 new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3, Bytes.toBytes("family"), 0, 6, 207 Bytes.toBytes("qualifier"), 0, 100, 0L, KeyValue.Type.Put, 0, HConstants.EMPTY_BYTE_ARRAY, 208 0, 0, HConstants.EMPTY_BYTE_ARRAY, 0, 0); 209 }); 210 } 211 212 @Test 213 public void testIllegalTimestamp() { 214 assertThrows(IllegalArgumentException.class, () -> { 215 new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3, Bytes.toBytes("family"), 0, 6, 216 Bytes.toBytes("qualifier"), 0, 9, -100, KeyValue.Type.Put, 0, HConstants.EMPTY_BYTE_ARRAY, 217 0, 0, HConstants.EMPTY_BYTE_ARRAY, 0, 0); 218 }); 219 } 220 221 @Test 222 public void testIllegalValue() { 223 assertThrows(IllegalArgumentException.class, () -> { 224 new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3, Bytes.toBytes("family"), 0, 6, 225 Bytes.toBytes("qualifier"), 0, 9, 0L, KeyValue.Type.Put, 0, Bytes.toBytes("value"), 0, 100, 226 HConstants.EMPTY_BYTE_ARRAY, 0, 0); 227 }); 228 } 229 230 @Test 231 public void testIllegalTags() { 232 assertThrows(IllegalArgumentException.class, () -> { 233 new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3, Bytes.toBytes("family"), 0, 6, 234 Bytes.toBytes("qualifier"), 0, 9, 0L, KeyValue.Type.Put, 0, Bytes.toBytes("value"), 0, 5, 235 Bytes.toBytes("tags"), 0, 100); 236 }); 237 } 238 239 @Test 240 public void testWriteTag() throws IOException { 241 byte[] tags = Bytes.toBytes("---tags---"); 242 int tagOffset = 3; 243 int length = 4; 244 IndividualBytesFieldCell cell = new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3, 245 Bytes.toBytes("family"), 0, 6, Bytes.toBytes("qualifier"), 0, 9, 0L, KeyValue.Type.Put, 0, 246 Bytes.toBytes("value"), 0, 5, tags, tagOffset, length); 247 248 try (ByteArrayOutputStream output = new ByteArrayOutputStream(300)) { 249 cell.write(output, true); 250 byte[] buf = output.toByteArray(); 251 assertEquals(cell.getSerializedSize(true), buf.length); 252 } 253 } 254 255 @Test 256 public void testWriteValue() throws IOException { 257 byte[] value = Bytes.toBytes("---value---"); 258 int valueOffset = 3; 259 int valueLength = 5; 260 IndividualBytesFieldCell cell = new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3, 261 Bytes.toBytes("family"), 0, 6, Bytes.toBytes("qualifier"), 0, 9, 0L, KeyValue.Type.Put, 0, 262 value, valueOffset, valueLength, Bytes.toBytes("value"), 0, 5); 263 264 try (ByteArrayOutputStream output = new ByteArrayOutputStream(300)) { 265 cell.write(output, true); 266 byte[] buf = output.toByteArray(); 267 assertEquals(cell.getSerializedSize(true), buf.length); 268 } 269 } 270}