Mercurial > hg > mpdl-group
diff software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/externalObjects/app/ExternalObjectsHandler.java @ 6:2396a569e446
new functions: externalObjects, normalizer, Unicode2Betacode
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Tue, 08 Feb 2011 14:54:09 +0100 |
parents | |
children | 1ec29fdd0db8 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/externalObjects/app/ExternalObjectsHandler.java Tue Feb 08 14:54:09 2011 +0100 @@ -0,0 +1,184 @@ +package de.mpg.mpiwg.berlin.mpdl.externalObjects.app; + +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Date; + +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.util.Util; +import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; +import de.mpg.mpiwg.berlin.mpdl.externalObjects.db.DbEnvExternalObjects; +import de.mpg.mpiwg.berlin.mpdl.general.MpdlConstants; + +public class ExternalObjectsHandler { + private static ExternalObjectsHandler instance; + private static String MPDL_DATA_DIR = MpdlConstants.MPDL_EXIST_DATA_DIR; + private static String DB_DIR_EXTERNAL_OBJECTS = MPDL_DATA_DIR + "/dataBerkeleyDB/externalObjects"; + private DbEnvExternalObjects dbEnvExternalObjects; + private Date beginOfOperation; + private Date endOfOperation; + + public static ExternalObjectsHandler getInstance() throws ApplicationException { + if (instance == null) { + instance = new ExternalObjectsHandler(); + instance.init(); + } + return instance; + } + + public ArrayList<ExtElement> readExternalElements(String documentId, String pageNumber) throws ApplicationException { + return readDBExternalElements(documentId, pageNumber); + } + + public void writeExternalElement(ExtElement element) throws ApplicationException { + writeDBExternalElement(element); + } + + public void deleteExternalElement(ExtElement element) throws ApplicationException { + deleteDBExternalElement(element); + } + + private void writeDBExternalElement(ExtElement element) throws ApplicationException { + try { + String keyStr = element.getDocumentId() + "###" + element.getPageNumber(); + String valueStr = element.getXmlString(); + DatabaseEntry dbEntryKey = new DatabaseEntry(keyStr.getBytes("utf-8")); + DatabaseEntry dbEntryValue = new DatabaseEntry(valueStr.getBytes("utf-8")); + Database elementDB = dbEnvExternalObjects.getElementDB(); + elementDB.put(null, dbEntryKey, dbEntryValue); + } catch (DatabaseException e) { + throw new ApplicationException(e); + } catch (UnsupportedEncodingException e) { + throw new ApplicationException(e); + } + } + + private void deleteDBExternalElement(ExtElement element) throws ApplicationException { + try { + String keyStr = element.getDocumentId() + "###" + element.getPageNumber(); + DatabaseEntry dbEntryKey = new DatabaseEntry(keyStr.getBytes("utf-8")); + Database elementDB = dbEnvExternalObjects.getElementDB(); + elementDB.delete(null, dbEntryKey); + } catch (DatabaseException e) { + throw new ApplicationException(e); + } catch (UnsupportedEncodingException e) { + throw new ApplicationException(e); + } + } + + private ArrayList<ExtElement> readDBExternalElements(String documentId, String pageNumber) throws ApplicationException { + ArrayList<ExtElement> retElements = new ArrayList<ExtElement>(); + String hashKey = documentId + "###" + pageNumber; + try { + Database elementDB = dbEnvExternalObjects.getElementDB(); + Cursor cursor = elementDB.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"); + ExtElement e = ExtElement.parseXmlStr(foundValueStr); + retElements.add(e); + operationStatus = cursor.getNextDup(dbEntryKey, foundValue, LockMode.DEFAULT); + } + cursor.close(); + } catch (DatabaseException e) { + throw new ApplicationException(e); + } catch (UnsupportedEncodingException e) { + throw new ApplicationException(e); + } + return retElements; + } + + private void init() throws ApplicationException { + dbEnvExternalObjects = new DbEnvExternalObjects(); + dbEnvExternalObjects.setDataDir(DB_DIR_EXTERNAL_OBJECTS); + dbEnvExternalObjects.init(); + dbEnvExternalObjects.openDatabases(); + } + + public static void main(String[] args) throws ApplicationException { + getInstance(); + instance.beginOperation(); + System.out.print("Start ..."); + // instance.deleteSampleData(); + // instance.writeSampleData(); + instance.readSampleData(); + instance.end(); + instance.endOperation(); + Double elapsedTime = new Util().getSecondWithMillisecondsBetween(instance.beginOfOperation, instance.endOfOperation); + System.out.println("End."); + System.out.println("Needed time: " + elapsedTime + " seconds"); + } + + private void deleteSampleData() throws ApplicationException { + ExtElement e = new ExtElement(); + e.setUid("joe"); + e.setDocumentId("/archimedes/it/l223.xml"); + e.setPageNumber("17"); + deleteExternalElement(e); + } + + private void writeSampleData() throws ApplicationException { + Date now = new Date(); + + String sId = "1.2.2.2.2.5"; + ExtElement e = new ExtElement(); + e.setUid("joe"); + e.setModificationDate(now); + e.setDocumentId("/archimedes/it/l223.xml"); + e.setPageNumber("17"); + e.setXmlNodeId(sId); + e.setContent("<note>This is a test note to sentence " + sId + "</note>"); + writeExternalElement(e); + + ExtElement e2 = new ExtElement(); + String sId2 = "1.2.2.2.2.7"; + e2.setUid("michael"); + e2.setModificationDate(now); + e2.setDocumentId("/archimedes/it/l223.xml"); + e2.setPageNumber("17"); + e2.setXmlNodeId(sId2); + e2.setCharPos("18"); + e2.setContent("<note>This is a test note to sentence " + sId2 + "</note>"); + writeExternalElement(e2); + + /* + String sId3 = "1.2.2.2.2.8.15.3.3"; + e3.setUid("joe"); + e3.setModificationDate(now); + e3.setDocumentId("/archimedes/it/l223.xml"); + e3.setPageNumber("17"); + e3.setXmlNodeId(sId3); + e2.setContent("<note>This is an external test note to sentence " + sId3 + "</note>"); + writeExternalElement(e3); + */ + + } + + private void readSampleData() throws ApplicationException { + ArrayList<ExtElement> elements = readExternalElements("/archimedes/it/l223.xml", "17"); + System.out.println(elements); + } + + private void end() throws ApplicationException { + dbEnvExternalObjects.close(); + } + + private void beginOperation() { + beginOfOperation = new Date(); + } + + private void endOperation() { + endOfOperation = new Date(); + } + +} \ No newline at end of file