KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > search > RAMIndexer


1 package org.jahia.services.search;
2
3 import org.apache.lucene.analysis.Analyzer;
4 import org.apache.lucene.document.Document;
5 import org.apache.lucene.index.IndexWriter;
6 import org.apache.lucene.index.IndexReader;
7 import org.apache.lucene.index.Term;
8 import org.apache.lucene.store.RAMDirectory;
9 import org.apache.lucene.store.Directory;
10 import org.apache.lucene.search.Searchable;
11 import org.apache.lucene.search.IndexSearcher;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Vector JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 /**
18  * Created by IntelliJ IDEA.
19  * User: hollis
20  * Date: 24 mai 2005
21  * Time: 12:30:42
22  * To change this template use File | Settings | File Templates.
23  */

24 public class RAMIndexer implements Runnable JavaDoc {
25
26     private static org.apache.log4j.Logger logger =
27         org.apache.log4j.Logger.getLogger(RAMIndexer.class);
28
29     private HashMap JavaDoc indexes = new HashMap JavaDoc();
30     private HashMap JavaDoc ramDirs = new HashMap JavaDoc();
31     private HashMap JavaDoc docs = new HashMap JavaDoc();
32     private int maxDoc = 500;
33     private Analyzer analyzer;
34     private Thread JavaDoc thread;
35     private boolean killed = false;
36
37     public void run(){
38         while ( !killed ){
39             try {
40                 Thread.sleep(30000);
41                 long now = System.currentTimeMillis();
42                 synchronized(this){
43                     Iterator JavaDoc iterator = docs.keySet().iterator();
44                     Integer JavaDoc I = null;
45                     Vector JavaDoc v = null;
46                     while ( iterator.hasNext() ){
47                         I = (Integer JavaDoc)iterator.next();
48                         this.closeIndexWriter(I.intValue());
49                         v = (Vector JavaDoc)docs.get(I);
50                         int size = v.size();
51                         TempDocument doc = null;
52                         Vector JavaDoc newV = new Vector JavaDoc();
53                         for ( int i=0; i<size; i++ ){
54                             doc = (TempDocument)v.get(i);
55                             if ( (now-doc.getCreationDate())<30000){
56                                 newV.add(doc);
57                                 this.addDoc(doc.getDoc(),I.intValue());
58                             }
59                         }
60                         docs.put(I,newV);
61                     }
62                 }
63             } catch ( Throwable JavaDoc t ){
64                 logger.debug("Exception removing old docs",t);
65             }
66         }
67     }
68
69     public RAMIndexer(Analyzer analyzer){
70         this.analyzer = analyzer;
71         thread = new Thread JavaDoc(this);
72         thread.start();
73     }
74
75     public RAMIndexer(Analyzer analyzer, int maxDoc){
76         this.analyzer = analyzer;
77         this.maxDoc = maxDoc;
78         thread = new Thread JavaDoc(this);
79         thread.start();
80     }
81
82     public synchronized void removeDoc(Term term, int siteId)
83     {
84         Directory dir = (Directory)this.ramDirs.get(new Integer JavaDoc(siteId));
85         if ( dir != null ){
86             try {
87                 IndexWriter writer = (IndexWriter)this.indexes.get(new Integer JavaDoc(siteId));
88                 if ( writer != null ){
89                     try {
90                         writer.close();
91                     } catch ( Throwable JavaDoc t){
92                     }
93                 }
94                 try {
95                     IndexReader reader = IndexReader.open(dir);
96                     reader.delete(term);
97                     writer = new IndexWriter(dir, analyzer, false);
98                 } catch ( Throwable JavaDoc t ){
99                 }
100                 if ( writer != null ){
101                     this.indexes.put(new Integer JavaDoc(siteId),writer);
102                 }
103             } catch ( Throwable JavaDoc t){
104                 logger.debug("Exception removing term from RAMIndexer",t);
105             }
106         }
107     }
108
109     public synchronized void addDoc(Document doc, int siteId){
110         if ( this.killed || doc == null ){
111             return;
112         }
113         int docCount = 0;
114         IndexWriter writer = getIndexWriter(siteId);
115         docCount = writer.docCount();
116         if ( docCount > maxDoc ){
117             try {
118                 closeIndexWriter(siteId);
119                 Directory newDir = new RAMDirectory();
120                 writer = new IndexWriter(newDir,analyzer,true);
121                 writer.minMergeDocs = 1000;
122                 writer.mergeFactor = 50;
123                 writer.setUseCompoundFile(false);
124                 Vector JavaDoc v = (Vector JavaDoc)docs.get(new Integer JavaDoc(siteId));
125                 for ( int i=0; i<50; i++ ){
126                     if ( v.size() > 50 ){
127                         v.remove(0);
128                     } else {
129                         break;
130                     }
131                 }
132                 int size = v.size();
133                 TempDocument tempDoc = null;
134                 for ( int i=0; i<size; i++ ){
135                     tempDoc = (TempDocument)v.get(i);
136                     writer.addDocument(tempDoc.getDoc());
137                 }
138                 this.indexes.put(new Integer JavaDoc(siteId),writer);
139                 this.ramDirs.put(new Integer JavaDoc(siteId),newDir);
140             } catch ( Throwable JavaDoc t){
141                 logger.debug("Exception adding doc to RAMIndexer",t);
142             }
143         }
144         try {
145             Vector JavaDoc v = (Vector JavaDoc)docs.get(new Integer JavaDoc(siteId));
146             if ( v == null ){
147                 v = new Vector JavaDoc();
148                 docs.put(new Integer JavaDoc(siteId),v);
149             }
150             TempDocument tempDoc = new TempDocument(doc);
151             v.add(tempDoc);
152             writer.addDocument(doc);
153             try {
154                 writer.close();
155             } catch ( Throwable JavaDoc t) {
156             }
157             RAMDirectory oldDirectory = (RAMDirectory)this.ramDirs.get(new Integer JavaDoc(siteId));
158             try {
159                 writer = new IndexWriter(oldDirectory, analyzer, false);
160             } catch ( Throwable JavaDoc t ){
161             }
162             if ( writer != null ){
163                 this.indexes.put(new Integer JavaDoc(siteId),writer);
164             }
165
166         } catch ( Throwable JavaDoc t) {
167             logger.debug("Exception adding doc to RAMIndexer",t);
168         }
169     }
170
171     public synchronized Searchable getSearcher(int siteId){
172         Directory dir = (Directory)this.ramDirs.get(new Integer JavaDoc(siteId));
173         if ( dir != null ) {
174             try {
175                 return new IndexSearcher(dir);
176             }catch(Throwable JavaDoc t){
177                 logger.debug("Exception creating searcher from RAMIndexer siteId=" + siteId,t);
178             }
179         }
180         return null;
181     }
182
183     private IndexWriter getIndexWriter(int siteId){
184         IndexWriter writer = (IndexWriter) this.indexes.get(new Integer JavaDoc(siteId));
185         if ( writer == null ) {
186             RAMDirectory ramDir = new RAMDirectory();
187             try {
188                 writer = new IndexWriter(ramDir,analyzer,true);
189                 writer.minMergeDocs = 1000;
190                 writer.mergeFactor = 50;
191                 writer.setUseCompoundFile(false);
192                 this.indexes.put(new Integer JavaDoc(siteId),writer);
193                 this.ramDirs.put(new Integer JavaDoc(siteId),ramDir);
194             } catch ( Throwable JavaDoc t) {
195                 logger.debug("Exception creatring IndexWriter for RAMIndexer",t);
196             }
197         }
198         return writer;
199     }
200
201     private void closeIndexWriter(int siteId){
202         this.indexes.remove(new Integer JavaDoc(siteId));
203         this.ramDirs.remove(new Integer JavaDoc(siteId));
204     }
205
206     public void shutDown(){
207         this.killed = true;
208     }
209
210     private class TempDocument {
211         private Document doc;
212         private long creationDate;
213
214         public TempDocument(Document doc){
215             this.doc = doc;
216             this.creationDate = System.currentTimeMillis();
217         }
218         public Document getDoc() {
219             return doc;
220         }
221
222         public void setDoc(Document doc) {
223             this.doc = doc;
224         }
225
226         public long getCreationDate() {
227             return creationDate;
228         }
229
230         public void setCreationDate(long creationDate) {
231             this.creationDate = creationDate;
232         }
233
234     }
235 }
236
Popular Tags