Mercurial > hg > mpdl-group
comparison software/mpdl-services/mpiwg-mpdl-lt/src/de/mpg/mpiwg/berlin/mpdl/lt/text/reg/DBRegularizationHandler.java @ 19:4a3641ae14d2
Erstellung
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 09 Nov 2011 15:32:05 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
18:dc5e9fcb3fdc | 19:4a3641ae14d2 |
---|---|
1 package de.mpg.mpiwg.berlin.mpdl.lt.text.reg; | |
2 | |
3 import java.io.UnsupportedEncodingException; | |
4 import java.util.ArrayList; | |
5 | |
6 import com.sleepycat.je.Cursor; | |
7 import com.sleepycat.je.Database; | |
8 import com.sleepycat.je.DatabaseEntry; | |
9 import com.sleepycat.je.DatabaseException; | |
10 import com.sleepycat.je.LockMode; | |
11 import com.sleepycat.je.OperationStatus; | |
12 | |
13 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; | |
14 import de.mpg.mpiwg.berlin.mpdl.lt.general.Language; | |
15 | |
16 public class DBRegularizationHandler { | |
17 private String dbDirectory; | |
18 private DbEnvRegularization regDbEnv; | |
19 | |
20 public DBRegularizationHandler(String dbDir) { | |
21 this.dbDirectory = dbDir; | |
22 } | |
23 | |
24 public void start() throws ApplicationException { | |
25 regDbEnv = new DbEnvRegularization(); | |
26 regDbEnv.setDataDir(dbDirectory); | |
27 regDbEnv.init(); // open databases in read/write mode | |
28 } | |
29 | |
30 public void openDatabases() throws ApplicationException { | |
31 regDbEnv.openDatabases(); | |
32 } | |
33 | |
34 public void closeDatabases() throws ApplicationException { | |
35 regDbEnv.close(); | |
36 } | |
37 | |
38 public void deleteData() throws ApplicationException { | |
39 regDbEnv.removeDatabases(); | |
40 } | |
41 | |
42 public void writeOrigReg(Regularization reg) throws ApplicationException { | |
43 try { | |
44 String language = Language.getInstance().getLanguageId(reg.getLanguage()); | |
45 String keyStr = language + "###" + reg.getOrig(); | |
46 String valueStr = reg.getXmlString(); | |
47 DatabaseEntry dbEntryKey = new DatabaseEntry(keyStr.getBytes("utf-8")); | |
48 DatabaseEntry dbEntryValue = new DatabaseEntry(valueStr.getBytes("utf-8")); | |
49 Database origDB = regDbEnv.getOrigDB(); | |
50 origDB.put(null, dbEntryKey, dbEntryValue); | |
51 } catch (DatabaseException e) { | |
52 throw new ApplicationException(e); | |
53 } catch (UnsupportedEncodingException e) { | |
54 throw new ApplicationException(e); | |
55 } | |
56 } | |
57 | |
58 public void writeNormReg(Regularization reg) throws ApplicationException { | |
59 try { | |
60 String language = Language.getInstance().getLanguageId(reg.getLanguage()); | |
61 String keyStr = language + "###" + reg.getNorm(); | |
62 String valueStr = reg.getXmlString(); | |
63 DatabaseEntry dbEntryKey = new DatabaseEntry(keyStr.getBytes("utf-8")); | |
64 DatabaseEntry dbEntryValue = new DatabaseEntry(valueStr.getBytes("utf-8")); | |
65 Database normDB = regDbEnv.getNormDB(); | |
66 normDB.put(null, dbEntryKey, dbEntryValue); | |
67 } catch (DatabaseException e) { | |
68 throw new ApplicationException(e); | |
69 } catch (UnsupportedEncodingException e) { | |
70 throw new ApplicationException(e); | |
71 } | |
72 } | |
73 | |
74 public void deleteReg(Regularization reg) throws ApplicationException { | |
75 try { | |
76 String language = Language.getInstance().getLanguageId(reg.getLanguage()); | |
77 String keyStrOrig = language + "###" + reg.getOrig(); | |
78 DatabaseEntry dbEntryKey = new DatabaseEntry(keyStrOrig.getBytes("utf-8")); | |
79 Database origDB = regDbEnv.getOrigDB(); | |
80 origDB.delete(null, dbEntryKey); | |
81 String keyStrNorm = reg.getLanguage() + "###" + reg.getNorm(); | |
82 dbEntryKey = new DatabaseEntry(keyStrNorm.getBytes("utf-8")); | |
83 Database normDB = regDbEnv.getNormDB(); | |
84 normDB.delete(null, dbEntryKey); | |
85 } catch (DatabaseException e) { | |
86 throw new ApplicationException(e); | |
87 } catch (UnsupportedEncodingException e) { | |
88 throw new ApplicationException(e); | |
89 } | |
90 } | |
91 | |
92 public ArrayList<Regularization> readRegsByOrig(String lang, String orig) throws ApplicationException { | |
93 String language = Language.getInstance().getLanguageId(lang); | |
94 ArrayList<Regularization> retRegs = new ArrayList<Regularization>(); | |
95 String hashKey = language + "###" + orig; | |
96 try { | |
97 Database origDB = regDbEnv.getOrigDB(); | |
98 Cursor cursor = origDB.openCursor(null, null); | |
99 byte[] bHashKey = hashKey.getBytes("utf-8"); | |
100 DatabaseEntry dbEntryKey = new DatabaseEntry(bHashKey); | |
101 DatabaseEntry foundValue = new DatabaseEntry(); | |
102 OperationStatus operationStatus = cursor.getSearchKey(dbEntryKey, foundValue, LockMode.DEFAULT); | |
103 while (operationStatus == OperationStatus.SUCCESS) { | |
104 byte[] foundValueBytes = foundValue.getData(); | |
105 String foundValueStr = new String(foundValueBytes, "utf-8"); | |
106 Regularization reg = Regularization.getInstance(foundValueStr); | |
107 retRegs.add(reg); | |
108 operationStatus = cursor.getNextDup(dbEntryKey, foundValue, LockMode.DEFAULT); | |
109 } | |
110 cursor.close(); | |
111 } catch (DatabaseException e) { | |
112 throw new ApplicationException(e); | |
113 } catch (UnsupportedEncodingException e) { | |
114 throw new ApplicationException(e); | |
115 } | |
116 return retRegs; | |
117 } | |
118 | |
119 public ArrayList<Regularization> readRegsByNorm(String lang, String norm) throws ApplicationException { | |
120 String language = Language.getInstance().getLanguageId(lang); | |
121 ArrayList<Regularization> retRegs = new ArrayList<Regularization>(); | |
122 String hashKey = language + "###" + norm; | |
123 try { | |
124 Database normDB = regDbEnv.getNormDB(); | |
125 Cursor cursor = normDB.openCursor(null, null); | |
126 byte[] bHashKey = hashKey.getBytes("utf-8"); | |
127 DatabaseEntry dbEntryKey = new DatabaseEntry(bHashKey); | |
128 DatabaseEntry foundValue = new DatabaseEntry(); | |
129 OperationStatus operationStatus = cursor.getSearchKey(dbEntryKey, foundValue, LockMode.DEFAULT); | |
130 while (operationStatus == OperationStatus.SUCCESS) { | |
131 byte[] foundValueBytes = foundValue.getData(); | |
132 String foundValueStr = new String(foundValueBytes, "utf-8"); | |
133 Regularization reg = Regularization.getInstance(foundValueStr); | |
134 retRegs.add(reg); | |
135 operationStatus = cursor.getNextDup(dbEntryKey, foundValue, LockMode.DEFAULT); | |
136 } | |
137 cursor.close(); | |
138 } catch (DatabaseException e) { | |
139 throw new ApplicationException(e); | |
140 } catch (UnsupportedEncodingException e) { | |
141 throw new ApplicationException(e); | |
142 } | |
143 return retRegs; | |
144 } | |
145 | |
146 } |