Mercurial > hg > mpdl-group
comparison 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 |
comparison
equal
deleted
inserted
replaced
5:94305c504178 | 6:2396a569e446 |
---|---|
1 package de.mpg.mpiwg.berlin.mpdl.externalObjects.app; | |
2 | |
3 import java.io.UnsupportedEncodingException; | |
4 import java.util.ArrayList; | |
5 import java.util.Date; | |
6 | |
7 import com.sleepycat.je.Cursor; | |
8 import com.sleepycat.je.Database; | |
9 import com.sleepycat.je.DatabaseEntry; | |
10 import com.sleepycat.je.DatabaseException; | |
11 import com.sleepycat.je.LockMode; | |
12 import com.sleepycat.je.OperationStatus; | |
13 | |
14 import de.mpg.mpiwg.berlin.mpdl.util.Util; | |
15 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; | |
16 import de.mpg.mpiwg.berlin.mpdl.externalObjects.db.DbEnvExternalObjects; | |
17 import de.mpg.mpiwg.berlin.mpdl.general.MpdlConstants; | |
18 | |
19 public class ExternalObjectsHandler { | |
20 private static ExternalObjectsHandler instance; | |
21 private static String MPDL_DATA_DIR = MpdlConstants.MPDL_EXIST_DATA_DIR; | |
22 private static String DB_DIR_EXTERNAL_OBJECTS = MPDL_DATA_DIR + "/dataBerkeleyDB/externalObjects"; | |
23 private DbEnvExternalObjects dbEnvExternalObjects; | |
24 private Date beginOfOperation; | |
25 private Date endOfOperation; | |
26 | |
27 public static ExternalObjectsHandler getInstance() throws ApplicationException { | |
28 if (instance == null) { | |
29 instance = new ExternalObjectsHandler(); | |
30 instance.init(); | |
31 } | |
32 return instance; | |
33 } | |
34 | |
35 public ArrayList<ExtElement> readExternalElements(String documentId, String pageNumber) throws ApplicationException { | |
36 return readDBExternalElements(documentId, pageNumber); | |
37 } | |
38 | |
39 public void writeExternalElement(ExtElement element) throws ApplicationException { | |
40 writeDBExternalElement(element); | |
41 } | |
42 | |
43 public void deleteExternalElement(ExtElement element) throws ApplicationException { | |
44 deleteDBExternalElement(element); | |
45 } | |
46 | |
47 private void writeDBExternalElement(ExtElement element) throws ApplicationException { | |
48 try { | |
49 String keyStr = element.getDocumentId() + "###" + element.getPageNumber(); | |
50 String valueStr = element.getXmlString(); | |
51 DatabaseEntry dbEntryKey = new DatabaseEntry(keyStr.getBytes("utf-8")); | |
52 DatabaseEntry dbEntryValue = new DatabaseEntry(valueStr.getBytes("utf-8")); | |
53 Database elementDB = dbEnvExternalObjects.getElementDB(); | |
54 elementDB.put(null, dbEntryKey, dbEntryValue); | |
55 } catch (DatabaseException e) { | |
56 throw new ApplicationException(e); | |
57 } catch (UnsupportedEncodingException e) { | |
58 throw new ApplicationException(e); | |
59 } | |
60 } | |
61 | |
62 private void deleteDBExternalElement(ExtElement element) throws ApplicationException { | |
63 try { | |
64 String keyStr = element.getDocumentId() + "###" + element.getPageNumber(); | |
65 DatabaseEntry dbEntryKey = new DatabaseEntry(keyStr.getBytes("utf-8")); | |
66 Database elementDB = dbEnvExternalObjects.getElementDB(); | |
67 elementDB.delete(null, dbEntryKey); | |
68 } catch (DatabaseException e) { | |
69 throw new ApplicationException(e); | |
70 } catch (UnsupportedEncodingException e) { | |
71 throw new ApplicationException(e); | |
72 } | |
73 } | |
74 | |
75 private ArrayList<ExtElement> readDBExternalElements(String documentId, String pageNumber) throws ApplicationException { | |
76 ArrayList<ExtElement> retElements = new ArrayList<ExtElement>(); | |
77 String hashKey = documentId + "###" + pageNumber; | |
78 try { | |
79 Database elementDB = dbEnvExternalObjects.getElementDB(); | |
80 Cursor cursor = elementDB.openCursor(null, null); | |
81 byte[] bHashKey = hashKey.getBytes("utf-8"); | |
82 DatabaseEntry dbEntryKey = new DatabaseEntry(bHashKey); | |
83 DatabaseEntry foundValue = new DatabaseEntry(); | |
84 OperationStatus operationStatus = cursor.getSearchKey(dbEntryKey, foundValue, LockMode.DEFAULT); | |
85 while (operationStatus == OperationStatus.SUCCESS) { | |
86 byte[] foundValueBytes = foundValue.getData(); | |
87 String foundValueStr = new String(foundValueBytes, "utf-8"); | |
88 ExtElement e = ExtElement.parseXmlStr(foundValueStr); | |
89 retElements.add(e); | |
90 operationStatus = cursor.getNextDup(dbEntryKey, foundValue, LockMode.DEFAULT); | |
91 } | |
92 cursor.close(); | |
93 } catch (DatabaseException e) { | |
94 throw new ApplicationException(e); | |
95 } catch (UnsupportedEncodingException e) { | |
96 throw new ApplicationException(e); | |
97 } | |
98 return retElements; | |
99 } | |
100 | |
101 private void init() throws ApplicationException { | |
102 dbEnvExternalObjects = new DbEnvExternalObjects(); | |
103 dbEnvExternalObjects.setDataDir(DB_DIR_EXTERNAL_OBJECTS); | |
104 dbEnvExternalObjects.init(); | |
105 dbEnvExternalObjects.openDatabases(); | |
106 } | |
107 | |
108 public static void main(String[] args) throws ApplicationException { | |
109 getInstance(); | |
110 instance.beginOperation(); | |
111 System.out.print("Start ..."); | |
112 // instance.deleteSampleData(); | |
113 // instance.writeSampleData(); | |
114 instance.readSampleData(); | |
115 instance.end(); | |
116 instance.endOperation(); | |
117 Double elapsedTime = new Util().getSecondWithMillisecondsBetween(instance.beginOfOperation, instance.endOfOperation); | |
118 System.out.println("End."); | |
119 System.out.println("Needed time: " + elapsedTime + " seconds"); | |
120 } | |
121 | |
122 private void deleteSampleData() throws ApplicationException { | |
123 ExtElement e = new ExtElement(); | |
124 e.setUid("joe"); | |
125 e.setDocumentId("/archimedes/it/l223.xml"); | |
126 e.setPageNumber("17"); | |
127 deleteExternalElement(e); | |
128 } | |
129 | |
130 private void writeSampleData() throws ApplicationException { | |
131 Date now = new Date(); | |
132 | |
133 String sId = "1.2.2.2.2.5"; | |
134 ExtElement e = new ExtElement(); | |
135 e.setUid("joe"); | |
136 e.setModificationDate(now); | |
137 e.setDocumentId("/archimedes/it/l223.xml"); | |
138 e.setPageNumber("17"); | |
139 e.setXmlNodeId(sId); | |
140 e.setContent("<note>This is a test note to sentence " + sId + "</note>"); | |
141 writeExternalElement(e); | |
142 | |
143 ExtElement e2 = new ExtElement(); | |
144 String sId2 = "1.2.2.2.2.7"; | |
145 e2.setUid("michael"); | |
146 e2.setModificationDate(now); | |
147 e2.setDocumentId("/archimedes/it/l223.xml"); | |
148 e2.setPageNumber("17"); | |
149 e2.setXmlNodeId(sId2); | |
150 e2.setCharPos("18"); | |
151 e2.setContent("<note>This is a test note to sentence " + sId2 + "</note>"); | |
152 writeExternalElement(e2); | |
153 | |
154 /* | |
155 String sId3 = "1.2.2.2.2.8.15.3.3"; | |
156 e3.setUid("joe"); | |
157 e3.setModificationDate(now); | |
158 e3.setDocumentId("/archimedes/it/l223.xml"); | |
159 e3.setPageNumber("17"); | |
160 e3.setXmlNodeId(sId3); | |
161 e2.setContent("<note>This is an external test note to sentence " + sId3 + "</note>"); | |
162 writeExternalElement(e3); | |
163 */ | |
164 | |
165 } | |
166 | |
167 private void readSampleData() throws ApplicationException { | |
168 ArrayList<ExtElement> elements = readExternalElements("/archimedes/it/l223.xml", "17"); | |
169 System.out.println(elements); | |
170 } | |
171 | |
172 private void end() throws ApplicationException { | |
173 dbEnvExternalObjects.close(); | |
174 } | |
175 | |
176 private void beginOperation() { | |
177 beginOfOperation = new Date(); | |
178 } | |
179 | |
180 private void endOperation() { | |
181 endOfOperation = new Date(); | |
182 } | |
183 | |
184 } |