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
.junit
.ClassRule
;
35 import org
.junit
.Test
;
36 import org
.junit
.experimental
.categories
.Category
;
39 * Test that I can Iterate Client Actions that hold Cells (Get does not have Cells).
41 @Category({SmallTests
.class, ClientTests
.class})
42 public class TestPutDeleteEtcCellIteration
{
45 public static final HBaseClassTestRule CLASS_RULE
=
46 HBaseClassTestRule
.forClass(TestPutDeleteEtcCellIteration
.class);
48 private static final byte [] ROW
= new byte [] {'r'};
49 private static final long TIMESTAMP
= System
.currentTimeMillis();
50 private static final int COUNT
= 10;
53 public void testPutIteration() throws IOException
{
55 for (int i
= 0; i
< COUNT
; i
++) {
56 byte [] bytes
= Bytes
.toBytes(i
);
57 p
.addColumn(bytes
, bytes
, TIMESTAMP
, bytes
);
60 for (CellScanner cellScanner
= p
.cellScanner(); cellScanner
.advance();) {
61 Cell cell
= cellScanner
.current();
62 byte [] bytes
= Bytes
.toBytes(index
++);
63 cell
.equals(new KeyValue(ROW
, bytes
, bytes
, TIMESTAMP
, bytes
));
65 assertEquals(COUNT
, index
);
68 @Test (expected
= ConcurrentModificationException
.class)
69 public void testPutConcurrentModificationOnIteration() throws IOException
{
71 for (int i
= 0; i
< COUNT
; i
++) {
72 byte [] bytes
= Bytes
.toBytes(i
);
73 p
.addColumn(bytes
, bytes
, TIMESTAMP
, bytes
);
77 for (CellScanner cellScanner
= p
.cellScanner(); cellScanner
.advance();) {
78 Cell cell
= cellScanner
.current();
79 byte [] bytes
= Bytes
.toBytes(index
++);
80 // When we hit the trigger, try inserting a new KV; should trigger exception
81 if (trigger
== 3) p
.addColumn(bytes
, bytes
, TIMESTAMP
, bytes
);
82 cell
.equals(new KeyValue(ROW
, bytes
, bytes
, TIMESTAMP
, bytes
));
84 assertEquals(COUNT
, index
);
88 public void testDeleteIteration() throws IOException
{
89 Delete d
= new Delete(ROW
);
90 for (int i
= 0; i
< COUNT
; i
++) {
91 byte [] bytes
= Bytes
.toBytes(i
);
92 d
.addColumn(bytes
, bytes
, TIMESTAMP
);
95 for (CellScanner cellScanner
= d
.cellScanner(); cellScanner
.advance();) {
96 Cell cell
= cellScanner
.current();
97 byte [] bytes
= Bytes
.toBytes(index
++);
98 cell
.equals(new KeyValue(ROW
, bytes
, bytes
, TIMESTAMP
, KeyValue
.Type
.DeleteColumn
));
100 assertEquals(COUNT
, index
);
104 public void testAppendIteration() throws IOException
{
105 Append a
= new Append(ROW
);
106 for (int i
= 0; i
< COUNT
; i
++) {
107 byte [] bytes
= Bytes
.toBytes(i
);
108 a
.addColumn(bytes
, bytes
, bytes
);
111 for (CellScanner cellScanner
= a
.cellScanner(); cellScanner
.advance();) {
112 Cell cell
= cellScanner
.current();
113 byte [] bytes
= Bytes
.toBytes(index
++);
114 KeyValue kv
= (KeyValue
)cell
;
115 assertTrue(Bytes
.equals(CellUtil
.cloneFamily(kv
), bytes
));
116 assertTrue(Bytes
.equals(CellUtil
.cloneValue(kv
), bytes
));
118 assertEquals(COUNT
, index
);
122 public void testIncrementIteration() throws IOException
{
123 Increment increment
= new Increment(ROW
);
124 for (int i
= 0; i
< COUNT
; i
++) {
125 byte [] bytes
= Bytes
.toBytes(i
);
126 increment
.addColumn(bytes
, bytes
, i
);
129 for (CellScanner cellScanner
= increment
.cellScanner(); cellScanner
.advance();) {
130 Cell cell
= cellScanner
.current();
132 byte [] bytes
= Bytes
.toBytes(index
++);
133 KeyValue kv
= (KeyValue
)cell
;
134 assertTrue(Bytes
.equals(CellUtil
.cloneFamily(kv
), bytes
));
135 long a
= Bytes
.toLong(CellUtil
.cloneValue(kv
));
136 assertEquals(value
, a
);
138 assertEquals(COUNT
, index
);
142 public void testResultIteration() throws IOException
{
143 Cell
[] cells
= new Cell
[COUNT
];
144 for(int i
= 0; i
< COUNT
; i
++) {
145 byte [] bytes
= Bytes
.toBytes(i
);
146 cells
[i
] = new KeyValue(ROW
, bytes
, bytes
, TIMESTAMP
, bytes
);
148 Result r
= Result
.create(Arrays
.asList(cells
));
150 for (CellScanner cellScanner
= r
.cellScanner(); cellScanner
.advance();) {
151 Cell cell
= cellScanner
.current();
152 byte [] bytes
= Bytes
.toBytes(index
++);
153 cell
.equals(new KeyValue(ROW
, bytes
, bytes
, TIMESTAMP
, bytes
));
155 assertEquals(COUNT
, index
);