Mercurial > hg > mpdl-group
diff software/mpdl-services/mpiwg-mpdl-lt/src/de/mpg/mpiwg/berlin/mpdl/lt/morph/app/MorphologyCache.java @ 23:e845310098ba
diverse Korrekturen
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Tue, 27 Nov 2012 12:35:19 +0100 |
parents | 7d6d969b10cf |
children |
line wrap: on
line diff
--- a/software/mpdl-services/mpiwg-mpdl-lt/src/de/mpg/mpiwg/berlin/mpdl/lt/morph/app/MorphologyCache.java Wed Dec 14 13:57:09 2011 +0100 +++ b/software/mpdl-services/mpiwg-mpdl-lt/src/de/mpg/mpiwg/berlin/mpdl/lt/morph/app/MorphologyCache.java Tue Nov 27 12:35:19 2012 +0100 @@ -25,11 +25,13 @@ private static String DB_DIR_DONATUS = DATA_DIR + "/dataBerkeleyDB/donatus"; public static int QUERY_MODE = 0; public static int DOCUMENT_MODE = 1; + private static long MIN_RAM = 500000000; private static int MAX_HASHTABLE_SIZE = Constants.MORPHOLOGY_CACHE_SIZE; + private Date touchTimer; protected int mode = QUERY_MODE; private Hashtable<String, Hashtable<String, Lemma>> forms = new Hashtable<String, Hashtable<String, Lemma>>(); // cache of forms: hashKey is formName private Hashtable<String, Lemma> lemmas = new Hashtable<String, Lemma>(); // cache of lemmas: hashKey is lemmaName - private DBMorphHandler dbMorphHandlerStatic; // handles static morph data (BerkeleyDB) + private DBMorphHandler dbMorphHandler; // handles morph data (BerkeleyDB) private Date beginOfOperation; private Date endOfOperation; @@ -42,13 +44,20 @@ } private void init() throws ApplicationException { + long maxMemory = Runtime.getRuntime().maxMemory(); + if (maxMemory < MIN_RAM) { + String message = "Morphology cache: at least " + MIN_RAM + " is needed as heap space: please start java with parameter -Xmx with more than this value)"; + LOGGER.severe(message); + throw new ApplicationException(message); + } + touchTimer = new Date(); instance.beginOperation(); - dbMorphHandlerStatic = new DBMorphHandler(DB_DIR_DONATUS); - dbMorphHandlerStatic.start(); - dbMorphHandlerStatic.openDatabases(); + dbMorphHandler = new DBMorphHandler(DB_DIR_DONATUS); + dbMorphHandler.startReadOnly(); + dbMorphHandler.openDatabases(); instance.endOperation(); Double elapsedTime = new Util().getSecondWithMillisecondsBetween(instance.beginOfOperation, instance.endOfOperation); - LOGGER.info("Morphology db cache: opened (needed " + elapsedTime + " seconds)"); + LOGGER.info("Morphology cache: morphology db opened read only (needed " + elapsedTime + " seconds, heap space: " + maxMemory + " bytes)"); } public int getMode() { @@ -60,16 +69,14 @@ } public void end() throws ApplicationException { - dbMorphHandlerStatic.closeDatabases(); - LOGGER.info("Morphology db cache: closed"); + dbMorphHandler.closeDatabases(); + LOGGER.info("Morphology cache: db closed"); + forms = null; + lemmas = null; + dbMorphHandler = null; + instance = null; } - /* - public ArrayList<Lemma> getLemmasByFormName(String lang, String formName, boolean normalize) throws ApplicationException { - return getLemmasByFormName(lang, formName, normalize, Normalizer.DISPLAY); - } - */ - public ArrayList<Lemma> getLemmasByFormName(String lang, String formNameArg, int normMode) throws ApplicationException { String language = Language.getInstance().getLanguageId(lang); ArrayList<Lemma> retFormLemmas = null; @@ -84,8 +91,16 @@ ArrayList<Lemma> dbFormLemmas = readLemmasByFormName(language, formName); // put lemmas into local cache int localHashTableSize = forms.size(); - if (localHashTableSize >= MAX_HASHTABLE_SIZE) { - clearCache(); + Date now = new Date(); + if (now.getTime() - touchTimer.getTime() > 900000) { // is true each 0,25 hours: then free memory is fetched (needs some time) + touchTimer = new Date(); + long freeMemory = Runtime.getRuntime().freeMemory(); + LOGGER.info(touchTimer + ": Morphology cache: free memory in heap space: " + freeMemory + " bytes"); + if (freeMemory < MIN_RAM || localHashTableSize >= MAX_HASHTABLE_SIZE) { // if freeMemory is less then MIN_RAM then clear cache to get some new memory + clearCache(); + freeMemory = Runtime.getRuntime().freeMemory(); + LOGGER.info(touchTimer + ": Morphology cache: cache cleared, free memory in heap space: " + freeMemory + " bytes"); + } } if (dbFormLemmas != null && ! dbFormLemmas.isEmpty()) { formLemmasHashtable = new Hashtable<String, Lemma>(); @@ -271,13 +286,13 @@ private ArrayList<Lemma> readLemmasByFormName(String lang, String formName) throws ApplicationException { String language = Language.getInstance().getLanguageId(lang); - ArrayList<Lemma> lemmasStatic = dbMorphHandlerStatic.readLemmas(language, formName); + ArrayList<Lemma> lemmasStatic = dbMorphHandler.readLemmas(language, formName); return lemmasStatic; } private ArrayList<Form> readFormsByLemmaName(String lang, String lemmaName) throws ApplicationException { String language = Language.getInstance().getLanguageId(lang); - ArrayList<Form> formsStatic = dbMorphHandlerStatic.readForms(language, lemmaName); + ArrayList<Form> formsStatic = dbMorphHandler.readForms(language, lemmaName); return formsStatic; }