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 | |
---|
19 | package org.apache.hadoop.io; |
---|
20 | |
---|
21 | import java.util.*; |
---|
22 | import java.io.*; |
---|
23 | |
---|
24 | public class RandomDatum implements WritableComparable { |
---|
25 | private int length; |
---|
26 | private byte[] data; |
---|
27 | |
---|
28 | public RandomDatum() {} |
---|
29 | |
---|
30 | public RandomDatum(Random random) { |
---|
31 | length = 10 + (int) Math.pow(10.0, random.nextFloat() * 3.0); |
---|
32 | data = new byte[length]; |
---|
33 | random.nextBytes(data); |
---|
34 | } |
---|
35 | |
---|
36 | public int getLength() { |
---|
37 | return length; |
---|
38 | } |
---|
39 | |
---|
40 | public void write(DataOutput out) throws IOException { |
---|
41 | out.writeInt(length); |
---|
42 | out.write(data); |
---|
43 | } |
---|
44 | |
---|
45 | public void readFields(DataInput in) throws IOException { |
---|
46 | length = in.readInt(); |
---|
47 | if (data == null || length > data.length) |
---|
48 | data = new byte[length]; |
---|
49 | in.readFully(data, 0, length); |
---|
50 | } |
---|
51 | |
---|
52 | public int compareTo(Object o) { |
---|
53 | RandomDatum that = (RandomDatum)o; |
---|
54 | return WritableComparator.compareBytes(this.data, 0, this.length, |
---|
55 | that.data, 0, that.length); |
---|
56 | } |
---|
57 | |
---|
58 | public boolean equals(Object o) { |
---|
59 | return compareTo(o) == 0; |
---|
60 | } |
---|
61 | |
---|
62 | private static final char[] HEX_DIGITS = |
---|
63 | {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; |
---|
64 | |
---|
65 | /** Returns a string representation of this object. */ |
---|
66 | public String toString() { |
---|
67 | StringBuffer buf = new StringBuffer(length*2); |
---|
68 | for (int i = 0; i < length; i++) { |
---|
69 | int b = data[i]; |
---|
70 | buf.append(HEX_DIGITS[(b >> 4) & 0xf]); |
---|
71 | buf.append(HEX_DIGITS[b & 0xf]); |
---|
72 | } |
---|
73 | return buf.toString(); |
---|
74 | } |
---|
75 | |
---|
76 | public static class Generator { |
---|
77 | Random random; |
---|
78 | |
---|
79 | private RandomDatum key; |
---|
80 | private RandomDatum value; |
---|
81 | |
---|
82 | public Generator() { random = new Random(); } |
---|
83 | public Generator(int seed) { random = new Random(seed); } |
---|
84 | |
---|
85 | public RandomDatum getKey() { return key; } |
---|
86 | public RandomDatum getValue() { return value; } |
---|
87 | |
---|
88 | public void next() { |
---|
89 | key = new RandomDatum(random); |
---|
90 | value = new RandomDatum(random); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | /** A WritableComparator optimized for RandomDatum. */ |
---|
95 | public static class Comparator extends WritableComparator { |
---|
96 | public Comparator() { |
---|
97 | super(RandomDatum.class); |
---|
98 | } |
---|
99 | |
---|
100 | public int compare(byte[] b1, int s1, int l1, |
---|
101 | byte[] b2, int s2, int l2) { |
---|
102 | int n1 = readInt(b1, s1); |
---|
103 | int n2 = readInt(b2, s2); |
---|
104 | return compareBytes(b1, s1+4, n1, b2, s2+4, n2); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | } |
---|