Revision 388
Added by Matt Jones over 23 years ago
DBReader.java | ||
---|---|---|
14 | 14 |
|
15 | 15 |
package edu.ucsb.nceas.metacat; |
16 | 16 |
|
17 |
import java.io.*; |
|
18 | 17 |
import java.sql.*; |
18 |
import java.io.PrintWriter; |
|
19 |
import java.util.TreeSet; |
|
19 | 20 |
|
20 | 21 |
/** |
21 | 22 |
* A Class that creates an XML text document |
... | ... | |
75 | 76 |
} |
76 | 77 |
|
77 | 78 |
/** |
78 |
* Get the root node id for an XML document given a document id
|
|
79 |
* Create an XML document from the database for the document with ID docid
|
|
79 | 80 |
* |
80 |
* @param docid the document node contains the root of the document |
|
81 |
* @returns long the nodeid of the root node for this document |
|
81 |
* @param docid the document identifier that we want retrieved |
|
82 | 82 |
*/ |
83 |
private long getRootNode(String docid) |
|
84 |
throws McdbDocNotFoundException |
|
85 |
{ |
|
86 |
// Now look up the root node id |
|
87 |
long rootnodeid = 0; |
|
88 |
|
|
89 |
try { |
|
90 |
PreparedStatement pstmt = |
|
91 |
conn.prepareStatement("SELECT rootnodeid " + |
|
92 |
"FROM xml_documents " + |
|
93 |
"WHERE docid = ?"); |
|
94 |
// Bind the values to the query |
|
95 |
pstmt.setString(1, docid); |
|
96 |
|
|
97 |
pstmt.execute(); |
|
98 |
ResultSet rs = pstmt.getResultSet(); |
|
99 |
boolean tableHasRows = rs.next(); |
|
100 |
if (tableHasRows) { |
|
101 |
rootnodeid = rs.getLong(1); |
|
102 |
|
|
103 |
} |
|
104 |
pstmt.close(); |
|
105 |
} catch (SQLException e) { |
|
106 |
throw new McdbDocNotFoundException( |
|
107 |
"Root node not found for: " + docid, e); |
|
108 |
} |
|
109 |
|
|
110 |
return rootnodeid; |
|
111 |
} |
|
112 |
|
|
113 |
/** |
|
114 |
* Create an XML document from the database starting with the element |
|
115 |
* having element_id nodeid |
|
116 |
* |
|
117 |
* @param docid the document that we want retrieved |
|
118 |
*/ |
|
119 | 83 |
public String readXMLDocument(String docid) |
120 | 84 |
throws McdbException |
121 | 85 |
{ |
122 | 86 |
try { |
123 | 87 |
StringBuffer doc = new StringBuffer(); |
88 |
|
|
89 |
// Look up the document information |
|
124 | 90 |
DoctypeInfo dti = getDoctypeInfo(docid); |
125 |
|
|
126 | 91 |
String docname = dti.getDocname(); |
127 | 92 |
String doctype = dti.getDoctype(); |
128 | 93 |
String sysid = dti.getSystemID(); |
94 |
long rootnodeid = dti.getRootNodeID(); |
|
129 | 95 |
|
130 |
ElementNode element = new ElementNode(conn, getRootNode(docid)); |
|
96 |
// Download all of the document nodes using a single SQL query |
|
97 |
TreeSet nodeRecordList = getNodeRecordList(docid); |
|
98 |
|
|
99 |
// Create the elements from the downloaded nodes |
|
100 |
//ElementNode element = new ElementNode(conn, rootnodeid); |
|
101 |
ElementNode element = new ElementNode(nodeRecordList, rootnodeid); |
|
102 |
|
|
103 |
// Append the resulting document to the StringBuffer |
|
131 | 104 |
doc.append("<?xml version=\"1.0\"?>\n"); |
132 | 105 |
|
133 | 106 |
if (docname != null) { |
... | ... | |
155 | 128 |
* @param docid the id of the document to look up |
156 | 129 |
*/ |
157 | 130 |
public DoctypeInfo getDoctypeInfo(String docid) |
158 |
throws McdbException |
|
131 |
throws McdbException
|
|
159 | 132 |
{ |
160 | 133 |
PreparedStatement pstmt; |
161 | 134 |
String doctype = null; |
162 | 135 |
String docname = null; |
163 | 136 |
String sysid = null; |
137 |
long rootnodeid = 0; |
|
164 | 138 |
DoctypeInfo dti = null; |
165 | 139 |
|
166 | 140 |
try { |
167 | 141 |
pstmt = |
168 |
conn.prepareStatement("SELECT docname,doctype " + |
|
142 |
conn.prepareStatement("SELECT docname,doctype,rootnodeid " +
|
|
169 | 143 |
"FROM xml_documents " + |
170 | 144 |
"WHERE docid = ?"); |
171 | 145 |
// Bind the values to the query |
... | ... | |
175 | 149 |
ResultSet rs = pstmt.getResultSet(); |
176 | 150 |
boolean tableHasRows = rs.next(); |
177 | 151 |
if (tableHasRows) { |
178 |
docname = rs.getString(1); |
|
179 |
doctype = rs.getString(2); |
|
152 |
docname = rs.getString(1); |
|
153 |
doctype = rs.getString(2); |
|
154 |
rootnodeid = rs.getLong(3); |
|
180 | 155 |
} |
181 | 156 |
pstmt.close(); |
182 | 157 |
|
... | ... | |
201 | 176 |
} |
202 | 177 |
|
203 | 178 |
if (docname != null) { |
204 |
dti = new DoctypeInfo(docname, doctype, sysid); |
|
179 |
dti = new DoctypeInfo(docname, doctype, sysid, rootnodeid);
|
|
205 | 180 |
return dti; |
206 | 181 |
} else { |
207 | 182 |
throw new McdbDocNotFoundException("Document not found: " + docid); |
... | ... | |
209 | 184 |
} |
210 | 185 |
|
211 | 186 |
/** |
187 |
* Look up the node data from the database |
|
188 |
* |
|
189 |
* @param docid the id of the document to look up |
|
190 |
*/ |
|
191 |
public TreeSet getNodeRecordList(String docid) |
|
192 |
throws McdbException |
|
193 |
{ |
|
194 |
PreparedStatement pstmt; |
|
195 |
TreeSet nodeRecordList = new TreeSet(new NodeComparator()); |
|
196 |
long nodeid = 0; |
|
197 |
long parentnodeid = 0; |
|
198 |
long nodeindex = 0; |
|
199 |
String nodetype = null; |
|
200 |
String nodename = null; |
|
201 |
String nodedata = null; |
|
202 |
|
|
203 |
try { |
|
204 |
pstmt = |
|
205 |
conn.prepareStatement("SELECT nodeid,parentnodeid,nodeindex, " + |
|
206 |
"nodetype,nodename,"+ |
|
207 |
"replace(" + |
|
208 |
"replace(" + |
|
209 |
"replace(nodedata,'&','&') " + |
|
210 |
",'<','<') " + |
|
211 |
",'>','>') " + |
|
212 |
"FROM xml_nodes WHERE docid = ?"); |
|
213 |
|
|
214 |
// Bind the values to the query |
|
215 |
pstmt.setString(1, docid); |
|
216 |
|
|
217 |
pstmt.execute(); |
|
218 |
ResultSet rs = pstmt.getResultSet(); |
|
219 |
boolean tableHasRows = rs.next(); |
|
220 |
while (tableHasRows) { |
|
221 |
nodeid = rs.getLong(1); |
|
222 |
parentnodeid = rs.getLong(2); |
|
223 |
nodeindex = rs.getLong(3); |
|
224 |
nodetype = rs.getString(4); |
|
225 |
nodename = rs.getString(5); |
|
226 |
nodedata = rs.getString(6); |
|
227 |
|
|
228 |
// add the data to the node record list hashtable |
|
229 |
NodeRecord currentRecord = new NodeRecord(nodeid, parentnodeid, |
|
230 |
nodeindex, nodetype, nodename, nodedata); |
|
231 |
nodeRecordList.add(currentRecord); |
|
232 |
|
|
233 |
// Advance to the next node |
|
234 |
tableHasRows = rs.next(); |
|
235 |
} |
|
236 |
pstmt.close(); |
|
237 |
|
|
238 |
} catch (SQLException e) { |
|
239 |
throw new McdbException("Error accessing database connection.", e); |
|
240 |
} |
|
241 |
|
|
242 |
if (nodeRecordList != null) { |
|
243 |
return nodeRecordList; |
|
244 |
} else { |
|
245 |
throw new McdbException("Error getting node data: " + docid); |
|
246 |
} |
|
247 |
} |
|
248 |
|
|
249 |
/** |
|
212 | 250 |
* A utility class that encapsulates document type information |
213 | 251 |
*/ |
214 | 252 |
public class DoctypeInfo { |
215 | 253 |
private String docname = null; |
216 | 254 |
private String doctype = null; |
217 | 255 |
private String system_id = null; |
256 |
private long rootnodeid; |
|
218 | 257 |
|
219 | 258 |
/** |
220 | 259 |
* Constructor |
221 | 260 |
*/ |
222 |
public DoctypeInfo(String docname, String doctype, String system_id) { |
|
261 |
public DoctypeInfo(String docname, String doctype, |
|
262 |
String system_id, long rootnodeid) { |
|
223 | 263 |
this.docname = docname; |
224 | 264 |
this.doctype = doctype; |
225 | 265 |
this.system_id = system_id; |
266 |
this.rootnodeid = rootnodeid; |
|
226 | 267 |
} |
227 | 268 |
|
228 | 269 |
/** |
... | ... | |
245 | 286 |
public String getSystemID() { |
246 | 287 |
return system_id; |
247 | 288 |
} |
289 |
|
|
290 |
/** |
|
291 |
* get the root node identifier |
|
292 |
*/ |
|
293 |
public long getRootNodeID() { |
|
294 |
return rootnodeid; |
|
295 |
} |
|
248 | 296 |
} |
249 |
|
|
250 | 297 |
} |
251 |
|
|
252 |
/** |
|
253 |
* '$Log$ |
|
254 |
* 'Revision 1.19 2000/08/11 22:20:04 jones |
|
255 |
* 'Changed exception handling mechanisms for DBReader |
|
256 |
* ' |
|
257 |
* 'Revision 1.18 2000/08/10 22:39:04 jones |
|
258 |
* 'changed getRootNode method from public to private |
|
259 |
* ' |
|
260 |
* 'Revision 1.17 2000/06/26 10:35:05 jones |
|
261 |
* 'Merged in substantial changes to DBWriter and associated classes and to |
|
262 |
* 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE |
|
263 |
* 'functions. The command line tools and the parameters for the |
|
264 |
* 'servlet have changed substantially. |
|
265 |
* ' |
|
266 |
* 'Revision 1.16.2.2 2000/06/25 23:38:16 jones |
|
267 |
* 'Added RCSfile keyword |
|
268 |
* ' |
|
269 |
* 'Revision 1.16.2.1 2000/06/25 23:34:17 jones |
|
270 |
* 'Changed documentation formatting, added log entries at bottom of source files |
|
271 |
* '' |
|
272 |
*/ |
Also available in: Unified diff
Modified the DBReader and ElementNode classes to more efficiently read
documents from the database. In the old implementation, a db connection
was opened for each and every node in a document, recursively fromthe root
node. In the new implementation, all of the data from xml_nodes is
retrieved from the database with a single query and stored in a
sorted list of NodeRecord, and the node tree is build from this data structure
recursively. This should speed up the document retieval substantially,
especially as the document repository grows.