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  /**
194   * @deprecated since 2.0.0 and will be removed in 3.0.0.
195   * @see <a href="https://issues.apache.org/jira/browse/HBASE-15583">HBASE-15583</a>
196   */
197  @Deprecated
198  String getOwnerString();
199
200  /**
201   * Getter for accessing the metadata associated with the key.
202   *
203   * @param key The key.
204   * @return A clone value. Null if no mapping for the key
205   */
206  Bytes getValue(Bytes key);
207
208  /**
209   * Getter for accessing the metadata associated with the key.
210   *
211   * @param key The key.
212   * @return A clone value. Null if no mapping for the key
213   */
214  byte[] getValue(byte[] key);
215
216  /**
217   * Getter for accessing the metadata associated with the key.
218   *
219   * @param key The key.
220   * @return Null if no mapping for the key
221   */
222  String getValue(String key);
223
224  /**
225   * @return Getter for fetching an unmodifiable map.
226   */
227  Map<Bytes, Bytes> getValues();
228
229  /**
230   * Check if the table has an attached co-processor represented by the name
231   * className
232   *
233   * @param classNameToMatch - Class name of the co-processor
234   * @return true of the table has a co-processor className
235   */
236  boolean hasCoprocessor(String classNameToMatch);
237
238  /**
239   * Checks to see if this table contains the given column family
240   *
241   * @param name Family name or column name.
242   * @return true if the table contains the specified family name
243   */
244  boolean hasColumnFamily(final byte[] name);
245
246  /**
247   * @return true if the read-replicas memstore replication is enabled.
248   */
249  boolean hasRegionMemStoreReplication();
250
251  /**
252   * Check if the compaction enable flag of the table is true. If flag is false
253   * then no minor/major compactions will be done in real.
254   *
255   * @return true if table compaction enabled
256   */
257  boolean isCompactionEnabled();
258
259  /**
260   * Checks if this table is <code> hbase:meta </code> region.
261   *
262   * @return true if this table is <code> hbase:meta </code> region
263   */
264  boolean isMetaRegion();
265
266  /**
267   * Checks if the table is a <code>hbase:meta</code> table
268   *
269   * @return true if table is <code> hbase:meta </code> region.
270   */
271  boolean isMetaTable();
272
273  /**
274   * Check if normalization enable flag of the table is true. If flag is false
275   * then no region normalizer won't attempt to normalize this table.
276   *
277   * @return true if region normalization is enabled for this table
278   */
279  boolean isNormalizationEnabled();
280
281  /**
282   * Check if the readOnly flag of the table is set. If the readOnly flag is set
283   * then the contents of the table can only be read from but not modified.
284   *
285   * @return true if all columns in the table should be read only
286   */
287  boolean isReadOnly();
288
289  /**
290   * Check if the table's cfs' replication scope matched with the replication state
291   * @param enabled replication state
292   * @return true if matched, otherwise false
293   */
294  default boolean matchReplicationScope(boolean enabled) {
295    boolean hasEnabled = false;
296    boolean hasDisabled = false;
297
298    for (ColumnFamilyDescriptor cf : getColumnFamilies()) {
299      if (cf.getScope() != HConstants.REPLICATION_SCOPE_GLOBAL) {
300        hasDisabled = true;
301      } else {
302        hasEnabled = true;
303      }
304    }
305
306    if (hasEnabled && hasDisabled) {
307      return false;
308    }
309    if (hasEnabled) {
310      return enabled;
311    }
312    return !enabled;
313  }
314}