1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.regionserver;
19
20 import java.util.Arrays;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.hadoop.hbase.classification.InterfaceAudience;
25
26
27
28
29
30
31
32
33 @InterfaceAudience.Private
34 public class KeyPrefixRegionSplitPolicy extends IncreasingToUpperBoundRegionSplitPolicy {
35 private static final Log LOG = LogFactory
36 .getLog(KeyPrefixRegionSplitPolicy.class);
37 @Deprecated
38 public static final String PREFIX_LENGTH_KEY_DEPRECATED = "prefix_split_key_policy.prefix_length";
39 public static final String PREFIX_LENGTH_KEY = "KeyPrefixRegionSplitPolicy.prefix_length";
40
41 private int prefixLength = 0;
42
43 @Override
44 protected void configureForRegion(HRegion region) {
45 super.configureForRegion(region);
46 prefixLength = 0;
47
48
49 String prefixLengthString = region.getTableDesc().getValue(
50 PREFIX_LENGTH_KEY);
51 if (prefixLengthString == null) {
52
53 prefixLengthString = region.getTableDesc().getValue(PREFIX_LENGTH_KEY_DEPRECATED);
54 if (prefixLengthString == null) {
55 LOG.error(PREFIX_LENGTH_KEY + " not specified for table "
56 + region.getTableDesc().getTableName()
57 + ". Using default RegionSplitPolicy");
58 return;
59 }
60 }
61 try {
62 prefixLength = Integer.parseInt(prefixLengthString);
63 } catch (NumberFormatException nfe) {
64
65 LOG.error("Number format exception when parsing " + PREFIX_LENGTH_KEY + " for table "
66 + region.getTableDesc().getTableName() + ":"
67 + prefixLengthString + ". " + nfe);
68 return;
69 }
70 if (prefixLength <= 0) {
71 LOG.error("Invalid value for " + PREFIX_LENGTH_KEY + " for table "
72 + region.getTableDesc().getTableName() + ":"
73 + prefixLengthString + ". Using default RegionSplitPolicy");
74 }
75 }
76
77 @Override
78 protected byte[] getSplitPoint() {
79 byte[] splitPoint = super.getSplitPoint();
80 if (prefixLength > 0 && splitPoint != null && splitPoint.length > 0) {
81
82 return Arrays.copyOf(splitPoint,
83 Math.min(prefixLength, splitPoint.length));
84 } else {
85 return splitPoint;
86 }
87 }
88 }