-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
HTableDescriptor.java
1604 lines (1459 loc) · 52.5 KB
/
HTableDescriptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.regex.Matcher;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.classification.InterfaceStability;
import org.apache.hadoop.hbase.client.Durability;
import org.apache.hadoop.hbase.client.RegionReplicaUtil;
import org.apache.hadoop.hbase.exceptions.DeserializationException;
import org.apache.hadoop.hbase.security.User;
import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.TableSchema;
import org.apache.hadoop.hbase.util.Bytes;
/**
* HTableDescriptor contains the details about an HBase table such as the descriptors of
* all the column families, is the table a catalog table, <code> -ROOT- </code> or
* <code> hbase:meta </code>, if the table is read only, the maximum size of the memstore,
* when the region split should occur, coprocessors associated with it etc...
*/
@InterfaceAudience.Public
@InterfaceStability.Evolving
public class HTableDescriptor implements Comparable<HTableDescriptor> {
private static final Log LOG = LogFactory.getLog(HTableDescriptor.class);
private TableName name = null;
/**
* A map which holds the metadata information of the table. This metadata
* includes values like IS_ROOT, IS_META, DEFERRED_LOG_FLUSH, SPLIT_POLICY,
* MAX_FILE_SIZE, READONLY, MEMSTORE_FLUSHSIZE etc...
*/
private final Map<Bytes, Bytes> values =
new HashMap<Bytes, Bytes>();
/**
* A map which holds the configuration specific to the table.
* The keys of the map have the same names as config keys and override the defaults with
* table-specific settings. Example usage may be for compactions, etc.
*/
private final Map<String, String> configuration = new HashMap<String, String>();
public static final String SPLIT_POLICY = "SPLIT_POLICY";
/**
* <em>INTERNAL</em> Used by HBase Shell interface to access this metadata
* attribute which denotes the maximum size of the store file after which
* a region split occurs
*
* @see #getMaxFileSize()
*/
public static final String MAX_FILESIZE = "MAX_FILESIZE";
private static final Bytes MAX_FILESIZE_KEY =
new Bytes(Bytes.toBytes(MAX_FILESIZE));
public static final String OWNER = "OWNER";
public static final Bytes OWNER_KEY =
new Bytes(Bytes.toBytes(OWNER));
/**
* <em>INTERNAL</em> Used by rest interface to access this metadata
* attribute which denotes if the table is Read Only
*
* @see #isReadOnly()
*/
public static final String READONLY = "READONLY";
private static final Bytes READONLY_KEY =
new Bytes(Bytes.toBytes(READONLY));
/**
* <em>INTERNAL</em> Used by HBase Shell interface to access this metadata
* attribute which denotes if the table is compaction enabled
*
* @see #isCompactionEnabled()
*/
public static final String COMPACTION_ENABLED = "COMPACTION_ENABLED";
private static final Bytes COMPACTION_ENABLED_KEY =
new Bytes(Bytes.toBytes(COMPACTION_ENABLED));
/**
* <em>INTERNAL</em> Used by HBase Shell interface to access this metadata
* attribute which represents the maximum size of the memstore after which
* its contents are flushed onto the disk
*
* @see #getMemStoreFlushSize()
*/
public static final String MEMSTORE_FLUSHSIZE = "MEMSTORE_FLUSHSIZE";
private static final Bytes MEMSTORE_FLUSHSIZE_KEY =
new Bytes(Bytes.toBytes(MEMSTORE_FLUSHSIZE));
public static final String FLUSH_POLICY = "FLUSH_POLICY";
/**
* <em>INTERNAL</em> Used by rest interface to access this metadata
* attribute which denotes if the table is a -ROOT- region or not
*
* @see #isRootRegion()
*/
public static final String IS_ROOT = "IS_ROOT";
private static final Bytes IS_ROOT_KEY =
new Bytes(Bytes.toBytes(IS_ROOT));
/**
* <em>INTERNAL</em> Used by rest interface to access this metadata
* attribute which denotes if it is a catalog table, either
* <code> hbase:meta </code> or <code> -ROOT- </code>
*
* @see #isMetaRegion()
*/
public static final String IS_META = "IS_META";
private static final Bytes IS_META_KEY =
new Bytes(Bytes.toBytes(IS_META));
/**
* <em>INTERNAL</em> Used by HBase Shell interface to access this metadata
* attribute which denotes if the deferred log flush option is enabled.
* @deprecated Use {@link #DURABILITY} instead.
*/
@Deprecated
public static final String DEFERRED_LOG_FLUSH = "DEFERRED_LOG_FLUSH";
@Deprecated
private static final Bytes DEFERRED_LOG_FLUSH_KEY =
new Bytes(Bytes.toBytes(DEFERRED_LOG_FLUSH));
/**
* <em>INTERNAL</em> {@link Durability} setting for the table.
*/
public static final String DURABILITY = "DURABILITY";
private static final Bytes DURABILITY_KEY =
new Bytes(Bytes.toBytes("DURABILITY"));
/**
* <em>INTERNAL</em> number of region replicas for the table.
*/
public static final String REGION_REPLICATION = "REGION_REPLICATION";
private static final Bytes REGION_REPLICATION_KEY =
new Bytes(Bytes.toBytes(REGION_REPLICATION));
/**
* <em>INTERNAL</em> flag to indicate whether or not the memstore should be replicated
* for read-replicas (CONSISTENCY => TIMELINE).
*/
public static final String REGION_MEMSTORE_REPLICATION = "REGION_MEMSTORE_REPLICATION";
private static final Bytes REGION_MEMSTORE_REPLICATION_KEY =
new Bytes(Bytes.toBytes(REGION_MEMSTORE_REPLICATION));
/**
* <em>INTERNAL</em> Used by shell/rest interface to access this metadata
* attribute which denotes if the table should be treated by region normalizer.
*
* @see #isNormalizationEnabled()
*/
public static final String NORMALIZATION_ENABLED = "NORMALIZATION_ENABLED";
private static final Bytes NORMALIZATION_ENABLED_KEY =
new Bytes(Bytes.toBytes(NORMALIZATION_ENABLED));
/** Default durability for HTD is USE_DEFAULT, which defaults to HBase-global default value */
private static final Durability DEFAULT_DURABLITY = Durability.USE_DEFAULT;
public static final String PRIORITY = "PRIORITY";
private static final Bytes PRIORITY_KEY =
new Bytes(Bytes.toBytes(PRIORITY));
/** Relative priority of the table used for rpc scheduling */
private static final int DEFAULT_PRIORITY = HConstants.NORMAL_QOS;
/*
* The below are ugly but better than creating them each time till we
* replace booleans being saved as Strings with plain booleans. Need a
* migration script to do this. TODO.
*/
private static final Bytes FALSE =
new Bytes(Bytes.toBytes(Boolean.FALSE.toString()));
private static final Bytes TRUE =
new Bytes(Bytes.toBytes(Boolean.TRUE.toString()));
private static final boolean DEFAULT_DEFERRED_LOG_FLUSH = false;
/**
* Constant that denotes whether the table is READONLY by default and is false
*/
public static final boolean DEFAULT_READONLY = false;
/**
* Constant that denotes whether the table is compaction enabled by default
*/
public static final boolean DEFAULT_COMPACTION_ENABLED = true;
/**
* Constant that denotes whether the table is normalized by default.
*/
public static final boolean DEFAULT_NORMALIZATION_ENABLED = false;
/**
* Constant that denotes the maximum default size of the memstore after which
* the contents are flushed to the store files
*/
public static final long DEFAULT_MEMSTORE_FLUSH_SIZE = 1024*1024*128L;
public static final int DEFAULT_REGION_REPLICATION = 1;
public static final boolean DEFAULT_REGION_MEMSTORE_REPLICATION = true;
private final static Map<String, String> DEFAULT_VALUES
= new HashMap<String, String>();
private final static Set<Bytes> RESERVED_KEYWORDS
= new HashSet<Bytes>();
static {
DEFAULT_VALUES.put(MAX_FILESIZE,
String.valueOf(HConstants.DEFAULT_MAX_FILE_SIZE));
DEFAULT_VALUES.put(READONLY, String.valueOf(DEFAULT_READONLY));
DEFAULT_VALUES.put(MEMSTORE_FLUSHSIZE,
String.valueOf(DEFAULT_MEMSTORE_FLUSH_SIZE));
DEFAULT_VALUES.put(DEFERRED_LOG_FLUSH,
String.valueOf(DEFAULT_DEFERRED_LOG_FLUSH));
DEFAULT_VALUES.put(DURABILITY, DEFAULT_DURABLITY.name()); //use the enum name
DEFAULT_VALUES.put(REGION_REPLICATION, String.valueOf(DEFAULT_REGION_REPLICATION));
DEFAULT_VALUES.put(NORMALIZATION_ENABLED, String.valueOf(DEFAULT_NORMALIZATION_ENABLED));
DEFAULT_VALUES.put(PRIORITY, String.valueOf(DEFAULT_PRIORITY));
for (String s : DEFAULT_VALUES.keySet()) {
RESERVED_KEYWORDS.add(new Bytes(Bytes.toBytes(s)));
}
RESERVED_KEYWORDS.add(IS_ROOT_KEY);
RESERVED_KEYWORDS.add(IS_META_KEY);
}
/**
* Cache of whether this is a meta table or not.
*/
private volatile Boolean meta = null;
/**
* Cache of whether this is root table or not.
*/
private volatile Boolean root = null;
/**
* Durability setting for the table
*/
private Durability durability = null;
/**
* Maps column family name to the respective HColumnDescriptors
*/
private final Map<byte [], HColumnDescriptor> families =
new TreeMap<byte [], HColumnDescriptor>(Bytes.BYTES_RAWCOMPARATOR);
/**
* <em> INTERNAL </em> Private constructor used internally creating table descriptors for
* catalog tables, <code>hbase:meta</code> and <code>-ROOT-</code>.
*/
@InterfaceAudience.Private
protected HTableDescriptor(final TableName name, HColumnDescriptor[] families) {
setName(name);
for(HColumnDescriptor descriptor : families) {
this.families.put(descriptor.getName(), descriptor);
}
}
/**
* <em> INTERNAL </em>Private constructor used internally creating table descriptors for
* catalog tables, <code>hbase:meta</code> and <code>-ROOT-</code>.
*/
protected HTableDescriptor(final TableName name, HColumnDescriptor[] families,
Map<Bytes, Bytes> values) {
setName(name);
for(HColumnDescriptor descriptor : families) {
this.families.put(descriptor.getName(), descriptor);
}
for (Map.Entry<Bytes, Bytes> entry :
values.entrySet()) {
setValue(entry.getKey(), entry.getValue());
}
}
/**
* Default constructor which constructs an empty object.
* For deserializing an HTableDescriptor instance only.
* @deprecated As of release 0.96 (<a href="https://issues.apache.org/jira/browse/HBASE-5453">HBASE-5453</a>).
* This was made protected in 2.0.0 and will be removed in HBase 3.0.0.
* Used by Writables and Writables are going away.
*/
@Deprecated
protected HTableDescriptor() {
super();
}
/**
* Construct a table descriptor specifying a TableName object
* @param name Table name.
* @see <a href="https://issues.apache.org/jira/browse/HBASE-174">HADOOP-1581 HBASE: (HBASE-174) Un-openable tablename bug</a>
*/
public HTableDescriptor(final TableName name) {
super();
setName(name);
}
/**
* Construct a table descriptor specifying a byte array table name
* @param name Table name.
* @see <a href="https://issues.apache.org/jira/browse/HBASE-174">HADOOP-1581 (HBASE-174) HBASE: Un-openable tablename bug</a>
*/
@Deprecated
public HTableDescriptor(final byte[] name) {
this(TableName.valueOf(name));
}
/**
* Construct a table descriptor specifying a String table name
* @param name Table name.
* @see <a href="https://issues.apache.org/jira/browse/HBASE-174">HADOOP-1581 (HBASE-174) HBASE: Un-openable tablename bug</a>
*/
@Deprecated
public HTableDescriptor(final String name) {
this(TableName.valueOf(name));
}
/**
* Construct a table descriptor by cloning the descriptor passed as a parameter.
* <p>
* Makes a deep copy of the supplied descriptor.
* Can make a modifiable descriptor from an UnmodifyableHTableDescriptor.
* @param desc The descriptor.
*/
public HTableDescriptor(final HTableDescriptor desc) {
this(desc.name, desc);
}
/**
* Construct a table descriptor by cloning the descriptor passed as a parameter
* but using a different table name.
* <p>
* Makes a deep copy of the supplied descriptor.
* Can make a modifiable descriptor from an UnmodifyableHTableDescriptor.
* @param name Table name.
* @param desc The descriptor.
*/
public HTableDescriptor(final TableName name, final HTableDescriptor desc) {
super();
setName(name);
setMetaFlags(this.name);
for (HColumnDescriptor c: desc.families.values()) {
this.families.put(c.getName(), new HColumnDescriptor(c));
}
for (Map.Entry<Bytes, Bytes> e :
desc.values.entrySet()) {
setValue(e.getKey(), e.getValue());
}
for (Map.Entry<String, String> e : desc.configuration.entrySet()) {
this.configuration.put(e.getKey(), e.getValue());
}
}
/*
* Set meta flags on this table.
* IS_ROOT_KEY is set if its a -ROOT- table
* IS_META_KEY is set either if its a -ROOT- or a hbase:meta table
* Called by constructors.
* @param name
*/
private void setMetaFlags(final TableName name) {
setMetaRegion(isRootRegion() ||
name.equals(TableName.META_TABLE_NAME));
}
/**
* Check if the descriptor represents a <code> -ROOT- </code> region.
*
* @return true if this is a <code> -ROOT- </code> region
*/
public boolean isRootRegion() {
if (this.root == null) {
this.root = isSomething(IS_ROOT_KEY, false)? Boolean.TRUE: Boolean.FALSE;
}
return this.root.booleanValue();
}
/**
* <em> INTERNAL </em> Used to denote if the current table represents
* <code> -ROOT- </code> region. This is used internally by the
* HTableDescriptor constructors
*
* @param isRoot true if this is the <code> -ROOT- </code> region
*/
protected void setRootRegion(boolean isRoot) {
// TODO: Make the value a boolean rather than String of boolean.
setValue(IS_ROOT_KEY, isRoot? TRUE: FALSE);
}
/**
* Checks if this table is <code> hbase:meta </code>
* region.
*
* @return true if this table is <code> hbase:meta </code>
* region
*/
public boolean isMetaRegion() {
if (this.meta == null) {
this.meta = calculateIsMetaRegion();
}
return this.meta.booleanValue();
}
private synchronized Boolean calculateIsMetaRegion() {
byte [] value = getValue(IS_META_KEY);
return (value != null)? Boolean.valueOf(Bytes.toString(value)): Boolean.FALSE;
}
private boolean isSomething(final Bytes key,
final boolean valueIfNull) {
byte [] value = getValue(key);
if (value != null) {
return Boolean.valueOf(Bytes.toString(value));
}
return valueIfNull;
}
/**
* <em> INTERNAL </em> Used to denote if the current table represents
* <code> -ROOT- </code> or <code> hbase:meta </code> region. This is used
* internally by the HTableDescriptor constructors
*
* @param isMeta true if its either <code> -ROOT- </code> or
* <code> hbase:meta </code> region
*/
protected void setMetaRegion(boolean isMeta) {
setValue(IS_META_KEY, isMeta? TRUE: FALSE);
}
/**
* Checks if the table is a <code>hbase:meta</code> table
*
* @return true if table is <code> hbase:meta </code> region.
*/
public boolean isMetaTable() {
return isMetaRegion() && !isRootRegion();
}
/**
* Getter for accessing the metadata associated with the key
*
* @param key The key.
* @return The value.
* @see #values
*/
public byte[] getValue(byte[] key) {
return getValue(new Bytes(key));
}
private byte[] getValue(final Bytes key) {
Bytes ibw = values.get(key);
if (ibw == null)
return null;
return ibw.get();
}
/**
* Getter for accessing the metadata associated with the key
*
* @param key The key.
* @return The value.
* @see #values
*/
public String getValue(String key) {
byte[] value = getValue(Bytes.toBytes(key));
if (value == null)
return null;
return Bytes.toString(value);
}
/**
* Getter for fetching an unmodifiable {@link #values} map.
*
* @return unmodifiable map {@link #values}.
* @see #values
*/
public Map<Bytes, Bytes> getValues() {
// shallow pointer copy
return Collections.unmodifiableMap(values);
}
/**
* Setter for storing metadata as a (key, value) pair in {@link #values} map
*
* @param key The key.
* @param value The value.
* @see #values
*/
public HTableDescriptor setValue(byte[] key, byte[] value) {
setValue(new Bytes(key), new Bytes(value));
return this;
}
/*
* @param key The key.
* @param value The value.
*/
private HTableDescriptor setValue(final Bytes key,
final String value) {
setValue(key, new Bytes(Bytes.toBytes(value)));
return this;
}
/*
* Setter for storing metadata as a (key, value) pair in {@link #values} map
*
* @param key The key.
* @param value The value.
*/
public HTableDescriptor setValue(final Bytes key, final Bytes value) {
if (key.compareTo(DEFERRED_LOG_FLUSH_KEY) == 0) {
boolean isDeferredFlush = Boolean.valueOf(Bytes.toString(value.get()));
LOG.warn("HTableDescriptor property:" + DEFERRED_LOG_FLUSH + " is deprecated, " +
"use " + DURABILITY + " instead");
setDurability(isDeferredFlush ? Durability.ASYNC_WAL : DEFAULT_DURABLITY);
return this;
}
values.put(key, value);
return this;
}
/**
* Setter for storing metadata as a (key, value) pair in {@link #values} map
*
* @param key The key.
* @param value The value.
* @see #values
*/
public HTableDescriptor setValue(String key, String value) {
if (value == null) {
remove(key);
} else {
setValue(Bytes.toBytes(key), Bytes.toBytes(value));
}
return this;
}
/**
* Remove metadata represented by the key from the {@link #values} map
*
* @param key Key whose key and value we're to remove from HTableDescriptor
* parameters.
*/
public void remove(final String key) {
remove(new Bytes(Bytes.toBytes(key)));
}
/**
* Remove metadata represented by the key from the {@link #values} map
*
* @param key Key whose key and value we're to remove from HTableDescriptor
* parameters.
*/
public void remove(Bytes key) {
values.remove(key);
}
/**
* Remove metadata represented by the key from the {@link #values} map
*
* @param key Key whose key and value we're to remove from HTableDescriptor
* parameters.
*/
public void remove(final byte [] key) {
remove(new Bytes(key));
}
/**
* Check if the readOnly flag of the table is set. If the readOnly flag is
* set then the contents of the table can only be read from but not modified.
*
* @return true if all columns in the table should be read only
*/
public boolean isReadOnly() {
return isSomething(READONLY_KEY, DEFAULT_READONLY);
}
/**
* Setting the table as read only sets all the columns in the table as read
* only. By default all tables are modifiable, but if the readOnly flag is
* set to true then the contents of the table can only be read but not modified.
*
* @param readOnly True if all of the columns in the table should be read
* only.
*/
public HTableDescriptor setReadOnly(final boolean readOnly) {
return setValue(READONLY_KEY, readOnly? TRUE: FALSE);
}
/**
* Check if the compaction enable flag of the table is true. If flag is
* false then no minor/major compactions will be done in real.
*
* @return true if table compaction enabled
*/
public boolean isCompactionEnabled() {
return isSomething(COMPACTION_ENABLED_KEY, DEFAULT_COMPACTION_ENABLED);
}
/**
* Setting the table compaction enable flag.
*
* @param isEnable True if enable compaction.
*/
public HTableDescriptor setCompactionEnabled(final boolean isEnable) {
setValue(COMPACTION_ENABLED_KEY, isEnable ? TRUE : FALSE);
return this;
}
/**
* Check if normalization enable flag of the table is true. If flag is
* false then no region normalizer won't attempt to normalize this table.
*
* @return true if region normalization is enabled for this table
*/
public boolean isNormalizationEnabled() {
return isSomething(NORMALIZATION_ENABLED_KEY, DEFAULT_NORMALIZATION_ENABLED);
}
/**
* Setting the table normalization enable flag.
*
* @param isEnable True if enable normalization.
*/
public HTableDescriptor setNormalizationEnabled(final boolean isEnable) {
setValue(NORMALIZATION_ENABLED_KEY, isEnable ? TRUE : FALSE);
return this;
}
/**
* Sets the {@link Durability} setting for the table. This defaults to Durability.USE_DEFAULT.
* @param durability enum value
*/
public HTableDescriptor setDurability(Durability durability) {
this.durability = durability;
setValue(DURABILITY_KEY, durability.name());
return this;
}
/**
* Returns the durability setting for the table.
* @return durability setting for the table.
*/
public Durability getDurability() {
if (this.durability == null) {
byte[] durabilityValue = getValue(DURABILITY_KEY);
if (durabilityValue == null) {
this.durability = DEFAULT_DURABLITY;
} else {
try {
this.durability = Durability.valueOf(Bytes.toString(durabilityValue));
} catch (IllegalArgumentException ex) {
LOG.warn("Received " + ex + " because Durability value for HTableDescriptor"
+ " is not known. Durability:" + Bytes.toString(durabilityValue));
this.durability = DEFAULT_DURABLITY;
}
}
}
return this.durability;
}
/**
* Get the name of the table
*
* @return TableName
*/
public TableName getTableName() {
return name;
}
/**
* Get the name of the table as a byte array.
*
* @return name of table
* @deprecated Use {@link #getTableName()} instead
*/
@Deprecated
public byte[] getName() {
return name.getName();
}
/**
* Get the name of the table as a String
*
* @return name of table as a String
*/
public String getNameAsString() {
return name.getNameAsString();
}
/**
* This sets the class associated with the region split policy which
* determines when a region split should occur. The class used by
* default is defined in {@link org.apache.hadoop.hbase.regionserver.RegionSplitPolicy}
* @param clazz the class name
*/
public HTableDescriptor setRegionSplitPolicyClassName(String clazz) {
setValue(SPLIT_POLICY, clazz);
return this;
}
/**
* This gets the class associated with the region split policy which
* determines when a region split should occur. The class used by
* default is defined in {@link org.apache.hadoop.hbase.regionserver.RegionSplitPolicy}
*
* @return the class name of the region split policy for this table.
* If this returns null, the default split policy is used.
*/
public String getRegionSplitPolicyClassName() {
return getValue(SPLIT_POLICY);
}
/**
* Set the name of the table.
*
* @param name name of table
*/
@Deprecated
public HTableDescriptor setName(byte[] name) {
setName(TableName.valueOf(name));
return this;
}
@Deprecated
public HTableDescriptor setName(TableName name) {
this.name = name;
setMetaFlags(this.name);
return this;
}
/**
* Returns the maximum size upto which a region can grow to after which a region
* split is triggered. The region size is represented by the size of the biggest
* store file in that region.
*
* @return max hregion size for table, -1 if not set.
*
* @see #setMaxFileSize(long)
*/
public long getMaxFileSize() {
byte [] value = getValue(MAX_FILESIZE_KEY);
if (value != null) {
return Long.parseLong(Bytes.toString(value));
}
return -1;
}
/**
* Sets the maximum size upto which a region can grow to after which a region
* split is triggered. The region size is represented by the size of the biggest
* store file in that region, i.e. If the biggest store file grows beyond the
* maxFileSize, then the region split is triggered. This defaults to a value of
* 256 MB.
* <p>
* This is not an absolute value and might vary. Assume that a single row exceeds
* the maxFileSize then the storeFileSize will be greater than maxFileSize since
* a single row cannot be split across multiple regions
* </p>
*
* @param maxFileSize The maximum file size that a store file can grow to
* before a split is triggered.
*/
public HTableDescriptor setMaxFileSize(long maxFileSize) {
setValue(MAX_FILESIZE_KEY, Long.toString(maxFileSize));
return this;
}
/**
* Returns the size of the memstore after which a flush to filesystem is triggered.
*
* @return memory cache flush size for each hregion, -1 if not set.
*
* @see #setMemStoreFlushSize(long)
*/
public long getMemStoreFlushSize() {
byte [] value = getValue(MEMSTORE_FLUSHSIZE_KEY);
if (value != null) {
return Long.parseLong(Bytes.toString(value));
}
return -1;
}
/**
* Represents the maximum size of the memstore after which the contents of the
* memstore are flushed to the filesystem. This defaults to a size of 64 MB.
*
* @param memstoreFlushSize memory cache flush size for each hregion
*/
public HTableDescriptor setMemStoreFlushSize(long memstoreFlushSize) {
setValue(MEMSTORE_FLUSHSIZE_KEY, Long.toString(memstoreFlushSize));
return this;
}
/**
* This sets the class associated with the flush policy which determines determines the stores
* need to be flushed when flushing a region. The class used by default is defined in
* {@link org.apache.hadoop.hbase.regionserver.FlushPolicy}
* @param clazz the class name
*/
public HTableDescriptor setFlushPolicyClassName(String clazz) {
setValue(FLUSH_POLICY, clazz);
return this;
}
/**
* This gets the class associated with the flush policy which determines the stores need to be
* flushed when flushing a region. The class used by default is defined in
* {@link org.apache.hadoop.hbase.regionserver.FlushPolicy}
* @return the class name of the flush policy for this table. If this returns null, the default
* flush policy is used.
*/
public String getFlushPolicyClassName() {
return getValue(FLUSH_POLICY);
}
/**
* Adds a column family.
* For the updating purpose please use {@link #modifyFamily(HColumnDescriptor)} instead.
* @param family HColumnDescriptor of family to add.
*/
public HTableDescriptor addFamily(final HColumnDescriptor family) {
if (family.getName() == null || family.getName().length <= 0) {
throw new IllegalArgumentException("Family name cannot be null or empty");
}
if (hasFamily(family.getName())) {
throw new IllegalArgumentException("Family '" +
family.getNameAsString() + "' already exists so cannot be added");
}
this.families.put(family.getName(), family);
return this;
}
/**
* Modifies the existing column family.
* @param family HColumnDescriptor of family to update
* @return this (for chained invocation)
*/
public HTableDescriptor modifyFamily(final HColumnDescriptor family) {
if (family.getName() == null || family.getName().length <= 0) {
throw new IllegalArgumentException("Family name cannot be null or empty");
}
if (!hasFamily(family.getName())) {
throw new IllegalArgumentException("Column family '" + family.getNameAsString()
+ "' does not exist");
}
this.families.put(family.getName(), family);
return this;
}
/**
* Checks to see if this table contains the given column family
* @param familyName Family name or column name.
* @return true if the table contains the specified family name
*/
public boolean hasFamily(final byte [] familyName) {
return families.containsKey(familyName);
}
/**
* @return Name of this table and then a map of all of the column family
* descriptors.
* @see #getNameAsString()
*/
@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append('\'').append(Bytes.toString(name.getName())).append('\'');
s.append(getValues(true));
for (HColumnDescriptor f : families.values()) {
s.append(", ").append(f);
}
return s.toString();
}
/**
* @return Name of this table and then a map of all of the column family
* descriptors (with only the non-default column family attributes)
*/
public String toStringCustomizedValues() {
StringBuilder s = new StringBuilder();
s.append('\'').append(Bytes.toString(name.getName())).append('\'');
s.append(getValues(false));
for(HColumnDescriptor hcd : families.values()) {
s.append(", ").append(hcd.toStringCustomizedValues());
}
return s.toString();
}
/**
* @return map of all table attributes formatted into string.
*/
public String toStringTableAttributes() {
return getValues(true).toString();
}
private StringBuilder getValues(boolean printDefaults) {
StringBuilder s = new StringBuilder();
// step 1: set partitioning and pruning
Set<Bytes> reservedKeys = new TreeSet<Bytes>();
Set<Bytes> userKeys = new TreeSet<Bytes>();
for (Map.Entry<Bytes, Bytes> entry : values.entrySet()) {
if (entry.getKey() == null || entry.getKey().get() == null) continue;
String key = Bytes.toString(entry.getKey().get());
// in this section, print out reserved keywords + coprocessor info
if (!RESERVED_KEYWORDS.contains(entry.getKey()) && !key.startsWith("coprocessor$")) {
userKeys.add(entry.getKey());
continue;
}
// only print out IS_ROOT/IS_META if true
String value = Bytes.toString(entry.getValue().get());
if (key.equalsIgnoreCase(IS_ROOT) || key.equalsIgnoreCase(IS_META)) {
if (Boolean.valueOf(value) == false) continue;
}
// see if a reserved key is a default value. may not want to print it out
if (printDefaults
|| !DEFAULT_VALUES.containsKey(key)
|| !DEFAULT_VALUES.get(key).equalsIgnoreCase(value)) {
reservedKeys.add(entry.getKey());
}
}
// early exit optimization
boolean hasAttributes = !reservedKeys.isEmpty() || !userKeys.isEmpty();
if (!hasAttributes && configuration.isEmpty()) return s;
s.append(", {");
// step 2: printing attributes
if (hasAttributes) {
s.append("TABLE_ATTRIBUTES => {");
// print all reserved keys first
boolean printCommaForAttr = false;
for (Bytes k : reservedKeys) {
String key = Bytes.toString(k.get());
String value = Bytes.toStringBinary(values.get(k).get());
if (printCommaForAttr) s.append(", ");
printCommaForAttr = true;
s.append(key);
s.append(" => ");
s.append('\'').append(value).append('\'');
}
if (!userKeys.isEmpty()) {
// print all non-reserved, advanced config keys as a separate subset
if (printCommaForAttr) s.append(", ");
printCommaForAttr = true;
s.append(HConstants.METADATA).append(" => ");
s.append("{");
boolean printCommaForCfg = false;
for (Bytes k : userKeys) {
String key = Bytes.toString(k.get());
String value = Bytes.toStringBinary(values.get(k).get());
if (printCommaForCfg) s.append(", ");
printCommaForCfg = true;
s.append('\'').append(key).append('\'');
s.append(" => ");
s.append('\'').append(value).append('\'');
}
s.append("}");
}
}
// step 3: printing all configuration: