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