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
9 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package org
.apache
.hadoop
.hbase
;
19 import java
.io
.IOException
;
20 import java
.util
.Arrays
;
21 import java
.util
.HashSet
;
23 import java
.util
.Collections
;
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
;
29 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
32 * Similar to {@link HConstants} but for tests. Also provides some simple static utility functions
33 * to generate test data.
35 public class HTestConst
{
37 private HTestConst() {
40 public static final String DEFAULT_TABLE_STR
= "MyTestTable";
41 public static final byte[] DEFAULT_TABLE_BYTES
= Bytes
.toBytes(DEFAULT_TABLE_STR
);
42 public static final TableName DEFAULT_TABLE
= TableName
.valueOf(DEFAULT_TABLE_BYTES
);
44 public static final String DEFAULT_CF_STR
= "MyDefaultCF";
45 public static final byte[] DEFAULT_CF_BYTES
= Bytes
.toBytes(DEFAULT_CF_STR
);
47 public static final Set
<String
> DEFAULT_CF_STR_SET
=
48 Collections
.unmodifiableSet(new HashSet
<>(Arrays
.asList(new String
[] { DEFAULT_CF_STR
})));
50 public static final String DEFAULT_ROW_STR
= "MyTestRow";
51 public static final byte[] DEFAULT_ROW_BYTES
= Bytes
.toBytes(DEFAULT_ROW_STR
);
53 public static final String DEFAULT_QUALIFIER_STR
= "MyColumnQualifier";
54 public static final byte[] DEFAULT_QUALIFIER_BYTES
= Bytes
.toBytes(DEFAULT_QUALIFIER_STR
);
56 public static String DEFAULT_VALUE_STR
= "MyTestValue";
57 public static byte[] DEFAULT_VALUE_BYTES
= Bytes
.toBytes(DEFAULT_VALUE_STR
);
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
};
64 * Generate the given number of unique byte sequences by appending numeric suffixes (ASCII
65 * representations of decimal numbers).
67 public static byte[][] makeNAscii(byte[] base
, int n
) {
68 byte[][] ret
= new byte[n
][];
69 for (int i
= 0; i
< n
; i
++) {
70 byte[] tail
= Bytes
.toBytes(Integer
.toString(i
));
71 ret
[i
] = Bytes
.add(base
, tail
);
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.
81 public static long addContent(final Region r
, final byte[] columnFamily
, final byte[] column
)
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
;
89 return addContent(new RegionAsTable(r
), Bytes
.toString(columnFamily
), Bytes
.toString(column
),
90 startKeyBytes
, endKey
, -1);
93 public static long addContent(final Region r
, final byte[] columnFamily
) throws IOException
{
94 return addContent(r
, columnFamily
, null);
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.
102 public static long addContent(Table updater
, String columnFamily
) throws IOException
{
103 return addContent(updater
, columnFamily
, START_KEY_BYTES
, null);
106 public static long addContent(Table updater
, String family
, String column
) throws IOException
{
107 return addContent(updater
, family
, column
, START_KEY_BYTES
, null);
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.
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);
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);
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.
130 public static long addContent(Table updater
, String columnFamily
, String column
,
131 byte[] startKeyBytes
, byte[] endKey
, long ts
) throws IOException
{
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) {
148 put
= new Put(t
, ts
);
152 StringBuilder sb
= new StringBuilder();
153 if (column
!= null && column
.contains(":")) {
156 if (columnFamily
!= null) {
157 sb
.append(columnFamily
);
158 if (!columnFamily
.endsWith(":")) {
161 if (column
!= null) {
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
);
171 put
.addColumn(split
[0], split
[1], t
);
173 put
.setDurability(Durability
.SKIP_WAL
);
177 // Set start character back to FIRST_CHAR after we've done first loop.
178 thirdCharStart
= FIRST_CHAR
;
180 secondCharStart
= FIRST_CHAR
;