Skip to content

Commit 1594d0b

Browse files
authored
Merge pull request #33 from picoded/fileWorkspace-keySet-support
File workspace key set support
2 parents 5824953 + 3e345d5 commit 1594d0b

6 files changed

Lines changed: 208 additions & 11 deletions

File tree

src/main/java/picoded/dstack/file/layered/FileLayered_FileWorkspaceMap.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import picoded.dstack.file.simple.FileSimple_FileWorkspaceMap;
44

55
import java.io.File;
6+
import java.util.HashSet;
7+
import java.util.Set;
68

79
public class FileLayered_FileWorkspaceMap extends FileSimple_FileWorkspaceMap {
810

@@ -51,4 +53,64 @@ public File workspaceDirObj(String oid) {
5153
return new File(baseDir, workspacePath);
5254
}
5355

56+
//--------------------------------------------------------------------------
57+
//
58+
// KeySet support
59+
//
60+
//--------------------------------------------------------------------------
61+
62+
/**
63+
* Get and returns all the GUID's, note that due to its
64+
* potential of returning a large data set, production use
65+
* should be avoided.
66+
*
67+
* @return set of keys
68+
**/
69+
@Override
70+
public Set<String> keySet() {
71+
// The return hashset
72+
HashSet<String> ret = new HashSet<String>();
73+
74+
// List all the files/folders
75+
File[] l1_dirList = baseDir.listFiles();
76+
77+
// For each one of it, process it!
78+
for (File l1_dir : l1_dirList) {
79+
// Skip if its not a directory
80+
if( !l1_dir.isDirectory() ) {
81+
continue;
82+
}
83+
84+
// List all the files/folders
85+
File[] l2_dirList = baseDir.listFiles();
86+
87+
for(File l2_dir : l2_dirList) {
88+
// Skip if its not a directory
89+
if( !l2_dir.isDirectory() ) {
90+
continue;
91+
}
92+
93+
// Get the oidDirLIst
94+
File[] oid_list = l2_dir.listFiles();
95+
96+
// For each oid dir
97+
for(File oid_dir : oid_list) {
98+
// Get the presumed oid
99+
String oid = oid_dir.getName();
100+
101+
// Validate the dir name (oid)
102+
if( !validateOid(oid) ) {
103+
continue;
104+
}
105+
106+
// Add the oid to the ret set
107+
ret.add(oid);
108+
}
109+
}
110+
}
111+
112+
// Return the full keyset
113+
return ret;
114+
}
115+
54116
}

src/main/java/picoded/dstack/file/simple/FileSimple_FileWorkspaceMap.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,4 +627,48 @@ public Set<String> backend_getFileAndFolderPathSet(final String oid, final Strin
627627
return backend_filterPathSet(retSet, "", minDepth, maxDepth, 0);
628628
}
629629

630+
//--------------------------------------------------------------------------
631+
//
632+
// KeySet support
633+
//
634+
//--------------------------------------------------------------------------
635+
636+
/**
637+
* Get and returns all the GUID's, note that due to its
638+
* potential of returning a large data set, production use
639+
* should be avoided.
640+
*
641+
* @return set of keys
642+
**/
643+
@Override
644+
public Set<String> keySet() {
645+
// The return hashset
646+
HashSet<String> ret = new HashSet<String>();
647+
648+
// List all the files/folders
649+
File[] dirList = baseDir.listFiles();
650+
651+
// For each one of it, process it!
652+
for (File subFile : dirList) {
653+
// Skip if its not a directory
654+
if( !subFile.isDirectory() ) {
655+
continue;
656+
}
657+
658+
// Get the presumed oid
659+
String oid = subFile.getName();
660+
661+
// Validate the dir name (oid)
662+
if( !validateOid(oid) ) {
663+
continue;
664+
}
665+
666+
// Add the oid to the ret set
667+
ret.add(oid);
668+
}
669+
670+
// Return the full keyset
671+
return ret;
672+
}
673+
630674
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,12 @@ public Set<String> keySet() {
293293
HashSet<String> ret = new HashSet<String>();
294294

295295
// Lets fetch everything ... D=
296-
FindIterable<Document> search = collection.find();
297-
search = search.projection(Projections.include("_oid"));
298-
296+
DistinctIterable<String> search = collection.distinct("_oid", String.class);
297+
299298
// Lets iterate the search
300-
try (MongoCursor<Document> cursor = search.iterator()) {
299+
try (MongoCursor<String> cursor = search.iterator()) {
301300
while (cursor.hasNext()) {
302-
ret.add(cursor.next().getString("_oid"));
301+
ret.add(cursor.next());
303302
}
304303
}
305304

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,4 +894,37 @@ public Set<String> backend_getFileAndFolderPathSet(final String oid, String fold
894894
return backend_filterPathSet( ret, folderPath, minDepth, maxDepth, 0);
895895
}
896896

897+
//--------------------------------------------------------------------------
898+
//
899+
// KeySet support
900+
//
901+
//--------------------------------------------------------------------------
902+
903+
/**
904+
* Get and returns all the GUID's, note that due to its
905+
* potential of returning a large data set, production use
906+
* should be avoided.
907+
*
908+
* @return set of keys
909+
**/
910+
@Override
911+
public Set<String> keySet() {
912+
// The return hashset
913+
HashSet<String> ret = new HashSet<String>();
914+
915+
// Lets fetch everything ... D=
916+
DistinctIterable<String> search = filesCollection.distinct("metadata.oid", String.class);
917+
918+
// Lets iterate the search
919+
try (MongoCursor<String> cursor = search.iterator()) {
920+
while (cursor.hasNext()) {
921+
ret.add(cursor.next());
922+
}
923+
}
924+
925+
// Return the full keyset
926+
return ret;
927+
}
928+
929+
897930
}

src/main/java/picoded/dstack/stack/Stack_FileWorkspaceMap.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,64 @@ public Set<String> backend_getFolderPathSet(final String oid, final String folde
576576
return queryLayer.backend_getFolderPathSet(oid, folderPath, minDepth, maxDepth);
577577
}
578578

579+
//--------------------------------------------------------------------------
580+
//
581+
// KeySet support
582+
//
583+
//--------------------------------------------------------------------------
584+
585+
/**
586+
* List all the oid supported in the current backend
587+
*/
588+
public Set<String> keySet() {
589+
return queryLayer.keySet();
590+
}
591+
592+
/**
593+
* Gets and return a random object ID
594+
*
595+
* @return Random object ID
596+
**/
597+
public String randomObjectID() {
598+
return queryLayer.randomObjectID();
599+
}
600+
601+
/**
602+
* Gets and return the next object ID key for iteration given the current ID,
603+
* null gets the first object in iteration.
604+
*
605+
* It is important to note actual iteration sequence is implementation dependent.
606+
* And does not gurantee that newly added objects, after the iteration started,
607+
* will be part of the chain of results.
608+
*
609+
* Similarly if the currentID was removed midway during iteration, the return
610+
* result is not properly defined, and can either be null, or the closest object matched
611+
* or even a random object.
612+
*
613+
* It is however guranteed, if no changes / writes occurs. A complete iteration
614+
* will iterate all existing objects.
615+
*
616+
* The larger intention of this function, is to allow a background thread to slowly
617+
* iterate across all objects, eventually. With an acceptable margin of loss on,
618+
* recently created/edited object. As these objects will eventually be iterated in
619+
* repeated rounds on subsequent calls.
620+
*
621+
* Due to its roughly random nature in production (with concurrent objects generated)
622+
* and its iterative nature as an eventuality. The phrase looselyIterate was chosen,
623+
* to properly reflect its nature.
624+
*
625+
* Another way to phrase it, in worse case scenerio, its completely random,
626+
* eventually iterating all objects. In best case scenerio, it does proper
627+
* iteration as per normal.
628+
*
629+
* @param Current object ID, can be NULL
630+
*
631+
* @return Next object ID, if found
632+
**/
633+
public String looselyIterateObjectID(String currentID) {
634+
return queryLayer.looselyIterateObjectID(currentID);
635+
}
636+
579637
//--------------------------------------------------------------------------
580638
//
581639
// Copy pasta code, I wished could have worked in an interface

src/main/java/picoded/dstack/struct/cache/StructCache_DataObjectMap.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,9 @@ public void systemSetup() {
146146

147147
//
148148
// Alright, time to build a new cache
149-
// We are in the era of GB ram computing, 100k cache would
150-
// be a good sane default in server environment.
149+
// We are in the era of GB ram computing, 10k cache would
150+
// be a good sane default in server environment. Even if there are
151+
// multiple sets of StructCache, as it would take ~60MB
151152
//
152153
// to consider : auto detect RAM size in KB - and use that?
153154
// a good rough guideline would be 1/4 of free ram space divided by 6kb
@@ -177,13 +178,13 @@ public void systemSetup() {
177178
// 5006 bytes : Total bytes per object map
178179
// ~ 6 kilo bytes : Rounded up
179180
//
180-
// # RAM cost for 100k objects
181+
// # RAM cost for 10k objects
181182
//
182-
// 100,000 * 6 KB = 600 MB
183+
// 100,000 * 6 KB = 60 MB
183184
//
184-
// > So yea, we are ok to assume a 100k objects for most parts
185+
// > So yea, we are ok to assume a 10k objects for most parts
185186
//
186-
int capicity = configMap().getInt("capacity", 100000);
187+
int capicity = configMap().getInt("capacity", 10000);
187188
_valueMap = new Cache2kBuilder<String, Map<String, Object>>() {
188189
} //
189190
.name(cacheName())//

0 commit comments

Comments
 (0)