Skip to content

Commit 2652a46

Browse files
committed
Expanding keySet test and support for FileWorkspaceMap
1 parent 1594d0b commit 2652a46

4 files changed

Lines changed: 63 additions & 0 deletions

File tree

src/main/java/picoded/dstack/FileWorkspaceMap.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,14 @@ default FileWorkspace[] getArrayFromID(String[] idArray, boolean isUnchecked) {
123123
*
124124
* This is equivalent of "TRUNCATE TABLE {TABLENAME}"
125125
**/
126+
@Override
126127
void clear();
128+
129+
/**
130+
* Return the current list of ID keys for fileWorkspaceMap
131+
**/
132+
@Override
133+
Set<String> keySet();
127134

128135
// Special iteration support
129136
//--------------------------------------------------------------------------

src/main/java/picoded/dstack/jsql/JSql_FileWorkspaceMap.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package picoded.dstack.jsql;
22

33
import picoded.dstack.core.Core_FileWorkspaceMap;
4+
import picoded.core.conv.ListValueConv;
45
import picoded.core.file.FileUtil;
56
import picoded.core.struct.GenericConvertList;
67
import picoded.dstack.connector.jsql.JSql;
@@ -657,4 +658,38 @@ public Set<String> backend_getFileAndFolderPathSet(final String oid, final Strin
657658
return backend_filterPathSet(rawSet, folderPath, minDepth, maxDepth, 0);
658659
}
659660

661+
/**
662+
* Return the current list of ID keys for fileWorkspaceMap
663+
**/
664+
@Override
665+
public Set<String> keySet() {
666+
// Search blank "paths" which represents the workspace root
667+
JSqlResult r = sqlObj.select(fileWorkspaceTableName, "oID", "path = ?", new Object[] { "" });
668+
669+
// Convert it into a set
670+
if (r == null || r.get("oID") == null) {
671+
return new HashSet<String>();
672+
}
673+
return ListValueConv.toStringSet(r.getObjectList("oID"));
674+
}
675+
676+
/**
677+
* Gets and return a random object ID
678+
*
679+
* @return Random object ID
680+
**/
681+
public String randomObjectID() {
682+
// Get a random ID
683+
JSqlResult r = sqlObj.randomSelect(fileWorkspaceTableName, "oID", null, null, 1);
684+
685+
// No result : NULL
686+
if (r == null || r.get("oID") == null || r.rowCount() <= 0) {
687+
return null;
688+
}
689+
690+
// Return the result
691+
return r.getStringArray("oID")[0];
692+
}
693+
694+
660695
}

src/main/java/picoded/dstack/struct/simple/StructSimple_FileWorkspaceMap.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,14 @@ public Set<String> backend_getFileAndFolderPathSet(final String oid, final Strin
620620
}
621621
}
622622

623+
/**
624+
* Return the current list of ID keys for fileWorkspaceMap
625+
**/
626+
@Override
627+
public Set<String> keySet() {
628+
return new HashSet<>(fileContentMap.keySet());
629+
}
630+
623631
//--------------------------------------------------------------------------
624632
//
625633
// Constructor and maintenance

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.io.InputStream;
1313
import java.util.Arrays;
1414
import java.util.HashSet;
15+
import java.util.Set;
1516

1617
import org.apache.commons.io.IOUtils;
1718
// Test Case include
@@ -435,4 +436,16 @@ public void deleteWorkspace() {
435436
testObj.remove(fileWorkspace._oid());
436437
assertNull(testObj.get(fileWorkspace._oid()));
437438
}
439+
440+
441+
@Test
442+
public void keySetTest() {
443+
FileWorkspace fileWorkspace = testObj.newEntry();
444+
fileWorkspace.writeByteArray("filepath", "anything".getBytes());
445+
446+
// Get the workspace keyset
447+
Set<String> keyset = testObj.keySet();
448+
assertNotNull(keyset);
449+
assertTrue( keyset.contains(fileWorkspace._oid()) );
450+
}
438451
}

0 commit comments

Comments
 (0)