Project

General

Profile

1
/**
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
 *    Release: @release@
8
 *
9
 *   '$Author: sgarg $'
10
 *     '$Date: 2005-11-18 13:17:18 -0800 (Fri, 18 Nov 2005) $'
11
 * '$Revision: 2764 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

    
28
package edu.ucsb.nceas.metacat;
29

    
30
import java.sql.PreparedStatement;
31
import java.sql.ResultSet;
32
import java.sql.SQLException;
33
import java.util.Vector;
34
import java.util.HashMap;
35
import java.lang.Comparable;
36
import edu.ucsb.nceas.metacat.MetaCatUtil;
37
import org.apache.log4j.Logger;
38

    
39
public class IndexingQueue {
40

    
41
	private Logger logMetacat = Logger.getLogger(IndexingQueue.class);
42
	//	 Map used to keep tracks of docids to be indexed
43
	private HashMap indexingMap = new HashMap();     
44
	private Vector currentThreads = new Vector();
45
	public Vector currentDocidsBeingIndexed = new Vector();
46
	
47
	private static IndexingQueue instance = null;
48

    
49
	final static int NUMBEROFINDEXINGTHREADS = 
50
		Integer.parseInt(MetaCatUtil.getOption("numberOfIndexingThreads"));
51
	private int sleepTime = 2000; 
52

    
53
    public IndexingQueue() {
54
	    for (int i = 0; i < NUMBEROFINDEXINGTHREADS; i++) {
55
	      Thread thread = new IndexingTask();
56
	      thread.start();
57
	    }
58
    }
59
	
60
	public static synchronized IndexingQueue getInstance(){
61
		if (instance == null) {
62
			instance = new IndexingQueue();
63
		}
64
		return instance;
65
	}//getInstance
66

    
67
    public void add(String docid, String rev) {
68
    	add(new IndexingQueueObject(docid, rev, 0));
69
    }
70
    
71
    protected void add(IndexingQueueObject queueObject) {
72
	    synchronized (indexingMap) {
73
	    	if(!indexingMap.containsKey(queueObject.getDocid())){
74
	    		indexingMap.put(queueObject.getDocid(), queueObject);
75
	    		indexingMap.notify();
76
	    	} else {
77
	    		IndexingQueueObject oldQueueObject = 
78
	    			(IndexingQueueObject) indexingMap.get(queueObject.getDocid());
79
	    		if(oldQueueObject.compareTo(queueObject) < 0){
80
	    	  		indexingMap.put(queueObject.getDocid(), queueObject);
81
		    		indexingMap.notify();  			
82
	    		}
83
	    	}
84
	    }
85
	  }
86
    
87
	
88
    protected IndexingQueueObject getNext() {
89
    	IndexingQueueObject returnVal = null;
90
        synchronized (indexingMap) {
91
          while (indexingMap.isEmpty()) {
92
            try {
93
            	indexingMap.wait();
94
            } catch (InterruptedException ex) {
95
              System.err.println("Interrupted");
96
            }
97
          }
98
          String docid = (String) indexingMap.keySet().iterator().next();
99
          returnVal = (IndexingQueueObject)indexingMap.get(docid);
100
          indexingMap.remove(docid);
101
        }
102
        return returnVal;
103
      }
104

    
105
}
106

    
107
class IndexingTask extends Thread {
108
  	  private Logger logMetacat = Logger.getLogger(IndexingTask.class);
109
      protected final long MAXIMUMINDEXDELAY = Integer.
110
      	parseInt(MetaCatUtil.getOption("maximumIndexDelay"));;
111

    
112
	  public void run() {
113
	    while (true) {
114
	      // blocks until job
115
	      IndexingQueueObject returnVal = 
116
	    	  IndexingQueue.getInstance().getNext();
117

    
118
    	  if(!IndexingQueue.getInstance().
119
	    			currentDocidsBeingIndexed.contains(returnVal.getDocid())){
120
    		  try {
121
    			  IndexingQueue.getInstance().
122
    			  		currentDocidsBeingIndexed.add(returnVal.getDocid());
123
    		      String docid = returnVal.getDocid() + "." + returnVal.getRev();
124
    			  if(checkDocumentTable(docid, "xml_documents")){
125
    				  logMetacat.warn("Calling buildIndex for " + docid);
126
    				  DocumentImpl doc = new DocumentImpl(docid, false);
127
    				  doc.buildIndex();
128
    			  } else {
129
    				  logMetacat.warn("Couldn't find the docid:" + docid 
130
	                			+ " in xml_documents table");
131
	                  sleep(MAXIMUMINDEXDELAY);
132
	                  throw(new Exception("Couldn't find the docid:" + docid 
133
	                			+ " in xml_documents table"));
134
    			  }
135
    		  } catch (Exception e) {
136
    			  logMetacat.warn("Exception: " + e);
137
    			  e.printStackTrace();
138
	        
139
    			  if(returnVal.getCount() < 25){
140
    				  returnVal.setCount(returnVal.getCount()+1);
141
    				  // add the docid back to the list
142
    				  IndexingQueue.getInstance().add(returnVal);
143
    			  } else {
144
    				  logMetacat.fatal("Docid " + returnVal.getDocid() 
145
	        			+ " has been inserted to IndexingQueue "
146
	        			+ "more than 25 times. Not adding the docid to"
147
	        			+ " the queue again.");
148
    			  }
149
    		  } finally {
150
    			  IndexingQueue.getInstance().currentDocidsBeingIndexed
151
	    	  			.remove(returnVal.getDocid());	    	  
152
    		  }
153
    	  } else {
154
				returnVal.setCount(returnVal.getCount()+1);
155
				IndexingQueue.getInstance().add(returnVal);    		  
156
    	  }
157
	    }
158
	  }
159
	  
160
      private boolean checkDocumentTable(String docid, String tablename) throws Exception{
161
	        DBConnection dbConn = null;
162
	        int serialNumber = -1;
163
	        boolean inxmldoc = false;
164
            
165
	        String revision = docid.substring(docid.lastIndexOf(".")+1,docid.length());
166
	        docid = docid.substring(0,docid.lastIndexOf("."));
167

    
168
	        logMetacat.info("Checking if document exsists in xml_documents: docid is " 
169
	        		+ docid + " and revision is " + revision);
170

    
171
	        try {
172
	            // Opening separate db connection for writing XML Index
173
	            dbConn = DBConnectionPool
174
	                    .getDBConnection("DBSAXHandler.checkDocumentTable");
175
	            serialNumber = dbConn.getCheckOutSerialNumber();
176

    
177
	            String xmlDocumentsCheck = "SELECT distinct docid FROM " + tablename
178
	                        + " WHERE docid ='" + docid
179
	                        + "' AND rev ='" + revision + "'";
180

    
181
                PreparedStatement xmlDocCheck = dbConn.prepareStatement(xmlDocumentsCheck);
182
                // Increase usage count
183
	            
184
                dbConn.increaseUsageCount(1);
185
	            xmlDocCheck.execute();
186
	            ResultSet doccheckRS = xmlDocCheck.getResultSet();
187
	            boolean tableHasRows = doccheckRS.next();
188
	            if (tableHasRows) {
189
	            	inxmldoc = true;
190
	            }
191
	            doccheckRS.close();
192
	            xmlDocCheck.close();
193
	        } catch (SQLException e) {
194
	        	   e.printStackTrace();
195
	        } finally {
196
	            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
197
	        }//finally
198
	        
199
	        return inxmldoc;
200
      }
201
	}
202

    
203
class IndexingQueueObject implements Comparable{
204
	// the docid of the document to be indexed. 
205
	private String docid;
206
	// the docid of the document to be indexed. 
207
	private String rev;
208
	// the count of number of times the document has been in the queue
209
	private int count;
210
	
211
	IndexingQueueObject(String docid, String rev, int count){
212
		this.docid = docid;
213
		this.rev = rev;
214
		this.count = count;
215
	}
216
	
217
	public int getCount(){
218
		return count;
219
	}
220

    
221
	public String getDocid(){
222
		return docid;
223
	}
224

    
225
	public String getRev(){
226
		return rev;
227
	}
228

    
229
	public void setCount(int count){
230
		this.count = count;
231
	}
232
	
233
	public void setDocid(String docid){
234
		this.docid = docid;
235
	}
236

    
237
	public void setRev(String rev){
238
		this.rev = rev;
239
	}
240
	
241
	public int compareTo(Object o){
242
		if(o instanceof IndexingQueueObject){
243
			int revision = Integer.parseInt(rev);
244
			int oRevision = Integer.parseInt(((IndexingQueueObject)o).getRev());
245
			
246
			if(revision == oRevision) {
247
				return 0;
248
			} else if (revision > oRevision) {
249
				return 1;
250
			} else {
251
				return -1;
252
			}
253
		} else {
254
			throw new java.lang.ClassCastException();
255
		}
256
	}
257
}
(40-40/65)