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 super();
383 setName(desc.name);
384 setMetaFlags(this.name);
385 for (HColumnDescriptor c: desc.families.values()) {
386 this.families.put(c.getName(), new HColumnDescriptor(c));
387 }
388 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
389 desc.values.entrySet()) {
390 setValue(e.getKey(), e.getValue());
391 }
392 for (Map.Entry<String, String> e : desc.configuration.entrySet()) {
393 this.configuration.put(e.getKey(), e.getValue());
394 }
395 }
396
397
398
399
400
401
402
403
404 private void setMetaFlags(final TableName name) {
405 setMetaRegion(isRootRegion() ||
406 name.equals(TableName.META_TABLE_NAME));
407 }
408
409
410
411
412
413
414 public boolean isRootRegion() {
415 if (this.root == null) {
416 this.root = isSomething(IS_ROOT_KEY, false)? Boolean.TRUE: Boolean.FALSE;
417 }
418 return this.root.booleanValue();
419 }
420
421
422
423
424
425
426
427
428 protected void setRootRegion(boolean isRoot) {
429
430 setValue(IS_ROOT_KEY, isRoot ? TRUE : FALSE);
431 }
432
433
434
435
436
437
438
439
440 public boolean isMetaRegion() {
441 if (this.meta == null) {
442 this.meta = calculateIsMetaRegion();
443 }
444 return this.meta.booleanValue();
445 }
446
447 private synchronized Boolean calculateIsMetaRegion() {
448 byte [] value = getValue(IS_META_KEY);
449 return (value != null)? Boolean.valueOf(Bytes.toString(value)): Boolean.FALSE;
450 }
451
452 private boolean isSomething(final ImmutableBytesWritable key,
453 final boolean valueIfNull) {
454 byte [] value = getValue(key);
455 if (value != null) {
456 return Boolean.valueOf(Bytes.toString(value));
457 }
458 return valueIfNull;
459 }
460
461
462
463
464
465
466
467
468
469 protected void setMetaRegion(boolean isMeta) {
470 setValue(IS_META_KEY, isMeta? TRUE: FALSE);
471 }
472
473
474
475
476
477
478 public boolean isMetaTable() {
479 return isMetaRegion() && !isRootRegion();
480 }
481
482
483
484
485
486
487
488
489 public byte[] getValue(byte[] key) {
490 return getValue(new ImmutableBytesWritable(key));
491 }
492
493 private byte[] getValue(final ImmutableBytesWritable key) {
494 ImmutableBytesWritable ibw = values.get(key);
495 if (ibw == null)
496 return null;
497 return ibw.get();
498 }
499
500
501
502
503
504
505
506
507 public String getValue(String key) {
508 byte[] value = getValue(Bytes.toBytes(key));
509 if (value == null)
510 return null;
511 return Bytes.toString(value);
512 }
513
514
515
516
517
518
519
520 public Map<ImmutableBytesWritable,ImmutableBytesWritable> getValues() {
521
522 return Collections.unmodifiableMap(values);
523 }
524
525
526
527
528
529
530
531
532 public HTableDescriptor setValue(byte[] key, byte[] value) {
533 setValue(new ImmutableBytesWritable(key), new ImmutableBytesWritable(value));
534 return this;
535 }
536
537
538
539
540
541 private HTableDescriptor setValue(final ImmutableBytesWritable key,
542 final String value) {
543 setValue(key, new ImmutableBytesWritable(Bytes.toBytes(value)));
544 return this;
545 }
546
547
548
549
550
551
552
553 public HTableDescriptor setValue(final ImmutableBytesWritable key,
554 final ImmutableBytesWritable value) {
555 if (key.compareTo(DEFERRED_LOG_FLUSH_KEY) == 0) {
556 boolean isDeferredFlush = Boolean.valueOf(Bytes.toString(value.get()));
557 LOG.warn("HTableDescriptor property:" + DEFERRED_LOG_FLUSH + " is deprecated, " +
558 "use " + DURABILITY + " instead");
559 setDurability(isDeferredFlush ? Durability.ASYNC_WAL : DEFAULT_DURABLITY);
560 return this;
561 }
562 values.put(key, value);
563 return this;
564 }
565
566
567
568
569
570
571
572
573 public HTableDescriptor setValue(String key, String value) {
574 if (value == null) {
575 remove(key);
576 } else {
577 setValue(Bytes.toBytes(key), Bytes.toBytes(value));
578 }
579 return this;
580 }
581
582
583
584
585
586
587
588 public void remove(final String key) {
589 remove(new ImmutableBytesWritable(Bytes.toBytes(key)));
590 }
591
592
593
594
595
596
597
598 public void remove(ImmutableBytesWritable key) {
599 values.remove(key);
600 }
601
602
603
604
605
606
607
608 public void remove(final byte [] key) {
609 remove(new ImmutableBytesWritable(key));
610 }
611
612
613
614
615
616
617
618 public boolean isReadOnly() {
619 return isSomething(READONLY_KEY, DEFAULT_READONLY);
620 }
621
622
623
624
625
626
627
628
629
630 public HTableDescriptor setReadOnly(final boolean readOnly) {
631 return setValue(READONLY_KEY, readOnly? TRUE: FALSE);
632 }
633
634
635
636
637
638
639
640 public boolean isCompactionEnabled() {
641 return isSomething(COMPACTION_ENABLED_KEY, DEFAULT_COMPACTION_ENABLED);
642 }
643
644
645
646
647
648
649 public HTableDescriptor setCompactionEnabled(final boolean isEnable) {
650 setValue(COMPACTION_ENABLED_KEY, isEnable ? TRUE : FALSE);
651 return this;
652 }
653
654
655
656
657
658
659
660 public boolean isNormalizationEnabled() {
661 return isSomething(NORMALIZATION_ENABLED_KEY, DEFAULT_NORMALIZATION_ENABLED);
662 }
663
664
665
666
667
668
669 public HTableDescriptor setNormalizationEnabled(final boolean isEnable) {
670 setValue(NORMALIZATION_ENABLED_KEY, isEnable ? TRUE : FALSE);
671 return this;
672 }
673
674
675
676
677
678 public HTableDescriptor setDurability(Durability durability) {
679 this.durability = durability;
680 setValue(DURABILITY_KEY, durability.name());
681 return this;
682 }
683
684
685
686
687
688 public Durability getDurability() {
689 if (this.durability == null) {
690 byte[] durabilityValue = getValue(DURABILITY_KEY);
691 if (durabilityValue == null) {
692 this.durability = DEFAULT_DURABLITY;
693 } else {
694 try {
695 this.durability = Durability.valueOf(Bytes.toString(durabilityValue));
696 } catch (IllegalArgumentException ex) {
697 LOG.warn("Received " + ex + " because Durability value for HTableDescriptor"
698 + " is not known. Durability:" + Bytes.toString(durabilityValue));
699 this.durability = DEFAULT_DURABLITY;
700 }
701 }
702 }
703 return this.durability;
704 }
705
706
707
708
709
710
711 public TableName getTableName() {
712 return name;
713 }
714
715
716
717
718
719
720
721 @Deprecated
722 public byte[] getName() {
723 return name.getName();
724 }
725
726
727
728
729
730
731 public String getNameAsString() {
732 return name.getNameAsString();
733 }
734
735
736
737
738
739
740
741 public HTableDescriptor setRegionSplitPolicyClassName(String clazz) {
742 setValue(SPLIT_POLICY, clazz);
743 return this;
744 }
745
746
747
748
749
750
751
752
753
754 public String getRegionSplitPolicyClassName() {
755 return getValue(SPLIT_POLICY);
756 }
757
758
759
760
761
762
763 @Deprecated
764 public HTableDescriptor setName(byte[] name) {
765 setName(TableName.valueOf(name));
766 return this;
767 }
768
769 @Deprecated
770 public HTableDescriptor setName(TableName name) {
771 this.name = name;
772 setMetaFlags(this.name);
773 return this;
774 }
775
776
777
778
779
780
781
782
783
784
785 public long getMaxFileSize() {
786 byte [] value = getValue(MAX_FILESIZE_KEY);
787 if (value != null) {
788 return Long.parseLong(Bytes.toString(value));
789 }
790 return -1;
791 }
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808 public HTableDescriptor setMaxFileSize(long maxFileSize) {
809 setValue(MAX_FILESIZE_KEY, Long.toString(maxFileSize));
810 return this;
811 }
812
813
814
815
816
817
818
819
820 public long getMemStoreFlushSize() {
821 byte [] value = getValue(MEMSTORE_FLUSHSIZE_KEY);
822 if (value != null) {
823 return Long.parseLong(Bytes.toString(value));
824 }
825 return -1;
826 }
827
828
829
830
831
832
833
834 public HTableDescriptor setMemStoreFlushSize(long memstoreFlushSize) {
835 setValue(MEMSTORE_FLUSHSIZE_KEY, Long.toString(memstoreFlushSize));
836 return this;
837 }
838
839
840
841
842
843
844
845 public HTableDescriptor setFlushPolicyClassName(String clazz) {
846 setValue(FLUSH_POLICY, clazz);
847 return this;
848 }
849
850
851
852
853
854
855
856
857 public String getFlushPolicyClassName() {
858 return getValue(FLUSH_POLICY);
859 }
860
861
862
863
864
865
866 public HTableDescriptor addFamily(final HColumnDescriptor family) {
867 if (family.getName() == null || family.getName().length <= 0) {
868 throw new IllegalArgumentException("Family name cannot be null or empty");
869 }
870 if (hasFamily(family.getName())) {
871 throw new IllegalArgumentException("Family '" +
872 family.getNameAsString() + "' already exists so cannot be added");
873 }
874 this.families.put(family.getName(), family);
875 return this;
876 }
877
878
879
880
881
882
883 public HTableDescriptor modifyFamily(final HColumnDescriptor family) {
884 if (family.getName() == null || family.getName().length <= 0) {
885 throw new IllegalArgumentException("Family name cannot be null or empty");
886 }
887 if (!hasFamily(family.getName())) {
888 throw new IllegalArgumentException("Column family '" + family.getNameAsString()
889 + "' does not exist");
890 }
891 this.families.put(family.getName(), family);
892 return this;
893 }
894
895
896
897
898
899
900 public boolean hasFamily(final byte [] familyName) {
901 return families.containsKey(familyName);
902 }
903
904
905
906
907
908
909 @Override
910 public String toString() {
911 StringBuilder s = new StringBuilder();
912 s.append('\'').append(Bytes.toString(name.getName())).append('\'');
913 s.append(getValues(true));
914 for (HColumnDescriptor f : families.values()) {
915 s.append(", ").append(f);
916 }
917 return s.toString();
918 }
919
920
921
922
923
924 public String toStringCustomizedValues() {
925 StringBuilder s = new StringBuilder();
926 s.append('\'').append(Bytes.toString(name.getName())).append('\'');
927 s.append(getValues(false));
928 for(HColumnDescriptor hcd : families.values()) {
929 s.append(", ").append(hcd.toStringCustomizedValues());
930 }
931 return s.toString();
932 }
933
934
935
936
937 public String toStringTableAttributes() {
938 return getValues(true).toString();
939 }
940
941 private StringBuilder getValues(boolean printDefaults) {
942 StringBuilder s = new StringBuilder();
943
944
945 Set<ImmutableBytesWritable> reservedKeys = new TreeSet<ImmutableBytesWritable>();
946 Set<ImmutableBytesWritable> userKeys = new TreeSet<ImmutableBytesWritable>();
947 for (ImmutableBytesWritable k : values.keySet()) {
948 if (k == null || k.get() == null) continue;
949 String key = Bytes.toString(k.get());
950
951 if (!RESERVED_KEYWORDS.contains(k) && !key.startsWith("coprocessor$")) {
952 userKeys.add(k);
953 continue;
954 }
955
956 String value = Bytes.toString(values.get(k).get());
957 if (key.equalsIgnoreCase(IS_ROOT) || key.equalsIgnoreCase(IS_META)) {
958 if (Boolean.valueOf(value) == false) continue;
959 }
960
961 if (printDefaults
962 || !DEFAULT_VALUES.containsKey(key)
963 || !DEFAULT_VALUES.get(key).equalsIgnoreCase(value)) {
964 reservedKeys.add(k);
965 }
966 }
967
968
969 boolean hasAttributes = !reservedKeys.isEmpty() || !userKeys.isEmpty();
970 if (!hasAttributes && configuration.isEmpty()) return s;
971
972 s.append(", {");
973
974 if (hasAttributes) {
975 s.append("TABLE_ATTRIBUTES => {");
976
977
978 boolean printCommaForAttr = false;
979 for (ImmutableBytesWritable k : reservedKeys) {
980 String key = Bytes.toString(k.get());
981 String value = Bytes.toStringBinary(values.get(k).get());
982 if (printCommaForAttr) s.append(", ");
983 printCommaForAttr = true;
984 s.append(key);
985 s.append(" => ");
986 s.append('\'').append(value).append('\'');
987 }
988
989 if (!userKeys.isEmpty()) {
990
991 if (printCommaForAttr) s.append(", ");
992 printCommaForAttr = true;
993 s.append(HConstants.METADATA).append(" => ");
994 s.append("{");
995 boolean printCommaForCfg = false;
996 for (ImmutableBytesWritable k : userKeys) {
997 String key = Bytes.toString(k.get());
998 String value = Bytes.toStringBinary(values.get(k).get());
999 if (printCommaForCfg) s.append(", ");
1000 printCommaForCfg = true;
1001 s.append('\'').append(key).append('\'');
1002 s.append(" => ");
1003 s.append('\'').append(value).append('\'');
1004 }
1005 s.append("}");
1006 }
1007 }
1008
1009
1010 if (!configuration.isEmpty()) {
1011 if (hasAttributes) {
1012 s.append(", ");
1013 }
1014 s.append(HConstants.CONFIGURATION).append(" => ");
1015 s.append('{');
1016 boolean printCommaForConfig = false;
1017 for (Map.Entry<String, String> e : configuration.entrySet()) {
1018 if (printCommaForConfig) s.append(", ");
1019 printCommaForConfig = true;
1020 s.append('\'').append(e.getKey()).append('\'');
1021 s.append(" => ");
1022 s.append('\'').append(e.getValue()).append('\'');
1023 }
1024 s.append("}");
1025 }
1026 s.append("}");
1027 return s;
1028 }
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039 @Override
1040 public boolean equals(Object obj) {
1041 if (this == obj) {
1042 return true;
1043 }
1044 if (obj == null) {
1045 return false;
1046 }
1047 if (!(obj instanceof HTableDescriptor)) {
1048 return false;
1049 }
1050 return compareTo((HTableDescriptor)obj) == 0;
1051 }
1052
1053
1054
1055
1056 @Override
1057 public int hashCode() {
1058 int result = this.name.hashCode();
1059 result ^= Byte.valueOf(TABLE_DESCRIPTOR_VERSION).hashCode();
1060 if (this.families != null && this.families.size() > 0) {
1061 for (HColumnDescriptor e: this.families.values()) {
1062 result ^= e.hashCode();
1063 }
1064 }
1065 result ^= values.hashCode();
1066 result ^= configuration.hashCode();
1067 return result;
1068 }
1069
1070
1071
1072
1073
1074
1075 @Deprecated
1076 @Override
1077 public void readFields(DataInput in) throws IOException {
1078 int version = in.readInt();
1079 if (version < 3)
1080 throw new IOException("versions < 3 are not supported (and never existed!?)");
1081
1082 name = TableName.valueOf(Bytes.readByteArray(in));
1083 setRootRegion(in.readBoolean());
1084 setMetaRegion(in.readBoolean());
1085 values.clear();
1086 configuration.clear();
1087 int numVals = in.readInt();
1088 for (int i = 0; i < numVals; i++) {
1089 ImmutableBytesWritable key = new ImmutableBytesWritable();
1090 ImmutableBytesWritable value = new ImmutableBytesWritable();
1091 key.readFields(in);
1092 value.readFields(in);
1093 setValue(key, value);
1094 }
1095 families.clear();
1096 int numFamilies = in.readInt();
1097 for (int i = 0; i < numFamilies; i++) {
1098 HColumnDescriptor c = new HColumnDescriptor();
1099 c.readFields(in);
1100 families.put(c.getName(), c);
1101 }
1102 if (version >= 7) {
1103 int numConfigs = in.readInt();
1104 for (int i = 0; i < numConfigs; i++) {
1105 ImmutableBytesWritable key = new ImmutableBytesWritable();
1106 ImmutableBytesWritable value = new ImmutableBytesWritable();
1107 key.readFields(in);
1108 value.readFields(in);
1109 configuration.put(
1110 Bytes.toString(key.get(), key.getOffset(), key.getLength()),
1111 Bytes.toString(value.get(), value.getOffset(), value.getLength()));
1112 }
1113 }
1114 }
1115
1116
1117
1118
1119
1120
1121
1122 @Deprecated
1123 @Override
1124 public void write(DataOutput out) throws IOException {
1125 out.writeInt(TABLE_DESCRIPTOR_VERSION);
1126 Bytes.writeByteArray(out, name.toBytes());
1127 out.writeBoolean(isRootRegion());
1128 out.writeBoolean(isMetaRegion());
1129 out.writeInt(values.size());
1130 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
1131 values.entrySet()) {
1132 e.getKey().write(out);
1133 e.getValue().write(out);
1134 }
1135 out.writeInt(families.size());
1136 for(Iterator<HColumnDescriptor> it = families.values().iterator();
1137 it.hasNext(); ) {
1138 HColumnDescriptor family = it.next();
1139 family.write(out);
1140 }
1141 out.writeInt(configuration.size());
1142 for (Map.Entry<String, String> e : configuration.entrySet()) {
1143 new ImmutableBytesWritable(Bytes.toBytes(e.getKey())).write(out);
1144 new ImmutableBytesWritable(Bytes.toBytes(e.getValue())).write(out);
1145 }
1146 }
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157 @Override
1158 public int compareTo(final HTableDescriptor other) {
1159 int result = this.name.compareTo(other.name);
1160 if (result == 0) {
1161 result = families.size() - other.families.size();
1162 }
1163 if (result == 0 && families.size() != other.families.size()) {
1164 result = Integer.valueOf(families.size()).compareTo(
1165 Integer.valueOf(other.families.size()));
1166 }
1167 if (result == 0) {
1168 for (Iterator<HColumnDescriptor> it = families.values().iterator(),
1169 it2 = other.families.values().iterator(); it.hasNext(); ) {
1170 result = it.next().compareTo(it2.next());
1171 if (result != 0) {
1172 break;
1173 }
1174 }
1175 }
1176 if (result == 0) {
1177
1178 result = this.values.hashCode() - other.values.hashCode();
1179 if (result < 0)
1180 result = -1;
1181 else if (result > 0)
1182 result = 1;
1183 }
1184 if (result == 0) {
1185 result = this.configuration.hashCode() - other.configuration.hashCode();
1186 if (result < 0)
1187 result = -1;
1188 else if (result > 0)
1189 result = 1;
1190 }
1191 return result;
1192 }
1193
1194
1195
1196
1197
1198
1199
1200
1201 public Collection<HColumnDescriptor> getFamilies() {
1202 return Collections.unmodifiableCollection(this.families.values());
1203 }
1204
1205
1206
1207
1208 public int getRegionReplication() {
1209 return getIntValue(REGION_REPLICATION_KEY, DEFAULT_REGION_REPLICATION);
1210 }
1211
1212 private int getIntValue(ImmutableBytesWritable key, int defaultVal) {
1213 byte[] val = getValue(key);
1214 if (val == null || val.length == 0) {
1215 return defaultVal;
1216 }
1217 return Integer.parseInt(Bytes.toString(val));
1218 }
1219
1220
1221
1222
1223
1224 public HTableDescriptor setRegionReplication(int regionReplication) {
1225 setValue(REGION_REPLICATION_KEY,
1226 new ImmutableBytesWritable(Bytes.toBytes(Integer.toString(regionReplication))));
1227 return this;
1228 }
1229
1230
1231
1232
1233 public boolean hasRegionMemstoreReplication() {
1234 return isSomething(REGION_MEMSTORE_REPLICATION_KEY, DEFAULT_REGION_MEMSTORE_REPLICATION);
1235 }
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246 public HTableDescriptor setRegionMemstoreReplication(boolean memstoreReplication) {
1247 setValue(REGION_MEMSTORE_REPLICATION_KEY, memstoreReplication ? TRUE : FALSE);
1248
1249
1250 setConfiguration(RegionReplicaUtil.REGION_REPLICA_WAIT_FOR_PRIMARY_FLUSH_CONF_KEY,
1251 Boolean.toString(memstoreReplication));
1252 return this;
1253 }
1254
1255 public HTableDescriptor setPriority(int priority) {
1256 setValue(PRIORITY_KEY, Integer.toString(priority));
1257 return this;
1258 }
1259
1260 public int getPriority() {
1261 return getIntValue(PRIORITY_KEY, DEFAULT_PRIORITY);
1262 }
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272 public Set<byte[]> getFamiliesKeys() {
1273 return Collections.unmodifiableSet(this.families.keySet());
1274 }
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284 public HColumnDescriptor[] getColumnFamilies() {
1285 Collection<HColumnDescriptor> hColumnDescriptors = getFamilies();
1286 return hColumnDescriptors.toArray(new HColumnDescriptor[hColumnDescriptors.size()]);
1287 }
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298 public HColumnDescriptor getFamily(final byte [] column) {
1299 return this.families.get(column);
1300 }
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311 public HColumnDescriptor removeFamily(final byte [] column) {
1312 return this.families.remove(column);
1313 }
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326 public HTableDescriptor addCoprocessor(String className) throws IOException {
1327 addCoprocessor(className, null, Coprocessor.PRIORITY_USER, null);
1328 return this;
1329 }
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346 public HTableDescriptor addCoprocessor(String className, Path jarFilePath,
1347 int priority, final Map<String, String> kvs)
1348 throws IOException {
1349 if (hasCoprocessor(className)) {
1350 throw new IOException("Coprocessor " + className + " already exists.");
1351 }
1352
1353 StringBuilder kvString = new StringBuilder();
1354 if (kvs != null) {
1355 for (Map.Entry<String, String> e: kvs.entrySet()) {
1356 if (!e.getKey().matches(HConstants.CP_HTD_ATTR_VALUE_PARAM_KEY_PATTERN)) {
1357 throw new IOException("Illegal parameter key = " + e.getKey());
1358 }
1359 if (!e.getValue().matches(HConstants.CP_HTD_ATTR_VALUE_PARAM_VALUE_PATTERN)) {
1360 throw new IOException("Illegal parameter (" + e.getKey() +
1361 ") value = " + e.getValue());
1362 }
1363 if (kvString.length() != 0) {
1364 kvString.append(',');
1365 }
1366 kvString.append(e.getKey());
1367 kvString.append('=');
1368 kvString.append(e.getValue());
1369 }
1370 }
1371
1372
1373 int maxCoprocessorNumber = 0;
1374 Matcher keyMatcher;
1375 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
1376 this.values.entrySet()) {
1377 keyMatcher =
1378 HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(
1379 Bytes.toString(e.getKey().get()));
1380 if (!keyMatcher.matches()) {
1381 continue;
1382 }
1383 maxCoprocessorNumber = Math.max(Integer.parseInt(keyMatcher.group(1)),
1384 maxCoprocessorNumber);
1385 }
1386 maxCoprocessorNumber++;
1387
1388 String key = "coprocessor$" + Integer.toString(maxCoprocessorNumber);
1389 String value = ((jarFilePath == null)? "" : jarFilePath.toString()) +
1390 "|" + className + "|" + Integer.toString(priority) + "|" +
1391 kvString.toString();
1392 setValue(key, value);
1393 return this;
1394 }
1395
1396
1397
1398
1399
1400
1401
1402
1403 public boolean hasCoprocessor(String className) {
1404 Matcher keyMatcher;
1405 Matcher valueMatcher;
1406 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
1407 this.values.entrySet()) {
1408 keyMatcher =
1409 HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(
1410 Bytes.toString(e.getKey().get()));
1411 if (!keyMatcher.matches()) {
1412 continue;
1413 }
1414 valueMatcher =
1415 HConstants.CP_HTD_ATTR_VALUE_PATTERN.matcher(
1416 Bytes.toString(e.getValue().get()));
1417 if (!valueMatcher.matches()) {
1418 continue;
1419 }
1420
1421 String clazz = valueMatcher.group(2).trim();
1422 if (clazz.equals(className.trim())) {
1423 return true;
1424 }
1425 }
1426 return false;
1427 }
1428
1429
1430
1431
1432
1433
1434 public List<String> getCoprocessors() {
1435 List<String> result = new ArrayList<String>();
1436 Matcher keyMatcher;
1437 Matcher valueMatcher;
1438 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e : this.values.entrySet()) {
1439 keyMatcher = HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(Bytes.toString(e.getKey().get()));
1440 if (!keyMatcher.matches()) {
1441 continue;
1442 }
1443 valueMatcher = HConstants.CP_HTD_ATTR_VALUE_PATTERN.matcher(Bytes
1444 .toString(e.getValue().get()));
1445 if (!valueMatcher.matches()) {
1446 continue;
1447 }
1448 result.add(valueMatcher.group(2).trim());
1449 }
1450 return result;
1451 }
1452
1453
1454
1455
1456
1457 public void removeCoprocessor(String className) {
1458 ImmutableBytesWritable match = null;
1459 Matcher keyMatcher;
1460 Matcher valueMatcher;
1461 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e : this.values
1462 .entrySet()) {
1463 keyMatcher = HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(Bytes.toString(e
1464 .getKey().get()));
1465 if (!keyMatcher.matches()) {
1466 continue;
1467 }
1468 valueMatcher = HConstants.CP_HTD_ATTR_VALUE_PATTERN.matcher(Bytes
1469 .toString(e.getValue().get()));
1470 if (!valueMatcher.matches()) {
1471 continue;
1472 }
1473
1474 String clazz = valueMatcher.group(2).trim();
1475
1476 if (clazz.equals(className.trim())) {
1477 match = e.getKey();
1478 break;
1479 }
1480 }
1481
1482 if (match != null)
1483 remove(match);
1484 }
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496 @Deprecated
1497 public static Path getTableDir(Path rootdir, final byte [] tableName) {
1498
1499
1500 TableName name = TableName.valueOf(tableName);
1501 return new Path(rootdir, new Path(HConstants.BASE_NAMESPACE_DIR,
1502 new Path(name.getNamespaceAsString(), new Path(name.getQualifierAsString()))));
1503 }
1504
1505
1506
1507
1508
1509
1510 @Deprecated
1511 public static final HTableDescriptor META_TABLEDESC = new HTableDescriptor(
1512 TableName.META_TABLE_NAME,
1513 new HColumnDescriptor[] {
1514 new HColumnDescriptor(HConstants.CATALOG_FAMILY)
1515
1516 .setMaxVersions(HConstants.DEFAULT_HBASE_META_VERSIONS)
1517 .setInMemory(true)
1518 .setBlocksize(HConstants.DEFAULT_HBASE_META_BLOCK_SIZE)
1519 .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
1520
1521 .setBloomFilterType(BloomType.NONE)
1522
1523
1524 .setCacheDataInL1(true)
1525 });
1526
1527 static {
1528 try {
1529 META_TABLEDESC.addCoprocessor(
1530 "org.apache.hadoop.hbase.coprocessor.MultiRowMutationEndpoint",
1531 null, Coprocessor.PRIORITY_SYSTEM, null);
1532 } catch (IOException ex) {
1533
1534 throw new RuntimeException(ex);
1535 }
1536 }
1537
1538 public final static String NAMESPACE_FAMILY_INFO = "info";
1539 public final static byte[] NAMESPACE_FAMILY_INFO_BYTES = Bytes.toBytes(NAMESPACE_FAMILY_INFO);
1540 public final static byte[] NAMESPACE_COL_DESC_BYTES = Bytes.toBytes("d");
1541
1542
1543 public static final HTableDescriptor NAMESPACE_TABLEDESC = new HTableDescriptor(
1544 TableName.NAMESPACE_TABLE_NAME,
1545 new HColumnDescriptor[] {
1546 new HColumnDescriptor(NAMESPACE_FAMILY_INFO)
1547
1548 .setMaxVersions(10)
1549 .setInMemory(true)
1550 .setBlocksize(8 * 1024)
1551 .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
1552
1553
1554 .setCacheDataInL1(true)
1555 });
1556
1557 @Deprecated
1558 public HTableDescriptor setOwner(User owner) {
1559 return setOwnerString(owner != null ? owner.getShortName() : null);
1560 }
1561
1562
1563 @Deprecated
1564 public HTableDescriptor setOwnerString(String ownerString) {
1565 if (ownerString != null) {
1566 setValue(OWNER_KEY, ownerString);
1567 } else {
1568 remove(OWNER_KEY);
1569 }
1570 return this;
1571 }
1572
1573 @Deprecated
1574 public String getOwnerString() {
1575 if (getValue(OWNER_KEY) != null) {
1576 return Bytes.toString(getValue(OWNER_KEY));
1577 }
1578
1579
1580
1581 return null;
1582 }
1583
1584
1585
1586
1587
1588 public byte [] toByteArray() {
1589 return ProtobufUtil.prependPBMagic(convert().toByteArray());
1590 }
1591
1592
1593
1594
1595
1596
1597
1598
1599 public static HTableDescriptor parseFrom(final byte [] bytes)
1600 throws DeserializationException, IOException {
1601 if (!ProtobufUtil.isPBMagicPrefix(bytes)) {
1602 return (HTableDescriptor)Writables.getWritable(bytes, new HTableDescriptor());
1603 }
1604 int pblen = ProtobufUtil.lengthOfPBMagic();
1605 TableSchema.Builder builder = TableSchema.newBuilder();
1606 TableSchema ts;
1607 try {
1608 ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen);
1609 ts = builder.build();
1610 } catch (IOException e) {
1611 throw new DeserializationException(e);
1612 }
1613 return convert(ts);
1614 }
1615
1616
1617
1618
1619 public TableSchema convert() {
1620 TableSchema.Builder builder = TableSchema.newBuilder();
1621 builder.setTableName(ProtobufUtil.toProtoTableName(getTableName()));
1622 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e: this.values.entrySet()) {
1623 BytesBytesPair.Builder aBuilder = BytesBytesPair.newBuilder();
1624 aBuilder.setFirst(ByteStringer.wrap(e.getKey().get()));
1625 aBuilder.setSecond(ByteStringer.wrap(e.getValue().get()));
1626 builder.addAttributes(aBuilder.build());
1627 }
1628 for (HColumnDescriptor hcd: getColumnFamilies()) {
1629 builder.addColumnFamilies(hcd.convert());
1630 }
1631 for (Map.Entry<String, String> e : this.configuration.entrySet()) {
1632 NameStringPair.Builder aBuilder = NameStringPair.newBuilder();
1633 aBuilder.setName(e.getKey());
1634 aBuilder.setValue(e.getValue());
1635 builder.addConfiguration(aBuilder.build());
1636 }
1637 return builder.build();
1638 }
1639
1640
1641
1642
1643
1644 public static HTableDescriptor convert(final TableSchema ts) {
1645 List<ColumnFamilySchema> list = ts.getColumnFamiliesList();
1646 HColumnDescriptor [] hcds = new HColumnDescriptor[list.size()];
1647 int index = 0;
1648 for (ColumnFamilySchema cfs: list) {
1649 hcds[index++] = HColumnDescriptor.convert(cfs);
1650 }
1651 HTableDescriptor htd = new HTableDescriptor(
1652 ProtobufUtil.toTableName(ts.getTableName()),
1653 hcds);
1654 for (BytesBytesPair a: ts.getAttributesList()) {
1655 htd.setValue(a.getFirst().toByteArray(), a.getSecond().toByteArray());
1656 }
1657 for (NameStringPair a: ts.getConfigurationList()) {
1658 htd.setConfiguration(a.getName(), a.getValue());
1659 }
1660 return htd;
1661 }
1662
1663
1664
1665
1666 public String getConfigurationValue(String key) {
1667 return configuration.get(key);
1668 }
1669
1670
1671
1672
1673 public Map<String, String> getConfiguration() {
1674
1675 return Collections.unmodifiableMap(configuration);
1676 }
1677
1678
1679
1680
1681
1682
1683 public HTableDescriptor setConfiguration(String key, String value) {
1684 if (value == null) {
1685 removeConfiguration(key);
1686 } else {
1687 configuration.put(key, value);
1688 }
1689 return this;
1690 }
1691
1692
1693
1694
1695 public void removeConfiguration(final String key) {
1696 configuration.remove(key);
1697 }
1698
1699 public static HTableDescriptor metaTableDescriptor(final Configuration conf)
1700 throws IOException {
1701 HTableDescriptor metaDescriptor = new HTableDescriptor(
1702 TableName.META_TABLE_NAME,
1703 new HColumnDescriptor[] {
1704 new HColumnDescriptor(HConstants.CATALOG_FAMILY)
1705 .setMaxVersions(conf.getInt(HConstants.HBASE_META_VERSIONS,
1706 HConstants.DEFAULT_HBASE_META_VERSIONS))
1707 .setInMemory(true)
1708 .setBlocksize(conf.getInt(HConstants.HBASE_META_BLOCK_SIZE,
1709 HConstants.DEFAULT_HBASE_META_BLOCK_SIZE))
1710 .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
1711
1712 .setBloomFilterType(BloomType.NONE)
1713 });
1714 metaDescriptor.addCoprocessor(
1715 "org.apache.hadoop.hbase.coprocessor.MultiRowMutationEndpoint",
1716 null, Coprocessor.PRIORITY_SYSTEM, null);
1717 return metaDescriptor;
1718 }
1719
1720 }