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.Cell; 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 TestCompleteResultScanResultCache { 038 039 private static byte[] CF = Bytes.toBytes("cf"); 040 041 private static byte[] CQ1 = Bytes.toBytes("cq1"); 042 043 private static byte[] CQ2 = Bytes.toBytes("cq2"); 044 045 private static byte[] CQ3 = Bytes.toBytes("cq3"); 046 047 private CompleteScanResultCache resultCache; 048 049 @BeforeEach 050 public void setUp() { 051 resultCache = new CompleteScanResultCache(); 052 } 053 054 @AfterEach 055 public void tearDown() { 056 resultCache.clear(); 057 resultCache = null; 058 } 059 060 private static Cell createCell(int key, byte[] cq) { 061 return new KeyValue(Bytes.toBytes(key), CF, cq, Bytes.toBytes(key)); 062 } 063 064 @Test 065 public void testNoPartial() throws IOException { 066 assertSame(ScanResultCache.EMPTY_RESULT_ARRAY, 067 resultCache.addAndGet(ScanResultCache.EMPTY_RESULT_ARRAY, false)); 068 assertSame(ScanResultCache.EMPTY_RESULT_ARRAY, 069 resultCache.addAndGet(ScanResultCache.EMPTY_RESULT_ARRAY, true)); 070 int count = 10; 071 Result[] results = new Result[count]; 072 for (int i = 0; i < count; i++) { 073 results[i] = Result.create(Arrays.asList(createCell(i, CQ1))); 074 } 075 assertSame(results, resultCache.addAndGet(results, false)); 076 } 077 078 @Test 079 public void testCombine1() throws IOException { 080 Result previousResult = Result.create(Arrays.asList(createCell(0, CQ1)), null, false, true); 081 Result result1 = Result.create(Arrays.asList(createCell(1, CQ1)), null, false, true); 082 Result result2 = Result.create(Arrays.asList(createCell(1, CQ2)), null, false, true); 083 Result result3 = Result.create(Arrays.asList(createCell(1, CQ3)), null, false, true); 084 Result[] results = resultCache.addAndGet(new Result[] { previousResult, result1 }, false); 085 assertEquals(1, results.length); 086 assertSame(previousResult, results[0]); 087 088 assertEquals(0, resultCache.addAndGet(new Result[] { result2 }, false).length); 089 assertEquals(0, resultCache.addAndGet(new Result[] { result3 }, false).length); 090 assertEquals(0, resultCache.addAndGet(new Result[0], true).length); 091 092 results = resultCache.addAndGet(new Result[0], false); 093 assertEquals(1, results.length); 094 assertEquals(1, Bytes.toInt(results[0].getRow())); 095 assertEquals(3, results[0].rawCells().length); 096 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ1))); 097 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ2))); 098 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ3))); 099 } 100 101 @Test 102 public void testCombine2() throws IOException { 103 Result result1 = Result.create(Arrays.asList(createCell(1, CQ1)), null, false, true); 104 Result result2 = Result.create(Arrays.asList(createCell(1, CQ2)), null, false, true); 105 Result result3 = Result.create(Arrays.asList(createCell(1, CQ3)), null, false, true); 106 Result nextResult1 = Result.create(Arrays.asList(createCell(2, CQ1)), null, false, true); 107 Result nextToNextResult1 = Result.create(Arrays.asList(createCell(3, CQ2)), null, false, false); 108 109 assertEquals(0, resultCache.addAndGet(new Result[] { result1 }, false).length); 110 assertEquals(0, resultCache.addAndGet(new Result[] { result2 }, false).length); 111 assertEquals(0, resultCache.addAndGet(new Result[] { result3 }, false).length); 112 113 Result[] results = resultCache.addAndGet(new Result[] { nextResult1 }, false); 114 assertEquals(1, results.length); 115 assertEquals(1, Bytes.toInt(results[0].getRow())); 116 assertEquals(3, results[0].rawCells().length); 117 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ1))); 118 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ2))); 119 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ3))); 120 121 results = resultCache.addAndGet(new Result[] { nextToNextResult1 }, false); 122 assertEquals(2, results.length); 123 assertEquals(2, Bytes.toInt(results[0].getRow())); 124 assertEquals(1, results[0].rawCells().length); 125 assertEquals(2, Bytes.toInt(results[0].getValue(CF, CQ1))); 126 assertEquals(3, Bytes.toInt(results[1].getRow())); 127 assertEquals(1, results[1].rawCells().length); 128 assertEquals(3, Bytes.toInt(results[1].getValue(CF, CQ2))); 129 } 130 131 @Test 132 public void testCombine3() throws IOException { 133 Result result1 = Result.create(Arrays.asList(createCell(1, CQ1)), null, false, true); 134 Result result2 = Result.create(Arrays.asList(createCell(1, CQ2)), null, false, true); 135 Result nextResult1 = Result.create(Arrays.asList(createCell(2, CQ1)), null, false, false); 136 Result nextToNextResult1 = Result.create(Arrays.asList(createCell(3, CQ1)), null, false, true); 137 138 assertEquals(0, resultCache.addAndGet(new Result[] { result1 }, false).length); 139 assertEquals(0, resultCache.addAndGet(new Result[] { result2 }, false).length); 140 141 Result[] results = 142 resultCache.addAndGet(new Result[] { nextResult1, nextToNextResult1 }, false); 143 assertEquals(2, results.length); 144 assertEquals(1, Bytes.toInt(results[0].getRow())); 145 assertEquals(2, results[0].rawCells().length); 146 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ1))); 147 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ2))); 148 assertEquals(2, Bytes.toInt(results[1].getRow())); 149 assertEquals(1, results[1].rawCells().length); 150 assertEquals(2, Bytes.toInt(results[1].getValue(CF, CQ1))); 151 152 results = resultCache.addAndGet(new Result[0], false); 153 assertEquals(1, results.length); 154 assertEquals(3, Bytes.toInt(results[0].getRow())); 155 assertEquals(1, results[0].rawCells().length); 156 assertEquals(3, Bytes.toInt(results[0].getValue(CF, CQ1))); 157 } 158 159 @Test 160 public void testCombine4() throws IOException { 161 Result result1 = Result.create(Arrays.asList(createCell(1, CQ1)), null, false, true); 162 Result result2 = Result.create(Arrays.asList(createCell(1, CQ2)), null, false, false); 163 Result nextResult1 = Result.create(Arrays.asList(createCell(2, CQ1)), null, false, true); 164 Result nextResult2 = Result.create(Arrays.asList(createCell(2, CQ2)), null, false, false); 165 166 assertEquals(0, resultCache.addAndGet(new Result[] { result1 }, false).length); 167 168 Result[] results = resultCache.addAndGet(new Result[] { result2, nextResult1 }, false); 169 assertEquals(1, results.length); 170 assertEquals(1, Bytes.toInt(results[0].getRow())); 171 assertEquals(2, results[0].rawCells().length); 172 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ1))); 173 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ2))); 174 175 results = resultCache.addAndGet(new Result[] { nextResult2 }, false); 176 assertEquals(1, results.length); 177 assertEquals(2, Bytes.toInt(results[0].getRow())); 178 assertEquals(2, results[0].rawCells().length); 179 assertEquals(2, Bytes.toInt(results[0].getValue(CF, CQ1))); 180 assertEquals(2, Bytes.toInt(results[0].getValue(CF, CQ2))); 181 } 182}