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