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 TestCompleteResultScanResultCache {
38  
39    private static byte[] CF = Bytes.toBytes("cf");
40  
41    private static byte[] CQ1 = Bytes.toBytes("cq1");
42  
43    private static byte[] CQ2 = Bytes.toBytes("cq2");
44  
45    private static byte[] CQ3 = Bytes.toBytes("cq3");
46  
47    private CompleteScanResultCache resultCache;
48  
49    @Before
50    public void setUp() {
51      resultCache = new CompleteScanResultCache();
52    }
53  
54    @After
55    public void tearDown() {
56      resultCache.clear();
57      resultCache = null;
58    }
59  
60    private static Cell createCell(int key, byte[] cq) {
61      return new KeyValue(Bytes.toBytes(key), CF, cq, Bytes.toBytes(key));
62    }
63  
64    @Test
65    public void testNoPartial() throws IOException {
66      assertSame(ScanResultCache.EMPTY_RESULT_ARRAY,
67        resultCache.addAndGet(ScanResultCache.EMPTY_RESULT_ARRAY, false));
68      assertSame(ScanResultCache.EMPTY_RESULT_ARRAY,
69        resultCache.addAndGet(ScanResultCache.EMPTY_RESULT_ARRAY, true));
70      int count = 10;
71      Result[] results = new Result[count];
72      for (int i = 0; i < count; i++) {
73        results[i] = Result.create(Arrays.asList(createCell(i, CQ1)));
74      }
75      assertSame(results, resultCache.addAndGet(results, false));
76    }
77  
78    @Test
79    public void testCombine1() throws IOException {
80      Result previousResult = Result.create(Arrays.asList(createCell(0, CQ1)), null, false, true);
81      Result result1 = Result.create(Arrays.asList(createCell(1, CQ1)), null, false, true);
82      Result result2 = Result.create(Arrays.asList(createCell(1, CQ2)), null, false, true);
83      Result result3 = Result.create(Arrays.asList(createCell(1, CQ3)), null, false, true);
84      Result[] results = resultCache.addAndGet(new Result[] { previousResult, result1 }, false);
85      assertEquals(1, results.length);
86      assertSame(previousResult, results[0]);
87  
88      assertEquals(0, resultCache.addAndGet(new Result[] { result2 }, false).length);
89      assertEquals(0, resultCache.addAndGet(new Result[] { result3 }, false).length);
90      assertEquals(0, resultCache.addAndGet(new Result[0], true).length);
91  
92      results = resultCache.addAndGet(new Result[0], false);
93      assertEquals(1, results.length);
94      assertEquals(1, Bytes.toInt(results[0].getRow()));
95      assertEquals(3, results[0].rawCells().length);
96      assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ1)));
97      assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ2)));
98      assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ3)));
99    }
100 
101   @Test
102   public void testCombine2() throws IOException {
103     Result result1 = Result.create(Arrays.asList(createCell(1, CQ1)), null, false, true);
104     Result result2 = Result.create(Arrays.asList(createCell(1, CQ2)), null, false, true);
105     Result result3 = Result.create(Arrays.asList(createCell(1, CQ3)), null, false, true);
106     Result nextResult1 = Result.create(Arrays.asList(createCell(2, CQ1)), null, false, true);
107     Result nextToNextResult1 = Result.create(Arrays.asList(createCell(3, CQ2)), null, false, false);
108 
109     assertEquals(0, resultCache.addAndGet(new Result[] { result1 }, false).length);
110     assertEquals(0, resultCache.addAndGet(new Result[] { result2 }, false).length);
111     assertEquals(0, resultCache.addAndGet(new Result[] { result3 }, false).length);
112 
113     Result[] results = resultCache.addAndGet(new Result[] { nextResult1 }, false);
114     assertEquals(1, results.length);
115     assertEquals(1, Bytes.toInt(results[0].getRow()));
116     assertEquals(3, results[0].rawCells().length);
117     assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ1)));
118     assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ2)));
119     assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ3)));
120 
121     results = resultCache.addAndGet(new Result[] { nextToNextResult1 }, false);
122     assertEquals(2, results.length);
123     assertEquals(2, Bytes.toInt(results[0].getRow()));
124     assertEquals(1, results[0].rawCells().length);
125     assertEquals(2, Bytes.toInt(results[0].getValue(CF, CQ1)));
126     assertEquals(3, Bytes.toInt(results[1].getRow()));
127     assertEquals(1, results[1].rawCells().length);
128     assertEquals(3, Bytes.toInt(results[1].getValue(CF, CQ2)));
129   }
130 
131   @Test
132   public void testCombine3() throws IOException {
133     Result result1 = Result.create(Arrays.asList(createCell(1, CQ1)), null, false, true);
134     Result result2 = Result.create(Arrays.asList(createCell(1, CQ2)), null, false, true);
135     Result nextResult1 = Result.create(Arrays.asList(createCell(2, CQ1)), null, false, false);
136     Result nextToNextResult1 = Result.create(Arrays.asList(createCell(3, CQ1)), null, false, true);
137 
138     assertEquals(0, resultCache.addAndGet(new Result[] { result1 }, false).length);
139     assertEquals(0, resultCache.addAndGet(new Result[] { result2 }, false).length);
140 
141     Result[] results =
142         resultCache.addAndGet(new Result[] { nextResult1, nextToNextResult1 }, false);
143     assertEquals(2, results.length);
144     assertEquals(1, Bytes.toInt(results[0].getRow()));
145     assertEquals(2, results[0].rawCells().length);
146     assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ1)));
147     assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ2)));
148     assertEquals(2, Bytes.toInt(results[1].getRow()));
149     assertEquals(1, results[1].rawCells().length);
150     assertEquals(2, Bytes.toInt(results[1].getValue(CF, CQ1)));
151 
152     results = resultCache.addAndGet(new Result[0], false);
153     assertEquals(1, results.length);
154     assertEquals(3, Bytes.toInt(results[0].getRow()));
155     assertEquals(1, results[0].rawCells().length);
156     assertEquals(3, Bytes.toInt(results[0].getValue(CF, CQ1)));
157   }
158 
159   @Test
160   public void testCombine4() throws IOException {
161     Result result1 = Result.create(Arrays.asList(createCell(1, CQ1)), null, false, true);
162     Result result2 = Result.create(Arrays.asList(createCell(1, CQ2)), null, false, false);
163     Result nextResult1 = Result.create(Arrays.asList(createCell(2, CQ1)), null, false, true);
164     Result nextResult2 = Result.create(Arrays.asList(createCell(2, CQ2)), null, false, false);
165 
166     assertEquals(0, resultCache.addAndGet(new Result[] { result1 }, false).length);
167 
168     Result[] results = resultCache.addAndGet(new Result[] { result2, nextResult1 }, false);
169     assertEquals(1, results.length);
170     assertEquals(1, Bytes.toInt(results[0].getRow()));
171     assertEquals(2, results[0].rawCells().length);
172     assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ1)));
173     assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ2)));
174 
175     results = resultCache.addAndGet(new Result[] { nextResult2 }, false);
176     assertEquals(1, results.length);
177     assertEquals(2, Bytes.toInt(results[0].getRow()));
178     assertEquals(2, results[0].rawCells().length);
179     assertEquals(2, Bytes.toInt(results[0].getValue(CF, CQ1)));
180     assertEquals(2, Bytes.toInt(results[0].getValue(CF, CQ2)));
181   }
182 }