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.client; 019 020import static org.junit.Assert.assertNotEquals; 021import static org.junit.Assert.assertTrue; 022 023import java.io.IOException; 024import java.util.List; 025import org.apache.hadoop.hbase.Cell; 026import org.apache.hadoop.hbase.CellBuilderFactory; 027import org.apache.hadoop.hbase.CellBuilderType; 028import org.apache.hadoop.hbase.CellUtil; 029import org.apache.hadoop.hbase.HBaseClassTestRule; 030import org.apache.hadoop.hbase.io.TimeRange; 031import org.apache.hadoop.hbase.testclassification.ClientTests; 032import org.apache.hadoop.hbase.testclassification.SmallTests; 033import org.apache.hadoop.hbase.util.Bytes; 034import org.junit.Assert; 035import org.junit.ClassRule; 036import org.junit.Test; 037import org.junit.experimental.categories.Category; 038 039@Category({ SmallTests.class, ClientTests.class }) 040public class TestMutation { 041 042 @ClassRule 043 public static final HBaseClassTestRule CLASS_RULE = 044 HBaseClassTestRule.forClass(TestMutation.class); 045 046 @Test 047 public void testAppendCopyConstructor() throws IOException { 048 Append origin = new Append(Bytes.toBytes("ROW-01")); 049 origin.setPriority(100); 050 byte[] family = Bytes.toBytes("CF-01"); 051 052 origin.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(origin.getRow()) 053 .setFamily(family).setQualifier(Bytes.toBytes("q")).setType(Cell.Type.Put) 054 .setValue(Bytes.toBytes(100)).build()); 055 origin.addColumn(family, Bytes.toBytes("q0"), Bytes.toBytes("value")); 056 origin.setTimeRange(100, 1000); 057 Append clone = new Append(origin); 058 assertEquals(origin, clone); 059 origin.addColumn(family, Bytes.toBytes("q1"), Bytes.toBytes("value")); 060 061 // They should have different cell lists 062 assertNotEquals(origin.getCellList(family), clone.getCellList(family)); 063 } 064 065 @Test 066 public void testIncrementCopyConstructor() throws IOException { 067 Increment origin = new Increment(Bytes.toBytes("ROW-01")); 068 origin.setPriority(100); 069 byte[] family = Bytes.toBytes("CF-01"); 070 071 origin.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(origin.getRow()) 072 .setFamily(family).setQualifier(Bytes.toBytes("q")).setType(Cell.Type.Put) 073 .setValue(Bytes.toBytes(100)).build()); 074 origin.addColumn(family, Bytes.toBytes("q0"), 4); 075 origin.setTimeRange(100, 1000); 076 Increment clone = new Increment(origin); 077 assertEquals(origin, clone); 078 origin.addColumn(family, Bytes.toBytes("q1"), 3); 079 080 // They should have different cell lists 081 assertNotEquals(origin.getCellList(family), clone.getCellList(family)); 082 } 083 084 @Test 085 public void testDeleteCopyConstructor() throws IOException { 086 Delete origin = new Delete(Bytes.toBytes("ROW-01")); 087 origin.setPriority(100); 088 byte[] family = Bytes.toBytes("CF-01"); 089 090 origin.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(origin.getRow()) 091 .setFamily(family).setQualifier(Bytes.toBytes("q")).setType(Cell.Type.Delete).build()); 092 origin.addColumn(family, Bytes.toBytes("q0")); 093 origin.addColumns(family, Bytes.toBytes("q1")); 094 origin.addFamily(family); 095 origin.addColumns(family, Bytes.toBytes("q2"), 100); 096 origin.addFamilyVersion(family, 1000); 097 Delete clone = new Delete(origin); 098 assertEquals(origin, clone); 099 origin.addColumn(family, Bytes.toBytes("q3")); 100 101 // They should have different cell lists 102 assertNotEquals(origin.getCellList(family), clone.getCellList(family)); 103 } 104 105 @Test 106 public void testPutCopyConstructor() throws IOException { 107 Put origin = new Put(Bytes.toBytes("ROW-01")); 108 origin.setPriority(100); 109 byte[] family = Bytes.toBytes("CF-01"); 110 111 origin.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(origin.getRow()) 112 .setFamily(family).setQualifier(Bytes.toBytes("q")).setType(Cell.Type.Put) 113 .setValue(Bytes.toBytes("value")).build()); 114 origin.addColumn(family, Bytes.toBytes("q0"), Bytes.toBytes("V-01")); 115 origin.addColumn(family, Bytes.toBytes("q1"), 100, Bytes.toBytes("V-01")); 116 Put clone = new Put(origin); 117 assertEquals(origin, clone); 118 origin.addColumn(family, Bytes.toBytes("q2"), Bytes.toBytes("V-02")); 119 120 // They should have different cell lists 121 assertNotEquals(origin.getCellList(family), clone.getCellList(family)); 122 } 123 124 private void assertEquals(Mutation origin, Mutation clone) { 125 Assert.assertEquals(origin.getFamilyCellMap().size(), clone.getFamilyCellMap().size()); 126 for (byte[] family : origin.getFamilyCellMap().keySet()) { 127 List<Cell> originCells = origin.getCellList(family); 128 List<Cell> cloneCells = clone.getCellList(family); 129 Assert.assertEquals(originCells.size(), cloneCells.size()); 130 for (int i = 0; i != cloneCells.size(); ++i) { 131 Cell originCell = originCells.get(i); 132 Cell cloneCell = cloneCells.get(i); 133 assertTrue(CellUtil.equals(originCell, cloneCell)); 134 assertTrue(CellUtil.matchingValue(originCell, cloneCell)); 135 } 136 } 137 Assert.assertEquals(origin.getAttributesMap().size(), clone.getAttributesMap().size()); 138 for (String name : origin.getAttributesMap().keySet()) { 139 byte[] originValue = origin.getAttributesMap().get(name); 140 byte[] cloneValue = clone.getAttributesMap().get(name); 141 assertTrue(Bytes.equals(originValue, cloneValue)); 142 } 143 Assert.assertEquals(origin.getTimestamp(), clone.getTimestamp()); 144 Assert.assertEquals(origin.getPriority(), clone.getPriority()); 145 if (origin instanceof Append) { 146 assertEquals(((Append) origin).getTimeRange(), ((Append) clone).getTimeRange()); 147 } 148 if (origin instanceof Increment) { 149 assertEquals(((Increment) origin).getTimeRange(), ((Increment) clone).getTimeRange()); 150 } 151 } 152 153 private static void assertEquals(TimeRange origin, TimeRange clone) { 154 Assert.assertEquals(origin.getMin(), clone.getMin()); 155 Assert.assertEquals(origin.getMax(), clone.getMax()); 156 } 157 158 // HBASE-14881 159 @Test 160 public void testRowIsImmutableOrNot() { 161 byte[] rowKey = Bytes.toBytes("immutable"); 162 163 // Test when row key is immutable 164 Put putRowIsImmutable = new Put(rowKey, true); 165 assertTrue(rowKey == putRowIsImmutable.getRow()); // No local copy is made 166 167 // Test when row key is not immutable 168 Put putRowIsNotImmutable = new Put(rowKey, 1000L, false); 169 assertTrue(rowKey != putRowIsNotImmutable.getRow()); // A local copy is made 170 } 171 172 // HBASE-14882 173 @Test 174 public void testAddImmutableToPut() throws IOException { 175 byte[] row = Bytes.toBytes("immutable-row"); 176 byte[] family = Bytes.toBytes("immutable-family"); 177 178 byte[] qualifier0 = Bytes.toBytes("immutable-qualifier-0"); 179 byte[] value0 = Bytes.toBytes("immutable-value-0"); 180 181 byte[] qualifier1 = Bytes.toBytes("immutable-qualifier-1"); 182 byte[] value1 = Bytes.toBytes("immutable-value-1"); 183 long ts1 = 5000L; 184 185 // "true" indicates that the input row is immutable 186 Put put = new Put(row, true); 187 put 188 .add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(row).setFamily(family) 189 .setQualifier(qualifier0).setTimestamp(put.getTimestamp()).setType(Cell.Type.Put) 190 .setValue(value0).build()) 191 .add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(row).setFamily(family) 192 .setQualifier(qualifier1).setTimestamp(ts1).setType(Cell.Type.Put).setValue(value1) 193 .build()); 194 195 // Verify the cell of family:qualifier0 196 Cell cell0 = put.get(family, qualifier0).get(0); 197 198 // Verify no local copy is made for family, qualifier or value 199 assertTrue(cell0.getFamilyArray() == family); 200 assertTrue(cell0.getQualifierArray() == qualifier0); 201 assertTrue(cell0.getValueArray() == value0); 202 203 // Verify timestamp 204 assertTrue(cell0.getTimestamp() == put.getTimestamp()); 205 206 // Verify the cell of family:qualifier1 207 Cell cell1 = put.get(family, qualifier1).get(0); 208 209 // Verify no local copy is made for family, qualifier or value 210 assertTrue(cell1.getFamilyArray() == family); 211 assertTrue(cell1.getQualifierArray() == qualifier1); 212 assertTrue(cell1.getValueArray() == value1); 213 214 // Verify timestamp 215 assertTrue(cell1.getTimestamp() == ts1); 216 } 217}