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  
22  import java.io.IOException;
23  import java.util.Arrays;
24  import java.util.LinkedList;
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    private final LinkedList<Result> cache = new LinkedList<Result>();
44  
45    @Before
46    public void setUp() {
47      resultCache = new BatchScanResultCache(cache, 4);
48    }
49  
50    @After
51    public void tearDown() {
52      resultCache.clear();
53      resultCache = null;
54    }
55  
56    static Cell createCell(byte[] cf, int key, int cq) {
57      return new KeyValue(Bytes.toBytes(key), cf, Bytes.toBytes("cq" + cq), Bytes.toBytes(key));
58    }
59  
60    static Cell[] createCells(byte[] cf, int key, int numCqs) {
61      Cell[] cells = new Cell[numCqs];
62      for (int i = 0; i < numCqs; i++) {
63        cells[i] = createCell(cf, key, i);
64      }
65      return cells;
66    }
67  
68    private void assertResultEquals(Result result, int key, int start, int to) {
69      assertEquals(to - start, result.size());
70      for (int i = start; i < to; i++) {
71        assertEquals(key, Bytes.toInt(result.getValue(CF, Bytes.toBytes("cq" + i))));
72      }
73      assertEquals(to - start == 4, result.mayHaveMoreCellsInRow());
74    }
75  
76    @Test
77    public void test() throws IOException {
78      resultCache.loadResultsToCache(ScanResultCache.EMPTY_RESULT_ARRAY, false);
79      assertEquals(0, cache.size());
80      resultCache.loadResultsToCache(ScanResultCache.EMPTY_RESULT_ARRAY, true);
81      assertEquals(0, cache.size());
82  
83      Cell[] cells1 = createCells(CF, 1, 10);
84      Cell[] cells2 = createCells(CF, 2, 10);
85      Cell[] cells3 = createCells(CF, 3, 10);
86      resultCache.loadResultsToCache(
87        new Result[] { Result.create(Arrays.copyOf(cells1, 3), null, false, true) }, false);
88      assertEquals(0, cache.size());
89      resultCache.loadResultsToCache(
90        new Result[] { Result.create(Arrays.copyOfRange(cells1, 3, 7), null, false, true),
91            Result.create(Arrays.copyOfRange(cells1, 7, 10), null, false, true) },
92        false);
93      Result[] results = cache.toArray(new Result[0]);
94      assertEquals(2, results.length);
95      assertResultEquals(results[0], 1, 0, 4);
96      assertResultEquals(results[1], 1, 4, 8);
97  
98      cache.clear();
99      resultCache.loadResultsToCache(ScanResultCache.EMPTY_RESULT_ARRAY, false);
100     results = cache.toArray(new Result[0]);
101     assertEquals(1, results.length);
102     assertResultEquals(results[0], 1, 8, 10);
103 
104     cache.clear();
105     resultCache.loadResultsToCache(
106       new Result[] { Result.create(Arrays.copyOfRange(cells2, 0, 4), null, false, true),
107           Result.create(Arrays.copyOfRange(cells2, 4, 8), null, false, true),
108           Result.create(Arrays.copyOfRange(cells2, 8, 10), null, false, true),
109           Result.create(Arrays.copyOfRange(cells3, 0, 4), null, false, true),
110           Result.create(Arrays.copyOfRange(cells3, 4, 8), null, false, true),
111           Result.create(Arrays.copyOfRange(cells3, 8, 10), null, false, false) },
112       false);
113     results = cache.toArray(new Result[0]);
114     assertEquals(6, results.length);
115     assertResultEquals(results[0], 2, 0, 4);
116     assertResultEquals(results[1], 2, 4, 8);
117     assertResultEquals(results[2], 2, 8, 10);
118     assertResultEquals(results[3], 3, 0, 4);
119     assertResultEquals(results[4], 3, 4, 8);
120     assertResultEquals(results[5], 3, 8, 10);
121   }
122 }