1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.hadoop.hbase.client;
19
20 import java.io.IOException;
21 import java.util.List;
22
23 import org.apache.hadoop.hbase.Cell;
24 import org.apache.hadoop.hbase.CellUtil;
25 import org.apache.hadoop.hbase.classification.InterfaceAudience;
26
27 /**
28 * Used to separate the row constructing logic.
29 * <p>
30 * After we add heartbeat support for scan, RS may return partial result even if allowPartial is
31 * false and batch is 0. With this interface, the implementation now looks like:
32 * <ol>
33 * <li>Get results from ScanResponse proto.</li>
34 * <li>Pass them to ScanResultCache and get something back.</li>
35 * <li>If we actually get something back, then pass it to ScanConsumer.</li>
36 * </ol>
37 */
38 @InterfaceAudience.Private
39 public abstract class ScanResultCache {
40
41 static final Result[] EMPTY_RESULT_ARRAY = new Result[0];
42 int numberOfCompleteRows;
43 long resultSize = 0;
44 int count = 0;
45 Result lastResult = null;
46 List<Result> cache;
47
48 ScanResultCache(List<Result> cache) {
49 this.cache = cache;
50 }
51
52 /**
53 * Process the results from the server and load it to cache.
54 * @param results the results of a scan next. Must not be null.
55 * @param isHeartbeatMessage indicate whether the results is gotten from a heartbeat response.
56 */
57 abstract void loadResultsToCache(Result[] results, boolean isHeartbeatMessage)
58 throws IOException;
59
60 /**
61 * Clear the cached result if any. Called when scan error and we will start from a start of a row
62 * again.
63 */
64 void clear() {
65 resetCount();
66 resetResultSize();
67 lastResult = null;
68 }
69
70 /**
71 * Return the number of complete rows. Used to implement limited scan.
72 */
73 int numberOfCompleteRows() {
74 return numberOfCompleteRows;
75 }
76
77 /**
78 * Add result array received from server to cache
79 * @param resultsToAddToCache The array of Results returned from the server
80 * @param start start index to cache from Results array
81 * @param end last index to cache from Results array
82 */
83 void addResultArrayToCache(Result[] resultsToAddToCache, int start, int end) {
84 if (resultsToAddToCache != null) {
85 for (int r = start; r < end; r++) {
86 checkUpdateNumberOfCompleteRowsAndCache(resultsToAddToCache[r]);
87 }
88 }
89 }
90
91 /**
92 * Check and update number of complete rows and add result to cache
93 * @param rs Result to cache from Results array or constructed from partial results
94 */
95 abstract void checkUpdateNumberOfCompleteRowsAndCache(Result rs);
96
97 /**
98 * Add the result received from server or result constructed from partials to cache
99 * @param rs Result to cache from Results array or constructed from partial results
100 */
101 void addResultToCache(Result rs) {
102 cache.add(rs);
103 for (Cell cell : rs.rawCells()) {
104 resultSize += CellUtil.estimatedHeapSizeOf(cell);
105 }
106 count++;
107 lastResult = rs;
108 }
109
110 long getResultSize() {
111 return resultSize;
112 }
113
114 int getCount() {
115 return count;
116 }
117
118 void resetResultSize() {
119 resultSize = 0;
120 }
121
122 void resetCount() {
123 count = 0;
124 }
125
126 Result getLastResult() {
127 return lastResult;
128 }
129 }