1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.io.hfile;
19
20 import org.apache.hadoop.hbase.KeyValue;
21
22 import java.util.Random;
23
24
25
26
27 public class RandomKeyValueUtil {
28 public static final String COLUMN_FAMILY_NAME = "_-myColumnFamily-_";
29 private static final int MIN_ROW_OR_QUALIFIER_LENGTH = 64;
30 private static final int MAX_ROW_OR_QUALIFIER_LENGTH = 128;
31
32 public static final char randomReadableChar(Random rand) {
33 int i = rand.nextInt(26 * 2 + 10 + 1);
34 if (i < 26)
35 return (char) ('A' + i);
36 i -= 26;
37
38 if (i < 26)
39 return (char) ('a' + i);
40 i -= 26;
41
42 if (i < 10)
43 return (char) ('0' + i);
44 i -= 10;
45
46 assert i == 0;
47 return '_';
48 }
49
50 public static KeyValue randomKeyValue(Random rand) {
51 return new KeyValue(randomRowOrQualifier(rand),
52 COLUMN_FAMILY_NAME.getBytes(), randomRowOrQualifier(rand),
53 randomValue(rand));
54 }
55
56 public static byte[] randomRowOrQualifier(Random rand) {
57 StringBuilder field = new StringBuilder();
58 int fieldLen = MIN_ROW_OR_QUALIFIER_LENGTH
59 + rand.nextInt(MAX_ROW_OR_QUALIFIER_LENGTH
60 - MIN_ROW_OR_QUALIFIER_LENGTH + 1);
61 for (int i = 0; i < fieldLen; ++i)
62 field.append(randomReadableChar(rand));
63 return field.toString().getBytes();
64 }
65
66 public static byte[] randomValue(Random rand) {
67 StringBuilder v = new StringBuilder();
68 for (int j = 0; j < 1 + rand.nextInt(2000); ++j) {
69 v.append((char) (32 + rand.nextInt(95)));
70 }
71
72 byte[] valueBytes = v.toString().getBytes();
73 return valueBytes;
74 }
75
76
77
78
79
80
81
82
83
84
85
86 public static byte[] randomOrderedKey(Random rand, int i) {
87 StringBuilder k = new StringBuilder();
88
89
90 for (int bitIndex = 31; bitIndex >= 0; --bitIndex) {
91 if ((i & (1 << bitIndex)) == 0)
92 k.append("a");
93 else
94 k.append("b");
95 }
96
97
98 for (int j = 0; j < rand.nextInt(50); ++j)
99 k.append(randomReadableChar(rand));
100
101 byte[] keyBytes = k.toString().getBytes();
102 return keyBytes;
103 }
104
105 public static byte[] randomOrderedFixedLengthKey(Random rand, int i, int suffixLength) {
106 StringBuilder k = new StringBuilder();
107
108
109 for (int bitIndex = 31; bitIndex >= 0; --bitIndex) {
110 if ((i & (1 << bitIndex)) == 0)
111 k.append("a");
112 else
113 k.append("b");
114 }
115
116
117 for (int j = 0; j < suffixLength; ++j)
118 k.append(randomReadableChar(rand));
119
120 byte[] keyBytes = k.toString().getBytes();
121 return keyBytes;
122 }
123
124 public static byte[] randomFixedLengthValue(Random rand, int valueLength) {
125 StringBuilder v = new StringBuilder();
126 for (int j = 0; j < valueLength; ++j) {
127 v.append((char) (32 + rand.nextInt(95)));
128 }
129
130 byte[] valueBytes = v.toString().getBytes();
131 return valueBytes;
132 }
133 }