1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21 import java.io.DataInput;
22 import java.io.DataOutput;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.TreeMap;
34 import java.util.TreeSet;
35 import java.util.regex.Matcher;
36
37 import org.apache.hadoop.hbase.util.ByteStringer;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.hadoop.hbase.classification.InterfaceAudience;
41 import org.apache.hadoop.hbase.classification.InterfaceStability;
42 import org.apache.hadoop.conf.Configuration;
43 import org.apache.hadoop.fs.Path;
44 import org.apache.hadoop.hbase.client.Durability;
45 import org.apache.hadoop.hbase.client.RegionReplicaUtil;
46 import org.apache.hadoop.hbase.exceptions.DeserializationException;
47 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
48 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
49 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.BytesBytesPair;
50 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.ColumnFamilySchema;
51 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameStringPair;
52 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.TableSchema;
53 import org.apache.hadoop.hbase.regionserver.BloomType;
54 import org.apache.hadoop.hbase.security.User;
55 import org.apache.hadoop.hbase.util.Bytes;
56 import org.apache.hadoop.hbase.util.Writables;
57 import org.apache.hadoop.io.WritableComparable;
58
59
60
61
62
63
64
65 @InterfaceAudience.Public
66 @InterfaceStability.Evolving
67 public class HTableDescriptor implements WritableComparable<HTableDescriptor> {
68
69 private static final Log LOG = LogFactory.getLog(HTableDescriptor.class);
70
71
72
73
74
75
76
77
78
79 private static final byte TABLE_DESCRIPTOR_VERSION = 7;
80
81 private TableName name = null;
82
83
84
85
86
87
88 private final Map<ImmutableBytesWritable, ImmutableBytesWritable> values =
89 new HashMap<ImmutableBytesWritable, ImmutableBytesWritable>();
90
91
92
93
94
95
96 private final Map<String, String> configuration = new HashMap<String, String>();
97
98 public static final String SPLIT_POLICY = "SPLIT_POLICY";
99
100
101
102
103
104
105
106
107 public static final String MAX_FILESIZE = "MAX_FILESIZE";
108 private static final ImmutableBytesWritable MAX_FILESIZE_KEY =
109 new ImmutableBytesWritable(Bytes.toBytes(MAX_FILESIZE));
110
111 public static final String OWNER = "OWNER";
112 public static final ImmutableBytesWritable OWNER_KEY =
113 new ImmutableBytesWritable(Bytes.toBytes(OWNER));
114
115
116
117
118
119
120
121 public static final String READONLY = "READONLY";
122 private static final ImmutableBytesWritable READONLY_KEY =
123 new ImmutableBytesWritable(Bytes.toBytes(READONLY));
124
125
126
127
128
129
130
131 public static final String COMPACTION_ENABLED = "COMPACTION_ENABLED";
132 private static final ImmutableBytesWritable COMPACTION_ENABLED_KEY =
133 new ImmutableBytesWritable(Bytes.toBytes(COMPACTION_ENABLED));
134
135
136
137
138
139
140
141
142 public static final String MEMSTORE_FLUSHSIZE = "MEMSTORE_FLUSHSIZE";
143 private static final ImmutableBytesWritable MEMSTORE_FLUSHSIZE_KEY =
144 new ImmutableBytesWritable(Bytes.toBytes(MEMSTORE_FLUSHSIZE));
145
146 public static final String FLUSH_POLICY = "FLUSH_POLICY";
147
148
149
150
151
152
153
154 public static final String IS_ROOT = "IS_ROOT";
155 private static final ImmutableBytesWritable IS_ROOT_KEY =
156 new ImmutableBytesWritable(Bytes.toBytes(IS_ROOT));
157
158
159
160
161
162
163
164
165 public static final String IS_META = "IS_META";
166 private static final ImmutableBytesWritable IS_META_KEY =
167 new ImmutableBytesWritable(Bytes.toBytes(IS_META));
168
169
170
171
172
173
174 @Deprecated
175 public static final String DEFERRED_LOG_FLUSH = "DEFERRED_LOG_FLUSH";
176 @Deprecated
177 private static final ImmutableBytesWritable DEFERRED_LOG_FLUSH_KEY =
178 new ImmutableBytesWritable(Bytes.toBytes(DEFERRED_LOG_FLUSH));
179
180
181
182
183 public static final String DURABILITY = "DURABILITY";
184 private static final ImmutableBytesWritable DURABILITY_KEY =
185 new ImmutableBytesWritable(Bytes.toBytes("DURABILITY"));
186
187
188
189
190 public static final String REGION_REPLICATION = "REGION_REPLICATION";
191 private static final ImmutableBytesWritable REGION_REPLICATION_KEY =
192 new ImmutableBytesWritable(Bytes.toBytes(REGION_REPLICATION));
193
194
195
196
197
198 public static final String REGION_MEMSTORE_REPLICATION = "REGION_MEMSTORE_REPLICATION";
199 private static final ImmutableBytesWritable REGION_MEMSTORE_REPLICATION_KEY =
200 new ImmutableBytesWritable(Bytes.toBytes(REGION_MEMSTORE_REPLICATION));
201
202
203
204
205
206
207
208 public static final String NORMALIZATION_ENABLED = "NORMALIZATION_ENABLED";
209 private static final ImmutableBytesWritable NORMALIZATION_ENABLED_KEY =
210 new ImmutableBytesWritable(Bytes.toBytes(NORMALIZATION_ENABLED));
211
212
213 private static final Durability DEFAULT_DURABLITY = Durability.USE_DEFAULT;
214
215 public static final String PRIORITY = "PRIORITY";
216 private static final ImmutableBytesWritable PRIORITY_KEY =
217 new ImmutableBytesWritable(Bytes.toBytes(PRIORITY));
218
219
220 private static final int DEFAULT_PRIORITY = HConstants.NORMAL_QOS;
221
222
223
224
225
226
227 private static final ImmutableBytesWritable FALSE =
228 new ImmutableBytesWritable(Bytes.toBytes(Boolean.FALSE.toString()));
229
230 private static final ImmutableBytesWritable TRUE =
231 new ImmutableBytesWritable(Bytes.toBytes(Boolean.TRUE.toString()));
232
233 private static final boolean DEFAULT_DEFERRED_LOG_FLUSH = false;
234
235
236
237
238 public static final boolean DEFAULT_READONLY = false;
239
240
241
242
243 public static final boolean DEFAULT_COMPACTION_ENABLED = true;
244
245
246
247
248 public static final boolean DEFAULT_NORMALIZATION_ENABLED = false;
249
250
251
252
253
254 public static final long DEFAULT_MEMSTORE_FLUSH_SIZE = 1024*1024*128L;
255
256 public static final int DEFAULT_REGION_REPLICATION = 1;
257
258 public static final boolean DEFAULT_REGION_MEMSTORE_REPLICATION = true;
259
260 private final static Map<String, String> DEFAULT_VALUES
261 = new HashMap<String, String>();
262 private final static Set<ImmutableBytesWritable> RESERVED_KEYWORDS
263 = new HashSet<ImmutableBytesWritable>();
264 static {
265 DEFAULT_VALUES.put(MAX_FILESIZE,
266 String.valueOf(HConstants.DEFAULT_MAX_FILE_SIZE));
267 DEFAULT_VALUES.put(READONLY, String.valueOf(DEFAULT_READONLY));
268 DEFAULT_VALUES.put(MEMSTORE_FLUSHSIZE,
269 String.valueOf(DEFAULT_MEMSTORE_FLUSH_SIZE));
270 DEFAULT_VALUES.put(DEFERRED_LOG_FLUSH,
271 String.valueOf(DEFAULT_DEFERRED_LOG_FLUSH));
272 DEFAULT_VALUES.put(DURABILITY, DEFAULT_DURABLITY.name());
273 DEFAULT_VALUES.put(REGION_REPLICATION, String.valueOf(DEFAULT_REGION_REPLICATION));
274 DEFAULT_VALUES.put(NORMALIZATION_ENABLED, String.valueOf(DEFAULT_NORMALIZATION_ENABLED));
275 DEFAULT_VALUES.put(PRIORITY, String.valueOf(DEFAULT_PRIORITY));
276 for (String s : DEFAULT_VALUES.keySet()) {
277 RESERVED_KEYWORDS.add(new ImmutableBytesWritable(Bytes.toBytes(s)));
278 }
279 RESERVED_KEYWORDS.add(IS_ROOT_KEY);
280 RESERVED_KEYWORDS.add(IS_META_KEY);
281 }
282
283
284
285
286 private volatile Boolean meta = null;
287
288
289
290 private volatile Boolean root = null;
291
292
293
294
295 private Durability durability = null;
296
297
298
299
300 private final Map<byte [], HColumnDescriptor> families =
301 new TreeMap<byte [], HColumnDescriptor>(Bytes.BYTES_RAWCOMPARATOR);
302
303
304
305
306
307 @InterfaceAudience.Private
308 protected HTableDescriptor(final TableName name, HColumnDescriptor[] families) {
309 setName(name);
310 for(HColumnDescriptor descriptor : families) {
311 this.families.put(descriptor.getName(), descriptor);
312 }
313 }
314
315
316
317
318
319 protected HTableDescriptor(final TableName name, HColumnDescriptor[] families,
320 Map<ImmutableBytesWritable,ImmutableBytesWritable> values) {
321 setName(name);
322 for(HColumnDescriptor descriptor : families) {
323 this.families.put(descriptor.getName(), descriptor);
324 }
325 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> entry:
326 values.entrySet()) {
327 setValue(entry.getKey(), entry.getValue());
328 }
329 }
330
331
332
333
334
335
336
337
338
339 @Deprecated
340 public HTableDescriptor() {
341 super();
342 }
343
344
345
346
347
348
349 public HTableDescriptor(final TableName name) {
350 super();
351 setName(name);
352 }
353
354
355
356
357
358
359 @Deprecated
360 public HTableDescriptor(final byte[] name) {
361 this(TableName.valueOf(name));
362 }
363
364
365
366
367
368
369 @Deprecated
370 public HTableDescriptor(final String name) {
371 this(TableName.valueOf(name));
372 }
373
374
375
376
377
378
379
380
381 public HTableDescriptor(final HTableDescriptor desc) {
382 this(desc.name, desc);
383 }
384
385
386
387
388
389
390
391
392
393
394 public HTableDescriptor(final TableName name, final HTableDescriptor desc) {
395 super();
396 setName(name);
397 setMetaFlags(this.name);
398 for (HColumnDescriptor c: desc.families.values()) {
399 this.families.put(c.getName(), new HColumnDescriptor(c));
400 }
401 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
402 desc.values.entrySet()) {
403 setValue(e.getKey(), e.getValue());
404 }
405 for (Map.Entry<String, String> e : desc.configuration.entrySet()) {
406 this.configuration.put(e.getKey(), e.getValue());
407 }
408 }
409
410
411
412
413
414
415
416
417 private void setMetaFlags(final TableName name) {
418 setMetaRegion(isRootRegion() ||
419 name.equals(TableName.META_TABLE_NAME));
420 }
421
422
423
424
425
426
427 public boolean isRootRegion() {
428 if (this.root == null) {
429 this.root = isSomething(IS_ROOT_KEY, false)? Boolean.TRUE: Boolean.FALSE;
430 }
431 return this.root.booleanValue();
432 }
433
434
435
436
437
438
439
440
441 protected void setRootRegion(boolean isRoot) {
442
443 setValue(IS_ROOT_KEY, isRoot ? TRUE : FALSE);
444 }
445
446
447
448
449
450
451
452
453 public boolean isMetaRegion() {
454 if (this.meta == null) {
455 this.meta = calculateIsMetaRegion();
456 }
457 return this.meta.booleanValue();
458 }
459
460 private synchronized Boolean calculateIsMetaRegion() {
461 byte [] value = getValue(IS_META_KEY);
462 return (value != null)? Boolean.valueOf(Bytes.toString(value)): Boolean.FALSE;
463 }
464
465 private boolean isSomething(final ImmutableBytesWritable key,
466 final boolean valueIfNull) {
467 byte [] value = getValue(key);
468 if (value != null) {
469 return Boolean.valueOf(Bytes.toString(value));
470 }
471 return valueIfNull;
472 }
473
474
475
476
477
478
479
480
481
482 protected void setMetaRegion(boolean isMeta) {
483 setValue(IS_META_KEY, isMeta? TRUE: FALSE);
484 }
485
486
487
488
489
490
491 public boolean isMetaTable() {
492 return isMetaRegion() && !isRootRegion();
493 }
494
495
496
497
498
499
500
501
502 public byte[] getValue(byte[] key) {
503 return getValue(new ImmutableBytesWritable(key));
504 }
505
506 private byte[] getValue(final ImmutableBytesWritable key) {
507 ImmutableBytesWritable ibw = values.get(key);
508 if (ibw == null)
509 return null;
510 return ibw.get();
511 }
512
513
514
515
516
517
518
519
520 public String getValue(String key) {
521 byte[] value = getValue(Bytes.toBytes(key));
522 if (value == null)
523 return null;
524 return Bytes.toString(value);
525 }
526
527
528
529
530
531
532
533 public Map<ImmutableBytesWritable,ImmutableBytesWritable> getValues() {
534
535 return Collections.unmodifiableMap(values);
536 }
537
538
539
540
541
542
543
544
545 public HTableDescriptor setValue(byte[] key, byte[] value) {
546 setValue(new ImmutableBytesWritable(key), new ImmutableBytesWritable(value));
547 return this;
548 }
549
550
551
552
553
554 private HTableDescriptor setValue(final ImmutableBytesWritable key,
555 final String value) {
556 setValue(key, new ImmutableBytesWritable(Bytes.toBytes(value)));
557 return this;
558 }
559
560
561
562
563
564
565
566 public HTableDescriptor setValue(final ImmutableBytesWritable key,
567 final ImmutableBytesWritable value) {
568 if (key.compareTo(DEFERRED_LOG_FLUSH_KEY) == 0) {
569 boolean isDeferredFlush = Boolean.valueOf(Bytes.toString(value.get()));
570 LOG.warn("HTableDescriptor property:" + DEFERRED_LOG_FLUSH + " is deprecated, " +
571 "use " + DURABILITY + " instead");
572 setDurability(isDeferredFlush ? Durability.ASYNC_WAL : DEFAULT_DURABLITY);
573 return this;
574 }
575 values.put(key, value);
576 return this;
577 }
578
579
580
581
582
583
584
585
586 public HTableDescriptor setValue(String key, String value) {
587 if (value == null) {
588 remove(key);
589 } else {
590 setValue(Bytes.toBytes(key), Bytes.toBytes(value));
591 }
592 return this;
593 }
594
595
596
597
598
599
600
601 public void remove(final String key) {
602 remove(new ImmutableBytesWritable(Bytes.toBytes(key)));
603 }
604
605
606
607
608
609
610
611 public void remove(ImmutableBytesWritable key) {
612 values.remove(key);
613 }
614
615
616
617
618
619
620
621 public void remove(final byte [] key) {
622 remove(new ImmutableBytesWritable(key));
623 }
624
625
626
627
628
629
630
631 public boolean isReadOnly() {
632 return isSomething(READONLY_KEY, DEFAULT_READONLY);
633 }
634
635
636
637
638
639
640
641
642
643 public HTableDescriptor setReadOnly(final boolean readOnly) {
644 return setValue(READONLY_KEY, readOnly? TRUE: FALSE);
645 }
646
647
648
649
650
651
652
653 public boolean isCompactionEnabled() {
654 return isSomething(COMPACTION_ENABLED_KEY, DEFAULT_COMPACTION_ENABLED);
655 }
656
657
658
659
660
661
662 public HTableDescriptor setCompactionEnabled(final boolean isEnable) {
663 setValue(COMPACTION_ENABLED_KEY, isEnable ? TRUE : FALSE);
664 return this;
665 }
666
667
668
669
670
671
672
673 public boolean isNormalizationEnabled() {
674 return isSomething(NORMALIZATION_ENABLED_KEY, DEFAULT_NORMALIZATION_ENABLED);
675 }
676
677
678
679
680
681
682 public HTableDescriptor setNormalizationEnabled(final boolean isEnable) {
683 setValue(NORMALIZATION_ENABLED_KEY, isEnable ? TRUE : FALSE);
684 return this;
685 }
686
687
688
689
690
691 public HTableDescriptor setDurability(Durability durability) {
692 this.durability = durability;
693 setValue(DURABILITY_KEY, durability.name());
694 return this;
695 }
696
697
698
699
700
701 public Durability getDurability() {
702 if (this.durability == null) {
703 byte[] durabilityValue = getValue(DURABILITY_KEY);
704 if (durabilityValue == null) {
705 this.durability = DEFAULT_DURABLITY;
706 } else {
707 try {
708 this.durability = Durability.valueOf(Bytes.toString(durabilityValue));
709 } catch (IllegalArgumentException ex) {
710 LOG.warn("Received " + ex + " because Durability value for HTableDescriptor"
711 + " is not known. Durability:" + Bytes.toString(durabilityValue));
712 this.durability = DEFAULT_DURABLITY;
713 }
714 }
715 }
716 return this.durability;
717 }
718
719
720
721
722
723
724 public TableName getTableName() {
725 return name;
726 }
727
728
729
730
731
732
733
734 @Deprecated
735 public byte[] getName() {
736 return name.getName();
737 }
738
739
740
741
742
743
744 public String getNameAsString() {
745 return name.getNameAsString();
746 }
747
748
749
750
751
752
753
754 public HTableDescriptor setRegionSplitPolicyClassName(String clazz) {
755 setValue(SPLIT_POLICY, clazz);
756 return this;
757 }
758
759
760
761
762
763
764
765
766
767 public String getRegionSplitPolicyClassName() {
768 return getValue(SPLIT_POLICY);
769 }
770
771
772
773
774
775
776 @Deprecated
777 public HTableDescriptor setName(byte[] name) {
778 setName(TableName.valueOf(name));
779 return this;
780 }
781
782 @Deprecated
783 public HTableDescriptor setName(TableName name) {
784 this.name = name;
785 setMetaFlags(this.name);
786 return this;
787 }
788
789
790
791
792
793
794
795
796
797
798 public long getMaxFileSize() {
799 byte [] value = getValue(MAX_FILESIZE_KEY);
800 if (value != null) {
801 return Long.parseLong(Bytes.toString(value));
802 }
803 return -1;
804 }
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821 public HTableDescriptor setMaxFileSize(long maxFileSize) {
822 setValue(MAX_FILESIZE_KEY, Long.toString(maxFileSize));
823 return this;
824 }
825
826
827
828
829
830
831
832
833 public long getMemStoreFlushSize() {
834 byte [] value = getValue(MEMSTORE_FLUSHSIZE_KEY);
835 if (value != null) {
836 return Long.parseLong(Bytes.toString(value));
837 }
838 return -1;
839 }
840
841
842
843
844
845
846
847 public HTableDescriptor setMemStoreFlushSize(long memstoreFlushSize) {
848 setValue(MEMSTORE_FLUSHSIZE_KEY, Long.toString(memstoreFlushSize));
849 return this;
850 }
851
852
853
854
855
856
857
858 public HTableDescriptor setFlushPolicyClassName(String clazz) {
859 setValue(FLUSH_POLICY, clazz);
860 return this;
861 }
862
863
864
865
866
867
868
869
870 public String getFlushPolicyClassName() {
871 return getValue(FLUSH_POLICY);
872 }
873
874
875
876
877
878
879 public HTableDescriptor addFamily(final HColumnDescriptor family) {
880 if (family.getName() == null || family.getName().length <= 0) {
881 throw new IllegalArgumentException("Family name cannot be null or empty");
882 }
883 if (hasFamily(family.getName())) {
884 throw new IllegalArgumentException("Family '" +
885 family.getNameAsString() + "' already exists so cannot be added");
886 }
887 this.families.put(family.getName(), family);
888 return this;
889 }
890
891
892
893
894
895
896 public HTableDescriptor modifyFamily(final HColumnDescriptor family) {
897 if (family.getName() == null || family.getName().length <= 0) {
898 throw new IllegalArgumentException("Family name cannot be null or empty");
899 }
900 if (!hasFamily(family.getName())) {
901 throw new IllegalArgumentException("Column family '" + family.getNameAsString()
902 + "' does not exist");
903 }
904 this.families.put(family.getName(), family);
905 return this;
906 }
907
908
909
910
911
912
913 public boolean hasFamily(final byte [] familyName) {
914 return families.containsKey(familyName);
915 }
916
917
918
919
920
921
922 @Override
923 public String toString() {
924 StringBuilder s = new StringBuilder();
925 s.append('\'').append(Bytes.toString(name.getName())).append('\'');
926 s.append(getValues(true));
927 for (HColumnDescriptor f : families.values()) {
928 s.append(", ").append(f);
929 }
930 return s.toString();
931 }
932
933
934
935
936
937 public String toStringCustomizedValues() {
938 StringBuilder s = new StringBuilder();
939 s.append('\'').append(Bytes.toString(name.getName())).append('\'');
940 s.append(getValues(false));
941 for(HColumnDescriptor hcd : families.values()) {
942 s.append(", ").append(hcd.toStringCustomizedValues());
943 }
944 return s.toString();
945 }
946
947
948
949
950 public String toStringTableAttributes() {
951 return getValues(true).toString();
952 }
953
954 private StringBuilder getValues(boolean printDefaults) {
955 StringBuilder s = new StringBuilder();
956
957
958 Set<ImmutableBytesWritable> reservedKeys = new TreeSet<ImmutableBytesWritable>();
959 Set<ImmutableBytesWritable> userKeys = new TreeSet<ImmutableBytesWritable>();
960 for (ImmutableBytesWritable k : values.keySet()) {
961 if (k == null || k.get() == null) continue;
962 String key = Bytes.toString(k.get());
963
964 if (!RESERVED_KEYWORDS.contains(k) && !key.startsWith("coprocessor$")) {
965 userKeys.add(k);
966 continue;
967 }
968
969 String value = Bytes.toString(values.get(k).get());
970 if (key.equalsIgnoreCase(IS_ROOT) || key.equalsIgnoreCase(IS_META)) {
971 if (Boolean.valueOf(value) == false) continue;
972 }
973
974 if (printDefaults
975 || !DEFAULT_VALUES.containsKey(key)
976 || !DEFAULT_VALUES.get(key).equalsIgnoreCase(value)) {
977 reservedKeys.add(k);
978 }
979 }
980
981
982 boolean hasAttributes = !reservedKeys.isEmpty() || !userKeys.isEmpty();
983 if (!hasAttributes && configuration.isEmpty()) return s;
984
985 s.append(", {");
986
987 if (hasAttributes) {
988 s.append("TABLE_ATTRIBUTES => {");
989
990
991 boolean printCommaForAttr = false;
992 for (ImmutableBytesWritable k : reservedKeys) {
993 String key = Bytes.toString(k.get());
994 String value = Bytes.toStringBinary(values.get(k).get());
995 if (printCommaForAttr) s.append(", ");
996 printCommaForAttr = true;
997 s.append(key);
998 s.append(" => ");
999 s.append('\'').append(value).append('\'');
1000 }
1001
1002 if (!userKeys.isEmpty()) {
1003
1004 if (printCommaForAttr) s.append(", ");
1005 printCommaForAttr = true;
1006 s.append(HConstants.METADATA).append(" => ");
1007 s.append("{");
1008 boolean printCommaForCfg = false;
1009 for (ImmutableBytesWritable k : userKeys) {
1010 String key = Bytes.toString(k.get());
1011 String value = Bytes.toStringBinary(values.get(k).get());
1012 if (printCommaForCfg) s.append(", ");
1013 printCommaForCfg = true;
1014 s.append('\'').append(key).append('\'');
1015 s.append(" => ");
1016 s.append('\'').append(value).append('\'');
1017 }
1018 s.append("}");
1019 }
1020 }
1021
1022
1023 if (!configuration.isEmpty()) {
1024 if (hasAttributes) {
1025 s.append(", ");
1026 }
1027 s.append(HConstants.CONFIGURATION).append(" => ");
1028 s.append('{');
1029 boolean printCommaForConfig = false;
1030 for (Map.Entry<String, String> e : configuration.entrySet()) {
1031 if (printCommaForConfig) s.append(", ");
1032 printCommaForConfig = true;
1033 s.append('\'').append(e.getKey()).append('\'');
1034 s.append(" => ");
1035 s.append('\'').append(e.getValue()).append('\'');
1036 }
1037 s.append("}");
1038 }
1039 s.append("}");
1040 return s;
1041 }
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052 @Override
1053 public boolean equals(Object obj) {
1054 if (this == obj) {
1055 return true;
1056 }
1057 if (obj == null) {
1058 return false;
1059 }
1060 if (!(obj instanceof HTableDescriptor)) {
1061 return false;
1062 }
1063 return compareTo((HTableDescriptor)obj) == 0;
1064 }
1065
1066
1067
1068
1069 @Override
1070 public int hashCode() {
1071 int result = this.name.hashCode();
1072 result ^= Byte.valueOf(TABLE_DESCRIPTOR_VERSION).hashCode();
1073 if (this.families != null && this.families.size() > 0) {
1074 for (HColumnDescriptor e: this.families.values()) {
1075 result ^= e.hashCode();
1076 }
1077 }
1078 result ^= values.hashCode();
1079 result ^= configuration.hashCode();
1080 return result;
1081 }
1082
1083
1084
1085
1086
1087
1088 @Deprecated
1089 @Override
1090 public void readFields(DataInput in) throws IOException {
1091 int version = in.readInt();
1092 if (version < 3)
1093 throw new IOException("versions < 3 are not supported (and never existed!?)");
1094
1095 name = TableName.valueOf(Bytes.readByteArray(in));
1096 setRootRegion(in.readBoolean());
1097 setMetaRegion(in.readBoolean());
1098 values.clear();
1099 configuration.clear();
1100 int numVals = in.readInt();
1101 for (int i = 0; i < numVals; i++) {
1102 ImmutableBytesWritable key = new ImmutableBytesWritable();
1103 ImmutableBytesWritable value = new ImmutableBytesWritable();
1104 key.readFields(in);
1105 value.readFields(in);
1106 setValue(key, value);
1107 }
1108 families.clear();
1109 int numFamilies = in.readInt();
1110 for (int i = 0; i < numFamilies; i++) {
1111 HColumnDescriptor c = new HColumnDescriptor();
1112 c.readFields(in);
1113 families.put(c.getName(), c);
1114 }
1115 if (version >= 7) {
1116 int numConfigs = in.readInt();
1117 for (int i = 0; i < numConfigs; i++) {
1118 ImmutableBytesWritable key = new ImmutableBytesWritable();
1119 ImmutableBytesWritable value = new ImmutableBytesWritable();
1120 key.readFields(in);
1121 value.readFields(in);
1122 configuration.put(
1123 Bytes.toString(key.get(), key.getOffset(), key.getLength()),
1124 Bytes.toString(value.get(), value.getOffset(), value.getLength()));
1125 }
1126 }
1127 }
1128
1129
1130
1131
1132
1133
1134
1135 @Deprecated
1136 @Override
1137 public void write(DataOutput out) throws IOException {
1138 out.writeInt(TABLE_DESCRIPTOR_VERSION);
1139 Bytes.writeByteArray(out, name.toBytes());
1140 out.writeBoolean(isRootRegion());
1141 out.writeBoolean(isMetaRegion());
1142 out.writeInt(values.size());
1143 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
1144 values.entrySet()) {
1145 e.getKey().write(out);
1146 e.getValue().write(out);
1147 }
1148 out.writeInt(families.size());
1149 for(Iterator<HColumnDescriptor> it = families.values().iterator();
1150 it.hasNext(); ) {
1151 HColumnDescriptor family = it.next();
1152 family.write(out);
1153 }
1154 out.writeInt(configuration.size());
1155 for (Map.Entry<String, String> e : configuration.entrySet()) {
1156 new ImmutableBytesWritable(Bytes.toBytes(e.getKey())).write(out);
1157 new ImmutableBytesWritable(Bytes.toBytes(e.getValue())).write(out);
1158 }
1159 }
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170 @Override
1171 public int compareTo(final HTableDescriptor other) {
1172 int result = this.name.compareTo(other.name);
1173 if (result == 0) {
1174 result = families.size() - other.families.size();
1175 }
1176 if (result == 0 && families.size() != other.families.size()) {
1177 result = Integer.valueOf(families.size()).compareTo(
1178 Integer.valueOf(other.families.size()));
1179 }
1180 if (result == 0) {
1181 for (Iterator<HColumnDescriptor> it = families.values().iterator(),
1182 it2 = other.families.values().iterator(); it.hasNext(); ) {
1183 result = it.next().compareTo(it2.next());
1184 if (result != 0) {
1185 break;
1186 }
1187 }
1188 }
1189 if (result == 0) {
1190
1191 result = this.values.hashCode() - other.values.hashCode();
1192 if (result < 0)
1193 result = -1;
1194 else if (result > 0)
1195 result = 1;
1196 }
1197 if (result == 0) {
1198 result = this.configuration.hashCode() - other.configuration.hashCode();
1199 if (result < 0)
1200 result = -1;
1201 else if (result > 0)
1202 result = 1;
1203 }
1204 return result;
1205 }
1206
1207
1208
1209
1210
1211
1212
1213
1214 public Collection<HColumnDescriptor> getFamilies() {
1215 return Collections.unmodifiableCollection(this.families.values());
1216 }
1217
1218
1219
1220
1221 public int getRegionReplication() {
1222 return getIntValue(REGION_REPLICATION_KEY, DEFAULT_REGION_REPLICATION);
1223 }
1224
1225 private int getIntValue(ImmutableBytesWritable key, int defaultVal) {
1226 byte[] val = getValue(key);
1227 if (val == null || val.length == 0) {
1228 return defaultVal;
1229 }
1230 return Integer.parseInt(Bytes.toString(val));
1231 }
1232
1233
1234
1235
1236
1237 public HTableDescriptor setRegionReplication(int regionReplication) {
1238 setValue(REGION_REPLICATION_KEY,
1239 new ImmutableBytesWritable(Bytes.toBytes(Integer.toString(regionReplication))));
1240 return this;
1241 }
1242
1243
1244
1245
1246 public boolean hasRegionMemstoreReplication() {
1247 return isSomething(REGION_MEMSTORE_REPLICATION_KEY, DEFAULT_REGION_MEMSTORE_REPLICATION);
1248 }
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259 public HTableDescriptor setRegionMemstoreReplication(boolean memstoreReplication) {
1260 setValue(REGION_MEMSTORE_REPLICATION_KEY, memstoreReplication ? TRUE : FALSE);
1261
1262
1263 setConfiguration(RegionReplicaUtil.REGION_REPLICA_WAIT_FOR_PRIMARY_FLUSH_CONF_KEY,
1264 Boolean.toString(memstoreReplication));
1265 return this;
1266 }
1267
1268 public HTableDescriptor setPriority(int priority) {
1269 setValue(PRIORITY_KEY, Integer.toString(priority));
1270 return this;
1271 }
1272
1273 public int getPriority() {
1274 return getIntValue(PRIORITY_KEY, DEFAULT_PRIORITY);
1275 }
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285 public Set<byte[]> getFamiliesKeys() {
1286 return Collections.unmodifiableSet(this.families.keySet());
1287 }
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297 public HColumnDescriptor[] getColumnFamilies() {
1298 Collection<HColumnDescriptor> hColumnDescriptors = getFamilies();
1299 return hColumnDescriptors.toArray(new HColumnDescriptor[hColumnDescriptors.size()]);
1300 }
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311 public HColumnDescriptor getFamily(final byte [] column) {
1312 return this.families.get(column);
1313 }
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324 public HColumnDescriptor removeFamily(final byte [] column) {
1325 return this.families.remove(column);
1326 }
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338 public HTableDescriptor addCoprocessor(String className) throws IOException {
1339 addCoprocessor(className, null, Coprocessor.PRIORITY_USER, null);
1340 return this;
1341 }
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357 public HTableDescriptor addCoprocessor(String className, Path jarFilePath,
1358 int priority, final Map<String, String> kvs)
1359 throws IOException {
1360 checkHasCoprocessor(className);
1361
1362
1363 StringBuilder kvString = new StringBuilder();
1364 if (kvs != null) {
1365 for (Map.Entry<String, String> e: kvs.entrySet()) {
1366 if (!e.getKey().matches(HConstants.CP_HTD_ATTR_VALUE_PARAM_KEY_PATTERN)) {
1367 throw new IOException("Illegal parameter key = " + e.getKey());
1368 }
1369 if (!e.getValue().matches(HConstants.CP_HTD_ATTR_VALUE_PARAM_VALUE_PATTERN)) {
1370 throw new IOException("Illegal parameter (" + e.getKey() +
1371 ") value = " + e.getValue());
1372 }
1373 if (kvString.length() != 0) {
1374 kvString.append(',');
1375 }
1376 kvString.append(e.getKey());
1377 kvString.append('=');
1378 kvString.append(e.getValue());
1379 }
1380 }
1381
1382 String value = ((jarFilePath == null)? "" : jarFilePath.toString()) +
1383 "|" + className + "|" + Integer.toString(priority) + "|" +
1384 kvString.toString();
1385 return addCoprocessorToMap(value);
1386 }
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399 public HTableDescriptor addCoprocessorWithSpec(final String specStr) throws IOException {
1400 String className = getCoprocessorClassNameFromSpecStr(specStr);
1401 if (className == null) {
1402 throw new IllegalArgumentException("Format does not match " +
1403 HConstants.CP_HTD_ATTR_VALUE_PATTERN + ": " + specStr);
1404 }
1405 checkHasCoprocessor(className);
1406 return addCoprocessorToMap(specStr);
1407 }
1408
1409 private void checkHasCoprocessor(final String className) throws IOException {
1410 if (hasCoprocessor(className)) {
1411 throw new IOException("Coprocessor " + className + " already exists.");
1412 }
1413 }
1414
1415
1416
1417
1418
1419
1420
1421 private HTableDescriptor addCoprocessorToMap(final String specStr) {
1422 if (specStr == null) return this;
1423
1424 int maxCoprocessorNumber = 0;
1425 Matcher keyMatcher;
1426 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
1427 this.values.entrySet()) {
1428 keyMatcher =
1429 HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(
1430 Bytes.toString(e.getKey().get()));
1431 if (!keyMatcher.matches()) {
1432 continue;
1433 }
1434 maxCoprocessorNumber = Math.max(Integer.parseInt(keyMatcher.group(1)), maxCoprocessorNumber);
1435 }
1436 maxCoprocessorNumber++;
1437 String key = "coprocessor$" + Integer.toString(maxCoprocessorNumber);
1438 this.values.put(new ImmutableBytesWritable(Bytes.toBytes(key)),
1439 new ImmutableBytesWritable(Bytes.toBytes(specStr)));
1440 return this;
1441 }
1442
1443
1444
1445
1446
1447
1448
1449 public boolean hasCoprocessor(String classNameToMatch) {
1450 Matcher keyMatcher;
1451 Matcher valueMatcher;
1452 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
1453 this.values.entrySet()) {
1454 keyMatcher =
1455 HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(
1456 Bytes.toString(e.getKey().get()));
1457 if (!keyMatcher.matches()) {
1458 continue;
1459 }
1460 String className = getCoprocessorClassNameFromSpecStr(Bytes.toString(e.getValue().get()));
1461 if (className == null) continue;
1462 if (className.equals(classNameToMatch.trim())) {
1463 return true;
1464 }
1465 }
1466 return false;
1467 }
1468
1469
1470
1471
1472
1473
1474 public List<String> getCoprocessors() {
1475 List<String> result = new ArrayList<String>();
1476 Matcher keyMatcher;
1477 Matcher valueMatcher;
1478 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e : this.values.entrySet()) {
1479 keyMatcher = HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(Bytes.toString(e.getKey().get()));
1480 if (!keyMatcher.matches()) {
1481 continue;
1482 }
1483 String className = getCoprocessorClassNameFromSpecStr(Bytes.toString(e.getValue().get()));
1484 if (className == null) continue;
1485 result.add(className);
1486 }
1487 return result;
1488 }
1489
1490
1491
1492
1493
1494 private static String getCoprocessorClassNameFromSpecStr(final String spec) {
1495 Matcher matcher = HConstants.CP_HTD_ATTR_VALUE_PATTERN.matcher(spec);
1496
1497 return matcher != null && matcher.matches()? matcher.group(2).trim(): null;
1498 }
1499
1500
1501
1502
1503
1504 public void removeCoprocessor(String className) {
1505 ImmutableBytesWritable match = null;
1506 Matcher keyMatcher;
1507 Matcher valueMatcher;
1508 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e : this.values
1509 .entrySet()) {
1510 keyMatcher = HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(Bytes.toString(e
1511 .getKey().get()));
1512 if (!keyMatcher.matches()) {
1513 continue;
1514 }
1515 valueMatcher = HConstants.CP_HTD_ATTR_VALUE_PATTERN.matcher(Bytes
1516 .toString(e.getValue().get()));
1517 if (!valueMatcher.matches()) {
1518 continue;
1519 }
1520
1521 String clazz = valueMatcher.group(2).trim();
1522
1523 if (clazz.equals(className.trim())) {
1524 match = e.getKey();
1525 break;
1526 }
1527 }
1528
1529 if (match != null)
1530 remove(match);
1531 }
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543 @Deprecated
1544 public static Path getTableDir(Path rootdir, final byte [] tableName) {
1545
1546
1547 TableName name = TableName.valueOf(tableName);
1548 return new Path(rootdir, new Path(HConstants.BASE_NAMESPACE_DIR,
1549 new Path(name.getNamespaceAsString(), new Path(name.getQualifierAsString()))));
1550 }
1551
1552
1553
1554
1555
1556
1557 @Deprecated
1558 public static final HTableDescriptor META_TABLEDESC = new HTableDescriptor(
1559 TableName.META_TABLE_NAME,
1560 new HColumnDescriptor[] {
1561 new HColumnDescriptor(HConstants.CATALOG_FAMILY)
1562
1563 .setMaxVersions(HConstants.DEFAULT_HBASE_META_VERSIONS)
1564 .setInMemory(true)
1565 .setBlocksize(HConstants.DEFAULT_HBASE_META_BLOCK_SIZE)
1566 .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
1567
1568 .setBloomFilterType(BloomType.NONE)
1569
1570
1571 .setCacheDataInL1(true)
1572 });
1573
1574 static {
1575 try {
1576 META_TABLEDESC.addCoprocessor(
1577 "org.apache.hadoop.hbase.coprocessor.MultiRowMutationEndpoint",
1578 null, Coprocessor.PRIORITY_SYSTEM, null);
1579 } catch (IOException ex) {
1580
1581 throw new RuntimeException(ex);
1582 }
1583 }
1584
1585 public final static String NAMESPACE_FAMILY_INFO = "info";
1586 public final static byte[] NAMESPACE_FAMILY_INFO_BYTES = Bytes.toBytes(NAMESPACE_FAMILY_INFO);
1587 public final static byte[] NAMESPACE_COL_DESC_BYTES = Bytes.toBytes("d");
1588
1589
1590 public static final HTableDescriptor NAMESPACE_TABLEDESC = new HTableDescriptor(
1591 TableName.NAMESPACE_TABLE_NAME,
1592 new HColumnDescriptor[] {
1593 new HColumnDescriptor(NAMESPACE_FAMILY_INFO)
1594
1595 .setMaxVersions(10)
1596 .setInMemory(true)
1597 .setBlocksize(8 * 1024)
1598 .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
1599
1600
1601 .setCacheDataInL1(true)
1602 });
1603
1604 @Deprecated
1605 public HTableDescriptor setOwner(User owner) {
1606 return setOwnerString(owner != null ? owner.getShortName() : null);
1607 }
1608
1609
1610 @Deprecated
1611 public HTableDescriptor setOwnerString(String ownerString) {
1612 if (ownerString != null) {
1613 setValue(OWNER_KEY, ownerString);
1614 } else {
1615 remove(OWNER_KEY);
1616 }
1617 return this;
1618 }
1619
1620 @Deprecated
1621 public String getOwnerString() {
1622 if (getValue(OWNER_KEY) != null) {
1623 return Bytes.toString(getValue(OWNER_KEY));
1624 }
1625
1626
1627
1628 return null;
1629 }
1630
1631
1632
1633
1634
1635 public byte [] toByteArray() {
1636 return ProtobufUtil.prependPBMagic(convert().toByteArray());
1637 }
1638
1639
1640
1641
1642
1643
1644
1645
1646 public static HTableDescriptor parseFrom(final byte [] bytes)
1647 throws DeserializationException, IOException {
1648 if (!ProtobufUtil.isPBMagicPrefix(bytes)) {
1649 return (HTableDescriptor)Writables.getWritable(bytes, new HTableDescriptor());
1650 }
1651 int pblen = ProtobufUtil.lengthOfPBMagic();
1652 TableSchema.Builder builder = TableSchema.newBuilder();
1653 TableSchema ts;
1654 try {
1655 ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen);
1656 ts = builder.build();
1657 } catch (IOException e) {
1658 throw new DeserializationException(e);
1659 }
1660 return convert(ts);
1661 }
1662
1663
1664
1665
1666 @Deprecated
1667 public TableSchema convert() {
1668 TableSchema.Builder builder = TableSchema.newBuilder();
1669 builder.setTableName(ProtobufUtil.toProtoTableName(getTableName()));
1670 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e: this.values.entrySet()) {
1671 BytesBytesPair.Builder aBuilder = BytesBytesPair.newBuilder();
1672 aBuilder.setFirst(ByteStringer.wrap(e.getKey().get()));
1673 aBuilder.setSecond(ByteStringer.wrap(e.getValue().get()));
1674 builder.addAttributes(aBuilder.build());
1675 }
1676 for (HColumnDescriptor hcd: getColumnFamilies()) {
1677 builder.addColumnFamilies(hcd.convert());
1678 }
1679 for (Map.Entry<String, String> e : this.configuration.entrySet()) {
1680 NameStringPair.Builder aBuilder = NameStringPair.newBuilder();
1681 aBuilder.setName(e.getKey());
1682 aBuilder.setValue(e.getValue());
1683 builder.addConfiguration(aBuilder.build());
1684 }
1685 return builder.build();
1686 }
1687
1688
1689
1690
1691
1692 @Deprecated
1693 public static HTableDescriptor convert(final TableSchema ts) {
1694 List<ColumnFamilySchema> list = ts.getColumnFamiliesList();
1695 HColumnDescriptor [] hcds = new HColumnDescriptor[list.size()];
1696 int index = 0;
1697 for (ColumnFamilySchema cfs: list) {
1698 hcds[index++] = HColumnDescriptor.convert(cfs);
1699 }
1700 HTableDescriptor htd = new HTableDescriptor(
1701 ProtobufUtil.toTableName(ts.getTableName()),
1702 hcds);
1703 for (BytesBytesPair a: ts.getAttributesList()) {
1704 htd.setValue(a.getFirst().toByteArray(), a.getSecond().toByteArray());
1705 }
1706 for (NameStringPair a: ts.getConfigurationList()) {
1707 htd.setConfiguration(a.getName(), a.getValue());
1708 }
1709 return htd;
1710 }
1711
1712
1713
1714
1715 public String getConfigurationValue(String key) {
1716 return configuration.get(key);
1717 }
1718
1719
1720
1721
1722 public Map<String, String> getConfiguration() {
1723
1724 return Collections.unmodifiableMap(configuration);
1725 }
1726
1727
1728
1729
1730
1731
1732 public HTableDescriptor setConfiguration(String key, String value) {
1733 if (value == null) {
1734 removeConfiguration(key);
1735 } else {
1736 configuration.put(key, value);
1737 }
1738 return this;
1739 }
1740
1741
1742
1743
1744 public void removeConfiguration(final String key) {
1745 configuration.remove(key);
1746 }
1747
1748 public static HTableDescriptor metaTableDescriptor(final Configuration conf)
1749 throws IOException {
1750 HTableDescriptor metaDescriptor = new HTableDescriptor(
1751 TableName.META_TABLE_NAME,
1752 new HColumnDescriptor[] {
1753 new HColumnDescriptor(HConstants.CATALOG_FAMILY)
1754 .setMaxVersions(conf.getInt(HConstants.HBASE_META_VERSIONS,
1755 HConstants.DEFAULT_HBASE_META_VERSIONS))
1756 .setInMemory(true)
1757 .setBlocksize(conf.getInt(HConstants.HBASE_META_BLOCK_SIZE,
1758 HConstants.DEFAULT_HBASE_META_BLOCK_SIZE))
1759 .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
1760
1761 .setBloomFilterType(BloomType.NONE)
1762 .setCacheDataInL1(true)
1763 });
1764 metaDescriptor.addCoprocessor(
1765 "org.apache.hadoop.hbase.coprocessor.MultiRowMutationEndpoint",
1766 null, Coprocessor.PRIORITY_SYSTEM, null);
1767 return metaDescriptor;
1768 }
1769
1770 }