View Javadoc

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.filter;
19  
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.Comparator;
24  import java.util.List;
25  
26  import org.apache.hadoop.hbase.Cell;
27  import org.apache.hadoop.hbase.KeyValue;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.classification.InterfaceStability;
30  import org.apache.hadoop.hbase.exceptions.DeserializationException;
31  import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
32  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.BytesBytesPair;
33  import org.apache.hadoop.hbase.util.ByteStringer;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.apache.hadoop.hbase.util.Pair;
36  import org.apache.hadoop.hbase.util.UnsafeAccess;
37  
38  import com.google.common.annotations.VisibleForTesting;
39  import com.google.protobuf.InvalidProtocolBufferException;
40  
41  /**
42   * This is optimized version of a standard FuzzyRowFilter Filters data based on fuzzy row key.
43   * Performs fast-forwards during scanning. It takes pairs (row key, fuzzy info) to match row keys.
44   * Where fuzzy info is a byte array with 0 or 1 as its values:
45   * <ul>
46   * <li>0 - means that this byte in provided row key is fixed, i.e. row key's byte at same position
47   * must match</li>
48   * <li>1 - means that this byte in provided row key is NOT fixed, i.e. row key's byte at this
49   * position can be different from the one in provided row key</li>
50   * </ul>
51   * Example: Let's assume row key format is userId_actionId_year_month. Length of userId is fixed and
52   * is 4, length of actionId is 2 and year and month are 4 and 2 bytes long respectively. Let's
53   * assume that we need to fetch all users that performed certain action (encoded as "99") in Jan of
54   * any year. Then the pair (row key, fuzzy info) would be the following: row key = "????_99_????_01"
55   * (one can use any value instead of "?") fuzzy info =
56   * "\x01\x01\x01\x01\x00\x00\x00\x00\x01\x01\x01\x01\x00\x00\x00" I.e. fuzzy info tells the matching
57   * mask is "????_99_????_01", where at ? can be any value.
58   */
59  @InterfaceAudience.Public
60  @InterfaceStability.Evolving
61  public class FuzzyRowFilter extends FilterBase {
62    private List<Pair<byte[], byte[]>> fuzzyKeysData;
63    private boolean done = false;
64  
65    /**
66     * The index of a last successfully found matching fuzzy string (in fuzzyKeysData). We will start
67     * matching next KV with this one. If they do not match then we will return back to the one-by-one
68     * iteration over fuzzyKeysData.
69     */
70    private int lastFoundIndex = -1;
71  
72    /**
73     * Row tracker (keeps all next rows after SEEK_NEXT_USING_HINT was returned)
74     */
75    private RowTracker tracker;
76  
77    public FuzzyRowFilter(List<Pair<byte[], byte[]>> fuzzyKeysData) {
78      Pair<byte[], byte[]> p;
79      for (int i = 0; i < fuzzyKeysData.size(); i++) {
80        p = fuzzyKeysData.get(i);
81        if (p.getFirst().length != p.getSecond().length) {
82          Pair<String, String> readable =
83              new Pair<String, String>(Bytes.toStringBinary(p.getFirst()), Bytes.toStringBinary(p
84                  .getSecond()));
85          throw new IllegalArgumentException("Fuzzy pair lengths do not match: " + readable);
86        }
87        // update mask ( 0 -> -1 (0xff), 1 -> 0)
88        p.setSecond(preprocessMask(p.getSecond()));
89        preprocessSearchKey(p);
90      }
91      this.fuzzyKeysData = fuzzyKeysData;
92      this.tracker = new RowTracker();
93    }
94  
95    private void preprocessSearchKey(Pair<byte[], byte[]> p) {
96      if (UnsafeAccess.isAvailable() == false) {
97        return;
98      }
99      byte[] key = p.getFirst();
100     byte[] mask = p.getSecond();
101     for (int i = 0; i < mask.length; i++) {
102       // set non-fixed part of a search key to 0.
103       if (mask[i] == 0) key[i] = 0;
104     }
105   }
106 
107   /**
108    * We need to preprocess mask array, as since we treat 0's as unfixed positions and -1 (0xff) as
109    * fixed positions
110    * @param mask
111    * @return mask array
112    */
113   private byte[] preprocessMask(byte[] mask) {
114     if (UnsafeAccess.isAvailable() == false) {
115       return mask;
116     }
117     if (isPreprocessedMask(mask)) return mask;
118     for (int i = 0; i < mask.length; i++) {
119       if (mask[i] == 0) {
120         mask[i] = -1; // 0 -> -1
121       } else if (mask[i] == 1) {
122         mask[i] = 0;// 1 -> 0
123       }
124     }
125     return mask;
126   }
127 
128   private boolean isPreprocessedMask(byte[] mask) {
129     for (int i = 0; i < mask.length; i++) {
130       if (mask[i] != -1 && mask[i] != 0) {
131         return false;
132       }
133     }
134     return true;
135   }
136 
137   @Override
138   public ReturnCode filterKeyValue(Cell c) {
139     final int startIndex = lastFoundIndex >= 0 ? lastFoundIndex : 0;
140     final int size = fuzzyKeysData.size();
141     for (int i = startIndex; i < size + startIndex; i++) {
142       final int index = i % size;
143       Pair<byte[], byte[]> fuzzyData = fuzzyKeysData.get(index);
144       SatisfiesCode satisfiesCode =
145           satisfies(isReversed(), c.getRowArray(), c.getRowOffset(), c.getRowLength(),
146             fuzzyData.getFirst(), fuzzyData.getSecond());
147       if (satisfiesCode == SatisfiesCode.YES) {
148         lastFoundIndex = index;
149         return ReturnCode.INCLUDE;
150       }
151     }
152     // NOT FOUND -> seek next using hint
153     lastFoundIndex = -1;
154     return ReturnCode.SEEK_NEXT_USING_HINT;
155 
156   }
157 
158   @Override
159   public Cell getNextCellHint(Cell currentCell) {
160     boolean result = true;
161     if (tracker.needsUpdate()) {
162       result = tracker.updateTracker(currentCell);
163     }
164     if (result == false) {
165       done = true;
166       return null;
167     }
168     byte[] nextRowKey = tracker.nextRow();
169     // We need to compare nextRowKey with currentCell
170     int compareResult =
171         Bytes.compareTo(nextRowKey, 0, nextRowKey.length, currentCell.getRowArray(),
172           currentCell.getRowOffset(), currentCell.getRowLength());
173     if ((reversed && compareResult > 0) || (!reversed && compareResult < 0)) {
174       // This can happen when we have multilpe filters and some other filter
175       // returns next row with hint which is larger (smaller for reverse)
176       // than the current (really?)
177       result = tracker.updateTracker(currentCell);
178       if (result == false) {
179         done = true;
180         return null;
181       } else {
182         nextRowKey = tracker.nextRow();
183       }
184     }
185     return KeyValue.createFirstOnRow(nextRowKey);
186   }
187 
188   /**
189    * If we have multiple fuzzy keys, row tracker should improve overall performance It calculates
190    * all next rows (one per every fuzzy key), sort them accordingly (ascending for regular and
191    * descending for reverse). Next time getNextCellHint is called we check row tracker first and
192    * return next row from the tracker if it exists, if there are no rows in the tracker we update
193    * tracker with a current cell and return first row.
194    */
195   private class RowTracker {
196     private final List<byte[]> nextRows;
197     private int next = -1;
198 
199     RowTracker() {
200       nextRows = new ArrayList<byte[]>();
201     }
202 
203     boolean needsUpdate() {
204       return next == -1 || next == nextRows.size();
205     }
206 
207     byte[] nextRow() {
208       if (next < 0 || next == nextRows.size()) return null;
209       return nextRows.get(next++);
210     }
211 
212     boolean updateTracker(Cell currentCell) {
213       nextRows.clear();
214       for (Pair<byte[], byte[]> fuzzyData : fuzzyKeysData) {
215         byte[] nextRowKeyCandidate =
216             getNextForFuzzyRule(isReversed(), currentCell.getRowArray(),
217               currentCell.getRowOffset(), currentCell.getRowLength(), fuzzyData.getFirst(),
218               fuzzyData.getSecond());
219         if (nextRowKeyCandidate == null) {
220           continue;
221         }
222         nextRows.add(nextRowKeyCandidate);
223       }
224       // Sort all next row candidates
225       Collections.sort(nextRows, new Comparator<byte[]>() {
226         @Override
227         public int compare(byte[] o1, byte[] o2) {
228           if (reversed) {
229             return -Bytes.compareTo(o1, o2);
230           } else {
231             return Bytes.compareTo(o1, o2);
232           }
233         }
234       });
235       next = 0;
236       return nextRows.size() > 0;
237     }
238 
239   }
240 
241   @Override
242   public boolean filterAllRemaining() {
243     return done;
244   }
245 
246   /**
247    * @return The filter serialized using pb
248    */
249   public byte[] toByteArray() {
250     FilterProtos.FuzzyRowFilter.Builder builder = FilterProtos.FuzzyRowFilter.newBuilder();
251     for (Pair<byte[], byte[]> fuzzyData : fuzzyKeysData) {
252       BytesBytesPair.Builder bbpBuilder = BytesBytesPair.newBuilder();
253       bbpBuilder.setFirst(ByteStringer.wrap(fuzzyData.getFirst()));
254       bbpBuilder.setSecond(ByteStringer.wrap(fuzzyData.getSecond()));
255       builder.addFuzzyKeysData(bbpBuilder);
256     }
257     return builder.build().toByteArray();
258   }
259 
260   /**
261    * @param pbBytes A pb serialized {@link FuzzyRowFilter} instance
262    * @return An instance of {@link FuzzyRowFilter} made from <code>bytes</code>
263    * @throws DeserializationException
264    * @see #toByteArray
265    */
266   public static FuzzyRowFilter parseFrom(final byte[] pbBytes) throws DeserializationException {
267     FilterProtos.FuzzyRowFilter proto;
268     try {
269       proto = FilterProtos.FuzzyRowFilter.parseFrom(pbBytes);
270     } catch (InvalidProtocolBufferException e) {
271       throw new DeserializationException(e);
272     }
273     int count = proto.getFuzzyKeysDataCount();
274     ArrayList<Pair<byte[], byte[]>> fuzzyKeysData = new ArrayList<Pair<byte[], byte[]>>(count);
275     for (int i = 0; i < count; ++i) {
276       BytesBytesPair current = proto.getFuzzyKeysData(i);
277       byte[] keyBytes = current.getFirst().toByteArray();
278       byte[] keyMeta = current.getSecond().toByteArray();
279       fuzzyKeysData.add(new Pair<byte[], byte[]>(keyBytes, keyMeta));
280     }
281     return new FuzzyRowFilter(fuzzyKeysData);
282   }
283 
284   @Override
285   public String toString() {
286     final StringBuilder sb = new StringBuilder();
287     sb.append("FuzzyRowFilter");
288     sb.append("{fuzzyKeysData=");
289     for (Pair<byte[], byte[]> fuzzyData : fuzzyKeysData) {
290       sb.append('{').append(Bytes.toStringBinary(fuzzyData.getFirst())).append(":");
291       sb.append(Bytes.toStringBinary(fuzzyData.getSecond())).append('}');
292     }
293     sb.append("}, ");
294     return sb.toString();
295   }
296 
297   // Utility methods
298 
299   static enum SatisfiesCode {
300     /** row satisfies fuzzy rule */
301     YES,
302     /** row doesn't satisfy fuzzy rule, but there's possible greater row that does */
303     NEXT_EXISTS,
304     /** row doesn't satisfy fuzzy rule and there's no greater row that does */
305     NO_NEXT
306   }
307 
308   @VisibleForTesting
309   static SatisfiesCode satisfies(byte[] row, byte[] fuzzyKeyBytes, byte[] fuzzyKeyMeta) {
310     return satisfies(false, row, 0, row.length, fuzzyKeyBytes, fuzzyKeyMeta);
311   }
312 
313   @VisibleForTesting
314   static SatisfiesCode satisfies(boolean reverse, byte[] row, byte[] fuzzyKeyBytes,
315       byte[] fuzzyKeyMeta) {
316     return satisfies(reverse, row, 0, row.length, fuzzyKeyBytes, fuzzyKeyMeta);
317   }
318 
319   static SatisfiesCode satisfies(boolean reverse, byte[] row, int offset, int length,
320       byte[] fuzzyKeyBytes, byte[] fuzzyKeyMeta) {
321 
322     if (UnsafeAccess.isAvailable() == false) {
323       return satisfiesNoUnsafe(reverse, row, offset, length, fuzzyKeyBytes, fuzzyKeyMeta);
324     }
325 
326     if (row == null) {
327       // do nothing, let scan to proceed
328       return SatisfiesCode.YES;
329     }
330     length = Math.min(length, fuzzyKeyBytes.length);
331     int numWords = length / Bytes.SIZEOF_LONG;
332     int offsetAdj = offset + UnsafeAccess.BYTE_ARRAY_BASE_OFFSET;
333 
334     int j = numWords << 3; // numWords * SIZEOF_LONG;
335 
336     for (int i = 0; i < j; i += Bytes.SIZEOF_LONG) {
337 
338       long fuzzyBytes =
339           UnsafeAccess.theUnsafe.getLong(fuzzyKeyBytes, UnsafeAccess.BYTE_ARRAY_BASE_OFFSET
340               + (long) i);
341       long fuzzyMeta =
342           UnsafeAccess.theUnsafe.getLong(fuzzyKeyMeta, UnsafeAccess.BYTE_ARRAY_BASE_OFFSET
343               + (long) i);
344       long rowValue = UnsafeAccess.theUnsafe.getLong(row, offsetAdj + (long) i);
345       if ((rowValue & fuzzyMeta) != (fuzzyBytes)) {
346         // We always return NEXT_EXISTS
347         return SatisfiesCode.NEXT_EXISTS;
348       }
349     }
350 
351     int off = j;
352 
353     if (length - off >= Bytes.SIZEOF_INT) {
354       int fuzzyBytes =
355           UnsafeAccess.theUnsafe.getInt(fuzzyKeyBytes, UnsafeAccess.BYTE_ARRAY_BASE_OFFSET
356               + (long) off);
357       int fuzzyMeta =
358           UnsafeAccess.theUnsafe.getInt(fuzzyKeyMeta, UnsafeAccess.BYTE_ARRAY_BASE_OFFSET
359               + (long) off);
360       int rowValue = UnsafeAccess.theUnsafe.getInt(row, offsetAdj + (long) off);
361       if ((rowValue & fuzzyMeta) != (fuzzyBytes)) {
362         // We always return NEXT_EXISTS
363         return SatisfiesCode.NEXT_EXISTS;
364       }
365       off += Bytes.SIZEOF_INT;
366     }
367 
368     if (length - off >= Bytes.SIZEOF_SHORT) {
369       short fuzzyBytes =
370           UnsafeAccess.theUnsafe.getShort(fuzzyKeyBytes, UnsafeAccess.BYTE_ARRAY_BASE_OFFSET
371               + (long) off);
372       short fuzzyMeta =
373           UnsafeAccess.theUnsafe.getShort(fuzzyKeyMeta, UnsafeAccess.BYTE_ARRAY_BASE_OFFSET
374               + (long) off);
375       short rowValue = UnsafeAccess.theUnsafe.getShort(row, offsetAdj + (long) off);
376       if ((rowValue & fuzzyMeta) != (fuzzyBytes)) {
377         // We always return NEXT_EXISTS
378         // even if it does not (in this case getNextForFuzzyRule
379         // will return null)
380         return SatisfiesCode.NEXT_EXISTS;
381       }
382       off += Bytes.SIZEOF_SHORT;
383     }
384 
385     if (length - off >= Bytes.SIZEOF_BYTE) {
386       int fuzzyBytes = fuzzyKeyBytes[off] & 0xff;
387       int fuzzyMeta = fuzzyKeyMeta[off] & 0xff;
388       int rowValue = row[offset + off] & 0xff;
389       if ((rowValue & fuzzyMeta) != (fuzzyBytes)) {
390         // We always return NEXT_EXISTS
391         return SatisfiesCode.NEXT_EXISTS;
392       }
393     }
394     return SatisfiesCode.YES;
395   }
396 
397   static SatisfiesCode satisfiesNoUnsafe(boolean reverse, byte[] row, int offset,
398       int length, byte[] fuzzyKeyBytes, byte[] fuzzyKeyMeta) {
399     if (row == null) {
400       // do nothing, let scan to proceed
401       return SatisfiesCode.YES;
402     }
403 
404     Order order = Order.orderFor(reverse);
405     boolean nextRowKeyCandidateExists = false;
406 
407     for (int i = 0; i < fuzzyKeyMeta.length && i < length; i++) {
408       // First, checking if this position is fixed and not equals the given one
409       boolean byteAtPositionFixed = fuzzyKeyMeta[i] == 0;
410       boolean fixedByteIncorrect = byteAtPositionFixed && fuzzyKeyBytes[i] != row[i + offset];
411       if (fixedByteIncorrect) {
412         // in this case there's another row that satisfies fuzzy rule and bigger than this row
413         if (nextRowKeyCandidateExists) {
414           return SatisfiesCode.NEXT_EXISTS;
415         }
416 
417         // If this row byte is less than fixed then there's a byte array bigger than
418         // this row and which satisfies the fuzzy rule. Otherwise there's no such byte array:
419         // this row is simply bigger than any byte array that satisfies the fuzzy rule
420         boolean rowByteLessThanFixed = (row[i + offset] & 0xFF) < (fuzzyKeyBytes[i] & 0xFF);
421         if (rowByteLessThanFixed && !reverse) {
422           return SatisfiesCode.NEXT_EXISTS;
423         } else if (!rowByteLessThanFixed && reverse) {
424           return SatisfiesCode.NEXT_EXISTS;
425         } else {
426           return SatisfiesCode.NO_NEXT;
427         }
428       }
429 
430       // Second, checking if this position is not fixed and byte value is not the biggest. In this
431       // case there's a byte array bigger than this row and which satisfies the fuzzy rule. To get
432       // bigger byte array that satisfies the rule we need to just increase this byte
433       // (see the code of getNextForFuzzyRule below) by one.
434       // Note: if non-fixed byte is already at biggest value, this doesn't allow us to say there's
435       // bigger one that satisfies the rule as it can't be increased.
436       if (fuzzyKeyMeta[i] == 1 && !order.isMax(fuzzyKeyBytes[i])) {
437         nextRowKeyCandidateExists = true;
438       }
439     }
440     return SatisfiesCode.YES;
441   }
442 
443   @VisibleForTesting
444   static byte[] getNextForFuzzyRule(byte[] row, byte[] fuzzyKeyBytes, byte[] fuzzyKeyMeta) {
445     return getNextForFuzzyRule(false, row, 0, row.length, fuzzyKeyBytes, fuzzyKeyMeta);
446   }
447 
448   @VisibleForTesting
449   static byte[] getNextForFuzzyRule(boolean reverse, byte[] row, byte[] fuzzyKeyBytes,
450       byte[] fuzzyKeyMeta) {
451     return getNextForFuzzyRule(reverse, row, 0, row.length, fuzzyKeyBytes, fuzzyKeyMeta);
452   }
453 
454   /** Abstracts directional comparisons based on scan direction. */
455   private enum Order {
456     ASC {
457       public boolean lt(int lhs, int rhs) {
458         return lhs < rhs;
459       }
460 
461       public boolean gt(int lhs, int rhs) {
462         return lhs > rhs;
463       }
464 
465       public byte inc(byte val) {
466         // TODO: what about over/underflow?
467         return (byte) (val + 1);
468       }
469 
470       public boolean isMax(byte val) {
471         return val == (byte) 0xff;
472       }
473 
474       public byte min() {
475         return 0;
476       }
477     },
478     DESC {
479       public boolean lt(int lhs, int rhs) {
480         return lhs > rhs;
481       }
482 
483       public boolean gt(int lhs, int rhs) {
484         return lhs < rhs;
485       }
486 
487       public byte inc(byte val) {
488         // TODO: what about over/underflow?
489         return (byte) (val - 1);
490       }
491 
492       public boolean isMax(byte val) {
493         return val == 0;
494       }
495 
496       public byte min() {
497         return (byte) 0xFF;
498       }
499     };
500 
501     public static Order orderFor(boolean reverse) {
502       return reverse ? DESC : ASC;
503     }
504 
505     /** Returns true when {@code lhs < rhs}. */
506     public abstract boolean lt(int lhs, int rhs);
507 
508     /** Returns true when {@code lhs > rhs}. */
509     public abstract boolean gt(int lhs, int rhs);
510 
511     /** Returns {@code val} incremented by 1. */
512     public abstract byte inc(byte val);
513 
514     /** Return true when {@code val} is the maximum value */
515     public abstract boolean isMax(byte val);
516 
517     /** Return the minimum value according to this ordering scheme. */
518     public abstract byte min();
519   }
520 
521   /**
522    * @return greater byte array than given (row) which satisfies the fuzzy rule if it exists, null
523    *         otherwise
524    */
525   @VisibleForTesting
526   static byte[] getNextForFuzzyRule(boolean reverse, byte[] row, int offset, int length,
527       byte[] fuzzyKeyBytes, byte[] fuzzyKeyMeta) {
528     // To find out the next "smallest" byte array that satisfies fuzzy rule and "greater" than
529     // the given one we do the following:
530     // 1. setting values on all "fixed" positions to the values from fuzzyKeyBytes
531     // 2. if during the first step given row did not increase, then we increase the value at
532     // the first "non-fixed" position (where it is not maximum already)
533 
534     // It is easier to perform this by using fuzzyKeyBytes copy and setting "non-fixed" position
535     // values than otherwise.
536     byte[] result =
537         Arrays.copyOf(fuzzyKeyBytes, length > fuzzyKeyBytes.length ? length : fuzzyKeyBytes.length);
538     if (reverse && length > fuzzyKeyBytes.length) {
539       // we need trailing 0xff's instead of trailing 0x00's
540       for (int i = fuzzyKeyBytes.length; i < result.length; i++) {
541         result[i] = (byte) 0xFF;
542       }
543     }
544     int toInc = -1;
545     final Order order = Order.orderFor(reverse);
546 
547     boolean increased = false;
548     for (int i = 0; i < result.length; i++) {
549       if (i >= fuzzyKeyMeta.length || fuzzyKeyMeta[i] == 0 /* non-fixed */) {
550         result[i] = row[offset + i];
551         if (!order.isMax(row[offset + i])) {
552           // this is "non-fixed" position and is not at max value, hence we can increase it
553           toInc = i;
554         }
555       } else if (i < fuzzyKeyMeta.length && fuzzyKeyMeta[i] == -1 /* fixed */) {
556         if (order.lt((row[i + offset] & 0xFF), (fuzzyKeyBytes[i] & 0xFF))) {
557           // if setting value for any fixed position increased the original array,
558           // we are OK
559           increased = true;
560           break;
561         }
562 
563         if (order.gt((row[i + offset] & 0xFF), (fuzzyKeyBytes[i] & 0xFF))) {
564           // if setting value for any fixed position makes array "smaller", then just stop:
565           // in case we found some non-fixed position to increase we will do it, otherwise
566           // there's no "next" row key that satisfies fuzzy rule and "greater" than given row
567           break;
568         }
569       }
570     }
571 
572     if (!increased) {
573       if (toInc < 0) {
574         return null;
575       }
576       result[toInc] = order.inc(result[toInc]);
577 
578       // Setting all "non-fixed" positions to zeroes to the right of the one we increased so
579       // that found "next" row key is the smallest possible
580       for (int i = toInc + 1; i < result.length; i++) {
581         if (i >= fuzzyKeyMeta.length || fuzzyKeyMeta[i] == 0 /* non-fixed */) {
582           result[i] = order.min();
583         }
584       }
585     }
586 
587     return result;
588   }
589 
590   /**
591    * @return true if and only if the fields of the filter that are serialized are equal to the
592    *         corresponding fields in other. Used for testing.
593    */
594   boolean areSerializedFieldsEqual(Filter o) {
595     if (o == this) return true;
596     if (!(o instanceof FuzzyRowFilter)) return false;
597 
598     FuzzyRowFilter other = (FuzzyRowFilter) o;
599     if (this.fuzzyKeysData.size() != other.fuzzyKeysData.size()) return false;
600     for (int i = 0; i < fuzzyKeysData.size(); ++i) {
601       Pair<byte[], byte[]> thisData = this.fuzzyKeysData.get(i);
602       Pair<byte[], byte[]> otherData = other.fuzzyKeysData.get(i);
603       if (!(Bytes.equals(thisData.getFirst(), otherData.getFirst()) && Bytes.equals(
604         thisData.getSecond(), otherData.getSecond()))) {
605         return false;
606       }
607     }
608     return true;
609   }
610 }