-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLDR-17313 split out immutable DiskData in STFactory
- stuff we only want to calculate once, versus what can be expired from the cache.
- Loading branch information
Showing
2 changed files
with
104 additions
and
31 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
tools/cldr-apps/src/main/java/org/unicode/cldr/web/DiskDataCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.unicode.cldr.web; | ||
|
||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import java.util.Set; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.logging.Logger; | ||
import org.unicode.cldr.util.CLDRFile; | ||
import org.unicode.cldr.util.CLDRLocale; | ||
import org.unicode.cldr.util.Factory; | ||
import org.unicode.cldr.util.PathHeader; | ||
import org.unicode.cldr.util.XMLSource; | ||
|
||
/** Cache for on-disk immutable data. */ | ||
public class DiskDataCache { | ||
static final Logger logger = Logger.getLogger(DiskDataCache.class.getSimpleName()); | ||
|
||
private final Factory factory; | ||
private final CLDRFile english; | ||
private final PathHeader.Factory phf; | ||
|
||
/** this is the immutable cousin of STFactory.PerLocaleData, for the on-disk data */ | ||
class DiskDataEntry { | ||
final CLDRLocale locale; | ||
final XMLSource diskData; | ||
final CLDRFile diskFile; | ||
final Set<String> pathsForFile; | ||
|
||
public DiskDataEntry(CLDRLocale locale) { | ||
this.locale = locale; | ||
diskData = factory.makeSource(locale.getBaseName()).freeze(); | ||
diskFile = factory.make(locale.getBaseName(), true).freeze(); | ||
pathsForFile = getPathHeaderFactory().pathsForFile(diskFile); | ||
} | ||
} | ||
|
||
public DiskDataCache(Factory f, CLDRFile english) { | ||
this.factory = f; | ||
this.english = english; | ||
this.phf = PathHeader.getFactory(english); | ||
} | ||
|
||
public PathHeader.Factory getPathHeaderFactory() { | ||
return phf; | ||
} | ||
|
||
private LoadingCache<CLDRLocale, DiskDataEntry> cache = | ||
CacheBuilder.newBuilder() | ||
.build( | ||
new CacheLoader<CLDRLocale, DiskDataEntry>() { | ||
|
||
@Override | ||
public DiskDataEntry load(CLDRLocale l) throws Exception { | ||
return new DiskDataEntry(l); | ||
} | ||
}); | ||
|
||
public DiskDataEntry get(CLDRLocale locale) { | ||
logger.fine(() -> "Loading " + locale); | ||
try { | ||
return cache.get(locale); | ||
} catch (ExecutionException e) { | ||
SurveyLog.logException(logger, e, "Trying to construct " + locale); | ||
SurveyMain.busted("Loading " + locale, e); | ||
return null; /* notreached */ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters