Skip to content

Commit 666b0c1

Browse files
committed
HADOOP-19131. Assist reflection IO with WrappedOperations class
Change-Id: Ic29643e8844115ab5938cf041dac02e5b528586f
1 parent 5bfca65 commit 666b0c1

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.hadoop.io;
20+
21+
import java.io.IOException;
22+
import java.util.Map;
23+
24+
import org.apache.hadoop.classification.InterfaceAudience;
25+
import org.apache.hadoop.classification.InterfaceStability;
26+
import org.apache.hadoop.fs.FSDataInputStream;
27+
import org.apache.hadoop.fs.FileStatus;
28+
import org.apache.hadoop.fs.FileSystem;
29+
import org.apache.hadoop.fs.FutureDataInputStreamBuilder;
30+
import org.apache.hadoop.util.functional.FutureIO;
31+
32+
import static org.apache.hadoop.fs.Options.OpenFileOptions.FS_OPTION_OPENFILE_READ_POLICY;
33+
34+
/**
35+
* Reflection-friendly access to APIs which are not available in
36+
* some of the older Hadoop versions which libraries still
37+
* compile against.
38+
* <p>
39+
* The intent is to avoid the need for complex reflection operations
40+
* including wrapping of parameter classes, direct instatiation of
41+
* new classes etc.
42+
*/
43+
@InterfaceAudience.Public
44+
@InterfaceStability.Evolving
45+
public final class WrappedOperations {
46+
47+
private WrappedOperations() {
48+
}
49+
50+
/**
51+
* OpenFile assistant, easy reflection-based access to
52+
* {@link FileSystem#openFile(org.apache.hadoop.fs.Path)}.
53+
* @param fs filesystem
54+
* @param path path
55+
* @param policy read policy
56+
* @param status optional file status
57+
* @param options nullable map of other options
58+
* @return an opened file
59+
* @throws IOException for any failure.
60+
*/
61+
public static FSDataInputStream openFile(final FileSystem fs,
62+
final org.apache.hadoop.fs.Path path,
63+
final String policy,
64+
final FileStatus status,
65+
final Map<String, String> options) throws IOException {
66+
final FutureDataInputStreamBuilder builder = fs.openFile(path);
67+
if (policy != null) {
68+
builder.opt(FS_OPTION_OPENFILE_READ_POLICY, policy);
69+
}
70+
if (status != null) {
71+
builder.withFileStatus(status);
72+
}
73+
if (options != null) {
74+
// add all the options map entries
75+
options.forEach(builder::opt);
76+
}
77+
// wait for the opening.
78+
return FutureIO.awaitFuture(builder.build());
79+
}
80+
}

0 commit comments

Comments
 (0)