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.regionserver;
019
020import java.io.IOException;
021import org.apache.hadoop.hbase.KeepDeletedCells;
022import org.apache.hadoop.hbase.client.ImmutableScan;
023import org.apache.hadoop.hbase.client.Scan;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * Helper class for CP hooks to change max versions and TTL.
028 */
029@InterfaceAudience.Private
030public class CustomizedScanInfoBuilder implements ScanOptions {
031
032  private final ScanInfo scanInfo;
033
034  private Integer maxVersions;
035
036  private Long ttl;
037
038  private KeepDeletedCells keepDeletedCells = null;
039
040  private Integer minVersions;
041
042  private final Scan scan;
043
044  public CustomizedScanInfoBuilder(ScanInfo scanInfo) {
045    this.scanInfo = scanInfo;
046    this.scan = new ImmutableScan(new Scan());
047  }
048
049  public CustomizedScanInfoBuilder(ScanInfo scanInfo, Scan scan) {
050    this.scanInfo = scanInfo;
051    //copy the scan so no coproc using this ScanOptions can alter the "real" scan
052    this.scan = new ImmutableScan(scan);
053  }
054
055  @Override
056  public int getMaxVersions() {
057    return maxVersions != null ? maxVersions.intValue() : scanInfo.getMaxVersions();
058  }
059
060  @Override
061  public void setMaxVersions(int maxVersions) {
062    this.maxVersions = maxVersions;
063  }
064
065  @Override
066  public long getTTL() {
067    return ttl != null ? ttl.longValue() : scanInfo.getTtl();
068  }
069
070  @Override
071  public void setTTL(long ttl) {
072    this.ttl = ttl;
073  }
074
075  public ScanInfo build() {
076    if (maxVersions == null && ttl == null && keepDeletedCells == null) {
077      return scanInfo;
078    }
079    return scanInfo.customize(getMaxVersions(), getTTL(), getKeepDeletedCells(), getMinVersions());
080  }
081
082  @Override
083  public String toString() {
084    return "ScanOptions [maxVersions=" + getMaxVersions() + ", TTL=" + getTTL() +
085      ", KeepDeletedCells=" + getKeepDeletedCells() + ", MinVersions=" + getMinVersions() + "]";
086  }
087
088  @Override
089  public void setKeepDeletedCells(KeepDeletedCells keepDeletedCells) {
090    this.keepDeletedCells = keepDeletedCells;
091  }
092
093  @Override
094  public KeepDeletedCells getKeepDeletedCells() {
095    return keepDeletedCells != null ? keepDeletedCells : scanInfo.getKeepDeletedCells();
096  }
097
098  @Override
099  public int getMinVersions() {
100    return minVersions != null ? minVersions : scanInfo.getMinVersions();
101  }
102
103  @Override
104  public void setMinVersions(int minVersions) {
105    this.minVersions = minVersions;
106  }
107
108  @Override
109  public Scan getScan() {
110    return scan;
111  }
112
113}