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.assertEquals;
021
022import org.apache.hadoop.hbase.testclassification.MiscTests;
023import org.apache.hadoop.hbase.testclassification.SmallTests;
024import org.junit.ClassRule;
025import org.junit.Test;
026import org.junit.experimental.categories.Category;
027
028@Category({ MiscTests.class, SmallTests.class })
029public class TestCellBuilder {
030
031  @ClassRule
032  public static final HBaseClassTestRule CLASS_RULE =
033    HBaseClassTestRule.forClass(TestCellBuilder.class);
034
035  private static final byte OLD_DATA = 87;
036  private static final byte NEW_DATA = 100;
037
038  @Test
039  public void testCellBuilderWithDeepCopy() {
040    byte[] row = new byte[] { OLD_DATA };
041    byte[] family = new byte[] { OLD_DATA };
042    byte[] qualifier = new byte[] { OLD_DATA };
043    byte[] value = new byte[] { OLD_DATA };
044    Cell cell = CellBuilderFactory.create(CellBuilderType.DEEP_COPY).setRow(row).setFamily(family)
045      .setQualifier(qualifier).setType(Cell.Type.Put).setValue(value).build();
046    row[0] = NEW_DATA;
047    family[0] = NEW_DATA;
048    qualifier[0] = NEW_DATA;
049    value[0] = NEW_DATA;
050    assertEquals(OLD_DATA, cell.getRowArray()[cell.getRowOffset()]);
051    assertEquals(OLD_DATA, cell.getFamilyArray()[cell.getFamilyOffset()]);
052    assertEquals(OLD_DATA, cell.getQualifierArray()[cell.getQualifierOffset()]);
053    assertEquals(OLD_DATA, cell.getValueArray()[cell.getValueOffset()]);
054  }
055
056  @Test
057  public void testCellBuilderWithShallowCopy() {
058    byte[] row = new byte[] { OLD_DATA };
059    byte[] family = new byte[] { OLD_DATA };
060    byte[] qualifier = new byte[] { OLD_DATA };
061    byte[] value = new byte[] { OLD_DATA };
062    Cell cell = CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(row)
063      .setFamily(family).setQualifier(qualifier).setType(Cell.Type.Put).setValue(value).build();
064    row[0] = NEW_DATA;
065    family[0] = NEW_DATA;
066    qualifier[0] = NEW_DATA;
067    value[0] = NEW_DATA;
068    assertEquals(NEW_DATA, cell.getRowArray()[cell.getRowOffset()]);
069    assertEquals(NEW_DATA, cell.getFamilyArray()[cell.getFamilyOffset()]);
070    assertEquals(NEW_DATA, cell.getQualifierArray()[cell.getQualifierOffset()]);
071    assertEquals(NEW_DATA, cell.getValueArray()[cell.getValueOffset()]);
072  }
073
074  @Test
075  public void testExtendedCellBuilderWithShallowCopy() {
076    byte[] row = new byte[] { OLD_DATA };
077    byte[] family = new byte[] { OLD_DATA };
078    byte[] qualifier = new byte[] { OLD_DATA };
079    byte[] value = new byte[] { OLD_DATA };
080    byte[] tags = new byte[] { OLD_DATA };
081    long seqId = 999;
082    ExtendedCell cell = ExtendedCellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(row)
083      .setFamily(family).setQualifier(qualifier).setType(KeyValue.Type.Put.getCode())
084      .setValue(value).setTags(tags).setSequenceId(seqId).build();
085    row[0] = NEW_DATA;
086    family[0] = NEW_DATA;
087    qualifier[0] = NEW_DATA;
088    value[0] = NEW_DATA;
089    tags[0] = NEW_DATA;
090    assertEquals(NEW_DATA, cell.getRowArray()[cell.getRowOffset()]);
091    assertEquals(NEW_DATA, cell.getFamilyArray()[cell.getFamilyOffset()]);
092    assertEquals(NEW_DATA, cell.getQualifierArray()[cell.getQualifierOffset()]);
093    assertEquals(NEW_DATA, cell.getValueArray()[cell.getValueOffset()]);
094    assertEquals(NEW_DATA, cell.getTagsArray()[cell.getTagsOffset()]);
095    assertEquals(seqId, cell.getSequenceId());
096  }
097
098  @Test
099  public void testExtendedCellBuilderWithDeepCopy() {
100    byte[] row = new byte[] { OLD_DATA };
101    byte[] family = new byte[] { OLD_DATA };
102    byte[] qualifier = new byte[] { OLD_DATA };
103    byte[] value = new byte[] { OLD_DATA };
104    byte[] tags = new byte[] { OLD_DATA };
105    long seqId = 999;
106    ExtendedCell cell = ExtendedCellBuilderFactory.create(CellBuilderType.DEEP_COPY).setRow(row)
107      .setFamily(family).setQualifier(qualifier).setType(KeyValue.Type.Put.getCode())
108      .setValue(value).setTags(tags).setSequenceId(seqId).build();
109    row[0] = NEW_DATA;
110    family[0] = NEW_DATA;
111    qualifier[0] = NEW_DATA;
112    value[0] = NEW_DATA;
113    tags[0] = NEW_DATA;
114    assertEquals(OLD_DATA, cell.getRowArray()[cell.getRowOffset()]);
115    assertEquals(OLD_DATA, cell.getFamilyArray()[cell.getFamilyOffset()]);
116    assertEquals(OLD_DATA, cell.getQualifierArray()[cell.getQualifierOffset()]);
117    assertEquals(OLD_DATA, cell.getValueArray()[cell.getValueOffset()]);
118    assertEquals(OLD_DATA, cell.getTagsArray()[cell.getTagsOffset()]);
119    assertEquals(seqId, cell.getSequenceId());
120  }
121}