001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.filter;
021
022import org.apache.hadoop.hbase.Cell;
023import org.apache.yetus.audience.InterfaceAudience;
024
025import java.io.IOException;
026import java.util.ArrayList;
027import java.util.Collections;
028import java.util.List;
029
030/**
031 * FilterListWithAND represents an ordered list of filters which will be evaluated with an AND
032 * operator.
033 */
034@InterfaceAudience.Private
035public class FilterListWithAND extends FilterListBase {
036
037  private List<Filter> seekHintFilters = new ArrayList<>();
038
039  public FilterListWithAND(List<Filter> filters) {
040    super(filters);
041    // For FilterList with AND, when call FL's transformCell(), we should transform cell for all
042    // sub-filters (because all sub-filters return INCLUDE*). So here, fill this array with true. we
043    // keep this in FilterListWithAND for abstracting the transformCell() in FilterListBase.
044    subFiltersIncludedCell = new ArrayList<>(Collections.nCopies(filters.size(), true));
045  }
046
047  @Override
048  public void addFilterLists(List<Filter> filters) {
049    if (checkAndGetReversed(filters, isReversed()) != isReversed()) {
050      throw new IllegalArgumentException("Filters in the list must have the same reversed flag");
051    }
052    this.filters.addAll(filters);
053    this.subFiltersIncludedCell.addAll(Collections.nCopies(filters.size(), true));
054  }
055
056  @Override
057  protected String formatLogFilters(List<Filter> logFilters) {
058    return String.format("FilterList AND (%d/%d): %s", logFilters.size(), this.size(),
059      logFilters.toString());
060  }
061
062  /**
063   * FilterList with MUST_PASS_ALL choose the maximal forward step among sub-filters in filter list.
064   * Let's call it: The Maximal Step Rule. So if filter-A in filter list return INCLUDE and filter-B
065   * in filter list return INCLUDE_AND_NEXT_COL, then the filter list should return
066   * INCLUDE_AND_NEXT_COL. For SEEK_NEXT_USING_HINT, it's more special, and in method
067   * filterCellWithMustPassAll(), if any sub-filter return SEEK_NEXT_USING_HINT, then our filter
068   * list will return SEEK_NEXT_USING_HINT. so we don't care about the SEEK_NEXT_USING_HINT here.
069   * <br/>
070   * <br/>
071   * The jump step will be:
072   *
073   * <pre>
074   * INCLUDE &lt; SKIP &lt; INCLUDE_AND_NEXT_COL &lt; NEXT_COL &lt; INCLUDE_AND_SEEK_NEXT_ROW &lt; NEXT_ROW &lt; SEEK_NEXT_USING_HINT
075   * </pre>
076   *
077   * Here, we have the following map to describe The Maximal Step Rule. if current return code (for
078   * previous sub-filters in filter list) is <strong>ReturnCode</strong>, and current filter returns
079   * <strong>localRC</strong>, then we should return map[ReturnCode][localRC] for the merged result,
080   * according to The Maximal Step Rule. <br/>
081   *
082   * <pre>
083   * LocalCode\ReturnCode       INCLUDE                    INCLUDE_AND_NEXT_COL      INCLUDE_AND_SEEK_NEXT_ROW  SKIP                  NEXT_COL              NEXT_ROW              SEEK_NEXT_USING_HINT
084   * INCLUDE                    INCLUDE                    INCLUDE_AND_NEXT_COL      INCLUDE_AND_SEEK_NEXT_ROW  SKIP                  NEXT_COL              NEXT_ROW              SEEK_NEXT_USING_HINT
085   * INCLUDE_AND_NEXT_COL       INCLUDE_AND_NEXT_COL       INCLUDE_AND_NEXT_COL      INCLUDE_AND_SEEK_NEXT_ROW  NEXT_COL              NEXT_COL              NEXT_ROW              SEEK_NEXT_USING_HINT
086   * INCLUDE_AND_SEEK_NEXT_ROW  INCLUDE_AND_SEEK_NEXT_ROW  INCLUDE_AND_SEEK_NEXT_ROW INCLUDE_AND_SEEK_NEXT_ROW  NEXT_ROW              NEXT_ROW              NEXT_ROW              SEEK_NEXT_USING_HINT
087   * SKIP                       SKIP                       NEXT_COL                  NEXT_ROW                   SKIP                  NEXT_COL              NEXT_ROW              SEEK_NEXT_USING_HINT
088   * NEXT_COL                   NEXT_COL                   NEXT_COL                  NEXT_ROW                   NEXT_COL              NEXT_COL              NEXT_ROW              SEEK_NEXT_USING_HINT
089   * NEXT_ROW                   NEXT_ROW                   NEXT_ROW                  NEXT_ROW                   NEXT_ROW              NEXT_ROW              NEXT_ROW              SEEK_NEXT_USING_HINT
090   * SEEK_NEXT_USING_HINT       SEEK_NEXT_USING_HINT       SEEK_NEXT_USING_HINT      SEEK_NEXT_USING_HINT       SEEK_NEXT_USING_HINT  SEEK_NEXT_USING_HINT  SEEK_NEXT_USING_HINT  SEEK_NEXT_USING_HINT
091   * </pre>
092   *
093   * @param rc Return code which is calculated by previous sub-filter(s) in filter list.
094   * @param localRC Return code of the current sub-filter in filter list.
095   * @return Return code which is merged by the return code of previous sub-filter(s) and the return
096   *         code of current sub-filter.
097   */
098  private ReturnCode mergeReturnCode(ReturnCode rc, ReturnCode localRC) {
099    if (rc == ReturnCode.SEEK_NEXT_USING_HINT) {
100      return ReturnCode.SEEK_NEXT_USING_HINT;
101    }
102    switch (localRC) {
103      case SEEK_NEXT_USING_HINT:
104        return ReturnCode.SEEK_NEXT_USING_HINT;
105      case INCLUDE:
106        return rc;
107      case INCLUDE_AND_NEXT_COL:
108        if (isInReturnCodes(rc, ReturnCode.INCLUDE, ReturnCode.INCLUDE_AND_NEXT_COL)) {
109          return ReturnCode.INCLUDE_AND_NEXT_COL;
110        }
111        if (isInReturnCodes(rc, ReturnCode.INCLUDE_AND_SEEK_NEXT_ROW)) {
112          return ReturnCode.INCLUDE_AND_SEEK_NEXT_ROW;
113        }
114        if (isInReturnCodes(rc, ReturnCode.SKIP, ReturnCode.NEXT_COL)) {
115          return ReturnCode.NEXT_COL;
116        }
117        if (isInReturnCodes(rc, ReturnCode.NEXT_ROW)) {
118          return ReturnCode.NEXT_ROW;
119        }
120        break;
121      case INCLUDE_AND_SEEK_NEXT_ROW:
122        if (isInReturnCodes(rc, ReturnCode.INCLUDE, ReturnCode.INCLUDE_AND_NEXT_COL,
123          ReturnCode.INCLUDE_AND_SEEK_NEXT_ROW)) {
124          return ReturnCode.INCLUDE_AND_SEEK_NEXT_ROW;
125        }
126        if (isInReturnCodes(rc, ReturnCode.SKIP, ReturnCode.NEXT_COL, ReturnCode.NEXT_ROW)) {
127          return ReturnCode.NEXT_ROW;
128        }
129        break;
130      case SKIP:
131        if (isInReturnCodes(rc, ReturnCode.INCLUDE, ReturnCode.SKIP)) {
132          return ReturnCode.SKIP;
133        }
134        if (isInReturnCodes(rc, ReturnCode.INCLUDE_AND_NEXT_COL, ReturnCode.NEXT_COL)) {
135          return ReturnCode.NEXT_COL;
136        }
137        if (isInReturnCodes(rc, ReturnCode.INCLUDE_AND_SEEK_NEXT_ROW, ReturnCode.NEXT_ROW)) {
138          return ReturnCode.NEXT_ROW;
139        }
140        break;
141      case NEXT_COL:
142        if (isInReturnCodes(rc, ReturnCode.INCLUDE, ReturnCode.INCLUDE_AND_NEXT_COL, ReturnCode.SKIP,
143          ReturnCode.NEXT_COL)) {
144          return ReturnCode.NEXT_COL;
145        }
146        if (isInReturnCodes(rc, ReturnCode.INCLUDE_AND_SEEK_NEXT_ROW, ReturnCode.NEXT_ROW)) {
147          return ReturnCode.NEXT_ROW;
148        }
149        break;
150      case NEXT_ROW:
151        return ReturnCode.NEXT_ROW;
152    }
153    throw new IllegalStateException(
154        "Received code is not valid. rc: " + rc + ", localRC: " + localRC);
155  }
156
157  @Override
158  public ReturnCode filterCell(Cell c) throws IOException {
159    if (isEmpty()) {
160      return ReturnCode.INCLUDE;
161    }
162    ReturnCode rc = ReturnCode.INCLUDE;
163    this.seekHintFilters.clear();
164    for (int i = 0, n = filters.size(); i < n; i++) {
165      Filter filter = filters.get(i);
166      if (filter.filterAllRemaining()) {
167        return ReturnCode.NEXT_ROW;
168      }
169      ReturnCode localRC;
170      localRC = filter.filterCell(c);
171      rc = mergeReturnCode(rc, localRC);
172
173      if (localRC == ReturnCode.SEEK_NEXT_USING_HINT) {
174        seekHintFilters.add(filter);
175      }
176    }
177    if (!seekHintFilters.isEmpty()) {
178      return ReturnCode.SEEK_NEXT_USING_HINT;
179    }
180    return rc;
181  }
182
183  @Override
184  public void reset() throws IOException {
185    for (int i = 0, n = filters.size(); i < n; i++) {
186      filters.get(i).reset();
187    }
188    seekHintFilters.clear();
189  }
190
191  @Override
192  public boolean filterRowKey(byte[] rowKey, int offset, int length) throws IOException {
193    if (isEmpty()) {
194      return super.filterRowKey(rowKey, offset, length);
195    }
196    boolean retVal = false;
197    for (int i = 0, n = filters.size(); i < n; i++) {
198      Filter filter = filters.get(i);
199      if (filter.filterAllRemaining() || filter.filterRowKey(rowKey, offset, length)) {
200        retVal = true;
201      }
202    }
203    return retVal;
204  }
205
206  @Override
207  public boolean filterRowKey(Cell firstRowCell) throws IOException {
208    if (isEmpty()) {
209      return super.filterRowKey(firstRowCell);
210    }
211    boolean retVal = false;
212    for (int i = 0, n = filters.size(); i < n; i++) {
213      Filter filter = filters.get(i);
214      if (filter.filterAllRemaining() || filter.filterRowKey(firstRowCell)) {
215        // Can't just return true here, because there are some filters (such as PrefixFilter) which
216        // will catch the row changed event by filterRowKey(). If we return early here, those
217        // filters will have no chance to update their row state.
218        retVal = true;
219      }
220    }
221    return retVal;
222  }
223
224  @Override
225  public boolean filterAllRemaining() throws IOException {
226    if (isEmpty()) {
227      return super.filterAllRemaining();
228    }
229    for (int i = 0, n = filters.size(); i < n; i++) {
230      if (filters.get(i).filterAllRemaining()) {
231        return true;
232      }
233    }
234    return false;
235  }
236
237  @Override
238  public boolean filterRow() throws IOException {
239    if (isEmpty()) {
240      return super.filterRow();
241    }
242    for (int i = 0, n = filters.size(); i < n; i++) {
243      Filter filter = filters.get(i);
244      if (filter.filterRow()) {
245        return true;
246      }
247    }
248    return false;
249  }
250
251  @Override
252  public Cell getNextCellHint(Cell currentCell) throws IOException {
253    if (isEmpty()) {
254      return super.getNextCellHint(currentCell);
255    }
256    Cell maxHint = null;
257    for (Filter filter : seekHintFilters) {
258      if (filter.filterAllRemaining()) {
259        continue;
260      }
261      Cell curKeyHint = filter.getNextCellHint(currentCell);
262      if (maxHint == null) {
263        maxHint = curKeyHint;
264        continue;
265      }
266      if (this.compareCell(maxHint, curKeyHint) < 0) {
267        maxHint = curKeyHint;
268      }
269    }
270    return maxHint;
271  }
272}