View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations
15   * under the License.
16   */
17  package org.apache.hadoop.hbase.util;
18  
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Random;
23  
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.Cell;
26  import org.apache.hadoop.hbase.CellScanner;
27  import org.apache.hadoop.hbase.KeyValue;
28  import org.apache.hadoop.hbase.KeyValue.Type;
29  import org.apache.hadoop.hbase.Tag;
30  import org.apache.hadoop.hbase.client.Mutation;
31  import org.apache.hadoop.hbase.client.Put;
32  import org.apache.hadoop.hbase.util.MultiThreadedAction.DefaultDataGenerator;
33  
34  @InterfaceAudience.Private
35  public class LoadTestDataGeneratorWithTags extends DefaultDataGenerator {
36  
37    private int minNumTags, maxNumTags;
38    private int minTagLength, maxTagLength;
39    private Random random = new Random();
40  
41    public LoadTestDataGeneratorWithTags(int minValueSize, int maxValueSize, int minColumnsPerKey,
42        int maxColumnsPerKey, byte[]... columnFamilies) {
43      super(minValueSize, maxValueSize, minColumnsPerKey, maxColumnsPerKey, columnFamilies);
44    }
45  
46    @Override
47    public void initialize(String[] args) {
48      super.initialize(args);
49      if (args.length != 4) {
50        throw new IllegalArgumentException("LoadTestDataGeneratorWithTags must have "
51            + "4 initialization arguments. ie. minNumTags:maxNumTags:minTagLength:maxTagLength");
52      }
53      // 1st arg in args is the min number of tags to be used with every cell
54      this.minNumTags = Integer.parseInt(args[0]);
55      // 2nd arg in args is the max number of tags to be used with every cell
56      this.maxNumTags = Integer.parseInt(args[1]);
57      // 3rd arg in args is the min tag length
58      this.minTagLength = Integer.parseInt(args[2]);
59      // 4th arg in args is the max tag length
60      this.maxTagLength = Integer.parseInt(args[3]);
61    }
62  
63    @Override
64    public Mutation beforeMutate(long rowkeyBase, Mutation m) throws IOException {
65      if (m instanceof Put) {
66        List<Cell> updatedCells = new ArrayList<Cell>();
67        int numTags;
68        if (minNumTags == maxNumTags) {
69          numTags = minNumTags;
70        } else {
71          numTags = minNumTags + random.nextInt(maxNumTags - minNumTags);
72        }
73        List<Tag> tags;
74        for (CellScanner cellScanner = m.cellScanner(); cellScanner.advance();) {
75          Cell cell = cellScanner.current();
76          byte[] tag = LoadTestTool.generateData(random,
77              minTagLength + random.nextInt(maxTagLength - minTagLength));
78          tags = new ArrayList<Tag>();
79          for (int n = 0; n < numTags; n++) {
80            tags.add(new Tag((byte) 127, tag));
81          }
82          Cell updatedCell = new KeyValue(cell.getRowArray(), cell.getRowOffset(),
83              cell.getRowLength(), cell.getFamilyArray(), cell.getFamilyOffset(),
84              cell.getFamilyLength(), cell.getQualifierArray(), cell.getQualifierOffset(),
85              cell.getQualifierLength(), cell.getTimestamp(), Type.codeToType(cell.getTypeByte()),
86              cell.getValueArray(), cell.getValueOffset(), cell.getValueLength(), tags);
87          updatedCells.add(updatedCell);
88        }
89        m.getFamilyCellMap().clear();
90        // Clear and add new Cells to the Mutation.
91        for (Cell cell : updatedCells) {
92          ((Put) m).add(cell);
93        }
94      }
95      return m;
96    }
97  }