1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.accumulo.core.file.rfile.bcfile;
18
19 import java.util.Comparator;
20
21 import org.apache.hadoop.io.RawComparator;
22 import org.apache.hadoop.io.WritableComparator;
23
24 class CompareUtils {
25 /**
26 * Prevent the instantiation of class.
27 */
28 private CompareUtils() {
29
30 }
31
32 /**
33 * A comparator to compare anything that implements {@link RawComparable} using a customized comparator.
34 */
35 public static final class BytesComparator implements Comparator<RawComparable> {
36 private RawComparator<Object> cmp;
37
38 public BytesComparator(RawComparator<Object> cmp) {
39 this.cmp = cmp;
40 }
41
42 @Override
43 public int compare(RawComparable o1, RawComparable o2) {
44 return compare(o1.buffer(), o1.offset(), o1.size(), o2.buffer(), o2.offset(), o2.size());
45 }
46
47 public int compare(byte[] a, int off1, int len1, byte[] b, int off2, int len2) {
48 return cmp.compare(a, off1, len1, b, off2, len2);
49 }
50 }
51
52 /**
53 * Interface for all objects that has a single integer magnitude.
54 */
55 static interface Scalar {
56 long magnitude();
57 }
58
59 static final class ScalarLong implements Scalar {
60 private long magnitude;
61
62 public ScalarLong(long m) {
63 magnitude = m;
64 }
65
66 public long magnitude() {
67 return magnitude;
68 }
69 }
70
71 public static final class ScalarComparator implements Comparator<Scalar> {
72 @Override
73 public int compare(Scalar o1, Scalar o2) {
74 long diff = o1.magnitude() - o2.magnitude();
75 if (diff < 0)
76 return -1;
77 if (diff > 0)
78 return 1;
79 return 0;
80 }
81 }
82
83 public static final class MemcmpRawComparator implements RawComparator<Object> {
84 @Override
85 public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
86 return WritableComparator.compareBytes(b1, s1, l1, b2, s2, l2);
87 }
88
89 @Override
90 public int compare(Object o1, Object o2) {
91 throw new RuntimeException("Object comparison not supported");
92 }
93 }
94 }