1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase;
21
22 import java.io.DataInput;
23 import java.io.DataOutput;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.HashSet;
34 import java.util.TreeSet;
35 import java.util.TreeMap;
36 import java.util.regex.Matcher;
37
38 import org.apache.hadoop.conf.Configuration;
39 import org.apache.hadoop.fs.Path;
40 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
41 import org.apache.hadoop.hbase.security.User;
42 import org.apache.hadoop.hbase.util.Bytes;
43 import org.apache.hadoop.io.WritableComparable;
44
45
46
47
48
49
50
51 public class HTableDescriptor implements WritableComparable<HTableDescriptor> {
52
53
54
55
56
57
58
59 private static final byte TABLE_DESCRIPTOR_VERSION = 5;
60
61 private byte [] name = HConstants.EMPTY_BYTE_ARRAY;
62
63 private String nameAsString = "";
64
65
66
67
68
69
70 protected final Map<ImmutableBytesWritable, ImmutableBytesWritable> values =
71 new HashMap<ImmutableBytesWritable, ImmutableBytesWritable>();
72
73 private static final String FAMILIES = "FAMILIES";
74
75 public static final String SPLIT_POLICY = "SPLIT_POLICY";
76
77
78
79
80
81
82
83
84 public static final String MAX_FILESIZE = "MAX_FILESIZE";
85 private static final ImmutableBytesWritable MAX_FILESIZE_KEY =
86 new ImmutableBytesWritable(Bytes.toBytes(MAX_FILESIZE));
87
88 public static final String OWNER = "OWNER";
89 public static final ImmutableBytesWritable OWNER_KEY =
90 new ImmutableBytesWritable(Bytes.toBytes(OWNER));
91
92
93
94
95
96
97
98 public static final String READONLY = "READONLY";
99 private static final ImmutableBytesWritable READONLY_KEY =
100 new ImmutableBytesWritable(Bytes.toBytes(READONLY));
101
102
103
104
105
106
107
108
109 public static final String MEMSTORE_FLUSHSIZE = "MEMSTORE_FLUSHSIZE";
110 private static final ImmutableBytesWritable MEMSTORE_FLUSHSIZE_KEY =
111 new ImmutableBytesWritable(Bytes.toBytes(MEMSTORE_FLUSHSIZE));
112
113
114
115
116
117
118
119 public static final String IS_ROOT = "IS_ROOT";
120 private static final ImmutableBytesWritable IS_ROOT_KEY =
121 new ImmutableBytesWritable(Bytes.toBytes(IS_ROOT));
122
123
124
125
126
127
128
129
130 public static final String IS_META = "IS_META";
131 private static final ImmutableBytesWritable IS_META_KEY =
132 new ImmutableBytesWritable(Bytes.toBytes(IS_META));
133
134
135
136
137
138 public static final String DEFERRED_LOG_FLUSH = "DEFERRED_LOG_FLUSH";
139 private static final ImmutableBytesWritable DEFERRED_LOG_FLUSH_KEY =
140 new ImmutableBytesWritable(Bytes.toBytes(DEFERRED_LOG_FLUSH));
141
142
143
144
145
146
147 private static final ImmutableBytesWritable FALSE =
148 new ImmutableBytesWritable(Bytes.toBytes(Boolean.FALSE.toString()));
149
150 private static final ImmutableBytesWritable TRUE =
151 new ImmutableBytesWritable(Bytes.toBytes(Boolean.TRUE.toString()));
152
153 private static final boolean DEFAULT_DEFERRED_LOG_FLUSH = false;
154
155
156
157
158 public static final boolean DEFAULT_READONLY = false;
159
160
161
162
163
164 public static final long DEFAULT_MEMSTORE_FLUSH_SIZE = 1024*1024*128L;
165 private final static Map<String, String> DEFAULT_VALUES
166 = new HashMap<String, String>();
167 private final static Set<ImmutableBytesWritable> RESERVED_KEYWORDS
168 = new HashSet<ImmutableBytesWritable>();
169 static {
170 DEFAULT_VALUES.put(MAX_FILESIZE,
171 String.valueOf(HConstants.DEFAULT_MAX_FILE_SIZE));
172 DEFAULT_VALUES.put(READONLY, String.valueOf(DEFAULT_READONLY));
173 DEFAULT_VALUES.put(MEMSTORE_FLUSHSIZE,
174 String.valueOf(DEFAULT_MEMSTORE_FLUSH_SIZE));
175 DEFAULT_VALUES.put(DEFERRED_LOG_FLUSH,
176 String.valueOf(DEFAULT_DEFERRED_LOG_FLUSH));
177 for (String s : DEFAULT_VALUES.keySet()) {
178 RESERVED_KEYWORDS.add(new ImmutableBytesWritable(Bytes.toBytes(s)));
179 }
180 RESERVED_KEYWORDS.add(IS_ROOT_KEY);
181 RESERVED_KEYWORDS.add(IS_META_KEY);
182 }
183
184
185
186 private volatile Boolean meta = null;
187 private volatile Boolean root = null;
188 private Boolean isDeferredLog = null;
189
190
191
192
193 private final Map<byte [], HColumnDescriptor> families =
194 new TreeMap<byte [], HColumnDescriptor>(Bytes.BYTES_RAWCOMPARATOR);
195
196
197
198
199
200 protected HTableDescriptor(final byte [] name, HColumnDescriptor[] families) {
201 this.name = name.clone();
202 this.nameAsString = Bytes.toString(this.name);
203 setMetaFlags(name);
204 for(HColumnDescriptor descriptor : families) {
205 this.families.put(descriptor.getName(), descriptor);
206 }
207 }
208
209
210
211
212
213 protected HTableDescriptor(final byte [] name, HColumnDescriptor[] families,
214 Map<ImmutableBytesWritable,ImmutableBytesWritable> values) {
215 this.name = name.clone();
216 this.nameAsString = Bytes.toString(this.name);
217 setMetaFlags(name);
218 for(HColumnDescriptor descriptor : families) {
219 this.families.put(descriptor.getName(), descriptor);
220 }
221 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> entry:
222 values.entrySet()) {
223 this.values.put(entry.getKey(), entry.getValue());
224 }
225 }
226
227
228
229
230
231
232 public HTableDescriptor() {
233 super();
234 }
235
236
237
238
239
240
241
242
243
244 public HTableDescriptor(final String name) {
245 this(Bytes.toBytes(name));
246 }
247
248
249
250
251
252
253
254
255
256 public HTableDescriptor(final byte [] name) {
257 super();
258 setMetaFlags(this.name);
259 this.name = this.isMetaRegion()? name: isLegalTableName(name);
260 this.nameAsString = Bytes.toString(this.name);
261 }
262
263
264
265
266
267
268
269
270 public HTableDescriptor(final HTableDescriptor desc) {
271 super();
272 this.name = desc.name.clone();
273 this.nameAsString = Bytes.toString(this.name);
274 setMetaFlags(this.name);
275 for (HColumnDescriptor c: desc.families.values()) {
276 this.families.put(c.getName(), new HColumnDescriptor(c));
277 }
278 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
279 desc.values.entrySet()) {
280 this.values.put(e.getKey(), e.getValue());
281 }
282 }
283
284
285
286
287
288
289
290
291 private void setMetaFlags(final byte [] name) {
292 setRootRegion(Bytes.equals(name, HConstants.ROOT_TABLE_NAME));
293 setMetaRegion(isRootRegion() ||
294 Bytes.equals(name, HConstants.META_TABLE_NAME));
295 }
296
297
298
299
300
301
302 public boolean isRootRegion() {
303 if (this.root == null) {
304 this.root = isSomething(IS_ROOT_KEY, false)? Boolean.TRUE: Boolean.FALSE;
305 }
306 return this.root.booleanValue();
307 }
308
309
310
311
312
313
314
315
316 protected void setRootRegion(boolean isRoot) {
317
318 values.put(IS_ROOT_KEY, isRoot? TRUE: FALSE);
319 }
320
321
322
323
324
325
326
327
328 public boolean isMetaRegion() {
329 if (this.meta == null) {
330 this.meta = calculateIsMetaRegion();
331 }
332 return this.meta.booleanValue();
333 }
334
335 private synchronized Boolean calculateIsMetaRegion() {
336 byte [] value = getValue(IS_META_KEY);
337 return (value != null)? Boolean.valueOf(Bytes.toString(value)): Boolean.FALSE;
338 }
339
340 private boolean isSomething(final ImmutableBytesWritable key,
341 final boolean valueIfNull) {
342 byte [] value = getValue(key);
343 if (value != null) {
344
345 return Boolean.valueOf(Bytes.toString(value)).booleanValue();
346 }
347 return valueIfNull;
348 }
349
350
351
352
353
354
355
356
357
358 protected void setMetaRegion(boolean isMeta) {
359 values.put(IS_META_KEY, isMeta? TRUE: FALSE);
360 }
361
362
363
364
365
366
367 public boolean isMetaTable() {
368 return isMetaRegion() && !isRootRegion();
369 }
370
371
372
373
374
375
376
377
378 public static boolean isMetaTable(final byte [] tableName) {
379 return Bytes.equals(tableName, HConstants.ROOT_TABLE_NAME) ||
380 Bytes.equals(tableName, HConstants.META_TABLE_NAME);
381 }
382
383
384 public static final String VALID_USER_TABLE_REGEX = "(?:[a-zA-Z_0-9][a-zA-Z_0-9.-]*)";
385
386
387
388
389
390
391
392
393
394 public static byte [] isLegalTableName(final byte [] tableName) {
395 if (tableName == null || tableName.length <= 0) {
396 throw new IllegalArgumentException("Name is null or empty");
397 }
398 if (tableName[0] == '.' || tableName[0] == '-') {
399 throw new IllegalArgumentException("Illegal first character <" + tableName[0] +
400 "> at 0. User-space table names can only start with 'word " +
401 "characters': i.e. [a-zA-Z_0-9]: " + Bytes.toString(tableName));
402 }
403 for (int i = 0; i < tableName.length; i++) {
404 if (Character.isLetterOrDigit(tableName[i]) || tableName[i] == '_' ||
405 tableName[i] == '-' || tableName[i] == '.') {
406 continue;
407 }
408 throw new IllegalArgumentException("Illegal character <" + tableName[i] +
409 "> at " + i + ". User-space table names can only contain " +
410 "'word characters': i.e. [a-zA-Z_0-9-.]: " + Bytes.toString(tableName));
411 }
412 return tableName;
413 }
414
415
416
417
418
419
420
421
422 public byte[] getValue(byte[] key) {
423 return getValue(new ImmutableBytesWritable(key));
424 }
425
426 private byte[] getValue(final ImmutableBytesWritable key) {
427 ImmutableBytesWritable ibw = values.get(key);
428 if (ibw == null)
429 return null;
430 return ibw.get();
431 }
432
433
434
435
436
437
438
439
440 public String getValue(String key) {
441 byte[] value = getValue(Bytes.toBytes(key));
442 if (value == null)
443 return null;
444 return Bytes.toString(value);
445 }
446
447
448
449
450
451
452
453 public Map<ImmutableBytesWritable,ImmutableBytesWritable> getValues() {
454
455 return Collections.unmodifiableMap(values);
456 }
457
458
459
460
461
462
463
464
465 public void setValue(byte[] key, byte[] value) {
466 setValue(new ImmutableBytesWritable(key), value);
467 }
468
469
470
471
472
473 private void setValue(final ImmutableBytesWritable key,
474 final byte[] value) {
475 values.put(key, new ImmutableBytesWritable(value));
476 }
477
478
479
480
481
482 private void setValue(final ImmutableBytesWritable key,
483 final ImmutableBytesWritable value) {
484 values.put(key, value);
485 }
486
487
488
489
490
491
492
493
494 public void setValue(String key, String value) {
495 if (value == null) {
496 remove(Bytes.toBytes(key));
497 } else {
498 setValue(Bytes.toBytes(key), Bytes.toBytes(value));
499 }
500 }
501
502
503
504
505
506
507
508 public void remove(final byte [] key) {
509 values.remove(new ImmutableBytesWritable(key));
510 }
511
512
513
514
515
516
517
518 public void remove(final String key) {
519 remove(Bytes.toBytes(key));
520 }
521
522
523
524
525
526
527
528 public boolean isReadOnly() {
529 return isSomething(READONLY_KEY, DEFAULT_READONLY);
530 }
531
532
533
534
535
536
537
538
539
540 public void setReadOnly(final boolean readOnly) {
541 setValue(READONLY_KEY, readOnly? TRUE: FALSE);
542 }
543
544
545
546
547
548
549
550
551 public synchronized boolean isDeferredLogFlush() {
552 if(this.isDeferredLog == null) {
553 this.isDeferredLog =
554 isSomething(DEFERRED_LOG_FLUSH_KEY, DEFAULT_DEFERRED_LOG_FLUSH);
555 }
556 return this.isDeferredLog;
557 }
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573 public void setDeferredLogFlush(final boolean isDeferredLogFlush) {
574 setValue(DEFERRED_LOG_FLUSH_KEY, isDeferredLogFlush? TRUE: FALSE);
575 this.isDeferredLog = isDeferredLogFlush;
576 }
577
578
579
580
581
582
583 public byte [] getName() {
584 return name;
585 }
586
587
588
589
590
591
592 public String getNameAsString() {
593 return this.nameAsString;
594 }
595
596
597
598
599
600
601
602
603
604
605
606 public String getRegionSplitPolicyClassName() {
607 return getValue(SPLIT_POLICY);
608 }
609
610
611
612
613
614
615 public void setName(byte[] name) {
616 this.name = name;
617 this.nameAsString = Bytes.toString(this.name);
618 setMetaFlags(this.name);
619 }
620
621
622
623
624
625
626
627
628
629
630 public long getMaxFileSize() {
631 byte [] value = getValue(MAX_FILESIZE_KEY);
632 if (value != null) {
633 return Long.parseLong(Bytes.toString(value));
634 }
635 return -1;
636 }
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653 public void setMaxFileSize(long maxFileSize) {
654 setValue(MAX_FILESIZE_KEY, Bytes.toBytes(Long.toString(maxFileSize)));
655 }
656
657
658
659
660
661
662
663
664 public long getMemStoreFlushSize() {
665 byte [] value = getValue(MEMSTORE_FLUSHSIZE_KEY);
666 if (value != null) {
667 return Long.parseLong(Bytes.toString(value));
668 }
669 return -1;
670 }
671
672
673
674
675
676
677
678 public void setMemStoreFlushSize(long memstoreFlushSize) {
679 setValue(MEMSTORE_FLUSHSIZE_KEY,
680 Bytes.toBytes(Long.toString(memstoreFlushSize)));
681 }
682
683
684
685
686
687 public void addFamily(final HColumnDescriptor family) {
688 if (family.getName() == null || family.getName().length <= 0) {
689 throw new NullPointerException("Family name cannot be null or empty");
690 }
691 this.families.put(family.getName(), family);
692 }
693
694
695
696
697
698
699 public boolean hasFamily(final byte [] familyName) {
700 return families.containsKey(familyName);
701 }
702
703
704
705
706
707
708 @Override
709 public String toString() {
710 StringBuilder s = new StringBuilder();
711 s.append('\'').append(Bytes.toString(name)).append('\'');
712 s.append(getValues(true));
713 for (HColumnDescriptor f : families.values()) {
714 s.append(", ").append(f);
715 }
716 return s.toString();
717 }
718
719
720
721
722
723 public String toStringCustomizedValues() {
724 StringBuilder s = new StringBuilder();
725 s.append('\'').append(Bytes.toString(name)).append('\'');
726 s.append(getValues(false));
727 for(HColumnDescriptor hcd : families.values()) {
728 s.append(", ").append(hcd.toStringCustomizedValues());
729 }
730 return s.toString();
731 }
732
733 private StringBuilder getValues(boolean printDefaults) {
734 StringBuilder s = new StringBuilder();
735
736
737 Set<ImmutableBytesWritable> reservedKeys = new TreeSet<ImmutableBytesWritable>();
738 Set<ImmutableBytesWritable> configKeys = new TreeSet<ImmutableBytesWritable>();
739 for (ImmutableBytesWritable k : values.keySet()) {
740 if (k == null || k.get() == null) continue;
741 String key = Bytes.toString(k.get());
742
743 if (!RESERVED_KEYWORDS.contains(k) && !key.startsWith("coprocessor$")) {
744 configKeys.add(k);
745 continue;
746 }
747
748 String value = Bytes.toString(values.get(k).get());
749 if (key.equalsIgnoreCase(IS_ROOT) || key.equalsIgnoreCase(IS_META)) {
750
751 if (value.toLowerCase().equals(Boolean.FALSE.toString())) {
752 continue;
753 }
754 }
755
756
757 if (printDefaults
758 || !DEFAULT_VALUES.containsKey(key)
759 || !DEFAULT_VALUES.get(key).equalsIgnoreCase(value)) {
760 reservedKeys.add(k);
761 }
762 }
763
764
765
766
767 if (reservedKeys.isEmpty() && configKeys.isEmpty()) return s;
768
769
770 s.append(", {METHOD => 'table_att'");
771
772
773 for (ImmutableBytesWritable k : reservedKeys) {
774 String key = Bytes.toString(k.get());
775 String value = Bytes.toString(values.get(k).get());
776
777 s.append(", ");
778 s.append(key);
779 s.append(" => ");
780 s.append('\'').append(value).append('\'');
781 }
782 if (!configKeys.isEmpty()) {
783
784 s.append(", ");
785 s.append(HConstants.CONFIG).append(" => ");
786 s.append("{");
787 boolean printComma = false;
788 for (ImmutableBytesWritable k : configKeys) {
789 String key = Bytes.toString(k.get());
790 String value = Bytes.toString(values.get(k).get());
791 if (printComma) s.append(", ");
792 printComma = true;
793 s.append('\'').append(key).append('\'');
794 s.append(" => ");
795 s.append('\'').append(value).append('\'');
796 }
797 s.append("}");
798 }
799
800 s.append('}');
801 return s;
802 }
803
804 public static Map<String, String> getDefaultValues() {
805 return Collections.unmodifiableMap(DEFAULT_VALUES);
806 }
807
808
809
810
811
812
813
814
815
816
817 @Override
818 public boolean equals(Object obj) {
819 if (this == obj) {
820 return true;
821 }
822 if (obj == null) {
823 return false;
824 }
825 if (!(obj instanceof HTableDescriptor)) {
826 return false;
827 }
828 return compareTo((HTableDescriptor)obj) == 0;
829 }
830
831
832
833
834 @Override
835 public int hashCode() {
836 int result = Bytes.hashCode(this.name);
837 result ^= Byte.valueOf(TABLE_DESCRIPTOR_VERSION).hashCode();
838 if (this.families != null && this.families.size() > 0) {
839 for (HColumnDescriptor e: this.families.values()) {
840 result ^= e.hashCode();
841 }
842 }
843 result ^= values.hashCode();
844 return result;
845 }
846
847
848
849
850
851
852 @Override
853 public void readFields(DataInput in) throws IOException {
854 int version = in.readInt();
855 if (version < 3)
856 throw new IOException("versions < 3 are not supported (and never existed!?)");
857
858 name = Bytes.readByteArray(in);
859 nameAsString = Bytes.toString(this.name);
860 setRootRegion(in.readBoolean());
861 setMetaRegion(in.readBoolean());
862 values.clear();
863 int numVals = in.readInt();
864 for (int i = 0; i < numVals; i++) {
865 ImmutableBytesWritable key = new ImmutableBytesWritable();
866 ImmutableBytesWritable value = new ImmutableBytesWritable();
867 key.readFields(in);
868 value.readFields(in);
869 values.put(key, value);
870 }
871 families.clear();
872 int numFamilies = in.readInt();
873 for (int i = 0; i < numFamilies; i++) {
874 HColumnDescriptor c = new HColumnDescriptor();
875 c.readFields(in);
876 families.put(c.getName(), c);
877 }
878 if (version < 4) {
879 return;
880 }
881 }
882
883
884
885
886
887 @Override
888 public void write(DataOutput out) throws IOException {
889 out.writeInt(TABLE_DESCRIPTOR_VERSION);
890 Bytes.writeByteArray(out, name);
891 out.writeBoolean(isRootRegion());
892 out.writeBoolean(isMetaRegion());
893 out.writeInt(values.size());
894 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
895 values.entrySet()) {
896 e.getKey().write(out);
897 e.getValue().write(out);
898 }
899 out.writeInt(families.size());
900 for(Iterator<HColumnDescriptor> it = families.values().iterator();
901 it.hasNext(); ) {
902 HColumnDescriptor family = it.next();
903 family.write(out);
904 }
905 }
906
907
908
909
910
911
912
913
914
915
916 @Override
917 public int compareTo(final HTableDescriptor other) {
918 int result = Bytes.compareTo(this.name, other.name);
919 if (result == 0) {
920 result = families.size() - other.families.size();
921 }
922 if (result == 0 && families.size() != other.families.size()) {
923 result = Integer.valueOf(families.size()).compareTo(
924 Integer.valueOf(other.families.size()));
925 }
926 if (result == 0) {
927 for (Iterator<HColumnDescriptor> it = families.values().iterator(),
928 it2 = other.families.values().iterator(); it.hasNext(); ) {
929 result = it.next().compareTo(it2.next());
930 if (result != 0) {
931 break;
932 }
933 }
934 }
935 if (result == 0) {
936
937 result = this.values.hashCode() - other.values.hashCode();
938 if (result < 0)
939 result = -1;
940 else if (result > 0)
941 result = 1;
942 }
943 return result;
944 }
945
946
947
948
949
950
951
952
953 public Collection<HColumnDescriptor> getFamilies() {
954 return Collections.unmodifiableCollection(this.families.values());
955 }
956
957
958
959
960
961
962
963
964
965 public Set<byte[]> getFamiliesKeys() {
966 return Collections.unmodifiableSet(this.families.keySet());
967 }
968
969
970
971
972
973
974
975
976
977 public HColumnDescriptor[] getColumnFamilies() {
978 return getFamilies().toArray(new HColumnDescriptor[0]);
979 }
980
981
982
983
984
985
986
987
988
989
990 public HColumnDescriptor getFamily(final byte [] column) {
991 return this.families.get(column);
992 }
993
994
995
996
997
998
999
1000
1001
1002
1003 public HColumnDescriptor removeFamily(final byte [] column) {
1004 return this.families.remove(column);
1005 }
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018 public void addCoprocessor(String className) throws IOException {
1019 addCoprocessor(className, null, Coprocessor.PRIORITY_USER, null);
1020 }
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037 public void addCoprocessor(String className, Path jarFilePath,
1038 int priority, final Map<String, String> kvs)
1039 throws IOException {
1040 if (hasCoprocessor(className)) {
1041 throw new IOException("Coprocessor " + className + " already exists.");
1042 }
1043
1044 StringBuilder kvString = new StringBuilder();
1045 if (kvs != null) {
1046 for (Map.Entry<String, String> e: kvs.entrySet()) {
1047 if (!e.getKey().matches(HConstants.CP_HTD_ATTR_VALUE_PARAM_KEY_PATTERN)) {
1048 throw new IOException("Illegal parameter key = " + e.getKey());
1049 }
1050 if (!e.getValue().matches(HConstants.CP_HTD_ATTR_VALUE_PARAM_VALUE_PATTERN)) {
1051 throw new IOException("Illegal parameter (" + e.getKey() +
1052 ") value = " + e.getValue());
1053 }
1054 if (kvString.length() != 0) {
1055 kvString.append(',');
1056 }
1057 kvString.append(e.getKey());
1058 kvString.append('=');
1059 kvString.append(e.getValue());
1060 }
1061 }
1062
1063
1064 int maxCoprocessorNumber = 0;
1065 Matcher keyMatcher;
1066 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
1067 this.values.entrySet()) {
1068 keyMatcher =
1069 HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(
1070 Bytes.toString(e.getKey().get()));
1071 if (!keyMatcher.matches()) {
1072 continue;
1073 }
1074 maxCoprocessorNumber = Math.max(Integer.parseInt(keyMatcher.group(1)),
1075 maxCoprocessorNumber);
1076 }
1077 maxCoprocessorNumber++;
1078
1079 String key = "coprocessor$" + Integer.toString(maxCoprocessorNumber);
1080 String value = ((jarFilePath == null)? "" : jarFilePath.toString()) +
1081 "|" + className + "|" + Integer.toString(priority) + "|" +
1082 kvString.toString();
1083 setValue(key, value);
1084 }
1085
1086
1087
1088
1089
1090
1091
1092
1093 public boolean hasCoprocessor(String className) {
1094 Matcher keyMatcher;
1095 Matcher valueMatcher;
1096 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
1097 this.values.entrySet()) {
1098 keyMatcher =
1099 HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(
1100 Bytes.toString(e.getKey().get()));
1101 if (!keyMatcher.matches()) {
1102 continue;
1103 }
1104 valueMatcher =
1105 HConstants.CP_HTD_ATTR_VALUE_PATTERN.matcher(
1106 Bytes.toString(e.getValue().get()));
1107 if (!valueMatcher.matches()) {
1108 continue;
1109 }
1110
1111 String clazz = valueMatcher.group(2).trim();
1112 if (clazz.equals(className.trim())) {
1113 return true;
1114 }
1115 }
1116 return false;
1117 }
1118
1119
1120
1121
1122
1123
1124 public List<String> getCoprocessors() {
1125 List<String> result = new ArrayList<String>();
1126 Matcher keyMatcher;
1127 Matcher valueMatcher;
1128 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e : this.values.entrySet()) {
1129 keyMatcher = HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(Bytes.toString(e.getKey().get()));
1130 if (!keyMatcher.matches()) {
1131 continue;
1132 }
1133 valueMatcher = HConstants.CP_HTD_ATTR_VALUE_PATTERN.matcher(Bytes
1134 .toString(e.getValue().get()));
1135 if (!valueMatcher.matches()) {
1136 continue;
1137 }
1138 result.add(valueMatcher.group(2).trim());
1139 }
1140 return result;
1141 }
1142
1143
1144
1145
1146
1147 public void removeCoprocessor(String className) {
1148 ImmutableBytesWritable match = null;
1149 Matcher keyMatcher;
1150 Matcher valueMatcher;
1151 for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e : this.values
1152 .entrySet()) {
1153 keyMatcher = HConstants.CP_HTD_ATTR_KEY_PATTERN.matcher(Bytes.toString(e
1154 .getKey().get()));
1155 if (!keyMatcher.matches()) {
1156 continue;
1157 }
1158 valueMatcher = HConstants.CP_HTD_ATTR_VALUE_PATTERN.matcher(Bytes
1159 .toString(e.getValue().get()));
1160 if (!valueMatcher.matches()) {
1161 continue;
1162 }
1163
1164 String clazz = valueMatcher.group(2).trim();
1165
1166 if (clazz.equals(className.trim())) {
1167 match = e.getKey();
1168 break;
1169 }
1170 }
1171
1172 if (match != null)
1173 this.values.remove(match);
1174 }
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184 public static Path getTableDir(Path rootdir, final byte [] tableName) {
1185 return new Path(rootdir, Bytes.toString(tableName));
1186 }
1187
1188
1189 public static final HTableDescriptor ROOT_TABLEDESC = new HTableDescriptor(
1190 HConstants.ROOT_TABLE_NAME,
1191 new HColumnDescriptor[] {
1192 new HColumnDescriptor(HConstants.CATALOG_FAMILY)
1193
1194 .setMaxVersions(10)
1195 .setInMemory(true)
1196 .setBlocksize(8 * 1024)
1197 .setTimeToLive(HConstants.FOREVER)
1198 .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
1199 });
1200
1201
1202 public static final HTableDescriptor META_TABLEDESC = new HTableDescriptor(
1203 HConstants.META_TABLE_NAME, new HColumnDescriptor[] {
1204 new HColumnDescriptor(HConstants.CATALOG_FAMILY)
1205
1206 .setMaxVersions(10)
1207 .setInMemory(true)
1208 .setBlocksize(8 * 1024)
1209 .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
1210 });
1211
1212 @Deprecated
1213 public void setOwner(User owner) {
1214 setOwnerString(owner != null ? owner.getShortName() : null);
1215 }
1216
1217
1218 @Deprecated
1219 public void setOwnerString(String ownerString) {
1220 if (ownerString != null) {
1221 setValue(OWNER_KEY, Bytes.toBytes(ownerString));
1222 } else {
1223 values.remove(OWNER_KEY);
1224 }
1225 }
1226
1227 @Deprecated
1228 public String getOwnerString() {
1229 if (getValue(OWNER_KEY) != null) {
1230 return Bytes.toString(getValue(OWNER_KEY));
1231 }
1232
1233
1234
1235 return null;
1236 }
1237 }