Skip to content

Commit d98543a

Browse files
committed
Adding a thread pool for file saving operations
1 parent 800875a commit d98543a

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

src/main/java/com/neuronrobotics/bowlerstudio/scripting/cadoodle/CaDoodleFile.java

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.Random;
2121
import java.util.Set;
2222
import java.util.concurrent.CopyOnWriteArrayList;
23+
import java.util.concurrent.ExecutorService;
24+
import java.util.concurrent.Executors;
2325
import java.util.stream.Collectors;
2426

2527
import javax.imageio.ImageIO;
@@ -109,6 +111,8 @@ public void renderSplashFrame(int percent, String message) {
109111
private HashMap<String, MobileBaseBuilder> robots = new HashMap<String, MobileBaseBuilder>();
110112
private CSGDatabaseInstance csgDBinstance;
111113
private File objectDir;
114+
private ExecutorService executor = Executors.newFixedThreadPool(5);
115+
112116

113117
public ArrayList<MobileBase> getMobileBases() {
114118
ArrayList<MobileBase> back = new ArrayList<MobileBase>();
@@ -156,7 +160,7 @@ private List<CSG> getCachedCSGs(CaDoodleOperation op) {
156160
if (cache.get(op) == null && objectDir !=null) {
157161
try {
158162
int opIndex = opToIndex(op);
159-
File cacheFile = new File(objectDir.getAbsolutePath() + delim() + opIndex);
163+
File cacheFile = new File(objectDir.getAbsolutePath() + delim() + opIndex+".csg");
160164
if(cacheFile.exists()) {
161165
Log.debug("Loading Cached Objects from file: " + cacheFile.getAbsolutePath());
162166
// Log.error(new Exception());
@@ -172,32 +176,15 @@ private List<CSG> getCachedCSGs(CaDoodleOperation op) {
172176
}
173177

174178
private void memoryCheck() {
175-
if (getFreeMemory() > 85) {
179+
if (getFreeMemory() > 45) {
176180
com.neuronrobotics.sdk.common.Log.error("\n\nClearing Memory use: " + getFreeMemory() + "\n\n");
177-
// Set<CaDoodleOperation> keySet = cache.keySet();
178-
// int index = 0;
179-
// for (CaDoodleOperation op : keySet) {
180-
// List<CSG> cachedCopy = cache.get(op);
181-
// getSaveUpdate().renderSplashFrame((int) (((double) index) / ((double) keySet.size()) * 100),
182-
// "Clearing Ram to Disk");
183-
// int opIndex = opToIndex(op);
184-
// File cacheFile = new File(objectDir.getAbsolutePath() + delim() + opIndex);
185-
// if (!isInitialized())
186-
// if (cacheFile.exists())
187-
// return;
188-
// if (cacheFile.exists())
189-
// cacheFile.delete();
190-
// try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(cacheFile))) {
191-
// oos.writeObject(cachedCopy);
192-
// Log.debug("Saved " + cacheFile.getAbsolutePath());
193-
// } catch (Exception ex) {
194-
// Log.error(ex);
195-
// throw new RuntimeException(ex);
196-
// }
197-
// index++;
198-
// }
199-
// cache.clear();
181+
CaDoodleOperation op = getCurrentOpperation();
182+
List<CSG> back = cache.get(op);
183+
184+
cache.clear();
185+
cache.put(op, back);
200186
System.gc();
187+
com.neuronrobotics.sdk.common.Log.debug("Memory use down to: " + getFreeMemory());
201188
} else {
202189
// com.neuronrobotics.sdk.common.Log.debug("Memory use: " + getFreeMemory());
203190
}
@@ -210,6 +197,19 @@ private void placeCSGsInCache(CaDoodleOperation op, List<CSG> cachedCopy) {
210197
if (back != null)
211198
back.clear();
212199
cache.put(op, cachedCopy);
200+
executor.submit(()->{
201+
File cacheFile = new File(objectDir.getAbsolutePath() + delim() + opToIndex(op)+".csg");
202+
if (cacheFile.exists())
203+
cacheFile.delete();
204+
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(cacheFile))) {
205+
oos.writeObject(cachedCopy);
206+
Log.debug("Saved " + cacheFile.getAbsolutePath());
207+
} catch (Exception ex) {
208+
Log.error(ex);
209+
throw new RuntimeException(ex);
210+
}
211+
});
212+
213213
}
214214

215215
private void clearCache(CaDoodleOperation key) {
@@ -1006,6 +1006,8 @@ public String toJson() {
10061006
}
10071007

10081008
public File save() throws IOException {
1009+
if(!isInitialized())
1010+
return null;// do not save during initialize
10091011
if (timeCreated < 0)
10101012
timeCreated = System.currentTimeMillis();
10111013
String contents = toJson();
@@ -1040,10 +1042,10 @@ public File save() throws IOException {
10401042
for (int i = 0; i < opperations.size(); i++) {
10411043
File f = getTimelineImageFile(i);
10421044
CaDoodleOperation op = opperations.get(i);
1043-
int percent = (int) (((double) i) / ((double) opperations.size()) * 100.0);
1044-
List<CSG> process = getCachedCSGs(op);
1045-
if (!f.exists() && process != null)
1045+
if (!f.exists() && cache.get(op) != null)
10461046
try {
1047+
int percent = (int) (((double) i) / ((double) opperations.size()) * 100.0);
1048+
List<CSG> process = getCachedCSGs(op);
10471049
num++;
10481050
if (isTimelineOpen())
10491051
getSaveUpdate().renderSplashFrame(percent, "Save Timeline Image " + i + ".png");

0 commit comments

Comments
 (0)