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 TestBatchScanResultCache {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042      HBaseClassTestRule.forClass(TestBatchScanResultCache.class);
043
044  private static byte[] CF = Bytes.toBytes("cf");
045
046  private BatchScanResultCache resultCache;
047
048  @Before
049  public void setUp() {
050    resultCache = new BatchScanResultCache(4);
051  }
052
053  @After
054  public void tearDown() {
055    resultCache.clear();
056    resultCache = null;
057  }
058
059  static Cell createCell(byte[] cf, int key, int cq) {
060    return new KeyValue(Bytes.toBytes(key), cf, Bytes.toBytes("cq" + cq), Bytes.toBytes(key));
061  }
062
063  static Cell[] createCells(byte[] cf, int key, int numCqs) {
064    Cell[] cells = new Cell[numCqs];
065    for (int i = 0; i < numCqs; i++) {
066      cells[i] = createCell(cf, key, i);
067    }
068    return cells;
069  }
070
071  private void assertResultEquals(Result result, int key, int start, int to) {
072    assertEquals(to - start, result.size());
073    for (int i = start; i < to; i++) {
074      assertEquals(key, Bytes.toInt(result.getValue(CF, Bytes.toBytes("cq" + i))));
075    }
076    assertEquals(to - start == 4, result.mayHaveMoreCellsInRow());
077  }
078
079  @Test
080  public void test() throws IOException {
081    assertSame(ScanResultCache.EMPTY_RESULT_ARRAY,
082      resultCache.addAndGet(ScanResultCache.EMPTY_RESULT_ARRAY, false));
083    assertSame(ScanResultCache.EMPTY_RESULT_ARRAY,
084      resultCache.addAndGet(ScanResultCache.EMPTY_RESULT_ARRAY, true));
085
086    Cell[] cells1 = createCells(CF, 1, 10);
087    Cell[] cells2 = createCells(CF, 2, 10);
088    Cell[] cells3 = createCells(CF, 3, 10);
089    assertEquals(0, resultCache.addAndGet(
090      new Result[] { Result.create(Arrays.copyOf(cells1, 3), null, false, true) }, false).length);
091    Result[] results = resultCache.addAndGet(
092      new Result[] { Result.create(Arrays.copyOfRange(cells1, 3, 7), null, false, true),
093          Result.create(Arrays.copyOfRange(cells1, 7, 10), null, false, true) },
094      false);
095    assertEquals(2, results.length);
096    assertResultEquals(results[0], 1, 0, 4);
097    assertResultEquals(results[1], 1, 4, 8);
098    results = resultCache.addAndGet(ScanResultCache.EMPTY_RESULT_ARRAY, false);
099    assertEquals(1, results.length);
100    assertResultEquals(results[0], 1, 8, 10);
101
102    results = resultCache.addAndGet(
103      new Result[] { Result.create(Arrays.copyOfRange(cells2, 0, 4), null, false, true),
104          Result.create(Arrays.copyOfRange(cells2, 4, 8), null, false, true),
105          Result.create(Arrays.copyOfRange(cells2, 8, 10), null, false, true),
106          Result.create(Arrays.copyOfRange(cells3, 0, 4), null, false, true),
107          Result.create(Arrays.copyOfRange(cells3, 4, 8), null, false, true),
108          Result.create(Arrays.copyOfRange(cells3, 8, 10), null, false, false) },
109      false);
110    assertEquals(6, results.length);
111    assertResultEquals(results[0], 2, 0, 4);
112    assertResultEquals(results[1], 2, 4, 8);
113    assertResultEquals(results[2], 2, 8, 10);
114    assertResultEquals(results[3], 3, 0, 4);
115    assertResultEquals(results[4], 3, 4, 8);
116    assertResultEquals(results[5], 3, 8, 10);
117  }
118}