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;
021import org.apache.hadoop.hbase.util.Bytes;
022
023import java.util.Random;
024
025/**
026 * These helper methods generate random byte[]'s data for KeyValues
027 */
028public class RandomKeyValueUtil {
029  public static final String COLUMN_FAMILY_NAME = "_-myColumnFamily-_";
030  private static final int MIN_ROW_OR_QUALIFIER_LENGTH = 64;
031  private static final int MAX_ROW_OR_QUALIFIER_LENGTH = 128;
032
033  public static final char randomReadableChar(Random rand) {
034    int i = rand.nextInt(26 * 2 + 10 + 1);
035    if (i < 26)
036      return (char) ('A' + i);
037    i -= 26;
038
039    if (i < 26)
040      return (char) ('a' + i);
041    i -= 26;
042
043    if (i < 10)
044      return (char) ('0' + i);
045    i -= 10;
046
047    assert i == 0;
048    return '_';
049  }
050
051  public static KeyValue randomKeyValue(Random rand) {
052    return new KeyValue(randomRowOrQualifier(rand),
053        Bytes.toBytes(COLUMN_FAMILY_NAME), randomRowOrQualifier(rand),
054        randomValue(rand));
055  }
056
057  public static byte[] randomRowOrQualifier(Random rand) {
058    StringBuilder field = new StringBuilder();
059    int fieldLen = MIN_ROW_OR_QUALIFIER_LENGTH
060        + rand.nextInt(MAX_ROW_OR_QUALIFIER_LENGTH
061        - MIN_ROW_OR_QUALIFIER_LENGTH + 1);
062    for (int i = 0; i < fieldLen; ++i)
063      field.append(randomReadableChar(rand));
064    return Bytes.toBytes(field.toString());
065  }
066
067  public static byte[] randomValue(Random rand) {
068    StringBuilder v = new StringBuilder();
069    for (int j = 0; j < 1 + rand.nextInt(2000); ++j) {
070      v.append((char) (32 + rand.nextInt(95)));
071    }
072
073    byte[] valueBytes = Bytes.toBytes(v.toString());
074    return valueBytes;
075  }
076
077  /**
078   * Generates a random key that is guaranteed to increase as the given index i
079   * increases. The result consists of a prefix, which is a deterministic
080   * increasing function of i, and a random suffix.
081   *
082   * @param rand random number generator to use
083   * @param i
084   * @return the random key
085   */
086  public static byte[] randomOrderedKey(Random rand, int i) {
087    StringBuilder k = new StringBuilder();
088
089    // The fixed-length lexicographically increasing part of the key.
090    for (int bitIndex = 31; bitIndex >= 0; --bitIndex) {
091      if ((i & (1 << bitIndex)) == 0)
092        k.append("a");
093      else
094        k.append("b");
095    }
096
097    // A random-length random suffix of the key.
098    for (int j = 0; j < rand.nextInt(50); ++j)
099      k.append(randomReadableChar(rand));
100
101    byte[] keyBytes = Bytes.toBytes(k.toString());
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 = Bytes.toBytes(k.toString());
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 = Bytes.toBytes(v.toString());
131    return valueBytes;
132  }
133}