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.client;
019
020import java.util.Collections;
021import java.util.HashMap;
022import java.util.Map;
023import org.apache.hadoop.hbase.TableName;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * Base class for all table builders.
028 */
029@InterfaceAudience.Private
030abstract class TableBuilderBase implements TableBuilder {
031
032  protected TableName tableName;
033
034  protected int operationTimeout;
035
036  protected int rpcTimeout;
037
038  protected int readRpcTimeout;
039
040  protected int writeRpcTimeout;
041
042  protected Map<String, byte[]> requestAttributes = Collections.emptyMap();
043
044  TableBuilderBase(TableName tableName, ConnectionConfiguration connConf) {
045    if (tableName == null) {
046      throw new IllegalArgumentException("Given table name is null");
047    }
048    this.tableName = tableName;
049    this.operationTimeout = tableName.isSystemTable()
050      ? connConf.getMetaOperationTimeout()
051      : connConf.getOperationTimeout();
052    this.rpcTimeout = connConf.getRpcTimeout();
053    this.readRpcTimeout =
054      tableName.isSystemTable() ? connConf.getMetaReadRpcTimeout() : connConf.getReadRpcTimeout();
055    this.writeRpcTimeout = connConf.getWriteRpcTimeout();
056  }
057
058  @Override
059  public TableBuilderBase setOperationTimeout(int timeout) {
060    this.operationTimeout = timeout;
061    return this;
062  }
063
064  @Override
065  public TableBuilderBase setRpcTimeout(int timeout) {
066    this.rpcTimeout = timeout;
067    return this;
068  }
069
070  @Override
071  public TableBuilderBase setReadRpcTimeout(int timeout) {
072    this.readRpcTimeout = timeout;
073    return this;
074  }
075
076  @Override
077  public TableBuilderBase setWriteRpcTimeout(int timeout) {
078    this.writeRpcTimeout = timeout;
079    return this;
080  }
081
082  @Override
083  public TableBuilderBase setRequestAttribute(String key, byte[] value) {
084    if (this.requestAttributes.isEmpty()) {
085      this.requestAttributes = new HashMap<>();
086    }
087    this.requestAttributes.put(key, value);
088    return this;
089  }
090}