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