View Javadoc

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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  package org.apache.hadoop.hbase.client;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertSame;
22  
23  import java.io.IOException;
24  import java.util.Arrays;
25  
26  import org.apache.hadoop.hbase.Cell;
27  import org.apache.hadoop.hbase.KeyValue;
28  import org.apache.hadoop.hbase.testclassification.ClientTests;
29  import org.apache.hadoop.hbase.testclassification.SmallTests;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.junit.After;
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  
36  @Category({ SmallTests.class, ClientTests.class })
37  public class TestBatchScanResultCache {
38  
39    private static byte[] CF = Bytes.toBytes("cf");
40  
41    private BatchScanResultCache resultCache;
42  
43    @Before
44    public void setUp() {
45      resultCache = new BatchScanResultCache(4);
46    }
47  
48    @After
49    public void tearDown() {
50      resultCache.clear();
51      resultCache = null;
52    }
53  
54    static Cell createCell(byte[] cf, int key, int cq) {
55      return new KeyValue(Bytes.toBytes(key), cf, Bytes.toBytes("cq" + cq), Bytes.toBytes(key));
56    }
57  
58    static Cell[] createCells(byte[] cf, int key, int numCqs) {
59      Cell[] cells = new Cell[numCqs];
60      for (int i = 0; i < numCqs; i++) {
61        cells[i] = createCell(cf, key, i);
62      }
63      return cells;
64    }
65  
66    private void assertResultEquals(Result result, int key, int start, int to) {
67      assertEquals(to - start, result.size());
68      for (int i = start; i < to; i++) {
69        assertEquals(key, Bytes.toInt(result.getValue(CF, Bytes.toBytes("cq" + i))));
70      }
71      assertEquals(to - start == 4, result.mayHaveMoreCellsInRow());
72    }
73  
74    @Test
75    public void test() throws IOException {
76      assertSame(ScanResultCache.EMPTY_RESULT_ARRAY,
77        resultCache.addAndGet(ScanResultCache.EMPTY_RESULT_ARRAY, false));
78      assertSame(ScanResultCache.EMPTY_RESULT_ARRAY,
79        resultCache.addAndGet(ScanResultCache.EMPTY_RESULT_ARRAY, true));
80  
81      Cell[] cells1 = createCells(CF, 1, 10);
82      Cell[] cells2 = createCells(CF, 2, 10);
83      Cell[] cells3 = createCells(CF, 3, 10);
84      assertEquals(0, resultCache.addAndGet(
85        new Result[] { Result.create(Arrays.copyOf(cells1, 3), null, false, true) }, false).length);
86      Result[] results = resultCache.addAndGet(
87        new Result[] { Result.create(Arrays.copyOfRange(cells1, 3, 7), null, false, true),
88            Result.create(Arrays.copyOfRange(cells1, 7, 10), null, false, true) },
89        false);
90      assertEquals(2, results.length);
91      assertResultEquals(results[0], 1, 0, 4);
92      assertResultEquals(results[1], 1, 4, 8);
93      results = resultCache.addAndGet(ScanResultCache.EMPTY_RESULT_ARRAY, false);
94      assertEquals(1, results.length);
95      assertResultEquals(results[0], 1, 8, 10);
96  
97      results = resultCache.addAndGet(
98        new Result[] { Result.create(Arrays.copyOfRange(cells2, 0, 4), null, false, true),
99            Result.create(Arrays.copyOfRange(cells2, 4, 8), null, false, true),
100           Result.create(Arrays.copyOfRange(cells2, 8, 10), null, false, true),
101           Result.create(Arrays.copyOfRange(cells3, 0, 4), null, false, true),
102           Result.create(Arrays.copyOfRange(cells3, 4, 8), null, false, true),
103           Result.create(Arrays.copyOfRange(cells3, 8, 10), null, false, false) },
104       false);
105     assertEquals(6, results.length);
106     assertResultEquals(results[0], 2, 0, 4);
107     assertResultEquals(results[1], 2, 4, 8);
108     assertResultEquals(results[2], 2, 8, 10);
109     assertResultEquals(results[3], 3, 0, 4);
110     assertResultEquals(results[4], 3, 4, 8);
111     assertResultEquals(results[5], 3, 8, 10);
112   }
113 }