1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.hadoop.hbase.util.test;
18
19 import java.util.Random;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.hadoop.hbase.classification.InterfaceAudience;
24 import org.apache.hadoop.hbase.util.Bytes;
25 import org.apache.hadoop.hbase.util.MD5Hash;
26
27
28
29
30
31
32
33
34 @InterfaceAudience.Private
35 public class LoadTestKVGenerator {
36
37 private static final Log LOG = LogFactory.getLog(LoadTestKVGenerator.class);
38 private static int logLimit = 10;
39
40
41 private Random randomForValueSize = new Random();
42
43 private final int minValueSize;
44 private final int maxValueSize;
45
46 public LoadTestKVGenerator(int minValueSize, int maxValueSize) {
47 if (minValueSize <= 0 || maxValueSize <= 0) {
48 throw new IllegalArgumentException("Invalid min/max value sizes: " +
49 minValueSize + ", " + maxValueSize);
50 }
51 this.minValueSize = minValueSize;
52 this.maxValueSize = maxValueSize;
53 }
54
55
56
57
58
59
60
61
62 public static boolean verify(byte[] value, byte[]... seedStrings) {
63 byte[] expectedData = getValueForRowColumn(value.length, seedStrings);
64 boolean equals = Bytes.equals(expectedData, value);
65 if (!equals && LOG.isDebugEnabled() && logLimit > 0) {
66 LOG.debug("verify failed, expected value: " + Bytes.toStringBinary(expectedData)
67 + " actual value: "+ Bytes.toStringBinary(value));
68 logLimit--;
69 }
70 return equals;
71 }
72
73
74
75
76
77 public static String md5PrefixedKey(long key) {
78 String stringKey = Long.toString(key);
79 String md5hash = MD5Hash.getMD5AsHex(Bytes.toBytes(stringKey));
80
81
82 return md5hash + "-" + stringKey;
83 }
84
85
86
87
88
89
90
91
92
93
94
95 public byte[] generateRandomSizeValue(byte[]... seedStrings) {
96 int dataSize = minValueSize;
97 if(minValueSize != maxValueSize) {
98 dataSize = minValueSize + randomForValueSize.nextInt(Math.abs(maxValueSize - minValueSize));
99 }
100 return getValueForRowColumn(dataSize, seedStrings);
101 }
102
103
104
105
106
107 private static byte[] getValueForRowColumn(int dataSize, byte[]... seedStrings) {
108 long seed = dataSize;
109 for (byte[] str : seedStrings) {
110 seed += Bytes.toString(str).hashCode();
111 }
112 Random seededRandom = new Random(seed);
113 byte[] randomBytes = new byte[dataSize];
114 seededRandom.nextBytes(randomBytes);
115 return randomBytes;
116 }
117
118 }