001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertSame;
022
023import java.io.IOException;
024import java.util.Arrays;
025import org.apache.hadoop.hbase.Cell;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.KeyValue;
028import org.apache.hadoop.hbase.testclassification.ClientTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.junit.After;
032import org.junit.Before;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036
037@Category({ SmallTests.class, ClientTests.class })
038public class TestCompleteResultScanResultCache {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestCompleteResultScanResultCache.class);
043
044  private static byte[] CF = Bytes.toBytes("cf");
045
046  private static byte[] CQ1 = Bytes.toBytes("cq1");
047
048  private static byte[] CQ2 = Bytes.toBytes("cq2");
049
050  private static byte[] CQ3 = Bytes.toBytes("cq3");
051
052  private CompleteScanResultCache resultCache;
053
054  @Before
055  public void setUp() {
056    resultCache = new CompleteScanResultCache();
057  }
058
059  @After
060  public void tearDown() {
061    resultCache.clear();
062    resultCache = null;
063  }
064
065  private static Cell createCell(int key, byte[] cq) {
066    return new KeyValue(Bytes.toBytes(key), CF, cq, Bytes.toBytes(key));
067  }
068
069  @Test
070  public void testNoPartial() throws IOException {
071    assertSame(ScanResultCache.EMPTY_RESULT_ARRAY,
072      resultCache.addAndGet(ScanResultCache.EMPTY_RESULT_ARRAY, false));
073    assertSame(ScanResultCache.EMPTY_RESULT_ARRAY,
074      resultCache.addAndGet(ScanResultCache.EMPTY_RESULT_ARRAY, true));
075    int count = 10;
076    Result[] results = new Result[count];
077    for (int i = 0; i < count; i++) {
078      results[i] = Result.create(Arrays.asList(createCell(i, CQ1)));
079    }
080    assertSame(results, resultCache.addAndGet(results, false));
081  }
082
083  @Test
084  public void testCombine1() throws IOException {
085    Result previousResult = Result.create(Arrays.asList(createCell(0, CQ1)), null, false, true);
086    Result result1 = Result.create(Arrays.asList(createCell(1, CQ1)), null, false, true);
087    Result result2 = Result.create(Arrays.asList(createCell(1, CQ2)), null, false, true);
088    Result result3 = Result.create(Arrays.asList(createCell(1, CQ3)), null, false, true);
089    Result[] results = resultCache.addAndGet(new Result[] { previousResult, result1 }, false);
090    assertEquals(1, results.length);
091    assertSame(previousResult, results[0]);
092
093    assertEquals(0, resultCache.addAndGet(new Result[] { result2 }, false).length);
094    assertEquals(0, resultCache.addAndGet(new Result[] { result3 }, false).length);
095    assertEquals(0, resultCache.addAndGet(new Result[0], true).length);
096
097    results = resultCache.addAndGet(new Result[0], false);
098    assertEquals(1, results.length);
099    assertEquals(1, Bytes.toInt(results[0].getRow()));
100    assertEquals(3, results[0].rawCells().length);
101    assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ1)));
102    assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ2)));
103    assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ3)));
104  }
105
106  @Test
107  public void testCombine2() throws IOException {
108    Result result1 = Result.create(Arrays.asList(createCell(1, CQ1)), null, false, true);
109    Result result2 = Result.create(Arrays.asList(createCell(1, CQ2)), null, false, true);
110    Result result3 = Result.create(Arrays.asList(createCell(1, CQ3)), null, false, true);
111    Result nextResult1 = Result.create(Arrays.asList(createCell(2, CQ1)), null, false, true);
112    Result nextToNextResult1 = Result.create(Arrays.asList(createCell(3, CQ2)), null, false, false);
113
114    assertEquals(0, resultCache.addAndGet(new Result[] { result1 }, false).length);
115    assertEquals(0, resultCache.addAndGet(new Result[] { result2 }, false).length);
116    assertEquals(0, resultCache.addAndGet(new Result[] { result3 }, false).length);
117
118    Result[] results = resultCache.addAndGet(new Result[] { nextResult1 }, false);
119    assertEquals(1, results.length);
120    assertEquals(1, Bytes.toInt(results[0].getRow()));
121    assertEquals(3, results[0].rawCells().length);
122    assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ1)));
123    assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ2)));
124    assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ3)));
125
126    results = resultCache.addAndGet(new Result[] { nextToNextResult1 }, false);
127    assertEquals(2, results.length);
128    assertEquals(2, Bytes.toInt(results[0].getRow()));
129    assertEquals(1, results[0].rawCells().length);
130    assertEquals(2, Bytes.toInt(results[0].getValue(CF, CQ1)));
131    assertEquals(3, Bytes.toInt(results[1].getRow()));
132    assertEquals(1, results[1].rawCells().length);
133    assertEquals(3, Bytes.toInt(results[1].getValue(CF, CQ2)));
134  }
135
136  @Test
137  public void testCombine3() throws IOException {
138    Result result1 = Result.create(Arrays.asList(createCell(1, CQ1)), null, false, true);
139    Result result2 = Result.create(Arrays.asList(createCell(1, CQ2)), null, false, true);
140    Result nextResult1 = Result.create(Arrays.asList(createCell(2, CQ1)), null, false, false);
141    Result nextToNextResult1 = Result.create(Arrays.asList(createCell(3, CQ1)), null, false, true);
142
143    assertEquals(0, resultCache.addAndGet(new Result[] { result1 }, false).length);
144    assertEquals(0, resultCache.addAndGet(new Result[] { result2 }, false).length);
145
146    Result[] results =
147      resultCache.addAndGet(new Result[] { nextResult1, nextToNextResult1 }, false);
148    assertEquals(2, results.length);
149    assertEquals(1, Bytes.toInt(results[0].getRow()));
150    assertEquals(2, results[0].rawCells().length);
151    assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ1)));
152    assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ2)));
153    assertEquals(2, Bytes.toInt(results[1].getRow()));
154    assertEquals(1, results[1].rawCells().length);
155    assertEquals(2, Bytes.toInt(results[1].getValue(CF, CQ1)));
156
157    results = resultCache.addAndGet(new Result[0], false);
158    assertEquals(1, results.length);
159    assertEquals(3, Bytes.toInt(results[0].getRow()));
160    assertEquals(1, results[0].rawCells().length);
161    assertEquals(3, Bytes.toInt(results[0].getValue(CF, CQ1)));
162  }
163
164  @Test
165  public void testCombine4() throws IOException {
166    Result result1 = Result.create(Arrays.asList(createCell(1, CQ1)), null, false, true);
167    Result result2 = Result.create(Arrays.asList(createCell(1, CQ2)), null, false, false);
168    Result nextResult1 = Result.create(Arrays.asList(createCell(2, CQ1)), null, false, true);
169    Result nextResult2 = Result.create(Arrays.asList(createCell(2, CQ2)), null, false, false);
170
171    assertEquals(0, resultCache.addAndGet(new Result[] { result1 }, false).length);
172
173    Result[] results = resultCache.addAndGet(new Result[] { result2, nextResult1 }, false);
174    assertEquals(1, results.length);
175    assertEquals(1, Bytes.toInt(results[0].getRow()));
176    assertEquals(2, results[0].rawCells().length);
177    assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ1)));
178    assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ2)));
179
180    results = resultCache.addAndGet(new Result[] { nextResult2 }, false);
181    assertEquals(1, results.length);
182    assertEquals(2, Bytes.toInt(results[0].getRow()));
183    assertEquals(2, results[0].rawCells().length);
184    assertEquals(2, Bytes.toInt(results[0].getValue(CF, CQ1)));
185    assertEquals(2, Bytes.toInt(results[0].getValue(CF, CQ2)));
186  }
187}