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.fs.s3native; |
---|
20 | |
---|
21 | import static org.apache.hadoop.fs.s3native.NativeS3FileSystem.PATH_DELIMITER; |
---|
22 | import java.io.BufferedInputStream; |
---|
23 | import java.io.BufferedOutputStream; |
---|
24 | import java.io.ByteArrayOutputStream; |
---|
25 | import java.io.File; |
---|
26 | import java.io.FileInputStream; |
---|
27 | import java.io.FileOutputStream; |
---|
28 | import java.io.IOException; |
---|
29 | import java.io.InputStream; |
---|
30 | import java.net.URI; |
---|
31 | import java.util.ArrayList; |
---|
32 | import java.util.Iterator; |
---|
33 | import java.util.List; |
---|
34 | import java.util.SortedMap; |
---|
35 | import java.util.SortedSet; |
---|
36 | import java.util.TreeMap; |
---|
37 | import java.util.TreeSet; |
---|
38 | import java.util.Map.Entry; |
---|
39 | |
---|
40 | import org.apache.hadoop.conf.Configuration; |
---|
41 | |
---|
42 | /** |
---|
43 | * <p> |
---|
44 | * A stub implementation of {@link NativeFileSystemStore} for testing |
---|
45 | * {@link NativeS3FileSystem} without actually connecting to S3. |
---|
46 | * </p> |
---|
47 | */ |
---|
48 | class InMemoryNativeFileSystemStore implements NativeFileSystemStore { |
---|
49 | |
---|
50 | private Configuration conf; |
---|
51 | |
---|
52 | private SortedMap<String, FileMetadata> metadataMap = |
---|
53 | new TreeMap<String, FileMetadata>(); |
---|
54 | private SortedMap<String, byte[]> dataMap = new TreeMap<String, byte[]>(); |
---|
55 | |
---|
56 | public void initialize(URI uri, Configuration conf) throws IOException { |
---|
57 | this.conf = conf; |
---|
58 | } |
---|
59 | |
---|
60 | public void storeEmptyFile(String key) throws IOException { |
---|
61 | metadataMap.put(key, new FileMetadata(key, 0, System.currentTimeMillis())); |
---|
62 | dataMap.put(key, new byte[0]); |
---|
63 | } |
---|
64 | |
---|
65 | public void storeFile(String key, File file, byte[] md5Hash) |
---|
66 | throws IOException { |
---|
67 | |
---|
68 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
69 | byte[] buf = new byte[8192]; |
---|
70 | int numRead; |
---|
71 | BufferedInputStream in = null; |
---|
72 | try { |
---|
73 | in = new BufferedInputStream(new FileInputStream(file)); |
---|
74 | while ((numRead = in.read(buf)) >= 0) { |
---|
75 | out.write(buf, 0, numRead); |
---|
76 | } |
---|
77 | } finally { |
---|
78 | if (in != null) { |
---|
79 | in.close(); |
---|
80 | } |
---|
81 | } |
---|
82 | metadataMap.put(key, |
---|
83 | new FileMetadata(key, file.length(), System.currentTimeMillis())); |
---|
84 | dataMap.put(key, out.toByteArray()); |
---|
85 | } |
---|
86 | |
---|
87 | public InputStream retrieve(String key) throws IOException { |
---|
88 | return retrieve(key, 0); |
---|
89 | } |
---|
90 | |
---|
91 | public InputStream retrieve(String key, long byteRangeStart) |
---|
92 | throws IOException { |
---|
93 | |
---|
94 | byte[] data = dataMap.get(key); |
---|
95 | File file = createTempFile(); |
---|
96 | BufferedOutputStream out = null; |
---|
97 | try { |
---|
98 | out = new BufferedOutputStream(new FileOutputStream(file)); |
---|
99 | out.write(data, (int) byteRangeStart, |
---|
100 | data.length - (int) byteRangeStart); |
---|
101 | } finally { |
---|
102 | if (out != null) { |
---|
103 | out.close(); |
---|
104 | } |
---|
105 | } |
---|
106 | return new FileInputStream(file); |
---|
107 | } |
---|
108 | |
---|
109 | private File createTempFile() throws IOException { |
---|
110 | File dir = new File(conf.get("fs.s3.buffer.dir")); |
---|
111 | if (!dir.exists() && !dir.mkdirs()) { |
---|
112 | throw new IOException("Cannot create S3 buffer directory: " + dir); |
---|
113 | } |
---|
114 | File result = File.createTempFile("test-", ".tmp", dir); |
---|
115 | result.deleteOnExit(); |
---|
116 | return result; |
---|
117 | } |
---|
118 | |
---|
119 | public FileMetadata retrieveMetadata(String key) throws IOException { |
---|
120 | return metadataMap.get(key); |
---|
121 | } |
---|
122 | |
---|
123 | public PartialListing list(String prefix, int maxListingLength) |
---|
124 | throws IOException { |
---|
125 | return list(prefix, maxListingLength, null); |
---|
126 | } |
---|
127 | |
---|
128 | public PartialListing list(String prefix, int maxListingLength, |
---|
129 | String priorLastKey) throws IOException { |
---|
130 | |
---|
131 | return list(prefix, PATH_DELIMITER, maxListingLength, priorLastKey); |
---|
132 | } |
---|
133 | |
---|
134 | public PartialListing listAll(String prefix, int maxListingLength, |
---|
135 | String priorLastKey) throws IOException { |
---|
136 | |
---|
137 | return list(prefix, null, maxListingLength, priorLastKey); |
---|
138 | } |
---|
139 | |
---|
140 | private PartialListing list(String prefix, String delimiter, |
---|
141 | int maxListingLength, String priorLastKey) throws IOException { |
---|
142 | |
---|
143 | if (prefix.length() > 0 && !prefix.endsWith(PATH_DELIMITER)) { |
---|
144 | prefix += PATH_DELIMITER; |
---|
145 | } |
---|
146 | |
---|
147 | List<FileMetadata> metadata = new ArrayList<FileMetadata>(); |
---|
148 | SortedSet<String> commonPrefixes = new TreeSet<String>(); |
---|
149 | for (String key : dataMap.keySet()) { |
---|
150 | if (key.startsWith(prefix)) { |
---|
151 | if (delimiter == null) { |
---|
152 | metadata.add(retrieveMetadata(key)); |
---|
153 | } else { |
---|
154 | int delimIndex = key.indexOf(delimiter, prefix.length()); |
---|
155 | if (delimIndex == -1) { |
---|
156 | metadata.add(retrieveMetadata(key)); |
---|
157 | } else { |
---|
158 | String commonPrefix = key.substring(0, delimIndex); |
---|
159 | commonPrefixes.add(commonPrefix); |
---|
160 | } |
---|
161 | } |
---|
162 | } |
---|
163 | if (metadata.size() + commonPrefixes.size() == maxListingLength) { |
---|
164 | new PartialListing(key, metadata.toArray(new FileMetadata[0]), |
---|
165 | commonPrefixes.toArray(new String[0])); |
---|
166 | } |
---|
167 | } |
---|
168 | return new PartialListing(null, metadata.toArray(new FileMetadata[0]), |
---|
169 | commonPrefixes.toArray(new String[0])); |
---|
170 | } |
---|
171 | |
---|
172 | public void delete(String key) throws IOException { |
---|
173 | metadataMap.remove(key); |
---|
174 | dataMap.remove(key); |
---|
175 | } |
---|
176 | |
---|
177 | public void rename(String srcKey, String dstKey) throws IOException { |
---|
178 | metadataMap.put(dstKey, metadataMap.remove(srcKey)); |
---|
179 | dataMap.put(dstKey, dataMap.remove(srcKey)); |
---|
180 | } |
---|
181 | |
---|
182 | public void purge(String prefix) throws IOException { |
---|
183 | Iterator<Entry<String, FileMetadata>> i = |
---|
184 | metadataMap.entrySet().iterator(); |
---|
185 | while (i.hasNext()) { |
---|
186 | Entry<String, FileMetadata> entry = i.next(); |
---|
187 | if (entry.getKey().startsWith(prefix)) { |
---|
188 | dataMap.remove(entry.getKey()); |
---|
189 | i.remove(); |
---|
190 | } |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | public void dump() throws IOException { |
---|
195 | System.out.println(metadataMap.values()); |
---|
196 | System.out.println(dataMap.keySet()); |
---|
197 | } |
---|
198 | } |
---|