3535import com .mongodb .client .gridfs .model .GridFSFile ;
3636import com .mongodb .client .gridfs .model .GridFSUploadOptions ;
3737import com .mongodb .client .model .Filters ;
38+ import com .mongodb .client .model .IndexOptions ;
39+ import com .mongodb .client .model .Indexes ;
3840
3941/**
4042 * ## Purpose
@@ -56,9 +58,13 @@ public class MongoDB_FileWorkspaceMap extends Core_FileWorkspaceMap {
5658 //
5759 // --------------------------------------------------------------------------
5860
59- /** MongoDB instance representing the backend connection */
61+ /** MongoDB instance representing gridFS */
6062 GridFSBucket gridFSBucket = null ;
6163
64+ /** MongoDB instance representing the files and chunks collection (internal to the gridFSBucket) */
65+ MongoCollection <Document > filesCollection = null ;
66+ MongoCollection <Document > chunksCollection = null ;
67+
6268 /**
6369 * Constructor, with name constructor
6470 *
@@ -84,6 +90,9 @@ public MongoDB_FileWorkspaceMap(MongoDBStack inStack, String name) {
8490 // Meaning a full "8 * 1000 * 1000" chunk would use "8 * 1024 * 1024"
8591 // worth of space, after adding the unknown headers (<=4kb of space : 8*24*24)
8692 //
93+
94+ filesCollection = inStack .db_conn .getCollection (name +".files" );
95+ chunksCollection = inStack .db_conn .getCollection (name +".chunks" );
8796 }
8897
8998 //--------------------------------------------------------------------------
@@ -97,6 +106,21 @@ public MongoDB_FileWorkspaceMap(MongoDBStack inStack, String name) {
97106 **/
98107 @ Override
99108 public void systemSetup () {
109+
110+ // We insert a "root" object, to ensure the tables are initialized
111+ // ---
112+ if (!fullRawPathExist ("root" )) {
113+ setupAnchorFile ("root" , "root" , "root" );
114+ }
115+
116+ // Lets setup the index for the metadata fields (which is not enabled by default)
117+ // ---
118+
119+ // Lets create the index for the oid
120+ IndexOptions opt = new IndexOptions ();
121+ opt = opt .name ("metadata.oid" );
122+ filesCollection .createIndex (Indexes .ascending ("oid" ), opt );
123+
100124 }
101125
102126 /**
@@ -148,7 +172,7 @@ public boolean backend_workspaceExist(String oid) {
148172 public void backend_setupWorkspace (String oid ) {
149173 // We setup a blank file with type root
150174 if (!fullRawPathExist (oid )) {
151- setupAnchorFile (oid , oid , "root " );
175+ setupAnchorFile (oid , oid , "space " );
152176 }
153177 }
154178
@@ -341,7 +365,8 @@ public void backend_fileWrite(String oid, String filepath, byte[] data) {
341365 * @param filepath to use for the workspace
342366 * @param data to write the file with
343367 **/
344- public void backend_fileWriteInputStream (String oid , String filepath , InputStream data ) {
368+ @ Override
369+ public void backend_fileWriteInputStream (final String oid , final String filepath , InputStream data ) {
345370 // Build the full path
346371 String fullPath = oid + "/" + filepath ;
347372
@@ -567,25 +592,22 @@ public Set<String> backend_getFileAndFolderPathSet(final String oid, String fold
567592 // The fulle prefix path
568593 String fullPrefixPath = oid +"/" ;
569594
570- // Lets build the query, for fetchign the relevent items
571595 if ( folderPath == null || folderPath .equals ("/" ) || folderPath .isEmpty () ) {
572- // Handles query for all folder paths
573- query = Filters .and (
574- Filters .eq ("metadata.oid" , oid ),
575- // Filters.ne("filename", oid)
576- Filters .regex ("filename" , "^" +Pattern .quote (fullPrefixPath )+".*" )
577- );
596+ // Query everything (using only the oid)
597+ query = Filters .eq ("metadata.oid" , oid );
578598 } else {
579- // Cleanup the path
599+ // Query using oid and the path
580600 fullPrefixPath = fullPrefixPath +folderPath ;
581-
601+
582602 // Remove matching path
583603 query = Filters .and (
584604 Filters .eq ("metadata.oid" , oid ),
585605 Filters .regex ("filename" , "^" +Pattern .quote (fullPrefixPath )+".*" )
586606 );
587607 }
588608
609+ query = Filters .eq ("metadata.oid" , oid );
610+
589611 // The return set
590612 Set <String > ret = new HashSet <>();
591613
@@ -599,19 +621,30 @@ public Set<String> backend_getFileAndFolderPathSet(final String oid, String fold
599621 GridFSFile fileObj = cursor .next ();
600622 String fullFilename = fileObj .getFilename ();
601623
624+ // Skip the oid anchor
625+ if ( fullFilename .equals (oid ) ) {
626+ continue ;
627+ }
628+
602629 // Remove the oid prefix
603- String filepath = fullFilename .substring ( fullPrefixPath .length () );
630+ String filepath = fullFilename .substring ( oid .length ()+ 1 );
604631
605632 // Register the validpath
606633 ret .add (filepath );
607634
635+ // Prepare a clean path without ending slash
636+ String cleanPath = filepath ;
637+ if ( cleanPath .endsWith ("/" ) ) {
638+ cleanPath = cleanPath .substring (0 , cleanPath .length ()-1 );
639+ }
640+
608641 // Lets split the filepath
609- String [] filepathArr = filepath .split ("/" );
610- List <String > filepathList = Arrays .asList (filepathArr );
642+ String [] cleanPathArr = cleanPath .split ("/" );
643+ List <String > cleanPathList = Arrays .asList (cleanPathArr );
611644
612- // Lets handle parent folders
613- for (int i =1 + Math . max ( minDepth , 0 ) ; i <( filepathArr .length - 1 ) ; ++i ) {
614- ret .add ( String .join ("/" , filepathList .subList (0 ,i )+"/" ) );
645+ // Lets handle parent folders, note that i<cleanPathArr.length, alread excludes the file itself
646+ for (int i =1 ; i <cleanPathArr .length ; ++i ) {
647+ ret .add ( String .join ("/" , cleanPathList .subList (0 ,i ) ) +"/" );
615648 }
616649 }
617650 }
0 commit comments