Mercurial > hg > mpdl-group
view software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/lt/doc/regularization/DBRegularizationHandler.java @ 0:408254cf2f1d
Erstellung
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 24 Nov 2010 17:24:23 +0100 |
parents | |
children |
line wrap: on
line source
package de.mpg.mpiwg.berlin.mpdl.lt.doc.regularization; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import com.sleepycat.je.Cursor; import com.sleepycat.je.Database; import com.sleepycat.je.DatabaseEntry; import com.sleepycat.je.DatabaseException; import com.sleepycat.je.LockMode; import com.sleepycat.je.OperationStatus; import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; import de.mpg.mpiwg.berlin.mpdl.lt.general.Language; public class DBRegularizationHandler { private String dbDirectory; private DbEnvRegularization regDbEnv; public DBRegularizationHandler(String dbDir) { this.dbDirectory = dbDir; } public void start() throws ApplicationException { regDbEnv = new DbEnvRegularization(); regDbEnv.setDataDir(dbDirectory); regDbEnv.init(); // open databases in read/write mode } public void openDatabases() throws ApplicationException { regDbEnv.openDatabases(); } public void closeDatabases() throws ApplicationException { regDbEnv.close(); } public void deleteData() throws ApplicationException { regDbEnv.removeDatabases(); } public void writeOrigReg(Regularization reg) throws ApplicationException { try { String language = Language.getInstance().getLanguageId(reg.getLanguage()); String keyStr = language + "###" + reg.getOrig(); String valueStr = reg.getXmlString(); DatabaseEntry dbEntryKey = new DatabaseEntry(keyStr.getBytes("utf-8")); DatabaseEntry dbEntryValue = new DatabaseEntry(valueStr.getBytes("utf-8")); Database origDB = regDbEnv.getOrigDB(); origDB.put(null, dbEntryKey, dbEntryValue); } catch (DatabaseException e) { throw new ApplicationException(e); } catch (UnsupportedEncodingException e) { throw new ApplicationException(e); } } public void writeNormReg(Regularization reg) throws ApplicationException { try { String language = Language.getInstance().getLanguageId(reg.getLanguage()); String keyStr = language + "###" + reg.getNorm(); String valueStr = reg.getXmlString(); DatabaseEntry dbEntryKey = new DatabaseEntry(keyStr.getBytes("utf-8")); DatabaseEntry dbEntryValue = new DatabaseEntry(valueStr.getBytes("utf-8")); Database normDB = regDbEnv.getNormDB(); normDB.put(null, dbEntryKey, dbEntryValue); } catch (DatabaseException e) { throw new ApplicationException(e); } catch (UnsupportedEncodingException e) { throw new ApplicationException(e); } } public void deleteReg(Regularization reg) throws ApplicationException { try { String language = Language.getInstance().getLanguageId(reg.getLanguage()); String keyStrOrig = language + "###" + reg.getOrig(); DatabaseEntry dbEntryKey = new DatabaseEntry(keyStrOrig.getBytes("utf-8")); Database origDB = regDbEnv.getOrigDB(); origDB.delete(null, dbEntryKey); String keyStrNorm = reg.getLanguage() + "###" + reg.getNorm(); dbEntryKey = new DatabaseEntry(keyStrNorm.getBytes("utf-8")); Database normDB = regDbEnv.getNormDB(); normDB.delete(null, dbEntryKey); } catch (DatabaseException e) { throw new ApplicationException(e); } catch (UnsupportedEncodingException e) { throw new ApplicationException(e); } } public ArrayList<Regularization> readRegsByOrig(String lang, String orig) throws ApplicationException { String language = Language.getInstance().getLanguageId(lang); ArrayList<Regularization> retRegs = new ArrayList<Regularization>(); String hashKey = language + "###" + orig; try { Database origDB = regDbEnv.getOrigDB(); Cursor cursor = origDB.openCursor(null, null); byte[] bHashKey = hashKey.getBytes("utf-8"); DatabaseEntry dbEntryKey = new DatabaseEntry(bHashKey); DatabaseEntry foundValue = new DatabaseEntry(); OperationStatus operationStatus = cursor.getSearchKey(dbEntryKey, foundValue, LockMode.DEFAULT); while (operationStatus == OperationStatus.SUCCESS) { byte[] foundValueBytes = foundValue.getData(); String foundValueStr = new String(foundValueBytes, "utf-8"); Regularization reg = Regularization.getInstance(foundValueStr); retRegs.add(reg); operationStatus = cursor.getNextDup(dbEntryKey, foundValue, LockMode.DEFAULT); } cursor.close(); } catch (DatabaseException e) { throw new ApplicationException(e); } catch (UnsupportedEncodingException e) { throw new ApplicationException(e); } return retRegs; } public ArrayList<Regularization> readRegsByNorm(String lang, String norm) throws ApplicationException { String language = Language.getInstance().getLanguageId(lang); ArrayList<Regularization> retRegs = new ArrayList<Regularization>(); String hashKey = language + "###" + norm; try { Database normDB = regDbEnv.getNormDB(); Cursor cursor = normDB.openCursor(null, null); byte[] bHashKey = hashKey.getBytes("utf-8"); DatabaseEntry dbEntryKey = new DatabaseEntry(bHashKey); DatabaseEntry foundValue = new DatabaseEntry(); OperationStatus operationStatus = cursor.getSearchKey(dbEntryKey, foundValue, LockMode.DEFAULT); while (operationStatus == OperationStatus.SUCCESS) { byte[] foundValueBytes = foundValue.getData(); String foundValueStr = new String(foundValueBytes, "utf-8"); Regularization reg = Regularization.getInstance(foundValueStr); retRegs.add(reg); operationStatus = cursor.getNextDup(dbEntryKey, foundValue, LockMode.DEFAULT); } cursor.close(); } catch (DatabaseException e) { throw new ApplicationException(e); } catch (UnsupportedEncodingException e) { throw new ApplicationException(e); } return retRegs; } }