1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.hadoop.hbase.security.visibility;
18
19 import java.io.IOException;
20
21 import org.apache.hadoop.hbase.classification.InterfaceAudience;
22 import org.apache.hadoop.hbase.client.Get;
23 import org.apache.hadoop.hbase.client.Mutation;
24 import org.apache.hadoop.hbase.util.MultiThreadedAction.DefaultDataGenerator;
25
26 @InterfaceAudience.Private
27 public class LoadTestDataGeneratorWithVisibilityLabels extends DefaultDataGenerator {
28
29 private static final String COMMA = ",";
30 private String[] visibilityExps = null;
31 private String[][] authorizations = null;
32
33 public LoadTestDataGeneratorWithVisibilityLabels(int minValueSize, int maxValueSize,
34 int minColumnsPerKey, int maxColumnsPerKey, byte[]... columnFamilies) {
35 super(minValueSize, maxValueSize, minColumnsPerKey, maxColumnsPerKey, columnFamilies);
36 }
37
38 @Override
39 public void initialize(String[] args) {
40 super.initialize(args);
41 if (args.length < 1 || args.length > 2) {
42 throw new IllegalArgumentException("LoadTestDataGeneratorWithVisibilityLabels can have "
43 + "1 or 2 initialization arguments");
44 }
45
46 String temp = args[0];
47
48 this.visibilityExps = temp.split(COMMA);
49
50
51
52 if (args.length == 2) {
53 this.authorizations = toAuthorizationsSet(args[1]);
54 }
55 }
56
57 private static String[][] toAuthorizationsSet(String authorizationsStr) {
58
59 String[] split = authorizationsStr.split("],");
60 String[][] result = new String[split.length][];
61 for (int i = 0; i < split.length; i++) {
62 String s = split[i].trim();
63 assert s.charAt(0) == '[';
64 s = s.substring(1);
65 if (i == split.length - 1) {
66 assert s.charAt(s.length() - 1) == ']';
67 s = s.substring(0, s.length() - 1);
68 }
69 String[] tmp = s.split(COMMA);
70 for (int j = 0; j < tmp.length; j++) {
71 tmp[j] = tmp[j].trim();
72 }
73 result[i] = tmp;
74 }
75 return result;
76 }
77
78 @Override
79 public Mutation beforeMutate(long rowkeyBase, Mutation m) throws IOException {
80 m.setCellVisibility(new CellVisibility(this.visibilityExps[(int) rowkeyBase
81 % this.visibilityExps.length]));
82 return m;
83 }
84
85 @Override
86 public Get beforeGet(long rowkeyBase, Get get) {
87 get.setAuthorizations(new Authorizations(
88 authorizations[(int) (rowkeyBase % authorizations.length)]));
89 return get;
90 }
91 }