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.util;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.List;
023import java.util.Random;
024import java.util.concurrent.ThreadLocalRandom;
025import org.apache.hadoop.hbase.ArrayBackedTag;
026import org.apache.hadoop.hbase.Cell;
027import org.apache.hadoop.hbase.ExtendedCell;
028import org.apache.hadoop.hbase.ExtendedCellScanner;
029import org.apache.hadoop.hbase.KeyValue;
030import org.apache.hadoop.hbase.KeyValue.Type;
031import org.apache.hadoop.hbase.Tag;
032import org.apache.hadoop.hbase.client.Mutation;
033import org.apache.hadoop.hbase.client.Put;
034import org.apache.hadoop.hbase.util.MultiThreadedAction.DefaultDataGenerator;
035import org.apache.hadoop.hbase.util.test.LoadTestDataGenerator;
036import org.apache.yetus.audience.InterfaceAudience;
037
038@InterfaceAudience.Private
039public class LoadTestDataGeneratorWithTags extends DefaultDataGenerator {
040
041  private int minNumTags, maxNumTags;
042  private int minTagLength, maxTagLength;
043
044  public LoadTestDataGeneratorWithTags(int minValueSize, int maxValueSize, int minColumnsPerKey,
045    int maxColumnsPerKey, byte[]... columnFamilies) {
046    super(minValueSize, maxValueSize, minColumnsPerKey, maxColumnsPerKey, columnFamilies);
047  }
048
049  @Override
050  public void initialize(String[] args) {
051    super.initialize(args);
052    if (args.length != 4) {
053      throw new IllegalArgumentException("LoadTestDataGeneratorWithTags must have "
054        + "4 initialization arguments. ie. minNumTags:maxNumTags:minTagLength:maxTagLength");
055    }
056    // 1st arg in args is the min number of tags to be used with every cell
057    this.minNumTags = Integer.parseInt(args[0]);
058    // 2nd arg in args is the max number of tags to be used with every cell
059    this.maxNumTags = Integer.parseInt(args[1]);
060    // 3rd arg in args is the min tag length
061    this.minTagLength = Integer.parseInt(args[2]);
062    // 4th arg in args is the max tag length
063    this.maxTagLength = Integer.parseInt(args[3]);
064  }
065
066  @Override
067  public Mutation beforeMutate(long rowkeyBase, Mutation m) throws IOException {
068    if (m instanceof Put) {
069      List<Cell> updatedCells = new ArrayList<>();
070      Random rand = ThreadLocalRandom.current();
071      int numTags;
072      if (minNumTags == maxNumTags) {
073        numTags = minNumTags;
074      } else {
075        numTags = minNumTags + rand.nextInt(maxNumTags - minNumTags);
076      }
077      List<Tag> tags;
078      for (ExtendedCellScanner cellScanner = m.cellScanner(); cellScanner.advance();) {
079        ExtendedCell cell = cellScanner.current();
080        byte[] tag = LoadTestDataGenerator.generateData(rand,
081          minTagLength + rand.nextInt(maxTagLength - minTagLength));
082        tags = new ArrayList<>();
083        for (int n = 0; n < numTags; n++) {
084          tags.add(new ArrayBackedTag((byte) 127, tag));
085        }
086        Cell updatedCell = new KeyValue(cell.getRowArray(), cell.getRowOffset(),
087          cell.getRowLength(), cell.getFamilyArray(), cell.getFamilyOffset(),
088          cell.getFamilyLength(), cell.getQualifierArray(), cell.getQualifierOffset(),
089          cell.getQualifierLength(), cell.getTimestamp(), Type.codeToType(cell.getTypeByte()),
090          cell.getValueArray(), cell.getValueOffset(), cell.getValueLength(), tags);
091        updatedCells.add(updatedCell);
092      }
093      m.getFamilyCellMap().clear();
094      // Clear and add new Cells to the Mutation.
095      for (Cell cell : updatedCells) {
096        ((Put) m).add(cell);
097      }
098    }
099    return m;
100  }
101}