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.apache.hadoop.hbase.client.TestBatchScanResultCache.createCells;
21  import static org.junit.Assert.assertEquals;
22  
23  import java.io.IOException;
24  import java.util.Arrays;
25  import java.util.LinkedList;
26  
27  import org.apache.hadoop.hbase.Cell;
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 TestAllowPartialScanResultCache {
38  
39    private static byte[] CF = Bytes.toBytes("cf");
40  
41    private AllowPartialScanResultCache resultCache;
42    private static final LinkedList<Result> cache = new LinkedList<Result>();
43  
44    @Before
45    public void setUp() {
46      resultCache = new AllowPartialScanResultCache(cache);
47    }
48  
49    @After
50    public void tearDown() {
51      resultCache.clear();
52      resultCache = null;
53    }
54  
55    @Test
56    public void test() throws IOException {
57      resultCache.loadResultsToCache(ScanResultCache.EMPTY_RESULT_ARRAY, false);
58      assertEquals(0, cache.size());
59      resultCache.loadResultsToCache(ScanResultCache.EMPTY_RESULT_ARRAY, true);
60      assertEquals(0, cache.size());
61  
62      Cell[] cells1 = createCells(CF, 1, 10);
63      Cell[] cells2 = createCells(CF, 2, 10);
64  
65      resultCache.loadResultsToCache(
66        new Result[] { Result.create(Arrays.copyOf(cells1, 5), null, false, true) }, false);
67      Result[] results1 = cache.toArray(new Result[0]);
68      assertEquals(1, results1.length);
69      assertEquals(1, Bytes.toInt(results1[0].getRow()));
70      assertEquals(5, results1[0].rawCells().length);
71      for (int i = 0; i < 5; i++) {
72        assertEquals(1, Bytes.toInt(results1[0].getValue(CF, Bytes.toBytes("cq" + i))));
73      }
74  
75      cache.clear();
76      resultCache.loadResultsToCache(
77        new Result[] { Result.create(Arrays.copyOfRange(cells1, 1, 10), null, false, true) }, false);
78      Result[] results2 = cache.toArray(new Result[0]);
79      assertEquals(1, results2.length);
80      assertEquals(1, Bytes.toInt(results2[0].getRow()));
81      assertEquals(5, results2[0].rawCells().length);
82      for (int i = 5; i < 10; i++) {
83        assertEquals(1, Bytes.toInt(results2[0].getValue(CF, Bytes.toBytes("cq" + i))));
84      }
85  
86      cache.clear();
87      resultCache.loadResultsToCache(
88        new Result[] { Result.create(cells1), Result.create(cells2) }, false);
89      Result[] results3 = cache.toArray(new Result[0]);
90      assertEquals(1, results3.length);
91      assertEquals(2, Bytes.toInt(results3[0].getRow()));
92      assertEquals(10, results3[0].rawCells().length);
93      for (int i = 0; i < 10; i++) {
94        assertEquals(2, Bytes.toInt(results3[0].getValue(CF, Bytes.toBytes("cq" + i))));
95      }
96    }
97  }