1616 */
1717package org .apache .hadoop .hbase ;
1818
19+ import java .io .IOException ;
1920import java .util .Arrays ;
2021import java .util .HashSet ;
2122import java .util .Set ;
2223import java .util .Collections ;
23-
24+ import org .apache .hadoop .hbase .client .Durability ;
25+ import org .apache .hadoop .hbase .client .Put ;
26+ import org .apache .hadoop .hbase .client .Table ;
27+ import org .apache .hadoop .hbase .regionserver .Region ;
28+ import org .apache .hadoop .hbase .regionserver .RegionAsTable ;
2429import org .apache .hadoop .hbase .util .Bytes ;
2530
2631/**
27- * Similar to {@link HConstants} but for tests. Also provides some simple
28- * static utility functions to generate test data.
32+ * Similar to {@link HConstants} but for tests. Also provides some simple static utility functions
33+ * to generate test data.
2934 */
3035public class HTestConst {
3136
@@ -34,15 +39,13 @@ private HTestConst() {
3439
3540 public static final String DEFAULT_TABLE_STR = "MyTestTable" ;
3641 public static final byte [] DEFAULT_TABLE_BYTES = Bytes .toBytes (DEFAULT_TABLE_STR );
37- public static final TableName DEFAULT_TABLE =
38- TableName .valueOf (DEFAULT_TABLE_BYTES );
42+ public static final TableName DEFAULT_TABLE = TableName .valueOf (DEFAULT_TABLE_BYTES );
3943
4044 public static final String DEFAULT_CF_STR = "MyDefaultCF" ;
4145 public static final byte [] DEFAULT_CF_BYTES = Bytes .toBytes (DEFAULT_CF_STR );
4246
4347 public static final Set <String > DEFAULT_CF_STR_SET =
44- Collections .unmodifiableSet (new HashSet <>(
45- Arrays .asList (new String [] { DEFAULT_CF_STR })));
48+ Collections .unmodifiableSet (new HashSet <>(Arrays .asList (new String [] { DEFAULT_CF_STR })));
4649
4750 public static final String DEFAULT_ROW_STR = "MyTestRow" ;
4851 public static final byte [] DEFAULT_ROW_BYTES = Bytes .toBytes (DEFAULT_ROW_STR );
@@ -53,17 +56,129 @@ private HTestConst() {
5356 public static String DEFAULT_VALUE_STR = "MyTestValue" ;
5457 public static byte [] DEFAULT_VALUE_BYTES = Bytes .toBytes (DEFAULT_VALUE_STR );
5558
59+ private static final char FIRST_CHAR = 'a' ;
60+ private static final char LAST_CHAR = 'z' ;
61+ private static final byte [] START_KEY_BYTES = { FIRST_CHAR , FIRST_CHAR , FIRST_CHAR };
62+
5663 /**
57- * Generate the given number of unique byte sequences by appending numeric
58- * suffixes (ASCII representations of decimal numbers).
64+ * Generate the given number of unique byte sequences by appending numeric suffixes (ASCII
65+ * representations of decimal numbers).
5966 */
6067 public static byte [][] makeNAscii (byte [] base , int n ) {
61- byte [][] ret = new byte [n ][];
68+ byte [][] ret = new byte [n ][];
6269 for (int i = 0 ; i < n ; i ++) {
6370 byte [] tail = Bytes .toBytes (Integer .toString (i ));
6471 ret [i ] = Bytes .add (base , tail );
6572 }
6673 return ret ;
6774 }
6875
76+ /**
77+ * Add content to region <code>r</code> on the passed column <code>column</code>. Adds data of the
78+ * from 'aaa', 'aab', etc where key and value are the same.
79+ * @return count of what we added.
80+ */
81+ public static long addContent (final Region r , final byte [] columnFamily , final byte [] column )
82+ throws IOException {
83+ byte [] startKey = r .getRegionInfo ().getStartKey ();
84+ byte [] endKey = r .getRegionInfo ().getEndKey ();
85+ byte [] startKeyBytes = startKey ;
86+ if (startKeyBytes == null || startKeyBytes .length == 0 ) {
87+ startKeyBytes = START_KEY_BYTES ;
88+ }
89+ return addContent (new RegionAsTable (r ), Bytes .toString (columnFamily ), Bytes .toString (column ),
90+ startKeyBytes , endKey , -1 );
91+ }
92+
93+ public static long addContent (final Region r , final byte [] columnFamily ) throws IOException {
94+ return addContent (r , columnFamily , null );
95+ }
96+
97+ /**
98+ * Add content to region <code>r</code> on the passed column <code>column</code>. Adds data of the
99+ * from 'aaa', 'aab', etc where key and value are the same.
100+ * @return count of what we added.
101+ */
102+ public static long addContent (Table updater , String columnFamily ) throws IOException {
103+ return addContent (updater , columnFamily , START_KEY_BYTES , null );
104+ }
105+
106+ public static long addContent (Table updater , String family , String column ) throws IOException {
107+ return addContent (updater , family , column , START_KEY_BYTES , null );
108+ }
109+
110+ /**
111+ * Add content to region <code>r</code> on the passed column <code>column</code>. Adds data of the
112+ * from 'aaa', 'aab', etc where key and value are the same.
113+ * @return count of what we added.
114+ */
115+ public static long addContent (Table updater , String columnFamily , byte [] startKeyBytes ,
116+ byte [] endKey ) throws IOException {
117+ return addContent (updater , columnFamily , null , startKeyBytes , endKey , -1 );
118+ }
119+
120+ public static long addContent (Table updater , String family , String column , byte [] startKeyBytes ,
121+ byte [] endKey ) throws IOException {
122+ return addContent (updater , family , column , startKeyBytes , endKey , -1 );
123+ }
124+
125+ /**
126+ * Add content to region <code>r</code> on the passed column <code>column</code>. Adds data of the
127+ * from 'aaa', 'aab', etc where key and value are the same.
128+ * @return count of what we added.
129+ */
130+ public static long addContent (Table updater , String columnFamily , String column ,
131+ byte [] startKeyBytes , byte [] endKey , long ts ) throws IOException {
132+ long count = 0 ;
133+ // Add rows of three characters. The first character starts with the
134+ // 'a' character and runs up to 'z'. Per first character, we run the
135+ // second character over same range. And same for the third so rows
136+ // (and values) look like this: 'aaa', 'aab', 'aac', etc.
137+ char secondCharStart = (char ) startKeyBytes [1 ];
138+ char thirdCharStart = (char ) startKeyBytes [2 ];
139+ EXIT : for (char c = (char ) startKeyBytes [0 ]; c <= LAST_CHAR ; c ++) {
140+ for (char d = secondCharStart ; d <= LAST_CHAR ; d ++) {
141+ for (char e = thirdCharStart ; e <= LAST_CHAR ; e ++) {
142+ byte [] t = new byte [] { (byte ) c , (byte ) d , (byte ) e };
143+ if (endKey != null && endKey .length > 0 && Bytes .compareTo (endKey , t ) <= 0 ) {
144+ break EXIT ;
145+ }
146+ Put put ;
147+ if (ts != -1 ) {
148+ put = new Put (t , ts );
149+ } else {
150+ put = new Put (t );
151+ }
152+ StringBuilder sb = new StringBuilder ();
153+ if (column != null && column .contains (":" )) {
154+ sb .append (column );
155+ } else {
156+ if (columnFamily != null ) {
157+ sb .append (columnFamily );
158+ if (!columnFamily .endsWith (":" )) {
159+ sb .append (":" );
160+ }
161+ if (column != null ) {
162+ sb .append (column );
163+ }
164+ }
165+ }
166+ byte [][] split = CellUtil .parseColumn (Bytes .toBytes (sb .toString ()));
167+ if (split .length == 1 ) {
168+ byte [] qualifier = new byte [0 ];
169+ put .addColumn (split [0 ], qualifier , t );
170+ } else {
171+ put .addColumn (split [0 ], split [1 ], t );
172+ }
173+ put .setDurability (Durability .SKIP_WAL );
174+ updater .put (put );
175+ count ++;
176+ }
177+ // Set start character back to FIRST_CHAR after we've done first loop.
178+ thirdCharStart = FIRST_CHAR ;
179+ }
180+ secondCharStart = FIRST_CHAR ;
181+ }
182+ return count ;
183+ }
69184}
0 commit comments