Skip to content

Commit 7063e39

Browse files
committed
move the file save to in-line, but skip it when initializing
1 parent c0be3d1 commit 7063e39

File tree

1 file changed

+54
-39
lines changed

1 file changed

+54
-39
lines changed

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

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ public void renderSplashFrame(int percent, String message) {
112112
private HashMap<String, MobileBaseBuilder> robots = new HashMap<String, MobileBaseBuilder>();
113113
private CSGDatabaseInstance csgDBinstance;
114114
private File objectDir;
115-
private ExecutorService executor = Executors.newFixedThreadPool(5);
116-
115+
private ExecutorService executor = Executors.newFixedThreadPool(5);
116+
private File imageCacheDir;
117117

118118
public ArrayList<MobileBase> getMobileBases() {
119119
ArrayList<MobileBase> back = new ArrayList<MobileBase>();
@@ -150,19 +150,20 @@ private int opToIndex(CaDoodleOperation op) {
150150

151151
private boolean inCache(CaDoodleOperation op) {
152152
int opIndex = opToIndex(op);
153-
File cacheFile = new File(objectDir.getAbsolutePath() + delim() + opIndex);
153+
File cacheFile = new File(getObjectDir().getAbsolutePath() + delim() + opIndex);
154154
return cacheFile.exists();
155155
}
156156

157157
private List<CSG> getCachedCSGs(CaDoodleOperation op) {
158158
if (Platform.isFxApplicationThread()) {
159-
new RuntimeException("This should not be called from the UI thread!").printStackTrace();;
159+
new RuntimeException("This should not be called from the UI thread!").printStackTrace();
160+
;
160161
}
161-
if (cache.get(op) == null && objectDir !=null) {
162+
if (cache.get(op) == null && isInitialized()) {
162163
try {
163164
int opIndex = opToIndex(op);
164-
File cacheFile = new File(objectDir.getAbsolutePath() + delim() + opIndex+".csg");
165-
if(cacheFile.exists()) {
165+
File cacheFile = new File(getObjectDir().getAbsolutePath() + delim() + opIndex + ".csg");
166+
if (cacheFile.exists()) {
166167
Log.debug("Loading Cached Objects from file: " + cacheFile.getAbsolutePath());
167168
// Log.error(new Exception());
168169
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(cacheFile));
@@ -181,7 +182,7 @@ private void memoryCheck() {
181182
com.neuronrobotics.sdk.common.Log.error("\n\nClearing Memory use: " + getFreeMemory() + "\n\n");
182183
CaDoodleOperation op = getCurrentOpperation();
183184
List<CSG> back = cache.get(op);
184-
185+
185186
cache.clear();
186187
cache.put(op, back);
187188
System.gc();
@@ -191,18 +192,19 @@ private void memoryCheck() {
191192
}
192193
}
193194

194-
private void placeCSGsInCache(CaDoodleOperation op, List<CSG> cachedCopy) {
195+
private void placeCSGsInCache(CaDoodleOperation op, List<CSG> cachedCopyIn) {
195196
memoryCheck();
196197
// clear the stale cache value
197198
List<CSG> back = cache.remove(op);
198199
if (back != null)
199200
back.clear();
201+
List<CSG> cachedCopy=new ArrayList<>(cachedCopyIn);
200202
cache.put(op, cachedCopy);
201-
File cacheFile = new File(objectDir.getAbsolutePath() + delim() + opToIndex(op)+".csg");
202-
if (cacheFile.exists() && !isInitialized())
203-
return;
204-
executor.submit(()->{
205-
if (cacheFile.exists() )
203+
//executor.submit(() -> {
204+
File cacheFile = new File(getObjectDir().getAbsolutePath() + delim() + opToIndex(op) + ".csg");
205+
if (cacheFile.exists() && !isInitialized())
206+
return;
207+
if (cacheFile.exists())
206208
cacheFile.delete();
207209
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(cacheFile))) {
208210
oos.writeObject(cachedCopy);
@@ -211,13 +213,13 @@ private void placeCSGsInCache(CaDoodleOperation op, List<CSG> cachedCopy) {
211213
Log.error(ex);
212214
throw new RuntimeException(ex);
213215
}
214-
});
216+
//});
215217

216218
}
217219

218220
private void clearCache(CaDoodleOperation key) {
219221
int opIndex = opToIndex(key);
220-
File cacheFile = new File(objectDir.getAbsolutePath() + delim() + opIndex);
222+
File cacheFile = new File(getObjectDir().getAbsolutePath() + delim() + opIndex);
221223
if (cacheFile.exists())
222224
cacheFile.delete();
223225

@@ -251,13 +253,8 @@ public void initialize() {
251253
if (timeCreated < 0)
252254
timeCreated = System.currentTimeMillis();
253255
if (self != null) {
254-
File parent = self.getAbsoluteFile().getParentFile();
255-
File imageCacheDir = new File(parent.getAbsolutePath() + delim() + "timeline");
256-
if (!imageCacheDir.exists())
257-
imageCacheDir.mkdir();
258-
objectDir = new File(parent.getAbsolutePath() + delim() + "timeline" + delim() + "objectCache");
259-
if (!objectDir.exists())
260-
objectDir.mkdir();
256+
getImageCacheDir();
257+
getObjectDir();
261258
getCsgDBinstance();// initialize the instance on initialize
262259
// CSGDatabase.setInstance(getCsgDBinstance());
263260
bom = CaDoodleFile.getBillOfMaterials(this);
@@ -277,7 +274,7 @@ public void initialize() {
277274
CaDoodleOperation op = getOpperations().get(i);
278275
if (op == null)
279276
continue;
280-
//op.setCaDoodleFile(this);
277+
op.setCaDoodleFile(this);
281278
setPercentInitialized(((double) i) / (double) getOpperations().size());
282279
// if(!inCache(op))
283280
try {
@@ -643,7 +640,7 @@ public static int applyToAllConstituantElements(boolean addRet, String targetNam
643640
immutable.addAll(back);
644641
for (int i = 0; i < immutable.size(); i++) {
645642
CSG csg = immutable.get(i);
646-
if ( csg==null || csg.isLock())
643+
if (csg == null || csg.isLock())
647644
continue;
648645
// boolean inGroup = csg.isInGroup();
649646
boolean thisCSGIsInGroupNamedAfterTarget = csg.checkGroupMembership(targetName);
@@ -688,8 +685,7 @@ public File getTimelineImageFile(CaDoodleOperation test) {
688685
}
689686

690687
public File getTimelineImageFile(int i) {
691-
File parent = getSelf().getAbsoluteFile().getParentFile();
692-
File file = new File(parent.getAbsolutePath() + delim() + "timeline" + delim() + (i + 1) + ".png");
688+
File file = new File(getImageCacheDir().getAbsolutePath() + delim() + (i + 1) + ".png");
693689
return file;
694690
}
695691

@@ -1009,7 +1005,7 @@ public String toJson() {
10091005
}
10101006

10111007
public File save() throws IOException {
1012-
if(!isInitialized())
1008+
if (!isInitialized())
10131009
return null;// do not save during initialize
10141010
if (timeCreated < 0)
10151011
timeCreated = System.currentTimeMillis();
@@ -1053,7 +1049,7 @@ public File save() throws IOException {
10531049
if (isTimelineOpen())
10541050
getSaveUpdate().renderSplashFrame(percent, "Save Timeline Image " + i + ".png");
10551051
else
1056-
Log.debug(percent+ " Save Timeline Image " + i + ".png");
1052+
Log.debug(percent + " Save Timeline Image " + i + ".png");
10571053
setSaveImage(process, op);
10581054

10591055
} catch (IOException e) {
@@ -1093,10 +1089,8 @@ private void setSaveImage(List<CSG> currentState, CaDoodleOperation op) throws I
10931089
// if(currentIndex2==0)
10941090
// return;
10951091
File parent = getSelf().getAbsoluteFile().getParentFile();
1096-
File imageFolder = new File(parent.getAbsolutePath() + delim() + "timeline" + delim());
1097-
if (!imageFolder.exists())
1098-
imageFolder.mkdirs();
1099-
File imageCache = new File(parent.getAbsolutePath() + delim() + "timeline" + delim() + currentIndex2 + ".png");
1092+
1093+
File imageCache = new File(getImageCacheDir().getAbsolutePath() + delim() + currentIndex2 + ".png");
11001094
File image = new File(parent.getAbsolutePath() + delim() + "snapshot.png");
11011095

11021096
if (imageCache.exists())
@@ -1345,7 +1339,7 @@ public void setSaveUpdate(ICadoodleSaveStatusUpdate saveUpdate) {
13451339
}
13461340

13471341
public void setTimelineVisable(boolean timelineOpen) {
1348-
Log.debug("Setting timeline state "+timelineOpen);
1342+
Log.debug("Setting timeline state " + timelineOpen);
13491343
this.timelineOpen = timelineOpen;
13501344
}
13511345

@@ -1372,18 +1366,18 @@ public void setRobots(HashMap<String, MobileBaseBuilder> robots) {
13721366
}
13731367

13741368
public CSGDatabaseInstance getCsgDBinstance() {
1375-
if(csgDBinstance==null) {
1376-
if(self==null) {
1369+
if (csgDBinstance == null) {
1370+
if (self == null) {
13771371
try {
1378-
self=Files.createTempFile("temp", ".doodle").toFile();
1379-
Log.error("Failed to have a file! "+self.getAbsolutePath());
1372+
self = Files.createTempFile("temp", ".doodle").toFile();
1373+
Log.error("Failed to have a file! " + self.getAbsolutePath());
13801374
} catch (IOException e) {
13811375
// TODO Auto-generated catch block
13821376
e.printStackTrace();
13831377
}
13841378
}
13851379
File parent = self.getAbsoluteFile().getParentFile();
1386-
if(!parent.exists()) {
1380+
if (!parent.exists()) {
13871381
parent.mkdirs();
13881382
}
13891383
File db = new File(parent.getAbsolutePath() + delim() + "CSGdatabase.json");
@@ -1402,4 +1396,25 @@ public CSGDatabaseInstance getCsgDBinstance() {
14021396
private void setCsgDBinstance(CSGDatabaseInstance csgDBinstance) {
14031397
this.csgDBinstance = csgDBinstance;
14041398
}
1399+
1400+
public File getObjectDir() {
1401+
if (objectDir == null) {
1402+
objectDir = new File(getImageCacheDir().getAbsolutePath() + delim() + "objectCache");
1403+
if (!getObjectDir().exists())
1404+
getObjectDir().mkdir();
1405+
}
1406+
return objectDir;
1407+
}
1408+
1409+
public File getImageCacheDir() {
1410+
if (imageCacheDir == null) {
1411+
File parent = getSelf().getAbsoluteFile().getParentFile();
1412+
imageCacheDir=(new File(parent.getAbsolutePath() + delim() + "timeline"));
1413+
if (!imageCacheDir.exists())
1414+
imageCacheDir.mkdir();
1415+
}
1416+
return imageCacheDir;
1417+
}
1418+
1419+
14051420
}

0 commit comments

Comments
 (0)