|
From: David F. <dwf...@us...> - 2006-10-02 16:44:25
|
Update of /cvsroot/openmed/OpenEMed/src/tools/gov/lanl/Database In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv20636/src/tools/gov/lanl/Database Modified Files: CodeMapper.java Log Message: Modified CodeMapper to cache the data from the database so that it could be updated by a different server without duplicating the data. Index: CodeMapper.java =================================================================== RCS file: /cvsroot/openmed/OpenEMed/src/tools/gov/lanl/Database/CodeMapper.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** CodeMapper.java 27 Mar 2006 23:28:01 -0000 1.4 --- CodeMapper.java 2 Oct 2006 16:44:13 -0000 1.5 *************** *** 42,48 **** // the mapping hashtables ! HashMap idMap; ! HashMap codeMap; ! Vector topLevelCodes; // the singleton instance --- 42,48 ---- // the mapping hashtables ! HashMap<String, String> idMap; ! HashMap<String, String> codeMap; ! ArrayList<String> topLevelCodes; // the singleton instance *************** *** 96,101 **** emptyStringMap_ = createStringMap("", "", false); ! readMapping(); } --- 96,104 ---- emptyStringMap_ = createStringMap("", "", false); + idMap = new HashMap<String, String>(); + codeMap = new HashMap<String, String>(); + topLevelCodes = new ArrayList<String>(); ! // readMapping(); } *************** *** 154,162 **** // lookup id ! String id = (String) idMap.get(code); // id not found? if (id == null) { ! // add flag set? if (addCode) { --- 157,175 ---- // lookup id ! String id = idMap.get(code); // id not found? if (id == null) { ! // First read from database ! StringMap_ stringMap = readStringMap(code); ! if (stringMap != null) { ! id = stringMap.getId(); ! idMap.put(code, id); ! codeMap.put(id, code); ! if (isTopLevelCode != stringMap.isTopLevelCode()) ! cat.warn("getId: isTopLevelCode is " + isTopLevelCode + " doesn't match"); ! topLevelCodes.add(code); ! return id; ! } // add flag set? if (addCode) { *************** *** 188,197 **** // lookup code ! String code = (String) codeMap.get(id); if (code == null) { ! cat.info(".getCode: Can't lookup id '" + id + "'"); ! ! return String.valueOf(code); } --- 201,212 ---- // lookup code ! String code = codeMap.get(id); if (code == null) { ! code = readCode(id); ! if (code == null) { ! cat.info(".getCode: Can't lookup id '" + id + "'"); ! return String.valueOf(code); ! } } *************** *** 200,203 **** --- 215,328 ---- /** + * Find the StringMap in the database for the requested code + * + * @param code + * @return StringMap_ + */ + gov.lanl.Database.StringMap_ readStringMap(String code) { + Vector results = null; + StringMap_ stringMap = null; + try { + dbMgr.txn_begin(); + SearchFilter searchFilter = DBMgrFactory.createFilter(); + searchFilter.compareFilter("entry", code, SearchFilter.EQUAL); + Object obj = stringMap_Class.newInstance(); + results = dbMgr.retrieveElements(obj, searchFilter, DatabaseMgr.SHALLOW); + int size = results.size(); + if (size > 1) { + cat.error("duplicate values (" + size + ") for code=" + code + " found, returning first one!"); + } + if (size > 0) stringMap = (gov.lanl.Database.StringMap_) results.get(0); + cat.debug("readStringMap: " + stringMap); + dbMgr.txn_commit(); + } catch (Exception e) { + cat.error("failure in readStringMap of code " + code + " " + e); + dbMgr.txn_abort(); + + } + return stringMap; + + + } + + /** + * Obtain the code corresponding to the id from the database. + * @param id + * @return String + */ + String readCode(String id) { + Vector results = null; + try { + dbMgr.txn_begin(); + SearchFilter searchFilter = DBMgrFactory.createFilter(); + searchFilter.compareFilter("id", id, SearchFilter.EQUAL); + Object obj = stringMap_Class.newInstance(); + results = dbMgr.retrieveElements(obj, searchFilter, DatabaseMgr.SHALLOW); + int size = results.size(); + + if (size > 1) { + cat.error("duplicate values (" + size + ") for id=" + id + " found, returning first one!"); + } + cat.debug("readStringMap: " + results.get(0)); + StringMap_ stringMap = null; + if (size > 0) stringMap = (gov.lanl.Database.StringMap_) results.get(0); + dbMgr.txn_commit(); + if (size == 0) return null; + String entry = stringMap.getEntry(); + boolean isTopLevel = stringMap.isTopLevelCode(); + codeMap.put(id, entry); + idMap.put(entry, id); + if (isTopLevel) topLevelCodes.add(entry); + return entry; + + } catch (Exception e) { + cat.error("failure in readCode of id " + id + " " + e); + dbMgr.txn_abort(); + return null; + } + } + + /** + * read in top level codes + */ + public void readTopLevel() { + Vector results = null; + try { + dbMgr.txn_begin(); + SearchFilter searchFilter = DBMgrFactory.createFilter(); + searchFilter.matchValue("isTopLevelCode", 1, SearchFilter.EQUAL); + Object obj = stringMap_Class.newInstance(); + results = dbMgr.retrieveElements(obj, searchFilter, DatabaseMgr.SHALLOW); + + for (Iterator it = results.iterator(); it.hasNext();) { + Object result = (Object) it.next(); + StringMap_ stringMap = (StringMap_) result; + String id = stringMap.getId(); + String entry = stringMap.getEntry(); + + if (debug) { + cat.info(".readMapping: " + id + "/" + entry); + } + + // insert mapping into hashmaps + idMap.put(entry, id); + codeMap.put(id, entry); + + if (stringMap.isTopLevelCode()) { + topLevelCodes.add(entry); + } + } + dbMgr.txn_commit(); + + + } catch (Exception e) { + cat.error("failure in readTopLevel " + e); + dbMgr.txn_abort(); + + } + + } + + /** * This method can be used to add additional key/value pairs to the mapping tables * *************** *** 253,268 **** // iterate over result set and put elements into hashtables - int size = results.size(); if (size > 0) { ! idMap = new HashMap(2 * size); ! codeMap = new HashMap(2 * size); ! topLevelCodes = new Vector(size); } else { ! idMap = new HashMap(); ! codeMap = new HashMap(); ! topLevelCodes = new Vector(); } ! for (Iterator it = results.iterator(); it.hasNext();) { Object result = (Object) it.next(); --- 378,394 ---- // iterate over result set and put elements into hashtables + /* + int size = results.size(); if (size > 0) { ! idMap = new HashMap<String, String>(2 * size); ! codeMap = new HashMap<String, String>(2 * size); ! topLevelCodes = new ArrayList<String>(size); } else { ! idMap = new HashMap<String, String>(); ! codeMap = new HashMap<String, String>(); ! topLevelCodes = new ArrayList<String>(); } ! */ for (Iterator it = results.iterator(); it.hasNext();) { Object result = (Object) it.next(); *************** *** 288,301 **** } catch (DBException e) { cat.error("Can't retrieve code mapping! " + e); ! ! idMap = new HashMap(); ! codeMap = new HashMap(); ! topLevelCodes = new Vector(); ! dbMgr.txn_abort(); return; } ! cat.info("loaded: "+codeMap.size() + " codes, "+topLevelCodes.size()+" topLevelCodes found"); } --- 414,427 ---- } catch (DBException e) { cat.error("Can't retrieve code mapping! " + e); ! /* ! idMap = new HashMap<String, String>(); ! codeMap = new HashMap<String, String>(); ! topLevelCodes = new ArrayList<String>(); ! */ dbMgr.txn_abort(); return; } ! cat.info("loaded: " + codeMap.size() + " codes, " + topLevelCodes.size() + " topLevelCodes found"); } *************** *** 319,327 **** ArrayList array = new ArrayList(); Iterator it = null; ! if (!returnAll) it = topLevelCodes.iterator(); ! else it = ((Collection) codeMap.values()).iterator(); ! while (it.hasNext()) { String s = (String) it.next(); --- 445,456 ---- ArrayList array = new ArrayList(); Iterator it = null; ! ! if (!returnAll) { ! if (topLevelCodes.size() == 0) readTopLevel(); it = topLevelCodes.iterator(); ! } else { ! if (codeMap.size() == 0) readMapping(); it = ((Collection) codeMap.values()).iterator(); ! } while (it.hasNext()) { String s = (String) it.next(); *************** *** 366,369 **** --- 495,499 ---- if (returnAll) { + if (codeMap.size() == 0) readMapping(); Collection codesColl = codeMap.values(); *************** *** 371,379 **** codes = (String[]) codesColl.toArray(codes); } else { ! // copy vector into array codes = new String[topLevelCodes.size()]; ! topLevelCodes.copyInto(codes); } --- 501,509 ---- codes = (String[]) codesColl.toArray(codes); } else { ! if (topLevelCodes.size() == 0) readTopLevel(); // copy vector into array codes = new String[topLevelCodes.size()]; ! codes = topLevelCodes.toArray(codes); } *************** *** 433,437 **** // Class[] params = new Class[]{ String.class , String.class, Boolean.class}; Class[] params = new Class[]{ ! String.class, String.class, int.class }; Constructor constructor = stringMap_Class.getConstructor(params); --- 563,567 ---- // Class[] params = new Class[]{ String.class , String.class, Boolean.class}; Class[] params = new Class[]{ ! String.class, String.class, int.class }; Constructor constructor = stringMap_Class.getConstructor(params); *************** *** 445,449 **** Object[] args = new Object[]{ ! id, code, topLevel }; --- 575,579 ---- Object[] args = new Object[]{ ! id, code, topLevel }; |