1 |
2732
|
sgarg
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Purpose: A Class that tracks sessions for MetaCatServlet users.
|
4 |
|
|
* Copyright: 2000 Regents of the University of California and the
|
5 |
|
|
* National Center for Ecological Analysis and Synthesis
|
6 |
|
|
* Authors: Matt Jones
|
7 |
|
|
*
|
8 |
|
|
* '$Author$'
|
9 |
|
|
* '$Date$'
|
10 |
|
|
* '$Revision$'
|
11 |
|
|
*
|
12 |
|
|
* This program is free software; you can redistribute it and/or modify
|
13 |
|
|
* it under the terms of the GNU General Public License as published by
|
14 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
15 |
|
|
* (at your option) any later version.
|
16 |
|
|
*
|
17 |
|
|
* This program is distributed in the hope that it will be useful,
|
18 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20 |
|
|
* GNU General Public License for more details.
|
21 |
|
|
*
|
22 |
|
|
* You should have received a copy of the GNU General Public License
|
23 |
|
|
* along with this program; if not, write to the Free Software
|
24 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25 |
|
|
*/
|
26 |
|
|
|
27 |
|
|
package edu.ucsb.nceas.metacat;
|
28 |
|
|
|
29 |
|
|
import java.sql.PreparedStatement;
|
30 |
|
|
import java.sql.ResultSet;
|
31 |
|
|
import java.sql.SQLException;
|
32 |
|
|
import java.util.Vector;
|
33 |
2733
|
sgarg
|
import java.util.HashMap;
|
34 |
|
|
import java.lang.Comparable;
|
35 |
4080
|
daigle
|
|
36 |
8164
|
tao
|
import edu.ucsb.nceas.metacat.common.query.EnabledQueryEngines;
|
37 |
5015
|
daigle
|
import edu.ucsb.nceas.metacat.database.DBConnection;
|
38 |
|
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
39 |
5030
|
daigle
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
40 |
4080
|
daigle
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
41 |
|
|
|
42 |
2732
|
sgarg
|
import org.apache.log4j.Logger;
|
43 |
|
|
|
44 |
|
|
public class IndexingQueue {
|
45 |
|
|
|
46 |
7432
|
leinfelder
|
private static Logger logMetacat = Logger.getLogger(IndexingQueue.class);
|
47 |
2733
|
sgarg
|
// Map used to keep tracks of docids to be indexed
|
48 |
7432
|
leinfelder
|
private HashMap<String, IndexingQueueObject> indexingMap = new HashMap<String, IndexingQueueObject>();
|
49 |
|
|
private Vector<IndexingTask> currentThreads = new Vector<IndexingTask>();
|
50 |
|
|
public Vector<String> currentDocidsBeingIndexed = new Vector<String>();
|
51 |
2901
|
sgarg
|
private boolean metacatRunning = true;
|
52 |
2732
|
sgarg
|
|
53 |
|
|
private static IndexingQueue instance = null;
|
54 |
|
|
|
55 |
4080
|
daigle
|
final static int NUMBEROFINDEXINGTHREADS;
|
56 |
|
|
static {
|
57 |
|
|
int numIndexingThreads = 0;
|
58 |
|
|
try {
|
59 |
|
|
numIndexingThreads =
|
60 |
|
|
Integer.parseInt(PropertyService.getProperty("database.numberOfIndexingThreads"));
|
61 |
|
|
} catch (PropertyNotFoundException pnfe) {
|
62 |
7432
|
leinfelder
|
logMetacat.error("Could not get property in static block: "
|
63 |
4080
|
daigle
|
+ pnfe.getMessage());
|
64 |
|
|
}
|
65 |
|
|
NUMBEROFINDEXINGTHREADS = numIndexingThreads;
|
66 |
|
|
}
|
67 |
|
|
|
68 |
3199
|
tao
|
private IndexingQueue() {
|
69 |
8164
|
tao
|
if(!EnabledQueryEngines.getInstance().isEnabled(EnabledQueryEngines.PATHQUERYENGINE)) {
|
70 |
|
|
return;
|
71 |
|
|
}
|
72 |
2732
|
sgarg
|
for (int i = 0; i < NUMBEROFINDEXINGTHREADS; i++) {
|
73 |
7432
|
leinfelder
|
IndexingTask thread = new IndexingTask();
|
74 |
|
|
thread.start();
|
75 |
|
|
currentThreads.add(thread);
|
76 |
2732
|
sgarg
|
}
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
public static synchronized IndexingQueue getInstance(){
|
80 |
|
|
if (instance == null) {
|
81 |
|
|
instance = new IndexingQueue();
|
82 |
|
|
}
|
83 |
|
|
return instance;
|
84 |
|
|
}//getInstance
|
85 |
|
|
|
86 |
2733
|
sgarg
|
public void add(String docid, String rev) {
|
87 |
8164
|
tao
|
if(!EnabledQueryEngines.getInstance().isEnabled(EnabledQueryEngines.PATHQUERYENGINE)) {
|
88 |
|
|
return;
|
89 |
|
|
}
|
90 |
2733
|
sgarg
|
add(new IndexingQueueObject(docid, rev, 0));
|
91 |
|
|
}
|
92 |
2732
|
sgarg
|
|
93 |
2733
|
sgarg
|
protected void add(IndexingQueueObject queueObject) {
|
94 |
|
|
synchronized (indexingMap) {
|
95 |
|
|
if(!indexingMap.containsKey(queueObject.getDocid())){
|
96 |
|
|
indexingMap.put(queueObject.getDocid(), queueObject);
|
97 |
|
|
indexingMap.notify();
|
98 |
|
|
} else {
|
99 |
7432
|
leinfelder
|
IndexingQueueObject oldQueueObject = indexingMap.get(queueObject.getDocid());
|
100 |
2733
|
sgarg
|
if(oldQueueObject.compareTo(queueObject) < 0){
|
101 |
|
|
indexingMap.put(queueObject.getDocid(), queueObject);
|
102 |
|
|
indexingMap.notify();
|
103 |
|
|
}
|
104 |
|
|
}
|
105 |
2732
|
sgarg
|
}
|
106 |
|
|
}
|
107 |
|
|
|
108 |
2901
|
sgarg
|
public boolean getMetacatRunning(){
|
109 |
|
|
return this.metacatRunning;
|
110 |
|
|
}
|
111 |
2732
|
sgarg
|
|
112 |
2901
|
sgarg
|
public void setMetacatRunning(boolean metacatRunning){
|
113 |
8164
|
tao
|
if(!EnabledQueryEngines.getInstance().isEnabled(EnabledQueryEngines.PATHQUERYENGINE)) {
|
114 |
|
|
return;
|
115 |
|
|
}
|
116 |
2901
|
sgarg
|
this.metacatRunning = metacatRunning;
|
117 |
|
|
|
118 |
2902
|
sgarg
|
if(!metacatRunning){
|
119 |
|
|
for(int count=0; count<currentThreads.size(); count++){
|
120 |
7432
|
leinfelder
|
currentThreads.get(count).metacatRunning = false;
|
121 |
|
|
currentThreads.get(count).interrupt();
|
122 |
2902
|
sgarg
|
}
|
123 |
2901
|
sgarg
|
}
|
124 |
|
|
}
|
125 |
|
|
|
126 |
2732
|
sgarg
|
protected IndexingQueueObject getNext() {
|
127 |
|
|
IndexingQueueObject returnVal = null;
|
128 |
2733
|
sgarg
|
synchronized (indexingMap) {
|
129 |
2901
|
sgarg
|
while (indexingMap.isEmpty() && metacatRunning) {
|
130 |
2732
|
sgarg
|
try {
|
131 |
2733
|
sgarg
|
indexingMap.wait();
|
132 |
2732
|
sgarg
|
} catch (InterruptedException ex) {
|
133 |
7433
|
leinfelder
|
logMetacat.error("Interrupted");
|
134 |
2732
|
sgarg
|
}
|
135 |
|
|
}
|
136 |
2901
|
sgarg
|
|
137 |
|
|
if(metacatRunning){
|
138 |
7432
|
leinfelder
|
String docid = indexingMap.keySet().iterator().next();
|
139 |
|
|
returnVal = indexingMap.get(docid);
|
140 |
2901
|
sgarg
|
indexingMap.remove(docid);
|
141 |
|
|
}
|
142 |
2732
|
sgarg
|
}
|
143 |
|
|
return returnVal;
|
144 |
|
|
}
|
145 |
7433
|
leinfelder
|
|
146 |
|
|
/**
|
147 |
|
|
* Removes the Indexing Task object from the queue
|
148 |
|
|
* for the given docid. Currently, rev is ignored
|
149 |
|
|
* This method should be used to cancel scheduled indexing on a document
|
150 |
|
|
* (typically if it is being deleted but indexing has not completed yet)
|
151 |
|
|
* see http://bugzilla.ecoinformatics.org/show_bug.cgi?id=5750
|
152 |
|
|
* @param docid the docid (without revision)
|
153 |
|
|
* @param rev the docid's rev (ignored)
|
154 |
|
|
*/
|
155 |
|
|
public void remove(String docid, String rev) {
|
156 |
8164
|
tao
|
if(!EnabledQueryEngines.getInstance().isEnabled(EnabledQueryEngines.PATHQUERYENGINE)) {
|
157 |
|
|
return;
|
158 |
|
|
}
|
159 |
7433
|
leinfelder
|
synchronized (indexingMap) {
|
160 |
|
|
if (indexingMap.containsKey(docid)) {
|
161 |
7434
|
leinfelder
|
logMetacat.debug("Removing indexing queue task for docid: " + docid);
|
162 |
7433
|
leinfelder
|
indexingMap.remove(docid);
|
163 |
|
|
}
|
164 |
|
|
}
|
165 |
|
|
}
|
166 |
2732
|
sgarg
|
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
class IndexingTask extends Thread {
|
170 |
|
|
private Logger logMetacat = Logger.getLogger(IndexingTask.class);
|
171 |
4080
|
daigle
|
|
172 |
|
|
|
173 |
|
|
protected final static long MAXIMUMINDEXDELAY;
|
174 |
|
|
static {
|
175 |
|
|
long maxIndexDelay = 0;
|
176 |
|
|
try {
|
177 |
|
|
maxIndexDelay =
|
178 |
|
|
Integer.parseInt(PropertyService.getProperty("database.maximumIndexDelay"));
|
179 |
|
|
} catch (PropertyNotFoundException pnfe) {
|
180 |
|
|
System.err.println("Could not get property in static block: "
|
181 |
|
|
+ pnfe.getMessage());
|
182 |
|
|
}
|
183 |
|
|
MAXIMUMINDEXDELAY = maxIndexDelay;
|
184 |
|
|
}
|
185 |
|
|
|
186 |
2902
|
sgarg
|
protected boolean metacatRunning = true;
|
187 |
|
|
|
188 |
2732
|
sgarg
|
public void run() {
|
189 |
2902
|
sgarg
|
while (metacatRunning) {
|
190 |
2732
|
sgarg
|
// blocks until job
|
191 |
7433
|
leinfelder
|
IndexingQueueObject indexQueueObject =
|
192 |
2732
|
sgarg
|
IndexingQueue.getInstance().getNext();
|
193 |
2733
|
sgarg
|
|
194 |
7433
|
leinfelder
|
if(indexQueueObject != null){
|
195 |
2901
|
sgarg
|
if(!IndexingQueue.getInstance().
|
196 |
7433
|
leinfelder
|
currentDocidsBeingIndexed.contains(indexQueueObject.getDocid())){
|
197 |
2733
|
sgarg
|
try {
|
198 |
|
|
IndexingQueue.getInstance().
|
199 |
7433
|
leinfelder
|
currentDocidsBeingIndexed.add(indexQueueObject.getDocid());
|
200 |
|
|
String docid = indexQueueObject.getDocid() + "." + indexQueueObject.getRev();
|
201 |
2764
|
sgarg
|
if(checkDocumentTable(docid, "xml_documents")){
|
202 |
|
|
logMetacat.warn("Calling buildIndex for " + docid);
|
203 |
|
|
DocumentImpl doc = new DocumentImpl(docid, false);
|
204 |
|
|
doc.buildIndex();
|
205 |
3199
|
tao
|
logMetacat.warn("finish building index for doicd "+docid);
|
206 |
2764
|
sgarg
|
} else {
|
207 |
|
|
logMetacat.warn("Couldn't find the docid:" + docid
|
208 |
|
|
+ " in xml_documents table");
|
209 |
|
|
sleep(MAXIMUMINDEXDELAY);
|
210 |
|
|
throw(new Exception("Couldn't find the docid:" + docid
|
211 |
|
|
+ " in xml_documents table"));
|
212 |
|
|
}
|
213 |
2901
|
sgarg
|
} catch (Exception e) {
|
214 |
2733
|
sgarg
|
logMetacat.warn("Exception: " + e);
|
215 |
|
|
e.printStackTrace();
|
216 |
2732
|
sgarg
|
|
217 |
7433
|
leinfelder
|
if(indexQueueObject.getCount() < 25){
|
218 |
|
|
indexQueueObject.setCount(indexQueueObject.getCount()+1);
|
219 |
2733
|
sgarg
|
// add the docid back to the list
|
220 |
7433
|
leinfelder
|
IndexingQueue.getInstance().add(indexQueueObject);
|
221 |
2733
|
sgarg
|
} else {
|
222 |
7433
|
leinfelder
|
logMetacat.fatal("Docid " + indexQueueObject.getDocid()
|
223 |
2732
|
sgarg
|
+ " has been inserted to IndexingQueue "
|
224 |
2733
|
sgarg
|
+ "more than 25 times. Not adding the docid to"
|
225 |
|
|
+ " the queue again.");
|
226 |
|
|
}
|
227 |
2901
|
sgarg
|
} finally {
|
228 |
|
|
IndexingQueue.getInstance().currentDocidsBeingIndexed
|
229 |
7433
|
leinfelder
|
.remove(indexQueueObject.getDocid());
|
230 |
2901
|
sgarg
|
}
|
231 |
|
|
} else {
|
232 |
7433
|
leinfelder
|
indexQueueObject.setCount(indexQueueObject.getCount()+1);
|
233 |
|
|
IndexingQueue.getInstance().add(indexQueueObject);
|
234 |
2901
|
sgarg
|
}
|
235 |
|
|
}
|
236 |
2732
|
sgarg
|
}
|
237 |
|
|
}
|
238 |
|
|
|
239 |
2764
|
sgarg
|
private boolean checkDocumentTable(String docid, String tablename) throws Exception{
|
240 |
2732
|
sgarg
|
DBConnection dbConn = null;
|
241 |
|
|
int serialNumber = -1;
|
242 |
2764
|
sgarg
|
boolean inxmldoc = false;
|
243 |
|
|
|
244 |
2732
|
sgarg
|
String revision = docid.substring(docid.lastIndexOf(".")+1,docid.length());
|
245 |
6595
|
leinfelder
|
int rev = Integer.parseInt(revision);
|
246 |
2732
|
sgarg
|
docid = docid.substring(0,docid.lastIndexOf("."));
|
247 |
|
|
|
248 |
4743
|
walbridge
|
logMetacat.info("Checking if document exists in xml_documents: docid is "
|
249 |
2764
|
sgarg
|
+ docid + " and revision is " + revision);
|
250 |
2732
|
sgarg
|
|
251 |
|
|
try {
|
252 |
|
|
// Opening separate db connection for writing XML Index
|
253 |
|
|
dbConn = DBConnectionPool
|
254 |
|
|
.getDBConnection("DBSAXHandler.checkDocumentTable");
|
255 |
|
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
256 |
|
|
|
257 |
2764
|
sgarg
|
String xmlDocumentsCheck = "SELECT distinct docid FROM " + tablename
|
258 |
6595
|
leinfelder
|
+ " WHERE docid = ? "
|
259 |
|
|
+ " AND rev = ?";
|
260 |
2732
|
sgarg
|
|
261 |
2764
|
sgarg
|
PreparedStatement xmlDocCheck = dbConn.prepareStatement(xmlDocumentsCheck);
|
262 |
6595
|
leinfelder
|
xmlDocCheck.setString(1, docid);
|
263 |
|
|
xmlDocCheck.setInt(2, rev);
|
264 |
2764
|
sgarg
|
// Increase usage count
|
265 |
|
|
dbConn.increaseUsageCount(1);
|
266 |
|
|
xmlDocCheck.execute();
|
267 |
|
|
ResultSet doccheckRS = xmlDocCheck.getResultSet();
|
268 |
|
|
boolean tableHasRows = doccheckRS.next();
|
269 |
|
|
if (tableHasRows) {
|
270 |
|
|
inxmldoc = true;
|
271 |
|
|
}
|
272 |
|
|
doccheckRS.close();
|
273 |
|
|
xmlDocCheck.close();
|
274 |
2732
|
sgarg
|
} catch (SQLException e) {
|
275 |
|
|
e.printStackTrace();
|
276 |
|
|
} finally {
|
277 |
|
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
278 |
|
|
}//finally
|
279 |
2764
|
sgarg
|
|
280 |
|
|
return inxmldoc;
|
281 |
|
|
}
|
282 |
2732
|
sgarg
|
}
|
283 |
|
|
|
284 |
2733
|
sgarg
|
class IndexingQueueObject implements Comparable{
|
285 |
2732
|
sgarg
|
// the docid of the document to be indexed.
|
286 |
|
|
private String docid;
|
287 |
2733
|
sgarg
|
// the docid of the document to be indexed.
|
288 |
|
|
private String rev;
|
289 |
2732
|
sgarg
|
// the count of number of times the document has been in the queue
|
290 |
|
|
private int count;
|
291 |
|
|
|
292 |
2733
|
sgarg
|
IndexingQueueObject(String docid, String rev, int count){
|
293 |
|
|
this.docid = docid;
|
294 |
|
|
this.rev = rev;
|
295 |
2732
|
sgarg
|
this.count = count;
|
296 |
|
|
}
|
297 |
|
|
|
298 |
|
|
public int getCount(){
|
299 |
|
|
return count;
|
300 |
|
|
}
|
301 |
|
|
|
302 |
|
|
public String getDocid(){
|
303 |
|
|
return docid;
|
304 |
|
|
}
|
305 |
|
|
|
306 |
2733
|
sgarg
|
public String getRev(){
|
307 |
|
|
return rev;
|
308 |
|
|
}
|
309 |
|
|
|
310 |
2732
|
sgarg
|
public void setCount(int count){
|
311 |
|
|
this.count = count;
|
312 |
|
|
}
|
313 |
|
|
|
314 |
|
|
public void setDocid(String docid){
|
315 |
|
|
this.docid = docid;
|
316 |
|
|
}
|
317 |
2733
|
sgarg
|
|
318 |
|
|
public void setRev(String rev){
|
319 |
|
|
this.rev = rev;
|
320 |
|
|
}
|
321 |
|
|
|
322 |
|
|
public int compareTo(Object o){
|
323 |
|
|
if(o instanceof IndexingQueueObject){
|
324 |
|
|
int revision = Integer.parseInt(rev);
|
325 |
|
|
int oRevision = Integer.parseInt(((IndexingQueueObject)o).getRev());
|
326 |
|
|
|
327 |
|
|
if(revision == oRevision) {
|
328 |
|
|
return 0;
|
329 |
|
|
} else if (revision > oRevision) {
|
330 |
|
|
return 1;
|
331 |
|
|
} else {
|
332 |
|
|
return -1;
|
333 |
|
|
}
|
334 |
|
|
} else {
|
335 |
|
|
throw new java.lang.ClassCastException();
|
336 |
|
|
}
|
337 |
|
|
}
|
338 |
3077
|
jones
|
}
|