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