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 082 * random number generator to use 083 * @param i 084 * @return 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 = 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}