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