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  public AsyncBufferedMutatorBuilderImpl(AsyncConnectionConfiguration connConf,
044    AsyncTableBuilder<?> tableBuilder, HashedWheelTimer periodicalFlushTimer) {
045    this.tableBuilder = tableBuilder;
046    this.writeBufferSize = connConf.getWriteBufferSize();
047    this.periodicFlushTimeoutNs = connConf.getWriteBufferPeriodicFlushTimeoutNs();
048    this.maxKeyValueSize = connConf.getMaxKeyValueSize();
049    this.periodicalFlushTimer = periodicalFlushTimer;
050  }
051
052  @Override
053  public AsyncBufferedMutatorBuilder setOperationTimeout(long timeout, TimeUnit unit) {
054    tableBuilder.setOperationTimeout(timeout, unit);
055    return this;
056  }
057
058  @Override
059  public AsyncBufferedMutatorBuilder setRpcTimeout(long timeout, TimeUnit unit) {
060    tableBuilder.setRpcTimeout(timeout, unit);
061    return this;
062  }
063
064  @Override
065  public AsyncBufferedMutatorBuilder setRetryPause(long pause, TimeUnit unit) {
066    tableBuilder.setRetryPause(pause, unit);
067    return this;
068  }
069
070  @Override
071  public AsyncBufferedMutatorBuilder setMaxAttempts(int maxAttempts) {
072    tableBuilder.setMaxAttempts(maxAttempts);
073    return this;
074  }
075
076  @Override
077  public AsyncBufferedMutatorBuilder setStartLogErrorsCnt(int startLogErrorsCnt) {
078    tableBuilder.setStartLogErrorsCnt(startLogErrorsCnt);
079    return this;
080  }
081
082  @Override
083  public AsyncBufferedMutatorBuilder setRequestAttribute(String key, byte[] value) {
084    tableBuilder.setRequestAttribute(key, value);
085    return this;
086  }
087
088  @Override
089  public AsyncBufferedMutatorBuilder setRequestAttributes(Map<String, byte[]> requestAttributes) {
090    for (Map.Entry<String, byte[]> requestAttribute : requestAttributes.entrySet()) {
091      tableBuilder.setRequestAttribute(requestAttribute.getKey(), requestAttribute.getValue());
092    }
093    return this;
094  }
095
096  @Override
097  public AsyncBufferedMutatorBuilder setWriteBufferSize(long writeBufferSize) {
098    Preconditions.checkArgument(writeBufferSize > 0, "writeBufferSize %d must be > 0",
099      writeBufferSize);
100    this.writeBufferSize = writeBufferSize;
101    return this;
102  }
103
104  @Override
105  public AsyncBufferedMutatorBuilder setWriteBufferPeriodicFlush(long timeout, TimeUnit unit) {
106    this.periodicFlushTimeoutNs = unit.toNanos(timeout);
107    return this;
108  }
109
110  @Override
111  public AsyncBufferedMutatorBuilder setMaxKeyValueSize(int maxKeyValueSize) {
112    Preconditions.checkArgument(maxKeyValueSize > 0, "maxKeyValueSize %d must be > 0",
113      maxKeyValueSize);
114    this.maxKeyValueSize = maxKeyValueSize;
115    return this;
116  }
117
118  @Override
119  public AsyncBufferedMutator build() {
120    return new AsyncBufferedMutatorImpl(periodicalFlushTimer, tableBuilder.build(), writeBufferSize,
121      periodicFlushTimeoutNs, maxKeyValueSize);
122  }
123}