Skip to content

Commit a7f5578

Browse files
authored
Update MongoDB_FileWorkspaceMap.java
1 parent 254b229 commit a7f5578

1 file changed

Lines changed: 134 additions & 0 deletions

File tree

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

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,140 @@ public Set<String> backend_getFileAndFolderPathSet(final String oid, String fold
902902
return backend_filterPathSet(ret, folderPath, minDepth, maxDepth, 0);
903903
}
904904

905+
/**
906+
* Internal utility function used to filter a path set, and remove items that does not match.
907+
* This is used to help filter raw results, from existing implementation
908+
*
909+
* - its folderPath prefix
910+
* - min/max depth
911+
* - any / file / folder
912+
*
913+
* @param rawSet (note this expect the full RAW paths, without removing the folderPath prefix)
914+
* @param folderPath the folder path prefix to search and match against, and truncate
915+
* @param minDepth (0 = all items, 1 = must be in atleast a folder, 2 = folder, inside a folder)
916+
* @param maxDepth
917+
* @param pathType (0 = any, 1 = file, 2 = folder)
918+
* @return
919+
*/
920+
@Override
921+
protected Set<String> backend_filterPathSet(final Set<String> rawSet, final String folderPath,
922+
final int minDepth, final int maxDepth, final int pathType) {
923+
924+
// Normalize the folder path
925+
String searchPath = folderPath;
926+
if (searchPath == null || searchPath.equals("/")) {
927+
searchPath = "";
928+
}
929+
int searchPathLen = searchPath.length();
930+
931+
// // Debugging stuff
932+
// System.out.println( "#" );
933+
// System.out.println( "searchPath: "+searchPath );
934+
// System.out.println( "searchPathLen: "+searchPathLen );
935+
// System.out.println( "minDepth: "+minDepth );
936+
// System.out.println( "maxDepth: "+maxDepth );
937+
// System.out.println( "pathType: "+pathType );
938+
// System.out.println( ConvertJSON.fromObject(rawSet) );
939+
940+
// Return set
941+
Set<String> ret = new HashSet<>();
942+
943+
// Get the keyset, and iterate it
944+
for (String key : rawSet) {
945+
System.out.println("KEY="+key);
946+
947+
// Skip the root folder of a workspace
948+
if (key.equals("") || key.equals("/")) {
949+
continue;
950+
}
951+
952+
// If folder does not match - skip
953+
if (searchPathLen > 0 && !key.startsWith(searchPath)) {
954+
continue;
955+
}
956+
957+
// If folder path match - store it - maybe?
958+
String subPath = key.substring(searchPathLen);
959+
960+
//Dirty Fix for empty folder being deleted instead of moved
961+
//Empty folder being considered as "" somehow
962+
if (subPath.equals("")) {
963+
ret.add("/");
964+
continue;
965+
}
966+
967+
// Skip the root folder of a subpath
968+
if (subPath.equals("/")) {
969+
continue;
970+
}
971+
972+
// No filtering is needed, store and continue
973+
if (maxDepth <= 0 && minDepth <= 0) {
974+
// Does no checks, add and continue
975+
ret.add(subPath);
976+
continue;
977+
}
978+
979+
// Lets perform path filtering
980+
// ---
981+
982+
// Lets filter out the ending "/"
983+
String filteredSubPath = subPath;
984+
if (filteredSubPath.endsWith("/")) {
985+
filteredSubPath = filteredSubPath.substring(0, filteredSubPath.length() - 1);
986+
}
987+
988+
// Split and count
989+
String[] splitSubPath = filteredSubPath.split("/");
990+
int subPathLength = (filteredSubPath.length() <= 0) ? 0 : splitSubPath.length;
991+
992+
// Check min depth - skip key if check failed
993+
if (minDepth > 0 && subPathLength < minDepth) {
994+
continue;
995+
}
996+
997+
// Check max depth - skip key if check failed
998+
if (maxDepth > 0 && subPathLength > maxDepth) {
999+
continue;
1000+
}
1001+
1002+
// Alrighto - lets check file / folder type - and add it in
1003+
// ---
1004+
1005+
// Ignore empty
1006+
if (subPath.isEmpty()) {
1007+
continue;
1008+
}
1009+
1010+
// Expect a folder, reject files
1011+
if (pathType == 1) {
1012+
if (subPath.endsWith("/")) {
1013+
// Not a file - abort!
1014+
continue;
1015+
}
1016+
}
1017+
1018+
// Expect files, reject folders
1019+
if (pathType == 2) {
1020+
if (!subPath.endsWith("/")) {
1021+
// Not a folder - abort!
1022+
continue;
1023+
}
1024+
}
1025+
1026+
// Finally - all checks passed : add the path
1027+
ret.add(subPath);
1028+
}
1029+
1030+
// // Debugging stuff
1031+
// System.out.println( "Filtered Set" );
1032+
// System.out.println( ConvertJSON.fromObject(ret) );
1033+
1034+
1035+
// Return the filtered set
1036+
return ret;
1037+
}
1038+
9051039
//--------------------------------------------------------------------------
9061040
//
9071041
// KeySet support

0 commit comments

Comments
 (0)