HBASE-26567 Remove IndexType from ChunkCreator (#3947)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / client / TestPutDeleteEtcCellIteration.java
blob9f2cc0114649beb719daeab8af65e14baa2a9d31
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.client;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
23 import java.io.IOException;
24 import java.util.Arrays;
25 import java.util.ConcurrentModificationException;
26 import org.apache.hadoop.hbase.Cell;
27 import org.apache.hadoop.hbase.CellScanner;
28 import org.apache.hadoop.hbase.CellUtil;
29 import org.apache.hadoop.hbase.HBaseClassTestRule;
30 import org.apache.hadoop.hbase.KeyValue;
31 import org.apache.hadoop.hbase.testclassification.ClientTests;
32 import org.apache.hadoop.hbase.testclassification.SmallTests;
33 import org.apache.hadoop.hbase.util.Bytes;
34 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
35 import org.junit.ClassRule;
36 import org.junit.Test;
37 import org.junit.experimental.categories.Category;
39 /**
40 * Test that I can Iterate Client Actions that hold Cells (Get does not have Cells).
42 @Category({SmallTests.class, ClientTests.class})
43 public class TestPutDeleteEtcCellIteration {
45 @ClassRule
46 public static final HBaseClassTestRule CLASS_RULE =
47 HBaseClassTestRule.forClass(TestPutDeleteEtcCellIteration.class);
49 private static final byte [] ROW = new byte [] {'r'};
50 private static final long TIMESTAMP = EnvironmentEdgeManager.currentTime();
51 private static final int COUNT = 10;
53 @Test
54 public void testPutIteration() throws IOException {
55 Put p = new Put(ROW);
56 for (int i = 0; i < COUNT; i++) {
57 byte [] bytes = Bytes.toBytes(i);
58 p.addColumn(bytes, bytes, TIMESTAMP, bytes);
60 int index = 0;
61 for (CellScanner cellScanner = p.cellScanner(); cellScanner.advance();) {
62 Cell cell = cellScanner.current();
63 byte [] bytes = Bytes.toBytes(index++);
64 cell.equals(new KeyValue(ROW, bytes, bytes, TIMESTAMP, bytes));
66 assertEquals(COUNT, index);
69 @Test (expected = ConcurrentModificationException.class)
70 public void testPutConcurrentModificationOnIteration() throws IOException {
71 Put p = new Put(ROW);
72 for (int i = 0; i < COUNT; i++) {
73 byte [] bytes = Bytes.toBytes(i);
74 p.addColumn(bytes, bytes, TIMESTAMP, bytes);
76 int index = 0;
77 int trigger = 3;
78 for (CellScanner cellScanner = p.cellScanner(); cellScanner.advance();) {
79 Cell cell = cellScanner.current();
80 byte [] bytes = Bytes.toBytes(index++);
81 // When we hit the trigger, try inserting a new KV; should trigger exception
82 if (trigger == 3) p.addColumn(bytes, bytes, TIMESTAMP, bytes);
83 cell.equals(new KeyValue(ROW, bytes, bytes, TIMESTAMP, bytes));
85 assertEquals(COUNT, index);
88 @Test
89 public void testDeleteIteration() throws IOException {
90 Delete d = new Delete(ROW);
91 for (int i = 0; i < COUNT; i++) {
92 byte [] bytes = Bytes.toBytes(i);
93 d.addColumn(bytes, bytes, TIMESTAMP);
95 int index = 0;
96 for (CellScanner cellScanner = d.cellScanner(); cellScanner.advance();) {
97 Cell cell = cellScanner.current();
98 byte [] bytes = Bytes.toBytes(index++);
99 cell.equals(new KeyValue(ROW, bytes, bytes, TIMESTAMP, KeyValue.Type.DeleteColumn));
101 assertEquals(COUNT, index);
104 @Test
105 public void testAppendIteration() throws IOException {
106 Append a = new Append(ROW);
107 for (int i = 0; i < COUNT; i++) {
108 byte [] bytes = Bytes.toBytes(i);
109 a.addColumn(bytes, bytes, bytes);
111 int index = 0;
112 for (CellScanner cellScanner = a.cellScanner(); cellScanner.advance();) {
113 Cell cell = cellScanner.current();
114 byte [] bytes = Bytes.toBytes(index++);
115 KeyValue kv = (KeyValue)cell;
116 assertTrue(Bytes.equals(CellUtil.cloneFamily(kv), bytes));
117 assertTrue(Bytes.equals(CellUtil.cloneValue(kv), bytes));
119 assertEquals(COUNT, index);
122 @Test
123 public void testIncrementIteration() throws IOException {
124 Increment increment = new Increment(ROW);
125 for (int i = 0; i < COUNT; i++) {
126 byte [] bytes = Bytes.toBytes(i);
127 increment.addColumn(bytes, bytes, i);
129 int index = 0;
130 for (CellScanner cellScanner = increment.cellScanner(); cellScanner.advance();) {
131 Cell cell = cellScanner.current();
132 int value = index;
133 byte [] bytes = Bytes.toBytes(index++);
134 KeyValue kv = (KeyValue)cell;
135 assertTrue(Bytes.equals(CellUtil.cloneFamily(kv), bytes));
136 long a = Bytes.toLong(CellUtil.cloneValue(kv));
137 assertEquals(value, a);
139 assertEquals(COUNT, index);
142 @Test
143 public void testResultIteration() throws IOException {
144 Cell [] cells = new Cell[COUNT];
145 for(int i = 0; i < COUNT; i++) {
146 byte [] bytes = Bytes.toBytes(i);
147 cells[i] = new KeyValue(ROW, bytes, bytes, TIMESTAMP, bytes);
149 Result r = Result.create(Arrays.asList(cells));
150 int index = 0;
151 for (CellScanner cellScanner = r.cellScanner(); cellScanner.advance();) {
152 Cell cell = cellScanner.current();
153 byte [] bytes = Bytes.toBytes(index++);
154 cell.equals(new KeyValue(ROW, bytes, bytes, TIMESTAMP, bytes));
156 assertEquals(COUNT, index);