001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.client;
020
021import java.util.Arrays;
022import java.util.Collection;
023import java.util.Comparator;
024import java.util.Iterator;
025import java.util.Map;
026import java.util.Set;
027import java.util.stream.Collectors;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.apache.yetus.audience.InterfaceAudience;
032
033/**
034 * TableDescriptor contains the details about an HBase table such as the descriptors of
035 * all the column families, is the table a catalog table, <code> hbase:meta </code>,
036 * if the table is read only, the maximum size of the memstore,
037 * when the region split should occur, coprocessors associated with it etc...
038 */
039@InterfaceAudience.Public
040public interface TableDescriptor {
041
042  @InterfaceAudience.Private
043  Comparator<TableDescriptor> COMPARATOR = getComparator(ColumnFamilyDescriptor.COMPARATOR);
044
045  @InterfaceAudience.Private
046  Comparator<TableDescriptor> COMPARATOR_IGNORE_REPLICATION =
047      getComparator(ColumnFamilyDescriptor.COMPARATOR_IGNORE_REPLICATION);
048
049  static Comparator<TableDescriptor>
050      getComparator(Comparator<ColumnFamilyDescriptor> cfComparator) {
051    return (TableDescriptor lhs, TableDescriptor rhs) -> {
052      int result = lhs.getTableName().compareTo(rhs.getTableName());
053      if (result != 0) {
054        return result;
055      }
056      Collection<ColumnFamilyDescriptor> lhsFamilies = Arrays.asList(lhs.getColumnFamilies());
057      Collection<ColumnFamilyDescriptor> rhsFamilies = Arrays.asList(rhs.getColumnFamilies());
058      result = Integer.compare(lhsFamilies.size(), rhsFamilies.size());
059      if (result != 0) {
060        return result;
061      }
062
063      for (Iterator<ColumnFamilyDescriptor> it = lhsFamilies.iterator(), it2 =
064          rhsFamilies.iterator(); it.hasNext();) {
065        result = cfComparator.compare(it.next(), it2.next());
066        if (result != 0) {
067          return result;
068        }
069      }
070      // punt on comparison for ordering, just calculate difference
071      return Integer.compare(lhs.getValues().hashCode(), rhs.getValues().hashCode());
072    };
073  }
074
075  /**
076   * Returns the count of the column families of the table.
077   *
078   * @return Count of column families of the table
079   */
080  int getColumnFamilyCount();
081
082  /**
083   * Return the list of attached co-processor represented
084   *
085   * @return The list of CoprocessorDescriptor
086   */
087  Collection<CoprocessorDescriptor> getCoprocessorDescriptors();
088
089  /**
090   * Return the list of attached co-processor represented by their name
091   * className
092   * @return The list of co-processors classNames
093   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0.
094   *                       Use {@link #getCoprocessorDescriptors()} instead
095   */
096  @Deprecated
097  default Collection<String> getCoprocessors() {
098    return getCoprocessorDescriptors().stream()
099      .map(CoprocessorDescriptor::getClassName)
100      .collect(Collectors.toList());
101  }
102
103  /**
104   * Returns the durability setting for the table.
105   *
106   * @return durability setting for the table.
107   */
108  Durability getDurability();
109
110  /**
111   * Returns an unmodifiable collection of all the {@link ColumnFamilyDescriptor} of
112   * all the column families of the table.
113   *
114   * @return An array of {@link ColumnFamilyDescriptor} of all the column
115   * families.
116   */
117  ColumnFamilyDescriptor[] getColumnFamilies();
118
119  /**
120   * Returns all the column family names of the current table. The map of
121   * TableDescriptor contains mapping of family name to ColumnDescriptor.
122   * This returns all the keys of the family map which represents the column
123   * family names of the table.
124   *
125   * @return Immutable sorted set of the keys of the families.
126   */
127  Set<byte[]> getColumnFamilyNames();
128
129  /**
130   * Returns the ColumnDescriptor for a specific column family with name as
131   * specified by the parameter column.
132   *
133   * @param name Column family name
134   * @return Column descriptor for the passed family name or the family on
135   * passed in column.
136   */
137  ColumnFamilyDescriptor getColumnFamily(final byte[] name);
138
139  /**
140   * This gets the class associated with the flush policy which determines the
141   * stores need to be flushed when flushing a region. The class used by default
142   * is defined in org.apache.hadoop.hbase.regionserver.FlushPolicy.
143   *
144   * @return the class name of the flush policy for this table. If this returns
145   * null, the default flush policy is used.
146   */
147  String getFlushPolicyClassName();
148
149  /**
150   * Returns the maximum size upto which a region can grow to after which a
151   * region split is triggered. The region size is represented by the size of
152   * the biggest store file in that region.
153   *
154   * @return max hregion size for table, -1 if not set.
155   */
156  long getMaxFileSize();
157
158  /**
159   * Returns the size of the memstore after which a flush to filesystem is
160   * triggered.
161   *
162   * @return memory cache flush size for each hregion, -1 if not set.
163   */
164  long getMemStoreFlushSize();
165
166  // TODO: Currently this is used RPC scheduling only. Make it more generic than this; allow it
167  // to also be priority when scheduling procedures that pertain to this table scheduling first
168  // those tables with the highest priority (From Yi Liang over on HBASE-18109).
169  int getPriority();
170
171  /**
172   * @return Returns the configured replicas per region
173   */
174  int getRegionReplication();
175
176  /**
177   * This gets the class associated with the region split policy which
178   * determines when a region split should occur. The class used by default is
179   * defined in org.apache.hadoop.hbase.regionserver.RegionSplitPolicy
180   *
181   * @return the class name of the region split policy for this table. If this
182   * returns null, the default split policy is used.
183   */
184  String getRegionSplitPolicyClassName();
185
186  /**
187   * Get the name of the table
188   *
189   * @return TableName
190   */
191  TableName getTableName();
192
193  @Deprecated
194  String getOwnerString();
195
196  /**
197   * Getter for accessing the metadata associated with the key.
198   *
199   * @param key The key.
200   * @return A clone value. Null if no mapping for the key
201   */
202  Bytes getValue(Bytes key);
203
204  /**
205   * Getter for accessing the metadata associated with the key.
206   *
207   * @param key The key.
208   * @return A clone value. Null if no mapping for the key
209   */
210  byte[] getValue(byte[] key);
211
212  /**
213   * Getter for accessing the metadata associated with the key.
214   *
215   * @param key The key.
216   * @return Null if no mapping for the key
217   */
218  String getValue(String key);
219
220  /**
221   * @return Getter for fetching an unmodifiable map.
222   */
223  Map<Bytes, Bytes> getValues();
224
225  /**
226   * Check if the table has an attached co-processor represented by the name
227   * className
228   *
229   * @param classNameToMatch - Class name of the co-processor
230   * @return true of the table has a co-processor className
231   */
232  boolean hasCoprocessor(String classNameToMatch);
233
234  /**
235   * Checks to see if this table contains the given column family
236   *
237   * @param name Family name or column name.
238   * @return true if the table contains the specified family name
239   */
240  boolean hasColumnFamily(final byte[] name);
241
242  /**
243   * @return true if the read-replicas memstore replication is enabled.
244   */
245  boolean hasRegionMemStoreReplication();
246
247  /**
248   * Check if the compaction enable flag of the table is true. If flag is false
249   * then no minor/major compactions will be done in real.
250   *
251   * @return true if table compaction enabled
252   */
253  boolean isCompactionEnabled();
254
255  /**
256   * Checks if this table is <code> hbase:meta </code> region.
257   *
258   * @return true if this table is <code> hbase:meta </code> region
259   */
260  boolean isMetaRegion();
261
262  /**
263   * Checks if the table is a <code>hbase:meta</code> table
264   *
265   * @return true if table is <code> hbase:meta </code> region.
266   */
267  boolean isMetaTable();
268
269  /**
270   * Check if normalization enable flag of the table is true. If flag is false
271   * then no region normalizer won't attempt to normalize this table.
272   *
273   * @return true if region normalization is enabled for this table
274   */
275  boolean isNormalizationEnabled();
276
277  /**
278   * Check if the readOnly flag of the table is set. If the readOnly flag is set
279   * then the contents of the table can only be read from but not modified.
280   *
281   * @return true if all columns in the table should be read only
282   */
283  boolean isReadOnly();
284
285  /**
286   * Check if the table's cfs' replication scope matched with the replication state
287   * @param enabled replication state
288   * @return true if matched, otherwise false
289   */
290  default boolean matchReplicationScope(boolean enabled) {
291    boolean hasEnabled = false;
292    boolean hasDisabled = false;
293
294    for (ColumnFamilyDescriptor cf : getColumnFamilies()) {
295      if (cf.getScope() != HConstants.REPLICATION_SCOPE_GLOBAL) {
296        hasDisabled = true;
297      } else {
298        hasEnabled = true;
299      }
300    }
301
302    if (hasEnabled && hasDisabled) {
303      return false;
304    }
305    if (hasEnabled) {
306      return enabled;
307    }
308    return !enabled;
309  }
310}