11package picoded .dstack .mongodb ;
22
33import java .io .ByteArrayInputStream ;
4+ import java .io .IOException ;
45// Java imports
56import java .util .ArrayList ;
67import java .util .HashSet ;
2122import com .mongodb .client .*;
2223import com .mongodb .client .gridfs .*;
2324import com .mongodb .client .gridfs .GridFSBuckets ;
25+ import com .mongodb .client .gridfs .model .GridFSFile ;
2426import com .mongodb .client .gridfs .model .GridFSUploadOptions ;
2527
2628/**
3537 * - GridFS : https://www.mongodb.com/docs/drivers/java/sync/current/fundamentals/gridfs/
3638 * - API: https://mongodb.github.io/mongo-java-driver/4.7/apidocs/mongodb-driver-sync/com/mongodb/client/gridfs/GridFSBucket.html
3739 **/
38- public class MongoDB_FileWorkspaceMap /* extends Core_FileWorkspaceMap */ {
40+ abstract public class MongoDB_FileWorkspaceMap extends Core_FileWorkspaceMap {
3941
4042 // --------------------------------------------------------------------------
4143 //
@@ -54,12 +56,12 @@ public class MongoDB_FileWorkspaceMap /* extends Core_FileWorkspaceMap */ {
5456 */
5557 public MongoDB_FileWorkspaceMap (MongoDBStack inStack , String name ) {
5658 super ();
57-
59+
5860 // Initialize the gridfs bucket,
5961 // with the relevent DB, name, and config
6062 gridFSBucket = GridFSBuckets .create (inStack .db_conn , name ) //
61- .withChunkSizeBytes ( 8 * 1000 * 1000 );
62-
63+ .withChunkSizeBytes (8 * 1000 * 1000 );
64+
6365 //
6466 // Note that we intentionally chose 8*1000*1000 chunk sizes
6567 // As this will give about 1-4kb space for chunk headers to
@@ -119,20 +121,20 @@ public void clear() {
119121 **/
120122 @ Override
121123 public boolean backend_workspaceExist (String oid ) {
122- // Lets build the query for the "root file"
123- Bson query = Filters .eq ("filename" , oid );
124-
125- // Lets prepare the search
126- FindIterable <GridFSFile > search = gridFSBucket .find (query ).limit (1 );
127-
128- // Lets iterate the search result, and return true on an item
129- try (MongoCursor <Document > cursor = search .iterator ()) {
130- while (cursor .hasNext ()) {
131- // ret.add(cursor.next().getString("_oid"));
132- return true ;
133- }
134- }
135-
124+ // // Lets build the query for the "root file"
125+ // Bson query = Filters.eq("filename", oid);
126+
127+ // // Lets prepare the search
128+ // FindIterable<GridFSFile> search = gridFSBucket.find(query).limit(1);
129+
130+ // // Lets iterate the search result, and return true on an item
131+ // try (MongoCursor<GridFSFile > cursor = search.iterator()) {
132+ // while (cursor.hasNext()) {
133+ // // ret.add(cursor.next().getString("_oid"));
134+ // return true;
135+ // }
136+ // }
137+
136138 // Fail, as the search found no iterations
137139 return false ;
138140 }
@@ -151,15 +153,17 @@ public void backend_setupWorkspace(String oid) {
151153 // with the relevent _oid, that can be easily lookedup
152154 //
153155 // This is done using a closable input stream, with an empty byte array
154- try ( ByteArrayInputStream emptyStream = new ByteArrayInputStream (EmptyArray .BYTE ) ) {
156+ try (ByteArrayInputStream emptyStream = new ByteArrayInputStream (EmptyArray .BYTE )) {
155157 // Setup the metadata for the file
156158 Document metadata = new Document ();
157159 metadata .append ("_oid" , oid );
158160 metadata .append ("type" , "root" );
159-
161+
160162 // Prepare the upload options
161163 GridFSUploadOptions opt = (new GridFSUploadOptions ()).metadata (metadata );
162164 gridFSBucket .uploadFromStream (oid , emptyStream , opt );
165+ } catch (IOException e ) {
166+ throw new RuntimeException (e );
163167 }
164168 }
165169
@@ -201,7 +205,7 @@ public void backend_setupWorkspace(String oid) {
201205 // public byte[] backend_fileRead(String oid, String filepath) {
202206 // try {
203207 // accessLock.readLock().lock();
204-
208+
205209 // ConcurrentHashMap<String, byte[]> workspace = fileContentMap.get(oid);
206210 // if (workspace != null && filepath != null) {
207211 // return workspace.get(filepath);
@@ -210,7 +214,7 @@ public void backend_setupWorkspace(String oid) {
210214 // } finally {
211215 // accessLock.readLock().unlock();
212216 // }
213-
217+
214218 // }
215219
216220 // /**
@@ -230,7 +234,7 @@ public void backend_setupWorkspace(String oid) {
230234 // public boolean backend_fileExist(final String oid, final String filepath) {
231235 // try {
232236 // accessLock.readLock().lock();
233-
237+
234238 // ConcurrentHashMap<String, byte[]> workspace = fileContentMap.get(oid);
235239 // if (workspace != null && filepath != null) {
236240 // return workspace.get(filepath) != null;
@@ -254,14 +258,14 @@ public void backend_setupWorkspace(String oid) {
254258 // public void backend_fileWrite(String oid, String filepath, byte[] data) {
255259 // try {
256260 // accessLock.writeLock().lock();
257-
261+
258262 // // Get workspace, with normalized parent path
259263 // ConcurrentHashMap<String, byte[]> workspace = noLock_setupWorkspaceFolderPath(oid,
260264 // FileUtil.getParentPath(filepath));
261-
265+
262266 // // And put in the filepth data
263267 // workspace.put(filepath, data);
264-
268+
265269 // } finally {
266270 // accessLock.writeLock().unlock();
267271 // }
@@ -279,14 +283,14 @@ public void backend_setupWorkspace(String oid) {
279283 // public void backend_removeFile(String oid, String filepath) {
280284 // try {
281285 // accessLock.writeLock().lock();
282-
286+
283287 // ConcurrentHashMap<String, byte[]> workspace = fileContentMap.get(oid);
284-
288+
285289 // // workspace exist, remove the file in the workspace
286290 // if (workspace != null) {
287291 // workspace.remove(filepath);
288292 // }
289-
293+
290294 // } finally {
291295 // accessLock.writeLock().unlock();
292296 // }
@@ -312,13 +316,13 @@ public void backend_setupWorkspace(String oid) {
312316 // public void backend_removeFolderPath(final String oid, final String folderPath) {
313317 // try {
314318 // accessLock.writeLock().lock();
315-
319+
316320 // // Get the workspace, and abort if null
317321 // ConcurrentHashMap<String, byte[]> workspace = fileContentMap.get(oid);
318322 // if (workspace == null) {
319323 // return;
320324 // }
321-
325+
322326 // // Get the keyset - in a new hashset
323327 // // (so it wouldnt crash when we do modification)
324328 // Set<String> allKeys = new HashSet<>(workspace.keySet());
@@ -328,7 +332,7 @@ public void backend_setupWorkspace(String oid) {
328332 // workspace.remove(key);
329333 // }
330334 // }
331-
335+
332336 // } finally {
333337 // accessLock.writeLock().unlock();
334338 // }
@@ -409,25 +413,25 @@ public void backend_setupWorkspace(String oid) {
409413 // final String destinationFile) {
410414 // try {
411415 // accessLock.writeLock().lock();
412-
416+
413417 // // Get the workspace, and abort if null
414418 // ConcurrentHashMap<String, byte[]> workspace = fileContentMap.get(oid);
415419 // if (workspace == null) {
416420 // throw new RuntimeException("FileWorkspace does not exist : " + oid);
417421 // }
418-
422+
419423 // // Check if sourceFolder exist
420424 // if (workspace.get(sourceFile) == null) {
421425 // throw new RuntimeException("sourceFile does not exist (oid=" + oid + ") : "
422426 // + sourceFile);
423427 // }
424-
428+
425429 // // Initialize the destionation folder
426430 // noLock_setupWorkspaceFolderPath(oid, FileUtil.getParentPath(destinationFile));
427-
431+
428432 // // Copy the file
429433 // workspace.put(destinationFile, workspace.get(sourceFile));
430-
434+
431435 // // And remove the old copy
432436 // workspace.remove(sourceFile);
433437 // } finally {
@@ -459,19 +463,19 @@ public void backend_setupWorkspace(String oid) {
459463 // final String destinationFolder) {
460464 // try {
461465 // accessLock.writeLock().lock();
462-
466+
463467 // // Get the workspace, and abort if null
464468 // ConcurrentHashMap<String, byte[]> workspace = fileContentMap.get(oid);
465469 // if (workspace == null) {
466470 // throw new RuntimeException("FileWorkspace does not exist : " + oid);
467471 // }
468-
472+
469473 // // Check if sourceFolder exist
470474 // if (workspace.get(sourceFolder) == null) {
471475 // throw new RuntimeException("sourceFolder does not exist (oid=" + oid + ") : "
472476 // + sourceFolder);
473477 // }
474-
478+
475479 // // Get the keyset - in a new hashset
476480 // // (so it wouldnt crash when we do modification)
477481 // Set<String> allKeys = new HashSet<>(workspace.keySet());
@@ -485,7 +489,7 @@ public void backend_setupWorkspace(String oid) {
485489 // workspace.remove(key);
486490 // }
487491 // }
488-
492+
489493 // } finally {
490494 // accessLock.writeLock().unlock();
491495 // }
@@ -527,22 +531,22 @@ public void backend_setupWorkspace(String oid) {
527531 // final String destinationFile) {
528532 // try {
529533 // accessLock.writeLock().lock();
530-
534+
531535 // // Get the workspace, and abort if null
532536 // ConcurrentHashMap<String, byte[]> workspace = fileContentMap.get(oid);
533537 // if (workspace == null) {
534538 // throw new RuntimeException("FileWorkspace does not exist : " + oid);
535539 // }
536-
540+
537541 // // Check if sourceFolder exist
538542 // if (workspace.get(sourceFile) == null) {
539543 // throw new RuntimeException("sourceFile does not exist (oid=" + oid + ") : "
540544 // + sourceFile);
541545 // }
542-
546+
543547 // // Initialize the destionation folder
544548 // noLock_setupWorkspaceFolderPath(oid, FileUtil.getParentPath(destinationFile));
545-
549+
546550 // // Copy the file
547551 // workspace.put(destinationFile, workspace.get(sourceFile));
548552 // } finally {
@@ -574,19 +578,19 @@ public void backend_setupWorkspace(String oid) {
574578 // final String destinationFolder) {
575579 // try {
576580 // accessLock.writeLock().lock();
577-
581+
578582 // // Get the workspace, and abort if null
579583 // ConcurrentHashMap<String, byte[]> workspace = fileContentMap.get(oid);
580584 // if (workspace == null) {
581585 // throw new RuntimeException("FileWorkspace does not exist : " + oid);
582586 // }
583-
587+
584588 // // Check if sourceFolder exist
585589 // if (workspace.get(sourceFolder) == null) {
586590 // throw new RuntimeException("sourceFolder does not exist (oid=" + oid + ") : "
587591 // + sourceFolder);
588592 // }
589-
593+
590594 // // Get the keyset - in a new hashset
591595 // // (so it wouldnt crash when we do modification)
592596 // Set<String> allKeys = new HashSet<>(workspace.keySet());
@@ -598,7 +602,7 @@ public void backend_setupWorkspace(String oid) {
598602 // workspace.get(key));
599603 // }
600604 // }
601-
605+
602606 // } finally {
603607 // accessLock.writeLock().unlock();
604608 // }
@@ -624,13 +628,13 @@ public void backend_setupWorkspace(String oid) {
624628 // final int minDepth, final int maxDepth) {
625629 // try {
626630 // accessLock.readLock().lock();
627-
631+
628632 // // Get the workspace, and abort if null
629633 // ConcurrentHashMap<String, byte[]> workspace = fileContentMap.get(oid);
630634 // if (workspace == null) {
631635 // throw new RuntimeException("FileWorkspace does not exist : " + oid);
632636 // }
633-
637+
634638 // // Check if folderPath exist
635639 // String searchPath = folderPath;
636640 // if (searchPath.equals("/")) {
@@ -640,7 +644,7 @@ public void backend_setupWorkspace(String oid) {
640644 // throw new RuntimeException("folderPath does not exist (oid=" + oid + ") : "
641645 // + searchPath);
642646 // }
643-
647+
644648 // // Return a filtered set
645649 // return backend_filtterPathSet(workspace.keySet(), searchPath, minDepth, maxDepth, 0);
646650 // } finally {
@@ -656,7 +660,7 @@ public void backend_setupWorkspace(String oid) {
656660
657661 // @Override
658662 // public void systemSetup() {
659-
663+
660664 // }
661665
662666 // @Override
0 commit comments