View Javadoc

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  package org.apache.hadoop.hbase.io.hfile;
19  
20  import org.apache.hadoop.hbase.KeyValue;
21  
22  import java.util.Random;
23  
24  /**
25   * These helper methods generate random byte[]'s data for KeyValues
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     * Generates a random key that is guaranteed to increase as the given index i
78     * increases. The result consists of a prefix, which is a deterministic
79     * increasing function of i, and a random suffix.
80     *
81     * @param rand
82     *          random number generator to use
83     * @param i
84     * @return
85     */
86    public static byte[] randomOrderedKey(Random rand, int i) {
87      StringBuilder k = new StringBuilder();
88  
89      // The fixed-length lexicographically increasing part of the key.
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      // A random-length random suffix of the key.
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     // The fixed-length lexicographically increasing part of the key.
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     // A random suffix of the key.
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 }