Commit b2b39d5c authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

add new cacheDir lookup

parent 63e8dc76
......@@ -25,6 +25,7 @@ public class CellGridBinaryCacheObject extends AbstractCacheObject implements IC
@Override
public void initializeObjectFromCache(CellGrid object) throws CacheException {
try {
logger.infof("write cache: %s", getCacheLocation());
CellGridReadWriter.read(object).fromBinary(new DataInputStream(new FastBufferedInputStream(inputStream)));
} catch (Exception e) {
throw new CacheException("Cannot load cache from CSV InputStream", e);
......@@ -40,4 +41,8 @@ public class CellGridBinaryCacheObject extends AbstractCacheObject implements IC
}
}
@Override
public String getCacheLocation() {
return cacheLocation.getAbsolutePath().toString();
}
}
......@@ -32,10 +32,16 @@ public class CellGridTxtCacheObject extends AbstractCacheObject implements ICell
@Override
public void persistObject(CellGrid object) throws CacheException {
try {
logger.infof("write cache: %s", getCacheLocation());
CellGridReadWriter.write(object).toTextFile(cacheLocation);
} catch (Exception e) {
logger.errorf("cannot save cache %s", cacheLocation.getAbsolutePath());
}
}
@Override
public String getCacheLocation() {
return cacheLocation.getAbsolutePath().toString();
}
}
......@@ -27,6 +27,11 @@ public interface ICacheObject<T> {
return false;
}
@Override
public String getCacheLocation() {
return "";
}
@Override
public String getCacheIdentifier() {
return cacheIdentifier;
......@@ -38,6 +43,7 @@ public interface ICacheObject<T> {
void persistObject(T object) throws CacheException;
boolean readable();
boolean writable();
String getCacheLocation();
String getCacheIdentifier();
......
......@@ -8,12 +8,14 @@ import org.vadere.state.scenario.Topography;
import org.vadere.state.types.CacheType;
import org.vadere.state.types.EikonalSolverType;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.config.VadereConfig;
import org.vadere.util.logging.Logger;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
public class ScenarioCache {
......@@ -60,7 +62,31 @@ public class ScenarioCache {
this.scenario = null;
}
private ScenarioCache(Scenario scenario, Path scenarioParentDir){
private Path resolveCachePath(Path cacheParentDir, String scenarioFileConfig){
Path scenarioConfig = Paths.get(scenarioFileConfig);
// check if scenario file specifies an absolute path. --> If yes use it.
if (scenarioConfig.isAbsolute()){
logger.infof("Cache path overwritten: Scenario specified absolute cache path. %s",
scenarioConfig.toAbsolutePath().toString());
return scenarioConfig.toAbsolutePath();
}
Path base;
// scenario file uses relative path. Check config for global override
if (VadereConfig.getConfig().getBoolean("Vadere.cache.useGlobalCacheBaseDir", false)){
logger.info("Global override for cache location found.");
base = Paths.get(VadereConfig.getConfig().getString("Vadere.cache.flobalCacheBaseDir"));
} else {
base = cacheParentDir.resolve(CACHE_DIR_NAME);
}
Path ret = base.resolve(scenarioConfig);
logger.infof("Cache search location: %s", ret.toAbsolutePath());
return ret.toAbsolutePath();
}
private ScenarioCache(Scenario scenario, Path cacheParentDir){
this.empty = scenario == null;
this.scenario = scenario;
......@@ -74,7 +100,7 @@ public class ScenarioCache {
if(attFF != null){
this.attFF = attFF;
this.hash = StateJsonConverter.getFloorFieldHash(topography, attFF);
this.cachePath = scenarioParentDir.resolve(CACHE_DIR_NAME).resolve(attFF.getCacheDir());
this.cachePath = resolveCachePath(cacheParentDir, attFF.getCacheDir()); //cacheParentDir.resolve(CACHE_DIR_NAME).resolve(attFF.getCacheDir());
empty = !attFF.isUseCachedFloorField(); // deactivate cache object if caching is not active.
if (!empty)
findCacheOnFileSystem();
......
......@@ -3,7 +3,6 @@ package org.vadere.util.config;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.FileBasedConfiguration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.PropertiesConfigurationLayout;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.builder.fluent.PropertiesBuilderParameters;
......@@ -11,18 +10,14 @@ import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.vadere.util.logging.Logger;
import java.beans.IntrospectionException;
import java.io.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.List;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.ArrayList;
/**
......@@ -270,6 +265,8 @@ public class VadereConfig {
defaultConfig.put("SettingsDialog.showLogo", "false");
defaultConfig.put("Testing.stepCircleOptimization.compareBruteForceSolution", "false");
defaultConfig.put("TopographyCreator.dotRadius", "0.5");
defaultConfig.put("Vadere.cache.useGlobalCacheBaseDir", "false");
defaultConfig.put("Vadere.cache.flobalCacheBaseDir", defaultSearchDirectory + "/.cache/vadere-cache");
return defaultConfig;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment