001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.hfile;
019
020import org.apache.hadoop.hbase.KeyValue;
021
022import java.util.Random;
023
024/**
025 * These helper methods generate random byte[]'s data for KeyValues
026 */
027public class RandomKeyValueUtil {
028  public static final String COLUMN_FAMILY_NAME = "_-myColumnFamily-_";
029  private static final int MIN_ROW_OR_QUALIFIER_LENGTH = 64;
030  private static final int MAX_ROW_OR_QUALIFIER_LENGTH = 128;
031
032  public static final char randomReadableChar(Random rand) {
033    int i = rand.nextInt(26 * 2 + 10 + 1);
034    if (i < 26)
035      return (char) ('A' + i);
036    i -= 26;
037
038    if (i < 26)
039      return (char) ('a' + i);
040    i -= 26;
041
042    if (i < 10)
043      return (char) ('0' + i);
044    i -= 10;
045
046    assert i == 0;
047    return '_';
048  }
049
050  public static KeyValue randomKeyValue(Random rand) {
051    return new KeyValue(randomRowOrQualifier(rand),
052        COLUMN_FAMILY_NAME.getBytes(), randomRowOrQualifier(rand),
053        randomValue(rand));
054  }
055
056  public static byte[] randomRowOrQualifier(Random rand) {
057    StringBuilder field = new StringBuilder();
058    int fieldLen = MIN_ROW_OR_QUALIFIER_LENGTH
059        + rand.nextInt(MAX_ROW_OR_QUALIFIER_LENGTH
060        - MIN_ROW_OR_QUALIFIER_LENGTH + 1);
061    for (int i = 0; i < fieldLen; ++i)
062      field.append(randomReadableChar(rand));
063    return field.toString().getBytes();
064  }
065
066  public static byte[] randomValue(Random rand) {
067    StringBuilder v = new StringBuilder();
068    for (int j = 0; j < 1 + rand.nextInt(2000); ++j) {
069      v.append((char) (32 + rand.nextInt(95)));
070    }
071
072    byte[] valueBytes = v.toString().getBytes();
073    return valueBytes;
074  }
075
076  /**
077   * Generates a random key that is guaranteed to increase as the given index i
078   * increases. The result consists of a prefix, which is a deterministic
079   * increasing function of i, and a random suffix.
080   *
081   * @param rand random number generator to use
082   * @param i
083   * @return the random key
084   */
085  public static byte[] randomOrderedKey(Random rand, int i) {
086    StringBuilder k = new StringBuilder();
087
088    // The fixed-length lexicographically increasing part of the key.
089    for (int bitIndex = 31; bitIndex >= 0; --bitIndex) {
090      if ((i & (1 << bitIndex)) == 0)
091        k.append("a");
092      else
093        k.append("b");
094    }
095
096    // A random-length random suffix of the key.
097    for (int j = 0; j < rand.nextInt(50); ++j)
098      k.append(randomReadableChar(rand));
099
100    byte[] keyBytes = k.toString().getBytes();
101    return keyBytes;
102  }
103
104  public static byte[] randomOrderedFixedLengthKey(Random rand, int i, int suffixLength) {
105    StringBuilder k = new StringBuilder();
106
107    // The fixed-length lexicographically increasing part of the key.
108    for (int bitIndex = 31; bitIndex >= 0; --bitIndex) {
109      if ((i & (1 << bitIndex)) == 0)
110        k.append("a");
111      else
112        k.append("b");
113    }
114
115    // A random suffix of the key.
116    for (int j = 0; j < suffixLength; ++j)
117      k.append(randomReadableChar(rand));
118
119    byte[] keyBytes = k.toString().getBytes();
120    return keyBytes;
121  }
122
123  public static byte[] randomFixedLengthValue(Random rand, int valueLength) {
124    StringBuilder v = new StringBuilder();
125    for (int j = 0; j < valueLength; ++j) {
126      v.append((char) (32 + rand.nextInt(95)));
127    }
128
129    byte[] valueBytes = v.toString().getBytes();
130    return valueBytes;
131  }
132}