View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.mob;
20  
21  import java.io.IOException;
22  import java.util.Random;
23  
24  import org.apache.hadoop.hbase.Cell;
25  import org.apache.hadoop.hbase.CellUtil;
26  import org.apache.hadoop.hbase.KeyValue;
27  import org.apache.hadoop.hbase.regionserver.StoreFile;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.junit.Assert;
30  
31  public class MobTestUtil {
32    protected static final char FIRST_CHAR = 'a';
33    protected static final char LAST_CHAR = 'z';
34  
35    protected static String generateRandomString(int demoLength) {
36      String base = "abcdefghijklmnopqrstuvwxyz";
37      Random random = new Random();
38      StringBuffer sb = new StringBuffer();
39      for (int i = 0; i < demoLength; i++) {
40        int number = random.nextInt(base.length());
41        sb.append(base.charAt(number));
42      }
43      return sb.toString();
44    }
45    protected static void writeStoreFile(final StoreFile.Writer writer, String caseName)
46        throws IOException {
47      writeStoreFile(writer, Bytes.toBytes(caseName), Bytes.toBytes(caseName));
48    }
49  
50    /*
51     * Writes HStoreKey and ImmutableBytes data to passed writer and then closes
52     * it.
53     *
54     * @param writer
55     *
56     * @throws IOException
57     */
58    private static void writeStoreFile(final StoreFile.Writer writer, byte[] fam,
59        byte[] qualifier) throws IOException {
60      long now = System.currentTimeMillis();
61      try {
62        for (char d = FIRST_CHAR; d <= LAST_CHAR; d++) {
63          for (char e = FIRST_CHAR; e <= LAST_CHAR; e++) {
64            byte[] b = new byte[] { (byte) d, (byte) e };
65            writer.append(new KeyValue(b, fam, qualifier, now, b));
66          }
67        }
68      } finally {
69        writer.close();
70      }
71    }
72  
73    /**
74     * Compare two Cells only for their row family qualifier value
75     */
76    public static void assertCellEquals(Cell firstKeyValue,
77  	      Cell secondKeyValue) {
78  		    Assert.assertEquals(Bytes.toString(CellUtil.cloneRow(firstKeyValue)),
79  	            Bytes.toString(CellUtil.cloneRow(secondKeyValue)));
80  		    Assert.assertEquals(Bytes.toString(CellUtil.cloneFamily(firstKeyValue)),
81  	            Bytes.toString(CellUtil.cloneFamily(secondKeyValue)));
82  		    Assert.assertEquals(Bytes.toString(CellUtil.cloneQualifier(firstKeyValue)),
83  	            Bytes.toString(CellUtil.cloneQualifier(secondKeyValue)));
84  		    Assert.assertEquals(Bytes.toString(CellUtil.cloneValue(firstKeyValue)),
85  	            Bytes.toString(CellUtil.cloneValue(secondKeyValue)));
86  	  }
87  }