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(). Should have the same 099 * 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. Should have 139 * 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 = Bytes.copy(kv1.getTagsArray(), kv1.getTagsOffset(), kv1.getTagsLength()); 166 167 // getXXXArray() for family, qualifier, value and tags are supposed to return empty byte array, 168 // rather than null. 169 assertArrayEquals(familyArrayInKV, ic1.getFamilyArray()); 170 assertArrayEquals(qualifierArrayInKV, ic1.getQualifierArray()); 171 assertArrayEquals(valueArrayInKV, ic1.getValueArray()); 172 assertArrayEquals(tagsArrayInKV, ic1.getTagsArray()); 173 174 // getXXXLength() for family, qualifier, value and tags are supposed to return 0. 175 assertEquals(kv1.getFamilyLength(), ic1.getFamilyLength()); 176 assertEquals(kv1.getQualifierLength(), ic1.getQualifierLength()); 177 assertEquals(kv1.getValueLength(), ic1.getValueLength()); 178 assertEquals(kv1.getTagsLength(), ic1.getTagsLength()); 179 } 180 181 @Test 182 public void testIfExtendedCellImplemented() { 183 // Verify if ExtendedCell interface is implemented 184 ExtendedCell ec = (ExtendedCell) ic0; 185 ec.deepClone(); // Do something with ec 186 } 187 188 @Test(expected = IllegalArgumentException.class) 189 public void testIllegalRow() { 190 new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 100, HConstants.EMPTY_BYTE_ARRAY, 0, 0, 191 HConstants.EMPTY_BYTE_ARRAY, 0, 0, 0L, KeyValue.Type.Put, 0, HConstants.EMPTY_BYTE_ARRAY, 0, 192 0, HConstants.EMPTY_BYTE_ARRAY, 0, 0); 193 } 194 195 @Test(expected = IllegalArgumentException.class) 196 public void testIllegalFamily() { 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 @Test(expected = IllegalArgumentException.class) 203 public void testIllegalQualifier() { 204 new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3, Bytes.toBytes("family"), 0, 6, 205 Bytes.toBytes("qualifier"), 0, 100, 0L, KeyValue.Type.Put, 0, HConstants.EMPTY_BYTE_ARRAY, 0, 206 0, HConstants.EMPTY_BYTE_ARRAY, 0, 0); 207 } 208 209 @Test(expected = IllegalArgumentException.class) 210 public void testIllegalTimestamp() { 211 new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3, Bytes.toBytes("family"), 0, 6, 212 Bytes.toBytes("qualifier"), 0, 9, -100, KeyValue.Type.Put, 0, HConstants.EMPTY_BYTE_ARRAY, 0, 213 0, HConstants.EMPTY_BYTE_ARRAY, 0, 0); 214 } 215 216 @Test(expected = IllegalArgumentException.class) 217 public void testIllegalValue() { 218 new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3, Bytes.toBytes("family"), 0, 6, 219 Bytes.toBytes("qualifier"), 0, 9, 0L, KeyValue.Type.Put, 0, Bytes.toBytes("value"), 0, 100, 220 HConstants.EMPTY_BYTE_ARRAY, 0, 0); 221 } 222 223 @Test(expected = IllegalArgumentException.class) 224 public void testIllegalTags() { 225 new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3, Bytes.toBytes("family"), 0, 6, 226 Bytes.toBytes("qualifier"), 0, 9, 0L, KeyValue.Type.Put, 0, Bytes.toBytes("value"), 0, 5, 227 Bytes.toBytes("tags"), 0, 100); 228 } 229 230 @Test 231 public void testWriteTag() throws IOException { 232 byte[] tags = Bytes.toBytes("---tags---"); 233 int tagOffset = 3; 234 int length = 4; 235 IndividualBytesFieldCell cell = new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3, 236 Bytes.toBytes("family"), 0, 6, Bytes.toBytes("qualifier"), 0, 9, 0L, KeyValue.Type.Put, 0, 237 Bytes.toBytes("value"), 0, 5, tags, tagOffset, length); 238 239 try (ByteArrayOutputStream output = new ByteArrayOutputStream(300)) { 240 cell.write(output, true); 241 byte[] buf = output.toByteArray(); 242 assertEquals(cell.getSerializedSize(true), buf.length); 243 } 244 } 245 246 @Test 247 public void testWriteValue() throws IOException { 248 byte[] value = Bytes.toBytes("---value---"); 249 int valueOffset = 3; 250 int valueLength = 5; 251 IndividualBytesFieldCell cell = new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3, 252 Bytes.toBytes("family"), 0, 6, Bytes.toBytes("qualifier"), 0, 9, 0L, KeyValue.Type.Put, 0, 253 value, valueOffset, valueLength, Bytes.toBytes("value"), 0, 5); 254 255 try (ByteArrayOutputStream output = new ByteArrayOutputStream(300)) { 256 cell.write(output, true); 257 byte[] buf = output.toByteArray(); 258 assertEquals(cell.getSerializedSize(true), buf.length); 259 } 260 } 261}