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.apache.hadoop.hbase.client.TestBatchScanResultCache.createCells;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertSame;
023
024import java.io.IOException;
025import java.util.Arrays;
026import org.apache.hadoop.hbase.ExtendedCell;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
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 TestAllowPartialScanResultCache {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestAllowPartialScanResultCache.class);
043
044  private static byte[] CF = Bytes.toBytes("cf");
045
046  private AllowPartialScanResultCache resultCache;
047
048  @Before
049  public void setUp() {
050    resultCache = new AllowPartialScanResultCache();
051  }
052
053  @After
054  public void tearDown() {
055    resultCache.clear();
056    resultCache = null;
057  }
058
059  @Test
060  public void test() throws IOException {
061    assertSame(ScanResultCache.EMPTY_RESULT_ARRAY,
062      resultCache.addAndGet(ScanResultCache.EMPTY_RESULT_ARRAY, false));
063    assertSame(ScanResultCache.EMPTY_RESULT_ARRAY,
064      resultCache.addAndGet(ScanResultCache.EMPTY_RESULT_ARRAY, true));
065
066    ExtendedCell[] cells1 = createCells(CF, 1, 10);
067    ExtendedCell[] cells2 = createCells(CF, 2, 10);
068
069    Result[] results1 = resultCache.addAndGet(
070      new Result[] { Result.create(Arrays.copyOf(cells1, 5), null, false, true) }, false);
071    assertEquals(1, results1.length);
072    assertEquals(1, Bytes.toInt(results1[0].getRow()));
073    assertEquals(5, results1[0].rawCells().length);
074    for (int i = 0; i < 5; i++) {
075      assertEquals(1, Bytes.toInt(results1[0].getValue(CF, Bytes.toBytes("cq" + i))));
076    }
077
078    Result[] results2 = resultCache.addAndGet(
079      new Result[] { Result.create(Arrays.copyOfRange(cells1, 1, 10), null, false, true) }, false);
080    assertEquals(1, results2.length);
081    assertEquals(1, Bytes.toInt(results2[0].getRow()));
082    assertEquals(5, results2[0].rawCells().length);
083    for (int i = 5; i < 10; i++) {
084      assertEquals(1, Bytes.toInt(results2[0].getValue(CF, Bytes.toBytes("cq" + i))));
085    }
086
087    Result[] results3 =
088      resultCache.addAndGet(new Result[] { Result.create(cells1), Result.create(cells2) }, false);
089    assertEquals(1, results3.length);
090    assertEquals(2, Bytes.toInt(results3[0].getRow()));
091    assertEquals(10, results3[0].rawCells().length);
092    for (int i = 0; i < 10; i++) {
093      assertEquals(2, Bytes.toInt(results3[0].getValue(CF, Bytes.toBytes("cq" + i))));
094    }
095  }
096}