|
17 | 17 |
|
18 | 18 | package org.apache.spark.util.collection.unsafe.sort;
|
19 | 19 |
|
| 20 | +import com.google.common.base.Charsets; |
| 21 | +import com.google.common.primitives.Longs; |
20 | 22 | import org.apache.spark.annotation.Private;
|
| 23 | +import org.apache.spark.unsafe.types.UTF8String; |
21 | 24 |
|
22 | 25 | @Private
|
23 | 26 | public class PrefixComparators {
|
24 | 27 | private PrefixComparators() {}
|
25 | 28 |
|
| 29 | + public static final StringPrefixComparator STRING = new StringPrefixComparator(); |
26 | 30 | public static final IntPrefixComparator INTEGER = new IntPrefixComparator();
|
27 | 31 | public static final LongPrefixComparator LONG = new LongPrefixComparator();
|
28 | 32 | public static final FloatPrefixComparator FLOAT = new FloatPrefixComparator();
|
29 | 33 | public static final DoublePrefixComparator DOUBLE = new DoublePrefixComparator();
|
30 | 34 |
|
| 35 | + public static final class StringPrefixComparator extends PrefixComparator { |
| 36 | + @Override |
| 37 | + public int compare(long aPrefix, long bPrefix) { |
| 38 | + // TODO: this can certainly be done more efficiently |
| 39 | + byte[] a = Longs.toByteArray(aPrefix); |
| 40 | + byte[] b = Longs.toByteArray(bPrefix); |
| 41 | + for (int i = 0; i < 8; i++) { |
| 42 | + if (a[i] == b[i]) continue; |
| 43 | + if (a[i] > b[i]) return -1; |
| 44 | + else if (a[i] < b[i]) return 1; |
| 45 | + } |
| 46 | + return 0; |
| 47 | + } |
| 48 | + |
| 49 | + public long computePrefix(UTF8String value) { |
| 50 | + // TODO: this can certainly be done more efficiently |
| 51 | + return value == null ? 0L : computePrefix(value.toString()); |
| 52 | + } |
| 53 | + |
| 54 | + public long computePrefix(String value) { |
| 55 | + // TODO: this can certainly be done more efficiently |
| 56 | + if (value == null || value.length() == 0) { |
| 57 | + return 0L; |
| 58 | + } else { |
| 59 | + String first4Chars = value.substring(0, Math.min(3, value.length() - 1)); |
| 60 | + byte[] utf16Bytes = first4Chars.getBytes(Charsets.UTF_16); |
| 61 | + byte[] padded = new byte[8]; |
| 62 | + if (utf16Bytes.length < 8) { |
| 63 | + System.arraycopy(utf16Bytes, 0, padded, 0, utf16Bytes.length); |
| 64 | + } |
| 65 | + return Longs.fromByteArray(padded); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
31 | 70 | public static final class IntPrefixComparator extends PrefixComparator {
|
32 | 71 | @Override
|
33 | 72 | public int compare(long aPrefix, long bPrefix) {
|
|
0 commit comments