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.CellUtil;
25  import org.apache.hadoop.hbase.KeyValue;
26  import org.apache.hadoop.hbase.regionserver.StoreFile;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.junit.Assert;
29  
30  public class MobTestUtil {
31    protected static final char FIRST_CHAR = 'a';
32    protected static final char LAST_CHAR = 'z';
33  
34    protected static String generateRandomString(int demoLength) {
35      String base = "abcdefghijklmnopqrstuvwxyz";
36      Random random = new Random();
37      StringBuffer sb = new StringBuffer();
38      for (int i = 0; i < demoLength; i++) {
39        int number = random.nextInt(base.length());
40        sb.append(base.charAt(number));
41      }
42      return sb.toString();
43    }
44    protected static void writeStoreFile(final StoreFile.Writer writer, String caseName)
45        throws IOException {
46      writeStoreFile(writer, Bytes.toBytes(caseName), Bytes.toBytes(caseName));
47    }
48  
49    /*
50     * Writes HStoreKey and ImmutableBytes data to passed writer and then closes
51     * it.
52     *
53     * @param writer
54     *
55     * @throws IOException
56     */
57    private static void writeStoreFile(final StoreFile.Writer writer, byte[] fam,
58        byte[] qualifier) throws IOException {
59      long now = System.currentTimeMillis();
60      try {
61        for (char d = FIRST_CHAR; d <= LAST_CHAR; d++) {
62          for (char e = FIRST_CHAR; e <= LAST_CHAR; e++) {
63            byte[] b = new byte[] { (byte) d, (byte) e };
64            writer.append(new KeyValue(b, fam, qualifier, now, b));
65          }
66        }
67      } finally {
68        writer.close();
69      }
70    }
71  
72    /**
73     * Compare two KeyValue only for their row family qualifier value
74     */
75    public static void assertKeyValuesEquals(KeyValue firstKeyValue,
76  	      KeyValue secondKeyValue) {
77  		    Assert.assertEquals(Bytes.toString(CellUtil.cloneRow(firstKeyValue)),
78  	            Bytes.toString(CellUtil.cloneRow(secondKeyValue)));
79  		    Assert.assertEquals(Bytes.toString(CellUtil.cloneFamily(firstKeyValue)),
80  	            Bytes.toString(CellUtil.cloneFamily(secondKeyValue)));
81  		    Assert.assertEquals(Bytes.toString(CellUtil.cloneQualifier(firstKeyValue)),
82  	            Bytes.toString(CellUtil.cloneQualifier(secondKeyValue)));
83  		    Assert.assertEquals(Bytes.toString(CellUtil.cloneValue(firstKeyValue)),
84  	            Bytes.toString(CellUtil.cloneValue(secondKeyValue)));
85  	  }
86  }