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.jupiter.api.Assertions.assertEquals;
022import static org.junit.jupiter.api.Assertions.assertSame;
023
024import java.io.IOException;
025import java.util.Arrays;
026import org.apache.hadoop.hbase.ExtendedCell;
027import org.apache.hadoop.hbase.testclassification.ClientTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.junit.jupiter.api.AfterEach;
031import org.junit.jupiter.api.BeforeEach;
032import org.junit.jupiter.api.Tag;
033import org.junit.jupiter.api.Test;
034
035@Tag(SmallTests.TAG)
036@Tag(ClientTests.TAG)
037public class TestAllowPartialScanResultCache {
038
039  private static byte[] CF = Bytes.toBytes("cf");
040
041  private AllowPartialScanResultCache resultCache;
042
043  @BeforeEach
044  public void setUp() {
045    resultCache = new AllowPartialScanResultCache();
046  }
047
048  @AfterEach
049  public void tearDown() {
050    resultCache.clear();
051    resultCache = null;
052  }
053
054  @Test
055  public void test() throws IOException {
056    assertSame(ScanResultCache.EMPTY_RESULT_ARRAY,
057      resultCache.addAndGet(ScanResultCache.EMPTY_RESULT_ARRAY, false));
058    assertSame(ScanResultCache.EMPTY_RESULT_ARRAY,
059      resultCache.addAndGet(ScanResultCache.EMPTY_RESULT_ARRAY, true));
060
061    ExtendedCell[] cells1 = createCells(CF, 1, 10);
062    ExtendedCell[] cells2 = createCells(CF, 2, 10);
063
064    Result[] results1 = resultCache.addAndGet(
065      new Result[] { Result.create(Arrays.copyOf(cells1, 5), null, false, true) }, false);
066    assertEquals(1, results1.length);
067    assertEquals(1, Bytes.toInt(results1[0].getRow()));
068    assertEquals(5, results1[0].rawCells().length);
069    for (int i = 0; i < 5; i++) {
070      assertEquals(1, Bytes.toInt(results1[0].getValue(CF, Bytes.toBytes("cq" + i))));
071    }
072
073    Result[] results2 = resultCache.addAndGet(
074      new Result[] { Result.create(Arrays.copyOfRange(cells1, 1, 10), null, false, true) }, false);
075    assertEquals(1, results2.length);
076    assertEquals(1, Bytes.toInt(results2[0].getRow()));
077    assertEquals(5, results2[0].rawCells().length);
078    for (int i = 5; i < 10; i++) {
079      assertEquals(1, Bytes.toInt(results2[0].getValue(CF, Bytes.toBytes("cq" + i))));
080    }
081
082    Result[] results3 =
083      resultCache.addAndGet(new Result[] { Result.create(cells1), Result.create(cells2) }, false);
084    assertEquals(1, results3.length);
085    assertEquals(2, Bytes.toInt(results3[0].getRow()));
086    assertEquals(10, results3[0].rawCells().length);
087    for (int i = 0; i < 10; i++) {
088      assertEquals(2, Bytes.toInt(results3[0].getValue(CF, Bytes.toBytes("cq" + i))));
089    }
090  }
091}