Skip to content

Commit 6834150

Browse files
committed
Add support for directory sorting with optional parameter. default/undefined behavior is no sort.
1 parent 137da7b commit 6834150

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

src/main/java/org/apache/hadoop/fs/glusterfs/GlusterVolume.java

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Arrays;
2929
import java.util.Enumeration;
3030
import java.util.Hashtable;
31+
import java.util.Vector;
3132

3233
import org.apache.hadoop.conf.Configuration;
3334
import org.apache.hadoop.fs.BlockLocation;
@@ -57,7 +58,7 @@ public class GlusterVolume extends RawLocalFileSystem{
5758

5859
protected Hashtable<String,String> volumes=new Hashtable<String,String>();
5960
protected String default_volume = null;
60-
61+
protected boolean sortDirectoryListing = false;
6162

6263
protected static GlusterFSXattr attr = null;
6364

@@ -107,7 +108,14 @@ public void setConf(Configuration conf){
107108
if(conf!=null){
108109

109110
try{
110-
String[] v=conf.get("fs.glusterfs.volumes", "").split(",");
111+
String r=conf.get("fs.glusterfs.volumes", "");
112+
113+
if("".equals(r)){
114+
log.error("fs.glusterfs.volumes not defined.");
115+
throw new RuntimeException("Error loading gluster configuration.. fs.glusterfs.volumes not defined.");
116+
}
117+
String[] v=r.split(",");
118+
111119
default_volume = v[0];
112120
for(int i=0;i<v.length;i++){
113121
String vol = conf.get("fs.glusterfs.volume.fuse." + v[i] , null);
@@ -170,6 +178,10 @@ public void setConf(Configuration conf){
170178
}
171179
log.info("Default block size : " +conf.getInt("fs.local.block.size",-1)) ;
172180

181+
sortDirectoryListing=conf.getBoolean("fs.glusterfs.sort.directory.listing",false);
182+
183+
log.info("Directory list order : " + (sortDirectoryListing?"sorted":"fs ordering")) ;
184+
173185
}
174186
catch (Exception e){
175187
throw new RuntimeException(e);
@@ -269,10 +281,11 @@ public boolean mkdirs(Path f) throws IOException {
269281
return super.mkdirs(f);
270282
}
271283

272-
public FileStatus[] listStatus(Path f) throws IOException {
284+
public FileStatus[] listStatus(Path f) throws IOException {
273285
File localf = pathToFile(f);
274-
FileStatus[] results;
275-
286+
Vector<FileStatus> results = new Vector<FileStatus>();
287+
288+
276289
if (!localf.exists()) {
277290
throw new FileNotFoundException("File " + f + " does not exist");
278291
}
@@ -285,21 +298,34 @@ public FileStatus[] listStatus(Path f) throws IOException {
285298
if (names == null) {
286299
return null;
287300
}
288-
results = new FileStatus[names.length];
289-
int j = 0;
301+
290302
for (int i = 0; i < names.length; i++) {
291303
try {
292-
results[j] = getFileStatus(fileToPath(names[i]));
293-
j++;
304+
FileStatus listing = getFileStatus(fileToPath(names[i]));
305+
if(sortDirectoryListing){
306+
int j;
307+
for(j=0;j<results.size();j++){
308+
309+
if(results.get(j).compareTo(listing)>0){
310+
results.insertElementAt(listing,j);
311+
break;
312+
}
313+
314+
}
315+
if(results.size()==j)
316+
results.add(listing);
317+
}else{
318+
results.add(listing);
319+
}
320+
294321
} catch (FileNotFoundException e) {
295322
// ignore the files not found since the dir list may have have changed
296323
// since the names[] list was generated.
297324
}
298325
}
299-
if (j == names.length) {
300-
return results;
301-
}
302-
return Arrays.copyOf(results, j);
326+
327+
return results.toArray(new FileStatus[results.size()]);
328+
303329
}
304330

305331
public FileStatus getFileStatus(Path f) throws IOException {

0 commit comments

Comments
 (0)