001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019
020import java.io.Closeable;
021import java.io.IOException;
022import java.util.Map;
023import org.apache.hadoop.hbase.client.TableDescriptor;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * Get, remove and modify table descriptors.
028 */
029@InterfaceAudience.Private
030public interface TableDescriptors extends Closeable {
031
032  /**
033   * Test whether a given table exists, i.e, has a table descriptor.
034   */
035  default boolean exists(TableName tableName) throws IOException {
036    return get(tableName) != null;
037  }
038
039  @Override
040  default void close() throws IOException {
041    // do nothing by default
042  }
043
044  /**
045   * @return TableDescriptor for tablename
046   */
047  TableDescriptor get(TableName tableName) throws IOException;
048
049  /**
050   * Get Map of all NamespaceDescriptors for a given namespace.
051   * @return Map of all descriptors.
052   */
053  Map<String, TableDescriptor> getByNamespace(String name) throws IOException;
054
055  /**
056   * Get Map of all TableDescriptors. Populates the descriptor cache as a side effect.
057   * </p>
058   * Notice: the key of map is the table name which contains namespace. It was generated by
059   * {@link TableName#getNameWithNamespaceInclAsString()}.
060   * @return Map of all descriptors.
061   */
062  Map<String, TableDescriptor> getAll() throws IOException;
063
064  /**
065   * Add or update descriptor. Just call {@link #update(TableDescriptor, boolean)} with
066   * {@code cacheOnly} as {@code false}.
067   */
068  default void update(TableDescriptor htd) throws IOException {
069    update(htd, false);
070  }
071
072  /**
073   * Add or update descriptor
074   * @param htd       Descriptor to set into TableDescriptors
075   * @param cacheOnly only add the given {@code htd} to cache, without updating the storage. For
076   *                  example, when creating table, we will write the descriptor to fs when creating
077   *                  the fs layout, so we do not need to update the fs again.
078   */
079  void update(TableDescriptor htd, boolean cacheOnly) throws IOException;
080
081  /**
082   * @return Instance of table descriptor or null if none found.
083   */
084  TableDescriptor remove(TableName tablename) throws IOException;
085}