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.io.hfile; 019 020import static org.apache.hbase.thirdparty.com.google.common.base.Preconditions.checkArgument; 021import static org.apache.hbase.thirdparty.com.google.common.base.Preconditions.checkNotNull; 022 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.hadoop.hbase.keymeta.ManagedKeyDataCache; 030import org.apache.hadoop.hbase.keymeta.SystemKeyCache; 031import org.apache.yetus.audience.InterfaceAudience; 032 033/** 034 * A builder that helps in building up the ReaderContext 035 */ 036@InterfaceAudience.Private 037public class ReaderContextBuilder { 038 private Path filePath; 039 private FSDataInputStreamWrapper fsdis; 040 private long fileSize; 041 private HFileSystem hfs; 042 private boolean primaryReplicaReader = true; 043 private ReaderType type = ReaderType.PREAD; 044 private SystemKeyCache systemKeyCache; 045 private ManagedKeyDataCache managedKeyDataCache; 046 047 public ReaderContextBuilder() { 048 } 049 050 public static ReaderContextBuilder newBuilder(ReaderContext readerContext) { 051 return new ReaderContextBuilder(readerContext); 052 } 053 054 private ReaderContextBuilder(ReaderContext readerContext) { 055 this.filePath = readerContext.getFilePath(); 056 this.fsdis = readerContext.getInputStreamWrapper(); 057 this.fileSize = readerContext.getFileSize(); 058 this.hfs = readerContext.getFileSystem(); 059 this.type = readerContext.getReaderType(); 060 this.systemKeyCache = readerContext.getSystemKeyCache(); 061 this.managedKeyDataCache = readerContext.getManagedKeyDataCache(); 062 } 063 064 public ReaderContextBuilder withFilePath(Path filePath) { 065 this.filePath = filePath; 066 return this; 067 } 068 069 public ReaderContextBuilder withFileSize(long fileSize) { 070 this.fileSize = fileSize; 071 return this; 072 } 073 074 public ReaderContextBuilder withInputStreamWrapper(FSDataInputStreamWrapper fsdis) { 075 this.fsdis = fsdis; 076 return this; 077 } 078 079 public ReaderContextBuilder withFileSystem(HFileSystem hfs) { 080 this.hfs = hfs; 081 return this; 082 } 083 084 public ReaderContextBuilder withFileSystem(FileSystem fs) { 085 if (!(fs instanceof HFileSystem)) { 086 this.hfs = new HFileSystem(fs); 087 } else { 088 this.hfs = (HFileSystem) fs; 089 } 090 return this; 091 } 092 093 public ReaderContextBuilder withPrimaryReplicaReader(boolean primaryReplicaReader) { 094 this.primaryReplicaReader = primaryReplicaReader; 095 return this; 096 } 097 098 public ReaderContextBuilder withReaderType(ReaderType type) { 099 this.type = type; 100 return this; 101 } 102 103 public ReaderContextBuilder withFileSystemAndPath(FileSystem fs, Path filePath) 104 throws IOException { 105 this.withFileSystem(fs).withFilePath(filePath).withFileSize(fs.getFileStatus(filePath).getLen()) 106 .withInputStreamWrapper(new FSDataInputStreamWrapper(fs, filePath)); 107 return this; 108 } 109 110 public ReaderContextBuilder withManagedKeyDataCache(ManagedKeyDataCache managedKeyDataCache) { 111 this.managedKeyDataCache = managedKeyDataCache; 112 return this; 113 } 114 115 public ReaderContextBuilder withSystemKeyCache(SystemKeyCache systemKeyCache) { 116 this.systemKeyCache = systemKeyCache; 117 return this; 118 } 119 120 public ReaderContext build() { 121 validateFields(); 122 return new ReaderContext(filePath, fsdis, fileSize, hfs, primaryReplicaReader, type, 123 systemKeyCache, managedKeyDataCache); 124 } 125 126 private void validateFields() throws IllegalArgumentException { 127 checkNotNull(filePath, "Illegal ReaderContext, no filePath specified."); 128 checkNotNull(fsdis, "Illegal ReaderContext, no StreamWrapper specified."); 129 checkNotNull(hfs, "Illegal ReaderContext, no HFileSystem specified."); 130 checkArgument(fileSize > 0L, "Illegal ReaderContext, fileSize <= 0"); 131 } 132}