001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.io.hfile; 020 021import static org.apache.hbase.thirdparty.com.google.common.base.Preconditions.checkArgument; 022import static org.apache.hbase.thirdparty.com.google.common.base.Preconditions.checkNotNull; 023import java.io.IOException; 024import org.apache.hadoop.fs.FileSystem; 025import org.apache.hadoop.fs.Path; 026import org.apache.hadoop.hbase.fs.HFileSystem; 027import org.apache.hadoop.hbase.io.FSDataInputStreamWrapper; 028import org.apache.hadoop.hbase.io.hfile.ReaderContext.ReaderType; 029import org.apache.yetus.audience.InterfaceAudience; 030import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; 031 032/** 033 * A builder that helps in building up the ReaderContext 034 */ 035@InterfaceAudience.Private 036public class ReaderContextBuilder { 037 private Path filePath; 038 private FSDataInputStreamWrapper fsdis; 039 private long fileSize; 040 private HFileSystem hfs; 041 private boolean primaryReplicaReader = true; 042 private ReaderType type = ReaderType.PREAD; 043 044 public ReaderContextBuilder() {} 045 046 public ReaderContextBuilder withFilePath(Path filePath) { 047 this.filePath = filePath; 048 return this; 049 } 050 051 public ReaderContextBuilder withFileSize(long fileSize) { 052 this.fileSize = fileSize; 053 return this; 054 } 055 056 public ReaderContextBuilder withInputStreamWrapper(FSDataInputStreamWrapper fsdis) { 057 this.fsdis = fsdis; 058 return this; 059 } 060 061 public ReaderContextBuilder withFileSystem(HFileSystem hfs) { 062 this.hfs = hfs; 063 return this; 064 } 065 066 public ReaderContextBuilder withFileSystem(FileSystem fs) { 067 if (!(fs instanceof HFileSystem)) { 068 this.hfs = new HFileSystem(fs); 069 } else { 070 this.hfs = (HFileSystem) fs; 071 } 072 return this; 073 } 074 075 public ReaderContextBuilder withPrimaryReplicaReader(boolean primaryReplicaReader) { 076 this.primaryReplicaReader = primaryReplicaReader; 077 return this; 078 } 079 080 public ReaderContextBuilder withReaderType(ReaderType type) { 081 this.type = type; 082 return this; 083 } 084 085 @VisibleForTesting 086 public ReaderContextBuilder withFileSystemAndPath(FileSystem fs, Path filePath) 087 throws IOException { 088 this.withFileSystem(fs) 089 .withFilePath(filePath) 090 .withFileSize(fs.getFileStatus(filePath).getLen()) 091 .withInputStreamWrapper(new FSDataInputStreamWrapper(fs, filePath)); 092 return this; 093 } 094 095 public ReaderContext build() { 096 validateFields(); 097 return new ReaderContext(filePath, fsdis, fileSize, hfs, primaryReplicaReader, type); 098 } 099 100 private void validateFields() throws IllegalArgumentException { 101 checkNotNull(filePath, "Illegal ReaderContext, no filePath specified."); 102 checkNotNull(fsdis, "Illegal ReaderContext, no StreamWrapper specified."); 103 checkNotNull(hfs, "Illegal ReaderContext, no HFileSystem specified."); 104 checkArgument(fileSize > 0L, "Illegal ReaderContext, fileSize <= 0"); 105 } 106}