View Javadoc

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