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