Skip to content

Commit 3ce041d

Browse files
committed
Merge pull request #98 from childsb/bz1066366
Add support for directory sorting with optional parameter.
2 parents 7278986 + 6834150 commit 3ce041d

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
@@ -29,6 +29,7 @@
2929
import java.util.Arrays;
3030
import java.util.Enumeration;
3131
import java.util.Hashtable;
32+
import java.util.Vector;
3233

3334
import org.apache.hadoop.conf.Configuration;
3435
import org.apache.hadoop.fs.BlockLocation;
@@ -58,7 +59,7 @@ public class GlusterVolume extends RawLocalFileSystem{
5859

5960
protected Hashtable<String,String> volumes=new Hashtable<String,String>();
6061
protected String default_volume = null;
61-
62+
protected boolean sortDirectoryListing = false;
6263

6364
protected static GlusterFSXattr attr = null;
6465

@@ -115,7 +116,14 @@ public void setConf(Configuration conf){
115116
if(conf!=null){
116117

117118
try{
118-
String[] v=conf.get("fs.glusterfs.volumes", "").split(",");
119+
String r=conf.get("fs.glusterfs.volumes", "");
120+
121+
if("".equals(r)){
122+
log.error("fs.glusterfs.volumes not defined.");
123+
throw new RuntimeException("Error loading gluster configuration.. fs.glusterfs.volumes not defined.");
124+
}
125+
String[] v=r.split(",");
126+
119127
default_volume = v[0];
120128
for(int i=0;i<v.length;i++){
121129
String vol = conf.get("fs.glusterfs.volume.fuse." + v[i] , null);
@@ -183,6 +191,10 @@ public void setConf(Configuration conf){
183191
}
184192
log.info("Default block size : " +conf.getInt("fs.local.block.size",-1)) ;
185193

194+
sortDirectoryListing=conf.getBoolean("fs.glusterfs.sort.directory.listing",false);
195+
196+
log.info("Directory list order : " + (sortDirectoryListing?"sorted":"fs ordering")) ;
197+
186198
}
187199
catch (Exception e){
188200
throw new RuntimeException(e);
@@ -290,10 +302,11 @@ public boolean mkdirs(Path f) throws IOException {
290302
return super.mkdirs(f);
291303
}
292304

293-
public FileStatus[] listStatus(Path f) throws IOException {
305+
public FileStatus[] listStatus(Path f) throws IOException {
294306
File localf = pathToFile(f);
295-
FileStatus[] results;
296-
307+
Vector<FileStatus> results = new Vector<FileStatus>();
308+
309+
297310
if (!localf.exists()) {
298311
throw new FileNotFoundException("File " + f + " does not exist");
299312
}
@@ -310,20 +323,33 @@ public FileStatus[] listStatus(Path f) throws IOException {
310323
if (names == null) {
311324
return null;
312325
}
313-
results = new FileStatus[names.length];
314-
int j = 0;
326+
315327
for (int i = 0; i < names.length; i++) {
316328
try {
317-
results[j] = getFileStatus(fileToPath(names[i]));
318-
j++;
329+
FileStatus listing = getFileStatus(fileToPath(names[i]));
330+
if(sortDirectoryListing){
331+
int j;
332+
for(j=0;j<results.size();j++){
333+
334+
if(results.get(j).compareTo(listing)>0){
335+
results.insertElementAt(listing,j);
336+
break;
337+
}
338+
339+
}
340+
if(results.size()==j)
341+
results.add(listing);
342+
}else{
343+
results.add(listing);
344+
}
345+
319346
} catch (FileNotFoundException e) {
320347
log.info("ignoring invisible path : " + names[i]);
321348
}
322349
}
323-
if (j == names.length) {
324-
return results;
325-
}
326-
return Arrays.copyOf(results, j);
350+
351+
return results.toArray(new FileStatus[results.size()]);
352+
327353
}
328354

329355
public FileStatus getFileStatus(Path f) throws IOException {

0 commit comments

Comments
 (0)