Skip to content

Commit

Permalink
Merge branch 'cache_config' into create_circle
Browse files Browse the repository at this point in the history
  • Loading branch information
Amin-Tajgardoon committed Jul 12, 2017
2 parents 9d8cedf + 9672afa commit 9fa51e5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
6 changes: 5 additions & 1 deletion app/gateways/configuration/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public class Global extends GlobalSettings {
@Override
public void onStart(Application app) {
Logger.info(appName + " has started");
LocationProxyRule.scheduleCacheUpdate();
ConfReader confReader = new ConfReader();
String updateCache = confReader.readString("update.cache.on.start");
if(updateCache == null || updateCache.trim().toLowerCase().equals("true")){
LocationProxyRule.scheduleCacheUpdate();
}
}

@Override
Expand Down
62 changes: 33 additions & 29 deletions app/interactors/LocationProxyRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ public static void scheduleCacheUpdate(Location location) {
new Runnable() {
@Override
public void run() {
Logger.info("updating cache ...");
JPA.withTransaction(() -> {
LocationProxyRule.updateCache(location);
Logger.info("cache updated!");
updateCache(location);
});
}
}, Akka.system().dispatcher());
Expand All @@ -46,14 +44,19 @@ public void run() {
private static synchronized void updateCache(Location location) {

if (location != null) {
// DOES NOT UPDATE AUTREE
// ONLY EPIDEMIC ZONES ARE ALLOWED TO BE CREATED AT THIS MOMEMNT
updateUniqueSortedLocationNames(location);
return;
Logger.info("added names of location with gid = " + location.getGid()
+ " to UniqueSortedLocationNames cache ...");
} else {
Logger.info("initializing cache for AUtree and UniqueSortedLocationNames ...");
notifyChange();
uniqueSortedLocationNames = getUniqueSortedLocationNames();
roots = getHierarchy();
auTree = getAuTree();
Logger.info("cache updated!");
}

notifyChange();
uniqueSortedLocationNames = getUniqueSortedLocationNames();
roots = getHierarchy();
auTree = getAuTree();
}

private static void notifyChange() {
Expand Down Expand Up @@ -117,8 +120,10 @@ private static List<String> getUniqueSortedLocationNames() {
if (uniqueSortedLocationNames == null) {
uniqueSortedLocationNames = new ArrayList<>();
synchronized (uniqueSortedLocationNames) {
Logger.info("loading uniqueSortedLocationNames ...");
uniqueSortedLocationNames = new LocationDao().readUniqueNames();
Collections.sort(uniqueSortedLocationNames, String.CASE_INSENSITIVE_ORDER);
Logger.info("uniqueSortedLocationNames updated!");
}
}
return uniqueSortedLocationNames;
Expand All @@ -145,7 +150,7 @@ public static List<Map<String, String>> listUniqueNames(String prefixNames, int
Map<String, String> map;
String cleanedInputString = removeNonWordCharacters(prefixName);
String cleanedCompareString;

while (!names.isEmpty()) {
remainingNames = new ArrayList<>();

Expand All @@ -164,10 +169,10 @@ public static List<Map<String, String>> listUniqueNames(String prefixNames, int
Logger.debug("Yes, the regex didn't work right!");
Logger.debug(exception.toString());
}
for(int i = 0; i < tokens.length; i++) {

for (int i = 0; i < tokens.length; i++) {
cleanedCompareString = removeNonWordCharacters(tokens[i]);
if(cleanedCompareString.equalsIgnoreCase(cleanedInputString)) {
if (cleanedCompareString.equalsIgnoreCase(cleanedInputString)) {
tokenMatches.add(originalName);
break;
}
Expand Down Expand Up @@ -215,35 +220,34 @@ public static List<Map<String, String>> listUniqueNames(String prefixNames, int

return result;
}

public static String removeNonWordCharacters(String input) {
Pattern nonWordPattern = Pattern.compile("[^\\p{L}0-9]+", Pattern.UNICODE_CHARACTER_CLASS);
return input.replaceAll(nonWordPattern.toString(), "");
}

private static void updateUniqueSortedLocationNames(Location location) {
if (uniqueSortedLocationNames == null)
uniqueSortedLocationNames = new ArrayList<>();
List<String> names = getNames(location);
insertIntoUniqueSortedLocationNames(names);
}

private static void insertIntoUniqueSortedLocationNames(List<String> names) {
if (uniqueSortedLocationNames == null || names == null)
return;
int insertPoint;
for (String name : names) {
insertPoint = Collections.binarySearch(uniqueSortedLocationNames, name, String.CASE_INSENSITIVE_ORDER);
if (insertPoint < 0) {
uniqueSortedLocationNames.add(-insertPoint - 1, name);
if (uniqueSortedLocationNames == null)
uniqueSortedLocationNames = getUniqueSortedLocationNames();
else {
synchronized (uniqueSortedLocationNames) {
for (String name : names)
insert(uniqueSortedLocationNames, name);
}
}
}

private static void insert(List<String> uniqueSortedNames, String name) {
int insertPoint = Collections.binarySearch(uniqueSortedNames, name, String.CASE_INSENSITIVE_ORDER);
if (insertPoint < 0)
uniqueSortedNames.add(-insertPoint - 1, name);
}

private static List<String> getNames(Location location) {
if (location == null)
return null;

List<String> names = new ArrayList<>();
names.add(location.getData().getName());
List<AltName> altNames = location.getAltNames();
Expand Down
3 changes: 3 additions & 0 deletions conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ spew.base.url="spew.olympus.psc.edu/syneco/spe#"
# Allowable clients for updating/deleting locations
#permission.delete=""

# update cache on application start
# update.cache.on.start = true

# Optional configuration files
# 'application_overrides.conf' is used for environment-specific overrides & properties as it should
# be excluded via '.gitignore'
Expand Down

0 comments on commit 9fa51e5

Please sign in to comment.