Skip to content

Commit aa19c45

Browse files
committed
MongoDb file listing support
1 parent 386875a commit aa19c45

1 file changed

Lines changed: 63 additions & 103 deletions

File tree

src/main/java/picoded/dstack/mongodb/MongoDB_FileWorkspaceMap.java

Lines changed: 63 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// Java imports
44
import java.util.ArrayList;
5+
import java.util.Arrays;
56
import java.util.HashSet;
67
import java.util.List;
78
import java.util.Map;
@@ -505,108 +506,7 @@ public void backend_ensureFolderPath(final String oid, final String folderPath)
505506
}
506507
}
507508

508-
// Move support
509509
//--------------------------------------------------------------------------
510-
511-
/**
512-
* [Internal use, to be extended in future implementation]
513-
*
514-
* Move a given file within the system
515-
*
516-
* WARNING: Move operations are typically not "atomic" in nature, and can be unsafe where
517-
* missing files / corrupted data can occur when executed concurrently with other operations.
518-
*
519-
* In general "S3-like" object storage will not safely support atomic move operations.
520-
* Please use the `atomicMoveSupported()` function to validate if such operations are supported.
521-
*
522-
* This operation may in effect function as a rename
523-
* If the destionation file exists, it will be overwritten
524-
*
525-
* @param ObjectID of workspace
526-
* @param sourceFile
527-
* @param destinationFile
528-
*/
529-
public void backend_moveFile(final String oid, final String sourceFile,
530-
final String destinationFile) {
531-
throw new RuntimeException("Missing backend implementation");
532-
}
533-
534-
/**
535-
* [Internal use, to be extended in future implementation]
536-
*
537-
* Move a given file within the system
538-
*
539-
* WARNING: Move operations are typically not "atomic" in nature, and can be unsafe where
540-
* missing files / corrupted data can occur when executed concurrently with other operations.
541-
*
542-
* In general "S3-like" object storage will not safely support atomic move operations.
543-
* Please use the `atomicMoveSupported()` function to validate if such operations are supported.
544-
*
545-
* Note that both source, and destionation folder will be normalized to include the "/" path.
546-
* This operation may in effect function as a rename
547-
* If the destionation folder exists with content, the result will be merged. With the sourceFolder files, overwriting on conflicts.
548-
*
549-
* @param ObjectID of workspace
550-
* @param sourceFolder
551-
* @param destinationFolder
552-
*
553-
*/
554-
public void backend_moveFolderPath(final String oid, final String sourceFolder,
555-
final String destinationFolder) {
556-
throw new RuntimeException("Missing backend implementation");
557-
}
558-
559-
// Copy support
560-
//--------------------------------------------------------------------------
561-
562-
/**
563-
* [Internal use, to be extended in future implementation]
564-
*
565-
* Copy a given file within the system
566-
*
567-
* WARNING: Copy operations are typically not "atomic" in nature, and can be unsafe where
568-
* missing files / corrupted data can occur when executed concurrently with other operations.
569-
*
570-
* In general "S3-like" object storage will not safely support atomic copy operations.
571-
* Please use the `atomicCopySupported()` function to validate if such operations are supported.
572-
*
573-
* This operation may in effect function as a rename
574-
* If the destionation file exists, it will be overwritten
575-
*
576-
* @param ObjectID of workspace
577-
* @param sourceFile
578-
* @param destinationFile
579-
*/
580-
public void backend_copyFile(final String oid, final String sourceFile,
581-
final String destinationFile) {
582-
throw new RuntimeException("Missing backend implementation");
583-
}
584-
585-
/**
586-
* [Internal use, to be extended in future implementation]
587-
*
588-
* Copy a given file within the system
589-
*
590-
* WARNING: Copy operations are typically not "atomic" in nature, and can be unsafe where
591-
* missing files / corrupted data can occur when executed concurrently with other operations.
592-
*
593-
* In general "S3-like" object storage will not safely support atomic Copy operations.
594-
* Please use the `atomicCopySupported()` function to validate if such operations are supported.
595-
*
596-
* Note that both source, and destionation folder will be normalized to include the "/" path.
597-
* This operation may in effect function as a rename
598-
* If the destionation folder exists with content, the result will be merged. With the sourceFolder files, overwriting on conflicts.
599-
*
600-
* @param ObjectID of workspace
601-
* @param sourceFolder
602-
* @param destinationFolder
603-
*
604-
*/
605-
public void backend_copyFolderPath(final String oid, final String sourceFolder,
606-
final String destinationFolder) {
607-
throw new RuntimeException("Missing backend implementation");
608-
}
609-
610510
//
611511
// Create and updated timestamp support
612512
//
@@ -646,6 +546,12 @@ public long backend_modifiedTimestamp(final String oid, final String filepath) {
646546
return -1;
647547
}
648548

549+
//--------------------------------------------------------------------------
550+
//
551+
// Query, and listing support
552+
//
553+
//--------------------------------------------------------------------------
554+
649555
/**
650556
* List all the various files and folders found in the given folderPath
651557
*
@@ -657,9 +563,63 @@ public long backend_modifiedTimestamp(final String oid, final String filepath) {
657563
* @return list of path strings - relative to the given folderPath (folders end with "/")
658564
*/
659565
@Override
660-
public Set<String> backend_getFileAndFolderPathSet(final String oid, final String folderPath,
566+
public Set<String> backend_getFileAndFolderPathSet(final String oid, String folderPath,
661567
final int minDepth, final int maxDepth) {
662-
throw new RuntimeException("Missing backend implementation");
568+
569+
// Lets build the query for the "root file"
570+
Bson query = null;
571+
572+
// The fulle prefix path
573+
String fullPrefixPath = oid+"/";
574+
575+
// Lets build the query, for fetchign the relevent items
576+
if( folderPath == null || folderPath.equals("/") || folderPath.isEmpty() ) {
577+
// Handles query for all folder paths
578+
query = Filters.eq("metadata._oid", oid);
579+
} else {
580+
// Cleanup the path
581+
folderPath = cleanFilePath(folderPath);
582+
fullPrefixPath = fullPrefixPath+folderPath;
583+
584+
// Remove matching path
585+
query = Filters.and(
586+
Filters.eq("metadata._oid", oid),
587+
Filters.regex("filename", "^"+Pattern.quote(folderPath)+".*")
588+
);
589+
}
590+
591+
// The return set
592+
Set<String> ret = new HashSet<>();
593+
594+
// Lets prepare the search
595+
GridFSFindIterable search = gridFSBucket.find(query);
596+
597+
// Lets iterate the search result, and return true on an item
598+
try (MongoCursor<GridFSFile> cursor = search.iterator()) {
599+
while (cursor.hasNext()) {
600+
// Get the fileobj and filename
601+
GridFSFile fileObj = cursor.next();
602+
String fullFilename = fileObj.getFilename();
603+
604+
// Remove the oid prefix
605+
String filepath = fullFilename.substring( fullPrefixPath.length() );
606+
607+
// Register the validpath
608+
ret.add(filepath);
609+
610+
// Lets split the filepath
611+
String[] filepathArr = filepath.split("/");
612+
List<String> filepathList = Arrays.asList(filepathArr);
613+
614+
// Lets handle parent folders
615+
for(int i=1+Math.max(minDepth,0); i<(filepathArr.length-1); ++i) {
616+
ret.add( String.join("/", filepathList.subList(0,i)+"/" ) );
617+
}
618+
}
619+
}
620+
621+
// Filter and return the final set
622+
return backend_filtterPathSet( ret, folderPath, minDepth, maxDepth, 0);
663623
}
664624

665625
}

0 commit comments

Comments
 (0)