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  /** Returns TableDescriptor for tablename */
045  TableDescriptor get(TableName tableName) throws IOException;
046
047  /**
048   * Get Map of all NamespaceDescriptors for a given namespace.
049   * @return Map of all descriptors.
050   */
051  Map<String, TableDescriptor> getByNamespace(String name) throws IOException;
052
053  /**
054   * Get Map of all TableDescriptors. Populates the descriptor cache as a side effect.
055   * </p>
056   * Notice: the key of map is the table name which contains namespace. It was generated by
057   * {@link TableName#getNameWithNamespaceInclAsString()}.
058   * @return Map of all descriptors.
059   */
060  Map<String, TableDescriptor> getAll() throws IOException;
061
062  /**
063   * Add or update descriptor. Just call {@link #update(TableDescriptor, boolean)} with
064   * {@code cacheOnly} as {@code false}.
065   */
066  default void update(TableDescriptor htd) throws IOException {
067    update(htd, false);
068  }
069
070  /**
071   * Add or update descriptor
072   * @param htd       Descriptor to set into TableDescriptors
073   * @param cacheOnly only add the given {@code htd} to cache, without updating the storage. For
074   *                  example, when creating table, we will write the descriptor to fs when creating
075   *                  the fs layout, so we do not need to update the fs again.
076   */
077  void update(TableDescriptor htd, boolean cacheOnly) throws IOException;
078
079  /** Returns Instance of table descriptor or null if none found. */
080  TableDescriptor remove(TableName tablename) throws IOException;
081}