2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package org
.apache
.hadoop
.hbase
;
21 import java
.util
.Collections
;
22 import java
.util
.Comparator
;
23 import java
.util
.HashSet
;
26 import java
.util
.TreeMap
;
27 import java
.util
.TreeSet
;
29 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
30 import org
.apache
.yetus
.audience
.InterfaceAudience
;
33 * Namespace POJO class. Used to represent and define namespaces.
35 * Descriptors will be persisted in an hbase table.
36 * This works since namespaces are essentially metadata of a group of tables
37 * as opposed to a more tangible container.
39 @InterfaceAudience.Public
40 public class NamespaceDescriptor
{
42 /** System namespace name. */
43 public static final byte [] SYSTEM_NAMESPACE_NAME
= Bytes
.toBytes("hbase");
44 public static final String SYSTEM_NAMESPACE_NAME_STR
=
45 Bytes
.toString(SYSTEM_NAMESPACE_NAME
);
46 /** Default namespace name. */
47 public static final byte [] DEFAULT_NAMESPACE_NAME
= Bytes
.toBytes("default");
48 public static final String DEFAULT_NAMESPACE_NAME_STR
=
49 Bytes
.toString(DEFAULT_NAMESPACE_NAME
);
51 public static final NamespaceDescriptor DEFAULT_NAMESPACE
= NamespaceDescriptor
.create(
52 DEFAULT_NAMESPACE_NAME_STR
).build();
53 public static final NamespaceDescriptor SYSTEM_NAMESPACE
= NamespaceDescriptor
.create(
54 SYSTEM_NAMESPACE_NAME_STR
).build();
56 public final static Set
<String
> RESERVED_NAMESPACES
;
58 Set
<String
> set
= new HashSet
<>();
59 set
.add(NamespaceDescriptor
.DEFAULT_NAMESPACE_NAME_STR
);
60 set
.add(NamespaceDescriptor
.SYSTEM_NAMESPACE_NAME_STR
);
61 RESERVED_NAMESPACES
= Collections
.unmodifiableSet(set
);
63 public final static Set
<byte[]> RESERVED_NAMESPACES_BYTES
;
65 Set
<byte[]> set
= new TreeSet
<>(Bytes
.BYTES_RAWCOMPARATOR
);
66 for(String name
: RESERVED_NAMESPACES
) {
67 set
.add(Bytes
.toBytes(name
));
69 RESERVED_NAMESPACES_BYTES
= Collections
.unmodifiableSet(set
);
73 private Map
<String
, String
> configuration
;
75 public static final Comparator
<NamespaceDescriptor
> NAMESPACE_DESCRIPTOR_COMPARATOR
=
76 new Comparator
<NamespaceDescriptor
>() {
78 public int compare(NamespaceDescriptor namespaceDescriptor
,
79 NamespaceDescriptor namespaceDescriptor2
) {
80 return namespaceDescriptor
.getName().compareTo(namespaceDescriptor2
.getName());
84 private NamespaceDescriptor() {
87 private NamespaceDescriptor(String name
) {
91 public String
getName() {
96 * Getter for accessing the configuration value by key
98 public String
getConfigurationValue(String key
) {
99 return configuration
.get(key
);
103 * Getter for fetching an unmodifiable {@link #configuration} map.
105 public Map
<String
, String
> getConfiguration() {
106 // shallow pointer copy
107 return Collections
.unmodifiableMap(configuration
);
111 * Setter for storing a configuration setting in {@link #configuration} map.
112 * @param key Config key. Same as XML config key e.g. hbase.something.or.other.
113 * @param value String value. If null, removes the setting.
115 public void setConfiguration(String key
, String value
) {
117 removeConfiguration(key
);
119 configuration
.put(key
, value
);
124 * Remove a config setting represented by the key from the {@link #configuration} map
126 public void removeConfiguration(final String key
) {
127 configuration
.remove(key
);
131 public String
toString() {
132 StringBuilder s
= new StringBuilder();
134 s
.append(HConstants
.NAME
);
138 for (Map
.Entry
<String
, String
> e
: configuration
.entrySet()) {
139 String key
= e
.getKey();
140 String value
= e
.getValue();
154 public static Builder
create(String name
) {
155 return new Builder(name
);
158 public static Builder
create(NamespaceDescriptor ns
) {
159 return new Builder(ns
);
162 @InterfaceAudience.Public
163 public static class Builder
{
164 private String bName
;
165 private Map
<String
, String
> bConfiguration
= new TreeMap
<>();
167 private Builder(NamespaceDescriptor ns
) {
168 this.bName
= ns
.name
;
169 this.bConfiguration
.putAll(ns
.configuration
);
172 private Builder(String name
) {
176 public Builder
addConfiguration(Map
<String
, String
> configuration
) {
177 this.bConfiguration
.putAll(configuration
);
181 public Builder
addConfiguration(String key
, String value
) {
182 this.bConfiguration
.put(key
, value
);
186 public Builder
removeConfiguration(String key
) {
187 this.bConfiguration
.remove(key
);
191 public NamespaceDescriptor
build() {
192 if (this.bName
== null){
193 throw new IllegalArgumentException("A name has to be specified in a namespace.");
196 NamespaceDescriptor desc
= new NamespaceDescriptor(this.bName
);
197 desc
.configuration
= this.bConfiguration
;