Skip to content

Commit ec49c86

Browse files
committed
WIP fixing mongodb
1 parent 5a3fdfc commit ec49c86

1 file changed

Lines changed: 27 additions & 53 deletions

File tree

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

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import java.util.Map;
99
import java.util.Set;
1010
import java.util.regex.Pattern;
11+
12+
import javax.management.RuntimeErrorException;
13+
1114
import java.io.ByteArrayInputStream;
1215
import java.io.ByteArrayOutputStream;
1316
import java.io.IOException;
@@ -136,7 +139,7 @@ public boolean backend_workspaceExist(String oid) {
136139
/**
137140
* Setup the current fileWorkspace within the fileWorkspaceMap,
138141
*
139-
* This ensures the workspace _oid is registered within the map,
142+
* This ensures the workspace oid is registered within the map,
140143
* even if there is 0 files.
141144
*
142145
* Does not throw any error if workspace was previously setup
@@ -167,23 +170,6 @@ public void backend_workspaceRemove(String oid) {
167170
//
168171
//--------------------------------------------------------------------------
169172

170-
/**
171-
* Given a filepath, ensure a clean filepath (without starting "/")
172-
*/
173-
protected static String cleanFilePath(final String filepath) {
174-
// Note that the FileUtil.normalize step is not needed, as
175-
// this is already done in the Core_FileWorkspaceMap
176-
// ---
177-
// String cleanFilePath = FileUtil.normalize(filepath);
178-
179-
// Cleanup the file apth
180-
String cleanFilePath = filepath;
181-
while (cleanFilePath.startsWith("/")) {
182-
cleanFilePath = cleanFilePath.substring(1);
183-
}
184-
return cleanFilePath;
185-
}
186-
187173
/** Utility function used, to check if a workspace, or file exists **/
188174
protected boolean fullRawPathExist(String fullpath) {
189175
// Lets build the query for the "root file"
@@ -195,7 +181,7 @@ protected boolean fullRawPathExist(String fullpath) {
195181
// Lets iterate the search result, and return true on an item
196182
try (MongoCursor<GridFSFile> cursor = search.iterator()) {
197183
if (cursor.hasNext()) {
198-
// ret.add(cursor.next().getString("_oid"));
184+
// ret.add(cursor.next().getString("oid"));
199185
return true;
200186
}
201187
}
@@ -209,12 +195,9 @@ protected boolean prefixPathExist(String oid, String path) {
209195
// Lets build the query for the "root file"
210196
Bson query = null;
211197

212-
// Cleanup the path
213-
path = cleanFilePath(path);
214-
215198
// Remove matching path
216199
query = Filters.and(
217-
Filters.eq("metadata._oid", oid),
200+
Filters.eq("metadata.oid", oid),
218201
Filters.regex("filename", "^"+Pattern.quote(oid+"/"+path)+".*")
219202
);
220203

@@ -237,13 +220,13 @@ protected boolean prefixPathExist(String oid, String path) {
237220
*/
238221
public void setupAnchorFile(String oid, String fullPath, String type) {
239222
// In general we will upload a blank file
240-
// with the relevent _oid, that can be easily lookedup
223+
// with the relevent oid, that can be easily lookedup
241224
//
242225
// This is done using a closable input stream, with an empty byte array
243226
try (ByteArrayInputStream emptyStream = new ByteArrayInputStream(EmptyArray.BYTE)) {
244227
// Setup the metadata for the file
245228
Document metadata = new Document();
246-
metadata.append("_oid", oid);
229+
metadata.append("oid", oid);
247230
metadata.append("type", type);
248231

249232
// Prepare the upload options
@@ -263,14 +246,11 @@ protected void removeFilePathRecursively(String oid, String path) {
263246

264247
if( path == null || path.equals("/") || path.isEmpty() ) {
265248
// Remove everything under the oid
266-
query = Filters.eq("metadata._oid", oid);
249+
query = Filters.eq("metadata.oid", oid);
267250
} else {
268-
// Cleanup the path
269-
path = cleanFilePath(path);
270-
271251
// Remove matching path
272252
query = Filters.and(
273-
Filters.eq("metadata._oid", oid),
253+
Filters.eq("metadata.oid", oid),
274254
Filters.regex("filename", "^"+Pattern.quote(oid+"/"+path)+".*")
275255
);
276256
}
@@ -294,9 +274,6 @@ protected boolean removeFilePath(String oid, String path) {
294274
// Lets build the query for the "root file"
295275
Bson query = null;
296276

297-
// Cleanup the path
298-
path = cleanFilePath(path);
299-
300277
// Remove matching path
301278
query = Filters.eq("filename", oid+"/"+path);
302279

@@ -365,11 +342,8 @@ public void backend_fileWrite(String oid, String filepath, byte[] data) {
365342
* @param data to write the file with
366343
**/
367344
public void backend_fileWriteInputStream(String oid, String filepath, InputStream data) {
368-
// Get the clean file path
369-
String cleanPath = cleanFilePath(filepath);
370-
371345
// Build the full path
372-
String fullPath = oid + "/" + cleanPath;
346+
String fullPath = oid + "/" + filepath;
373347

374348
if (data == null) {
375349
data = new ByteArrayInputStream(EmptyArray.BYTE);
@@ -379,7 +353,7 @@ public void backend_fileWriteInputStream(String oid, String filepath, InputStrea
379353
try {
380354
// Setup the metadata for the file
381355
Document metadata = new Document();
382-
metadata.append("_oid", oid);
356+
metadata.append("oid", oid);
383357
metadata.append("type", "file");
384358

385359
// Prepare the upload options
@@ -445,18 +419,18 @@ public byte[] backend_fileRead(String oid, String filepath) {
445419
* @return the stored byte array of the file
446420
**/
447421
public InputStream backend_fileReadInputStream(String oid, String filepath) {
448-
return gridFSBucket.openDownloadStream(oid + "/" + cleanFilePath(filepath));
422+
return gridFSBucket.openDownloadStream(oid + "/" + filepath);
449423
}
450424

451425
@Override
452426
public boolean backend_fileExist(String oid, String filepath) {
453427
// Check against the full file path
454-
return fullRawPathExist(oid + "/" + cleanFilePath(filepath));
428+
return fullRawPathExist(oid + "/" + filepath);
455429
}
456430

457431
@Override
458432
public void backend_removeFile(String oid, String filepath) {
459-
removeFilePath(oid, cleanFilePath(filepath));
433+
removeFilePath(oid, filepath);
460434
}
461435

462436
// Folder Pathing support
@@ -474,7 +448,7 @@ public void backend_removeFile(String oid, String filepath) {
474448
* @return the stored byte array of the file
475449
**/
476450
public void backend_removeFolderPath(final String oid, final String folderPath) {
477-
removeFilePathRecursively(oid, cleanFilePath(folderPath));
451+
removeFilePathRecursively(oid, folderPath);
478452
}
479453

480454
/**
@@ -489,7 +463,7 @@ public void backend_removeFolderPath(final String oid, final String folderPath)
489463
**/
490464
public boolean backend_folderPathExist(final String oid, final String folderPath) {
491465
// Note that this passes if any of the files were created directly without folders
492-
return prefixPathExist(oid, cleanFilePath(folderPath));
466+
return prefixPathExist(oid, folderPath);
493467
}
494468

495469
/**
@@ -503,13 +477,10 @@ public boolean backend_folderPathExist(final String oid, final String folderPath
503477
* @return the stored byte array of the file
504478
**/
505479
public void backend_ensureFolderPath(final String oid, final String folderPath) {
506-
// Cleanup folderPath
507-
String path = cleanFilePath(folderPath);
508-
509480
// We setup a blank file with type root, this checks only for the anchor file
510481
// if it does not exists, we will make it
511-
if(!fullRawPathExist(oid+"/"+path)) {
512-
setupAnchorFile(oid, path, "dir");
482+
if(!fullRawPathExist(oid+"/"+folderPath)) {
483+
setupAnchorFile(oid, folderPath, "dir");
513484
}
514485
}
515486

@@ -553,7 +524,7 @@ public long backend_createdTimestamp(final String oid, final String filepath) {
553524
*/
554525
public long backend_modifiedTimestamp(final String oid, final String filepath) {
555526
// Lets build the query for the "root file"
556-
Bson query = Filters.eq("filename", cleanFilePath(filepath));
527+
Bson query = Filters.eq("filename", filepath);
557528

558529
// Lets prepare the search
559530
GridFSFindIterable search = gridFSBucket.find(query).limit(1);
@@ -599,15 +570,18 @@ public Set<String> backend_getFileAndFolderPathSet(final String oid, String fold
599570
// Lets build the query, for fetchign the relevent items
600571
if( folderPath == null || folderPath.equals("/") || folderPath.isEmpty() ) {
601572
// Handles query for all folder paths
602-
query = Filters.eq("metadata._oid", oid);
573+
query = Filters.and(
574+
Filters.eq("metadata.oid", oid),
575+
// Filters.ne("filename", oid)
576+
Filters.regex("filename", "^"+Pattern.quote(fullPrefixPath)+".*")
577+
);
603578
} else {
604579
// Cleanup the path
605-
folderPath = cleanFilePath(folderPath);
606580
fullPrefixPath = fullPrefixPath+folderPath;
607581

608582
// Remove matching path
609583
query = Filters.and(
610-
Filters.eq("metadata._oid", oid),
584+
Filters.eq("metadata.oid", oid),
611585
Filters.regex("filename", "^"+Pattern.quote(fullPrefixPath)+".*")
612586
);
613587
}
@@ -643,7 +617,7 @@ public Set<String> backend_getFileAndFolderPathSet(final String oid, String fold
643617
}
644618

645619
// Filter and return the final set
646-
return backend_filtterPathSet( ret, folderPath, minDepth, maxDepth, 0);
620+
return backend_filterPathSet( ret, folderPath, minDepth, maxDepth, 0);
647621
}
648622

649623
}

0 commit comments

Comments
 (0)