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