1 | /** |
---|
2 | * |
---|
3 | * Licensed under the Apache License, Version 2.0 |
---|
4 | * (the "License"); you may not use this file except in compliance with |
---|
5 | * the License. You may obtain a copy of the License at |
---|
6 | * |
---|
7 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | * |
---|
9 | * Unless required by applicable law or agreed to in writing, software |
---|
10 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
---|
12 | * implied. See the License for the specific language governing |
---|
13 | * permissions and limitations under the License. |
---|
14 | * |
---|
15 | * @author: Sriram Rao (Kosmix Corp.) |
---|
16 | * |
---|
17 | * We need to provide the ability to the code in fs/kfs without really |
---|
18 | * having a KFS deployment. For this purpose, use the LocalFileSystem |
---|
19 | * as a way to "emulate" KFS. |
---|
20 | */ |
---|
21 | |
---|
22 | package org.apache.hadoop.fs.kfs; |
---|
23 | |
---|
24 | import java.io.*; |
---|
25 | |
---|
26 | import org.apache.hadoop.conf.Configuration; |
---|
27 | |
---|
28 | import org.apache.hadoop.fs.FSDataInputStream; |
---|
29 | import org.apache.hadoop.fs.FSDataOutputStream; |
---|
30 | import org.apache.hadoop.fs.FileSystem; |
---|
31 | import org.apache.hadoop.fs.FileStatus; |
---|
32 | import org.apache.hadoop.fs.FileUtil; |
---|
33 | import org.apache.hadoop.fs.Path; |
---|
34 | import org.apache.hadoop.fs.BlockLocation; |
---|
35 | |
---|
36 | |
---|
37 | public class KFSEmulationImpl implements IFSImpl { |
---|
38 | FileSystem localFS; |
---|
39 | |
---|
40 | public KFSEmulationImpl(Configuration conf) throws IOException { |
---|
41 | localFS = FileSystem.getLocal(conf); |
---|
42 | } |
---|
43 | |
---|
44 | public boolean exists(String path) throws IOException { |
---|
45 | return localFS.exists(new Path(path)); |
---|
46 | } |
---|
47 | public boolean isDirectory(String path) throws IOException { |
---|
48 | return localFS.isDirectory(new Path(path)); |
---|
49 | } |
---|
50 | public boolean isFile(String path) throws IOException { |
---|
51 | return localFS.isFile(new Path(path)); |
---|
52 | } |
---|
53 | |
---|
54 | public String[] readdir(String path) throws IOException { |
---|
55 | FileStatus[] p = localFS.listStatus(new Path(path)); |
---|
56 | String[] entries = null; |
---|
57 | |
---|
58 | if (p == null) { |
---|
59 | return null; |
---|
60 | } |
---|
61 | |
---|
62 | entries = new String[p.length]; |
---|
63 | for (int i = 0; i < p.length; i++) |
---|
64 | entries[i] = p[i].getPath().toString(); |
---|
65 | return entries; |
---|
66 | } |
---|
67 | |
---|
68 | public FileStatus[] readdirplus(Path path) throws IOException { |
---|
69 | return localFS.listStatus(path); |
---|
70 | } |
---|
71 | |
---|
72 | public int mkdirs(String path) throws IOException { |
---|
73 | if (localFS.mkdirs(new Path(path))) |
---|
74 | return 0; |
---|
75 | |
---|
76 | return -1; |
---|
77 | } |
---|
78 | |
---|
79 | public int rename(String source, String dest) throws IOException { |
---|
80 | if (localFS.rename(new Path(source), new Path(dest))) |
---|
81 | return 0; |
---|
82 | return -1; |
---|
83 | } |
---|
84 | |
---|
85 | public int rmdir(String path) throws IOException { |
---|
86 | if (isDirectory(path)) { |
---|
87 | // the directory better be empty |
---|
88 | String[] dirEntries = readdir(path); |
---|
89 | if ((dirEntries.length <= 2) && (localFS.delete(new Path(path), true))) |
---|
90 | return 0; |
---|
91 | } |
---|
92 | return -1; |
---|
93 | } |
---|
94 | |
---|
95 | public int remove(String path) throws IOException { |
---|
96 | if (isFile(path) && (localFS.delete(new Path(path), true))) |
---|
97 | return 0; |
---|
98 | return -1; |
---|
99 | } |
---|
100 | |
---|
101 | public long filesize(String path) throws IOException { |
---|
102 | return localFS.getLength(new Path(path)); |
---|
103 | } |
---|
104 | public short getReplication(String path) throws IOException { |
---|
105 | return 1; |
---|
106 | } |
---|
107 | public short setReplication(String path, short replication) throws IOException { |
---|
108 | return 1; |
---|
109 | } |
---|
110 | public String[][] getDataLocation(String path, long start, long len) throws IOException { |
---|
111 | BlockLocation[] blkLocations = |
---|
112 | localFS.getFileBlockLocations(localFS.getFileStatus(new Path(path)), |
---|
113 | start, len); |
---|
114 | if ((blkLocations == null) || (blkLocations.length == 0)) { |
---|
115 | return new String[0][]; |
---|
116 | } |
---|
117 | int blkCount = blkLocations.length; |
---|
118 | String[][]hints = new String[blkCount][]; |
---|
119 | for (int i=0; i < blkCount ; i++) { |
---|
120 | String[] hosts = blkLocations[i].getHosts(); |
---|
121 | hints[i] = new String[hosts.length]; |
---|
122 | hints[i] = hosts; |
---|
123 | } |
---|
124 | return hints; |
---|
125 | } |
---|
126 | |
---|
127 | public long getModificationTime(String path) throws IOException { |
---|
128 | FileStatus s = localFS.getFileStatus(new Path(path)); |
---|
129 | if (s == null) |
---|
130 | return 0; |
---|
131 | |
---|
132 | return s.getModificationTime(); |
---|
133 | } |
---|
134 | |
---|
135 | public FSDataOutputStream create(String path, short replication, int bufferSize) throws IOException { |
---|
136 | // besides path/overwrite, the other args don't matter for |
---|
137 | // testing purposes. |
---|
138 | return localFS.create(new Path(path)); |
---|
139 | } |
---|
140 | |
---|
141 | public FSDataInputStream open(String path, int bufferSize) throws IOException { |
---|
142 | return localFS.open(new Path(path)); |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | }; |
---|