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