Skip to content

Commit ba81f9e

Browse files
committed
more permissive MongoDB workspaceExist check
1 parent e73fca5 commit ba81f9e

4 files changed

Lines changed: 63 additions & 50 deletions

File tree

src/main/java/picoded/dstack/core/Core_FileWorkspaceMap.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -568,18 +568,18 @@ protected Set<String> backend_filterPathSet(final Set<String> rawSet, final Stri
568568
// System.out.println( "maxDepth: "+maxDepth );
569569
// System.out.println( "pathType: "+pathType );
570570
// System.out.println( ConvertJSON.fromObject(rawSet) );
571-
571+
572572
// Return set
573573
Set<String> ret = new HashSet<>();
574574

575575
// Get the keyset, and iterate it
576576
for (String key : rawSet) {
577577

578578
// Skip the root folder of a workspace
579-
if( key.equals("") || key.equals("/") ) {
579+
if (key.equals("") || key.equals("/")) {
580580
continue;
581581
}
582-
582+
583583
// If folder does not match - skip
584584
if (searchPathLen > 0 && !key.startsWith(searchPath)) {
585585
continue;
@@ -589,16 +589,16 @@ protected Set<String> backend_filterPathSet(final Set<String> rawSet, final Stri
589589
String subPath = key.substring(searchPathLen);
590590

591591
// Skip the root folder of a subpath
592-
if( subPath.equals("") || subPath.equals("/") ) {
592+
if (subPath.equals("") || subPath.equals("/")) {
593593
continue;
594594
}
595-
595+
596596
// No filtering is needed, store and continue
597597
if (maxDepth <= 0 && minDepth <= 0) {
598598
// Does no checks, add and continue
599599
ret.add(subPath);
600600
continue;
601-
}
601+
}
602602

603603
// Lets perform path filtering
604604
// ---
@@ -654,7 +654,7 @@ protected Set<String> backend_filterPathSet(final Set<String> rawSet, final Stri
654654
// // Debugging stuff
655655
// System.out.println( "Filtered Set" );
656656
// System.out.println( ConvertJSON.fromObject(ret) );
657-
657+
658658
// Return the filtered set
659659
return ret;
660660
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,10 @@ public void DataObjectRemoteDataMap_update(String _oid, Map<String, Object> full
250250

251251
// If value is a Map, we recast it to avoid referencing and formatting issues
252252
// this also resolves the issue when a Map, contains an array
253-
if( value instanceof Map ) {
254-
value = ConvertJSON.toMap( ConvertJSON.fromObject(value) );
253+
if (value instanceof Map) {
254+
value = ConvertJSON.toMap(ConvertJSON.fromObject(value));
255255
}
256-
256+
257257
// Lets apply the update values
258258
if (updateKeys.contains(key)) {
259259
// Handle NULL values unset

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public void clear() {
155155
@Override
156156
public boolean backend_workspaceExist(String oid) {
157157
// The folder root, will only contain the "oid"
158-
return fullRawPathExist(oid);
158+
return prefixPathExist(oid, null);
159159
}
160160

161161
/**
@@ -217,14 +217,19 @@ protected boolean prefixPathExist(String oid, String path) {
217217
// Lets build the query for the "root file"
218218
Bson query = null;
219219

220-
// Get the full prefixpath
221-
String fullPrefixPath = oid + "/" + path;
222-
223-
// Remove matching path
224-
query = Filters.or(
225-
Filters.eq("filename", fullPrefixPath),
226-
Filters.and(Filters.eq("metadata.oid", oid),
227-
Filters.regex("filename", "^" + Pattern.quote(fullPrefixPath) + ".*")));
220+
// Handle search with null
221+
if (path == null || path.equals("")) {
222+
query = Filters.eq("metadata.oid", oid);
223+
} else {
224+
// Get the full prefixpath
225+
String fullPrefixPath = oid + "/" + path;
226+
227+
// Remove matching path
228+
query = Filters.or(
229+
Filters.eq("filename", fullPrefixPath),
230+
Filters.and(Filters.eq("metadata.oid", oid),
231+
Filters.regex("filename", "^" + Pattern.quote(fullPrefixPath) + ".*")));
232+
}
228233

229234
// Lets prepare the search
230235
GridFSFindIterable search = gridFSBucket.find(query).limit(1);
@@ -326,7 +331,7 @@ protected boolean removeFilePath(String oid, String path) {
326331
* Used mainly to ensure "parent" folder exists on file write/rm
327332
**/
328333
protected void ensureParentPath(String oid, String path) {
329-
// Does nothing if path is empty
334+
// Ensure
330335
if (path == null || path.equals("/") || path.isEmpty()) {
331336
return;
332337
}
@@ -692,7 +697,11 @@ public boolean backend_fileExist(String oid, String filepath) {
692697

693698
@Override
694699
public void backend_removeFile(String oid, String filepath) {
700+
// Ensure root anchor exists
701+
backend_setupWorkspace(oid);
702+
// Ensure any parend dir anchor exists if needed
695703
ensureParentPath(oid, filepath);
704+
// Remove the respective file
696705
removeFilePath(oid, filepath);
697706
}
698707

@@ -711,7 +720,11 @@ public void backend_removeFile(String oid, String filepath) {
711720
* @return the stored byte array of the file
712721
**/
713722
public void backend_removeFolderPath(final String oid, final String folderPath) {
723+
// Ensure root anchor exists
724+
backend_setupWorkspace(oid);
725+
// Ensure any parend dir anchor exists if needed
714726
ensureParentPath(oid, folderPath);
727+
// Remove the respective file
715728
removeFilePathRecursively(oid, folderPath);
716729
}
717730

src/test/java/picoded/dstack/struct/simple/StructSimple_FileWorkspaceMap_test.java

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -348,19 +348,19 @@ public FileWorkspace getPathSetLookup_setup() {
348348
fileWorkspace.writeString("test/d1/file.txt", "anything");
349349
fileWorkspace.writeString("test/d2/file1.txt", "anything");
350350
fileWorkspace.writeString("test/d2/file2.txt", "anything");
351-
351+
352352
// And return
353353
return fileWorkspace;
354354
}
355-
355+
356356
// Just perform and validate setup
357357
@Test
358358
public void getPathSetLookup_setupTest() {
359359
// Get the file workspace to use
360360
FileWorkspace fileWorkspace = getPathSetLookup_setup();
361361
assertNotNull(fileWorkspace);
362362
}
363-
363+
364364
// List all files and folders
365365
// this includes, 5 files, and 3 folders (test/, test/d1/, test/d2/)
366366
//
@@ -369,24 +369,24 @@ public void getPathSetLookup_setupTest() {
369369
public void getPathSetLookup_fullSet() {
370370
// Get the file workspace to use
371371
FileWorkspace fileWorkspace = getPathSetLookup_setup();
372-
372+
373373
// Test set to use for comparision
374374
Set<String> testFileSet = new HashSet<>();
375375
Set<String> testDirSet = new HashSet<>();
376376
Set<String> testFullSet = new HashSet<>();
377-
377+
378378
// Test files
379379
testFileSet.add("test/one.txt");
380380
testFileSet.add("test/two.txt");
381381
testFileSet.add("test/d1/file.txt");
382382
testFileSet.add("test/d2/file1.txt");
383383
testFileSet.add("test/d2/file2.txt");
384-
384+
385385
// Test dirs
386386
testDirSet.add("test/");
387387
testDirSet.add("test/d1/");
388388
testDirSet.add("test/d2/");
389-
389+
390390
// Full set
391391
testFullSet.addAll(testFileSet);
392392
testFullSet.addAll(testDirSet);
@@ -398,29 +398,29 @@ public void getPathSetLookup_fullSet() {
398398
assertEquals(testDirSet, fileWorkspace.getFolderPathSet("", -1, -1));
399399
assertEquals(testFileSet, fileWorkspace.getFilePathSet("", -1, -1));
400400
}
401-
401+
402402
// List all files and folders, with limits
403403
//
404404
// See `getPathSetLookup_setup` for setup code
405405
@Test
406406
public void getPathSetLookup_fullSet_withLimit() {
407407
// Get the file workspace to use
408408
FileWorkspace fileWorkspace = getPathSetLookup_setup();
409-
409+
410410
// Test set to use for comparision
411411
Set<String> testFileSet = new HashSet<>();
412412
Set<String> testDirSet = new HashSet<>();
413413
Set<String> testFullSet = new HashSet<>();
414-
414+
415415
// Test files
416416
testFileSet.add("test/one.txt");
417417
testFileSet.add("test/two.txt");
418-
418+
419419
// Test dirs
420420
testDirSet.add("test/");
421421
testDirSet.add("test/d1/");
422422
testDirSet.add("test/d2/");
423-
423+
424424
// Full set
425425
testFullSet.addAll(testFileSet);
426426
testFullSet.addAll(testDirSet);
@@ -432,31 +432,31 @@ public void getPathSetLookup_fullSet_withLimit() {
432432
assertEquals(testDirSet, fileWorkspace.getFolderPathSet("", -1, 2));
433433
assertEquals(testFileSet, fileWorkspace.getFilePathSet("", -1, 2));
434434
}
435-
435+
436436
// List all files and folders from the `test/` dir
437437
//
438438
// See `getPathSetLookup_setup` for setup code
439439
@Test
440440
public void getPathSetLookup_testDir_fullSet() {
441441
// Get the file workspace to use
442442
FileWorkspace fileWorkspace = getPathSetLookup_setup();
443-
443+
444444
// Test set to use for comparision
445445
Set<String> testFileSet = new HashSet<>();
446446
Set<String> testDirSet = new HashSet<>();
447447
Set<String> testFullSet = new HashSet<>();
448-
448+
449449
// Test files
450450
testFileSet.add("one.txt");
451451
testFileSet.add("two.txt");
452452
testFileSet.add("d1/file.txt");
453453
testFileSet.add("d2/file1.txt");
454454
testFileSet.add("d2/file2.txt");
455-
455+
456456
// Test dirs
457457
testDirSet.add("d1/");
458458
testDirSet.add("d2/");
459-
459+
460460
// Full set
461461
testFullSet.addAll(testFileSet);
462462
testFullSet.addAll(testDirSet);
@@ -467,26 +467,26 @@ public void getPathSetLookup_testDir_fullSet() {
467467
assertEquals(testDirSet, fileWorkspace.getFolderPathSet("test", -1, -1));
468468
assertEquals(testFileSet, fileWorkspace.getFilePathSet("test", -1, -1));
469469
}
470-
470+
471471
// List all files and folders from the `/` dir
472472
//
473473
// See `getPathSetLookup_setup` for setup code
474474
@Test
475475
public void getPathSetLookup_listRoot() {
476476
// Get the file workspace to use
477477
FileWorkspace fileWorkspace = getPathSetLookup_setup();
478-
478+
479479
// Test set to use for comparision
480480
Set<String> testFileSet = new HashSet<>();
481481
Set<String> testDirSet = new HashSet<>();
482482
Set<String> testFullSet = new HashSet<>();
483-
483+
484484
// Test files
485485
// -- there is none
486-
486+
487487
// Test dirs
488488
testDirSet.add("test/");
489-
489+
490490
// Full set
491491
testFullSet.addAll(testFileSet);
492492
testFullSet.addAll(testDirSet);
@@ -497,27 +497,27 @@ public void getPathSetLookup_listRoot() {
497497
assertEquals(testDirSet, fileWorkspace.getFolderPathSet(""));
498498
assertEquals(testFileSet, fileWorkspace.getFilePathSet(""));
499499
}
500-
500+
501501
// List all files and folders from the `test/d2/` dir
502502
//
503503
// See `getPathSetLookup_setup` for setup code
504504
@Test
505505
public void getPathSetLookup_testDir_d2() {
506506
// Get the file workspace to use
507507
FileWorkspace fileWorkspace = getPathSetLookup_setup();
508-
508+
509509
// Test set to use for comparision
510510
Set<String> testFileSet = new HashSet<>();
511511
Set<String> testDirSet = new HashSet<>();
512512
Set<String> testFullSet = new HashSet<>();
513-
513+
514514
// Test files
515515
testFileSet.add("file1.txt");
516516
testFileSet.add("file2.txt");
517-
517+
518518
// Test dirs
519519
// -- there is none
520-
520+
521521
// Full set
522522
testFullSet.addAll(testFileSet);
523523
testFullSet.addAll(testDirSet);
@@ -528,7 +528,7 @@ public void getPathSetLookup_testDir_d2() {
528528
assertEquals(testDirSet, fileWorkspace.getFolderPathSet("test/d2/", -1, -1));
529529
assertEquals(testFileSet, fileWorkspace.getFilePathSet("test/d2/", -1, -1));
530530
}
531-
531+
532532
@Test
533533
public void getPathSetLookup() {
534534
// Get the file workspace to use
@@ -538,12 +538,12 @@ public void getPathSetLookup() {
538538
assertEquals(4, fileWorkspace.getFileAndFolderPathSet("test").size());
539539
assertEquals(2, fileWorkspace.getFilePathSet("test").size());
540540
assertEquals(2, fileWorkspace.getFolderPathSet("test").size());
541-
541+
542542
// List files & folders in the `test/d2/` sub folders
543543
assertEquals(2, fileWorkspace.getFileAndFolderPathSet("test/d2/").size());
544544
assertEquals(2, fileWorkspace.getFilePathSet("test/d2/").size());
545545
assertEquals(0, fileWorkspace.getFolderPathSet("test/d2/").size());
546-
546+
547547
}
548548

549549
//-----------------------------------------------------------------------------------

0 commit comments

Comments
 (0)