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.Map;
021import java.util.concurrent.TimeUnit;
022import org.apache.yetus.audience.InterfaceAudience;
023
024import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
025import org.apache.hbase.thirdparty.io.netty.util.HashedWheelTimer;
026
027/**
028 * The implementation of {@link AsyncBufferedMutatorBuilder}.
029 */
030@InterfaceAudience.Private
031class AsyncBufferedMutatorBuilderImpl implements AsyncBufferedMutatorBuilder {
032
033  private final HashedWheelTimer periodicalFlushTimer;
034
035  private final AsyncTableBuilder<?> tableBuilder;
036
037  private long writeBufferSize;
038
039  private long periodicFlushTimeoutNs;
040
041  private int maxKeyValueSize;
042
043  private int maxMutations;
044
045  public AsyncBufferedMutatorBuilderImpl(AsyncConnectionConfiguration connConf,
046    AsyncTableBuilder<?> tableBuilder, HashedWheelTimer periodicalFlushTimer) {
047    this.tableBuilder = tableBuilder;
048    this.writeBufferSize = connConf.getWriteBufferSize();
049    this.periodicFlushTimeoutNs = connConf.getWriteBufferPeriodicFlushTimeoutNs();
050    this.maxKeyValueSize = connConf.getMaxKeyValueSize();
051    this.maxMutations = connConf.getBufferedMutatorMaxMutations();
052    this.periodicalFlushTimer = periodicalFlushTimer;
053  }
054
055  @Override
056  public AsyncBufferedMutatorBuilder setOperationTimeout(long timeout, TimeUnit unit) {
057    tableBuilder.setOperationTimeout(timeout, unit);
058    return this;
059  }
060
061  @Override
062  public AsyncBufferedMutatorBuilder setRpcTimeout(long timeout, TimeUnit unit) {
063    tableBuilder.setRpcTimeout(timeout, unit);
064    return this;
065  }
066
067  @Override
068  public AsyncBufferedMutatorBuilder setRetryPause(long pause, TimeUnit unit) {
069    tableBuilder.setRetryPause(pause, unit);
070    return this;
071  }
072
073  @Override
074  public AsyncBufferedMutatorBuilder setMaxAttempts(int maxAttempts) {
075    tableBuilder.setMaxAttempts(maxAttempts);
076    return this;
077  }
078
079  @Override
080  public AsyncBufferedMutatorBuilder setStartLogErrorsCnt(int startLogErrorsCnt) {
081    tableBuilder.setStartLogErrorsCnt(startLogErrorsCnt);
082    return this;
083  }
084
085  @Override
086  public AsyncBufferedMutatorBuilder setRequestAttribute(String key, byte[] value) {
087    tableBuilder.setRequestAttribute(key, value);
088    return this;
089  }
090
091  @Override
092  public AsyncBufferedMutatorBuilder setRequestAttributes(Map<String, byte[]> requestAttributes) {
093    for (Map.Entry<String, byte[]> requestAttribute : requestAttributes.entrySet()) {
094      tableBuilder.setRequestAttribute(requestAttribute.getKey(), requestAttribute.getValue());
095    }
096    return this;
097  }
098
099  @Override
100  public AsyncBufferedMutatorBuilder setWriteBufferSize(long writeBufferSize) {
101    Preconditions.checkArgument(writeBufferSize > 0, "writeBufferSize %d must be > 0",
102      writeBufferSize);
103    this.writeBufferSize = writeBufferSize;
104    return this;
105  }
106
107  @Override
108  public AsyncBufferedMutatorBuilder setWriteBufferPeriodicFlush(long timeout, TimeUnit unit) {
109    this.periodicFlushTimeoutNs = unit.toNanos(timeout);
110    return this;
111  }
112
113  @Override
114  public AsyncBufferedMutatorBuilder setMaxKeyValueSize(int maxKeyValueSize) {
115    Preconditions.checkArgument(maxKeyValueSize > 0, "maxKeyValueSize %d must be > 0",
116      maxKeyValueSize);
117    this.maxKeyValueSize = maxKeyValueSize;
118    return this;
119  }
120
121  @Override
122  public AsyncBufferedMutatorBuilder setMaxMutations(int maxMutations) {
123    Preconditions.checkArgument(maxMutations > 0, "maxMutations %d must be > 0", maxMutations);
124    this.maxMutations = maxMutations;
125    return this;
126  }
127
128  @Override
129  public AsyncBufferedMutator build() {
130    return new AsyncBufferedMutatorImpl(periodicalFlushTimer, tableBuilder.build(), writeBufferSize,
131      periodicFlushTimeoutNs, maxKeyValueSize, maxMutations);
132  }
133}