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  // Verify if ExtendedCell interface is implemented
182  @Test
183  public void testIfExtendedCellImplemented() {
184    assertTrue(ic0 instanceof ExtendedCell);
185  }
186
187  @Test(expected = IllegalArgumentException.class)
188  public void testIllegalRow() {
189    new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 100, HConstants.EMPTY_BYTE_ARRAY, 0, 0,
190      HConstants.EMPTY_BYTE_ARRAY, 0, 0, 0L, KeyValue.Type.Put, 0, HConstants.EMPTY_BYTE_ARRAY, 0,
191      0, HConstants.EMPTY_BYTE_ARRAY, 0, 0);
192  }
193
194  @Test(expected = IllegalArgumentException.class)
195  public void testIllegalFamily() {
196    new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3, Bytes.toBytes("family"), 0, 100,
197      HConstants.EMPTY_BYTE_ARRAY, 0, 0, 0L, KeyValue.Type.Put, 0, HConstants.EMPTY_BYTE_ARRAY, 0,
198      0, HConstants.EMPTY_BYTE_ARRAY, 0, 0);
199  }
200
201  @Test(expected = IllegalArgumentException.class)
202  public void testIllegalQualifier() {
203    new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3, Bytes.toBytes("family"), 0, 6,
204      Bytes.toBytes("qualifier"), 0, 100, 0L, KeyValue.Type.Put, 0, HConstants.EMPTY_BYTE_ARRAY, 0,
205      0, HConstants.EMPTY_BYTE_ARRAY, 0, 0);
206  }
207
208  @Test(expected = IllegalArgumentException.class)
209  public void testIllegalTimestamp() {
210    new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3, Bytes.toBytes("family"), 0, 6,
211      Bytes.toBytes("qualifier"), 0, 9, -100, KeyValue.Type.Put, 0, HConstants.EMPTY_BYTE_ARRAY, 0,
212      0, HConstants.EMPTY_BYTE_ARRAY, 0, 0);
213  }
214
215  @Test(expected = IllegalArgumentException.class)
216  public void testIllegalValue() {
217    new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3, Bytes.toBytes("family"), 0, 6,
218      Bytes.toBytes("qualifier"), 0, 9, 0L, KeyValue.Type.Put, 0, Bytes.toBytes("value"), 0, 100,
219      HConstants.EMPTY_BYTE_ARRAY, 0, 0);
220  }
221
222  @Test(expected = IllegalArgumentException.class)
223  public void testIllegalTags() {
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, 5,
226      Bytes.toBytes("tags"), 0, 100);
227  }
228
229  @Test
230  public void testWriteTag() throws IOException {
231    byte[] tags = Bytes.toBytes("---tags---");
232    int tagOffset = 3;
233    int length = 4;
234    IndividualBytesFieldCell cell = new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3,
235      Bytes.toBytes("family"), 0, 6, Bytes.toBytes("qualifier"), 0, 9, 0L, KeyValue.Type.Put, 0,
236      Bytes.toBytes("value"), 0, 5, tags, tagOffset, length);
237
238    try (ByteArrayOutputStream output = new ByteArrayOutputStream(300)) {
239      cell.write(output, true);
240      byte[] buf = output.toByteArray();
241      assertEquals(cell.getSerializedSize(true), buf.length);
242    }
243  }
244
245  @Test
246  public void testWriteValue() throws IOException {
247    byte[] value = Bytes.toBytes("---value---");
248    int valueOffset = 3;
249    int valueLength = 5;
250    IndividualBytesFieldCell cell = new IndividualBytesFieldCell(Bytes.toBytes("row"), 0, 3,
251      Bytes.toBytes("family"), 0, 6, Bytes.toBytes("qualifier"), 0, 9, 0L, KeyValue.Type.Put, 0,
252      value, valueOffset, valueLength, Bytes.toBytes("value"), 0, 5);
253
254    try (ByteArrayOutputStream output = new ByteArrayOutputStream(300)) {
255      cell.write(output, true);
256      byte[] buf = output.toByteArray();
257      assertEquals(cell.getSerializedSize(true), buf.length);
258    }
259  }
260}