001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.mob;
020
021import java.io.IOException;
022import java.util.List;
023import java.util.Random;
024
025import org.apache.hadoop.hbase.Cell;
026import org.apache.hadoop.hbase.CellUtil;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.KeyValue;
029import org.apache.hadoop.hbase.client.Result;
030import org.apache.hadoop.hbase.client.ResultScanner;
031import org.apache.hadoop.hbase.client.Scan;
032import org.apache.hadoop.hbase.client.Table;
033import org.apache.hadoop.hbase.regionserver.StoreFileWriter;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.junit.Assert;
036
037public class MobTestUtil {
038  protected static final char FIRST_CHAR = 'a';
039  protected static final char LAST_CHAR = 'z';
040
041  protected static String generateRandomString(int demoLength) {
042    String base = "abcdefghijklmnopqrstuvwxyz";
043    Random random = new Random();
044    StringBuilder sb = new StringBuilder();
045    for (int i = 0; i < demoLength; i++) {
046      int number = random.nextInt(base.length());
047      sb.append(base.charAt(number));
048    }
049    return sb.toString();
050  }
051  protected static void writeStoreFile(final StoreFileWriter writer, String caseName)
052      throws IOException {
053    writeStoreFile(writer, Bytes.toBytes(caseName), Bytes.toBytes(caseName));
054  }
055
056  /*
057   * Writes HStoreKey and ImmutableBytes data to passed writer and then closes
058   * it.
059   *
060   * @param writer
061   *
062   * @throws IOException
063   */
064  private static void writeStoreFile(final StoreFileWriter writer, byte[] fam,
065      byte[] qualifier) throws IOException {
066    long now = System.currentTimeMillis();
067    try {
068      for (char d = FIRST_CHAR; d <= LAST_CHAR; d++) {
069        for (char e = FIRST_CHAR; e <= LAST_CHAR; e++) {
070          byte[] b = new byte[] { (byte) d, (byte) e };
071          writer.append(new KeyValue(b, fam, qualifier, now, b));
072        }
073      }
074    } finally {
075      writer.close();
076    }
077  }
078
079  /**
080   * Compare two Cells only for their row family qualifier value
081   */
082  public static void assertCellEquals(Cell firstKeyValue, Cell secondKeyValue) {
083    Assert.assertArrayEquals(CellUtil.cloneRow(firstKeyValue),
084        CellUtil.cloneRow(secondKeyValue));
085    Assert.assertArrayEquals(CellUtil.cloneFamily(firstKeyValue),
086        CellUtil.cloneFamily(secondKeyValue));
087    Assert.assertArrayEquals(CellUtil.cloneQualifier(firstKeyValue),
088        CellUtil.cloneQualifier(secondKeyValue));
089    Assert.assertArrayEquals(CellUtil.cloneValue(firstKeyValue),
090        CellUtil.cloneValue(secondKeyValue));
091  }
092
093  public static void assertCellsValue(Table table, Scan scan,
094      byte[] expectedValue, int expectedCount) throws IOException {
095    ResultScanner results = table.getScanner(scan);
096    int count = 0;
097    for (Result res : results) {
098      List<Cell> cells = res.listCells();
099      for(Cell cell : cells) {
100        // Verify the value
101        Assert.assertArrayEquals(expectedValue, CellUtil.cloneValue(cell));
102        count++;
103      }
104    }
105    results.close();
106    Assert.assertEquals(expectedCount, count);
107  }
108
109  /**
110   * Gets the number of rows in the given table.
111   * @param table to get the scanner
112   * @return the number of rows
113   */
114  public static int countMobRows(HBaseTestingUtility util, Table table) throws IOException {
115    Scan scan = new Scan();
116    // Do not retrieve the mob data when scanning
117    scan.setAttribute(MobConstants.MOB_SCAN_RAW, Bytes.toBytes(Boolean.TRUE));
118    return util.countRows(table, scan);
119  }
120}