Skip to content

Commit 5824953

Browse files
authored
Merge pull request #32 from picoded/mongodb-support
Mongodb support
2 parents 36aef93 + e0c4c75 commit 5824953

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

.github/workflows/mongodb.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ jobs:
1111
# Configuration, for us to perform test against multiple version in the future
1212
strategy:
1313
matrix:
14-
mongodb-version: ['5.0']
14+
# , '6.0' is held back from testing until the official docker images are out
15+
mongodb-version: ['5.0']
1516

1617
# The test step
1718
steps:

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,15 @@ public void backend_fileWriteInputStream(final String oid, final String filepath
637637
**/
638638
@Override
639639
public byte[] backend_fileRead(String oid, String filepath) {
640+
// Get the buffer
640641
InputStream buffer = backend_fileReadInputStream(oid, filepath);
642+
643+
// Null handling
644+
if( buffer == null ) {
645+
return null;
646+
}
647+
648+
// Cast the inputstream to byte[]
641649
byte[] ret = null;
642650
try {
643651
ret = IOUtils.toByteArray(buffer);
@@ -667,7 +675,17 @@ public byte[] backend_fileRead(String oid, String filepath) {
667675
* @return the stored byte array of the file
668676
**/
669677
public InputStream backend_fileReadInputStream(String oid, String filepath) {
670-
return gridFSBucket.openDownloadStream(oid + "/" + filepath);
678+
try {
679+
return gridFSBucket.openDownloadStream(oid + "/" + filepath);
680+
} catch(Exception e) {
681+
if( e.getMessage().toLowerCase().indexOf("no file found") >= 0 ) {
682+
// Does nothing if no file is found
683+
return null;
684+
}
685+
686+
// rethrow the error
687+
throw e;
688+
}
671689
}
672690

673691
@Override

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,24 @@ public void folderSetupAndRemove_pathVarients() {
144144

145145
}
146146

147+
@Test
148+
public void noFileRead_returnsNull() {
149+
// Get the file workspace to use
150+
FileWorkspace fileWorkspace = testObj.newEntry();
151+
assertNotNull(fileWorkspace);
152+
153+
// Check for null (no file) for non existant file
154+
assertNull(fileWorkspace.readString("test/folder/file.txt"));
155+
156+
// Write file
157+
fileWorkspace.writeString("test/folder/file.txt", "anything");
158+
assertTrue(fileWorkspace.fileExist("test/folder/file.txt"));
159+
assertEquals(fileWorkspace.readString("test/folder/file.txt"), "anything");
160+
161+
// Check for null (no file) for non existant file
162+
assertNull(fileWorkspace.readString("test/folder/file-somethingElse.txt"));
163+
}
164+
147165
@Test
148166
public void fileWrite_andProperlySetupFolder() {
149167
// Get the file workspace to use
@@ -153,6 +171,9 @@ public void fileWrite_andProperlySetupFolder() {
153171
// Folder does not exist first
154172
assertFalse(fileWorkspace.folderPathExist("test/folder"));
155173

174+
// Check for null (no file) for non existant file
175+
assertNull(fileWorkspace.readString("test/folder/file.txt"));
176+
156177
// Write file
157178
fileWorkspace.writeString("test/folder/file.txt", "anything");
158179
assertTrue(fileWorkspace.folderPathExist("test/folder"));

0 commit comments

Comments
 (0)