001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with this
004 * work for additional information regarding copyright ownership. The ASF
005 * licenses this file to you under the Apache License, Version 2.0 (the
006 * "License"); you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014 * License for the specific language governing permissions and limitations
015 * under the License.
016 */
017package org.apache.hadoop.hbase.util;
018
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.List;
022import java.util.Random;
023
024import org.apache.yetus.audience.InterfaceAudience;
025import org.apache.hadoop.hbase.Cell;
026import org.apache.hadoop.hbase.CellScanner;
027import org.apache.hadoop.hbase.KeyValue;
028import org.apache.hadoop.hbase.KeyValue.Type;
029import org.apache.hadoop.hbase.Tag;
030import org.apache.hadoop.hbase.ArrayBackedTag;
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;
035
036@InterfaceAudience.Private
037public class LoadTestDataGeneratorWithTags extends DefaultDataGenerator {
038
039  private int minNumTags, maxNumTags;
040  private int minTagLength, maxTagLength;
041  private Random random = new Random();
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      int numTags;
070      if (minNumTags == maxNumTags) {
071        numTags = minNumTags;
072      } else {
073        numTags = minNumTags + random.nextInt(maxNumTags - minNumTags);
074      }
075      List<Tag> tags;
076      for (CellScanner cellScanner = m.cellScanner(); cellScanner.advance();) {
077        Cell cell = cellScanner.current();
078        byte[] tag = LoadTestDataGenerator.generateData(random,
079            minTagLength + random.nextInt(maxTagLength - minTagLength));
080        tags = new ArrayList<>();
081        for (int n = 0; n < numTags; n++) {
082          tags.add(new ArrayBackedTag((byte) 127, tag));
083        }
084        Cell updatedCell = new KeyValue(cell.getRowArray(), cell.getRowOffset(),
085            cell.getRowLength(), cell.getFamilyArray(), cell.getFamilyOffset(),
086            cell.getFamilyLength(), cell.getQualifierArray(), cell.getQualifierOffset(),
087            cell.getQualifierLength(), cell.getTimestamp(), Type.codeToType(cell.getTypeByte()),
088            cell.getValueArray(), cell.getValueOffset(), cell.getValueLength(), tags);
089        updatedCells.add(updatedCell);
090      }
091      m.getFamilyCellMap().clear();
092      // Clear and add new Cells to the Mutation.
093      for (Cell cell : updatedCells) {
094        ((Put) m).add(cell);
095      }
096    }
097    return m;
098  }
099}