[120] | 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.s3; |
---|
| 20 | |
---|
| 21 | import java.io.BufferedInputStream; |
---|
| 22 | import java.io.BufferedOutputStream; |
---|
| 23 | import java.io.ByteArrayOutputStream; |
---|
| 24 | import java.io.File; |
---|
| 25 | import java.io.FileInputStream; |
---|
| 26 | import java.io.FileOutputStream; |
---|
| 27 | import java.io.IOException; |
---|
| 28 | import java.net.URI; |
---|
| 29 | import java.util.HashMap; |
---|
| 30 | import java.util.LinkedHashSet; |
---|
| 31 | import java.util.Map; |
---|
| 32 | import java.util.Set; |
---|
| 33 | import java.util.SortedMap; |
---|
| 34 | import java.util.TreeMap; |
---|
| 35 | |
---|
| 36 | import org.apache.hadoop.conf.Configuration; |
---|
| 37 | import org.apache.hadoop.fs.Path; |
---|
| 38 | import org.apache.hadoop.fs.s3.INode.FileType; |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * A stub implementation of {@link FileSystemStore} for testing |
---|
| 42 | * {@link S3FileSystem} without actually connecting to S3. |
---|
| 43 | */ |
---|
| 44 | class InMemoryFileSystemStore implements FileSystemStore { |
---|
| 45 | |
---|
| 46 | private Configuration conf; |
---|
| 47 | private SortedMap<Path, INode> inodes = new TreeMap<Path, INode>(); |
---|
| 48 | private Map<Long, byte[]> blocks = new HashMap<Long, byte[]>(); |
---|
| 49 | |
---|
| 50 | public void initialize(URI uri, Configuration conf) { |
---|
| 51 | this.conf = conf; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | public String getVersion() throws IOException { |
---|
| 55 | return "0"; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | public void deleteINode(Path path) throws IOException { |
---|
| 59 | inodes.remove(normalize(path)); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | public void deleteBlock(Block block) throws IOException { |
---|
| 63 | blocks.remove(block.getId()); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | public boolean inodeExists(Path path) throws IOException { |
---|
| 67 | return inodes.containsKey(normalize(path)); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | public boolean blockExists(long blockId) throws IOException { |
---|
| 71 | return blocks.containsKey(blockId); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | public INode retrieveINode(Path path) throws IOException { |
---|
| 75 | return inodes.get(normalize(path)); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | public File retrieveBlock(Block block, long byteRangeStart) throws IOException { |
---|
| 79 | byte[] data = blocks.get(block.getId()); |
---|
| 80 | File file = createTempFile(); |
---|
| 81 | BufferedOutputStream out = null; |
---|
| 82 | try { |
---|
| 83 | out = new BufferedOutputStream(new FileOutputStream(file)); |
---|
| 84 | out.write(data, (int) byteRangeStart, data.length - (int) byteRangeStart); |
---|
| 85 | } finally { |
---|
| 86 | if (out != null) { |
---|
| 87 | out.close(); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | return file; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | private File createTempFile() throws IOException { |
---|
| 94 | File dir = new File(conf.get("fs.s3.buffer.dir")); |
---|
| 95 | if (!dir.exists() && !dir.mkdirs()) { |
---|
| 96 | throw new IOException("Cannot create S3 buffer directory: " + dir); |
---|
| 97 | } |
---|
| 98 | File result = File.createTempFile("test-", ".tmp", dir); |
---|
| 99 | result.deleteOnExit(); |
---|
| 100 | return result; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | public Set<Path> listSubPaths(Path path) throws IOException { |
---|
| 104 | Path normalizedPath = normalize(path); |
---|
| 105 | // This is inefficient but more than adequate for testing purposes. |
---|
| 106 | Set<Path> subPaths = new LinkedHashSet<Path>(); |
---|
| 107 | for (Path p : inodes.tailMap(normalizedPath).keySet()) { |
---|
| 108 | if (normalizedPath.equals(p.getParent())) { |
---|
| 109 | subPaths.add(p); |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | return subPaths; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | public Set<Path> listDeepSubPaths(Path path) throws IOException { |
---|
| 116 | Path normalizedPath = normalize(path); |
---|
| 117 | String pathString = normalizedPath.toUri().getPath(); |
---|
| 118 | if (!pathString.endsWith("/")) { |
---|
| 119 | pathString += "/"; |
---|
| 120 | } |
---|
| 121 | // This is inefficient but more than adequate for testing purposes. |
---|
| 122 | Set<Path> subPaths = new LinkedHashSet<Path>(); |
---|
| 123 | for (Path p : inodes.tailMap(normalizedPath).keySet()) { |
---|
| 124 | if (p.toUri().getPath().startsWith(pathString)) { |
---|
| 125 | subPaths.add(p); |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | return subPaths; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | public void storeINode(Path path, INode inode) throws IOException { |
---|
| 132 | inodes.put(normalize(path), inode); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | public void storeBlock(Block block, File file) throws IOException { |
---|
| 136 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
| 137 | byte[] buf = new byte[8192]; |
---|
| 138 | int numRead; |
---|
| 139 | BufferedInputStream in = null; |
---|
| 140 | try { |
---|
| 141 | in = new BufferedInputStream(new FileInputStream(file)); |
---|
| 142 | while ((numRead = in.read(buf)) >= 0) { |
---|
| 143 | out.write(buf, 0, numRead); |
---|
| 144 | } |
---|
| 145 | } finally { |
---|
| 146 | if (in != null) { |
---|
| 147 | in.close(); |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | blocks.put(block.getId(), out.toByteArray()); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | private Path normalize(Path path) { |
---|
| 154 | if (!path.isAbsolute()) { |
---|
| 155 | throw new IllegalArgumentException("Path must be absolute: " + path); |
---|
| 156 | } |
---|
| 157 | return new Path(path.toUri().getPath()); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | public void purge() throws IOException { |
---|
| 161 | inodes.clear(); |
---|
| 162 | blocks.clear(); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | public void dump() throws IOException { |
---|
| 166 | StringBuilder sb = new StringBuilder(getClass().getSimpleName()); |
---|
| 167 | sb.append(", \n"); |
---|
| 168 | for (Map.Entry<Path, INode> entry : inodes.entrySet()) { |
---|
| 169 | sb.append(entry.getKey()).append("\n"); |
---|
| 170 | INode inode = entry.getValue(); |
---|
| 171 | sb.append("\t").append(inode.getFileType()).append("\n"); |
---|
| 172 | if (inode.getFileType() == FileType.DIRECTORY) { |
---|
| 173 | continue; |
---|
| 174 | } |
---|
| 175 | for (int j = 0; j < inode.getBlocks().length; j++) { |
---|
| 176 | sb.append("\t").append(inode.getBlocks()[j]).append("\n"); |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | System.out.println(sb); |
---|
| 180 | |
---|
| 181 | System.out.println(inodes.keySet()); |
---|
| 182 | System.out.println(blocks.keySet()); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | } |
---|