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