File Source: IndexOperation.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */
18 /* Created on Jul 16, 2003 */
19 package org.apache.roller.weblogger.business.search.operations;
20
21 import java.io.IOException;
22 import java.util.Iterator;
23 import java.util.List;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.lucene.document.Document;
28 import org.apache.lucene.document.Field;
29 import org.apache.lucene.index.IndexReader;
30 import org.apache.lucene.index.IndexWriter;
31 import org.apache.roller.weblogger.business.search.IndexManagerImpl;
32 import org.apache.roller.weblogger.business.search.FieldConstants;
33 import org.apache.roller.weblogger.pojos.WeblogEntryComment;
34 import org.apache.roller.weblogger.pojos.WeblogCategory;
35 import org.apache.roller.weblogger.pojos.WeblogEntry;
36 import org.apache.roller.weblogger.util.Utilities;
37 import org.apache.roller.weblogger.config.WebloggerConfig;
38
39 /**
40 * This is the base class for all index operation.
41 * These operations include:<br>
42 * SearchOperation<br>
43 * AddWeblogOperation<br>
44 * RemoveWeblogOperation<br>
45 * RebuildUserIndexOperation
46 *
47 * @author Mindaugas Idzelis (min@idzelis.com)
48 */
49 public abstract class IndexOperation implements Runnable {
/*
P/P * Method: org.apache.roller.weblogger.business.search.operations.IndexOperation__static_init
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getFactory(...)@50 != null
*
* Postconditions:
* init'ed(mLogger)
*/
50 private static Log mLogger = LogFactory.getFactory().getInstance(IndexOperation.class);
51
52 //~ Instance fields
53 // ========================================================
54 protected IndexManagerImpl manager;
55
56 private IndexReader reader;
57
58 private IndexWriter writer;
59
60 //~ Constructors
61 // ===========================================================
62
/*
P/P * Method: void org.apache.roller.weblogger.business.search.operations.IndexOperation(IndexManagerImpl)
*
* Postconditions:
* this.manager == manager
* init'ed(this.manager)
*/
63 public IndexOperation(IndexManagerImpl manager) {
64 this.manager = manager;
65 }
66
67 //~ Methods
68 // ================================================================
69
70 protected Document getDocument(WeblogEntry data) {
71
72 // Actual comment content is indexed only if search.index.comments
73 // is true or absent from the (static) configuration properties.
74 // If false in the configuration, comments are treated as if empty.
/*
P/P * Method: Document getDocument(WeblogEntry)
*
* Preconditions:
* data != null
* org/apache/roller/weblogger/config/WebloggerConfig.config != null
* org/apache/roller/weblogger/config/WebloggerConfig.log != null
*
* Presumptions:
* java.util.Iterator:next(...)@87 != null
* org.apache.roller.weblogger.pojos.WeblogEntry:getCreator(...)@114 != null
* org.apache.roller.weblogger.pojos.WeblogEntry:getPubTime(...)@126 != null
* org.apache.roller.weblogger.pojos.WeblogEntry:getUpdateTime(...)@124 != null
* org.apache.roller.weblogger.pojos.WeblogEntry:getWebsite(...)@111 != null
*
* Postconditions:
* return_value == &new Document(getDocument#4)
* new Document(getDocument#4) num objects == 1
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@86: {0}, {1}
* org.apache.roller.weblogger.pojos.WeblogEntry:getCategory(...)@137: Inverse{null}, Addr_Set{null}
* org.apache.roller.weblogger.pojos.WeblogEntry:getComments(...)@81: Addr_Set{null}, Inverse{null}
* org.apache.roller.weblogger.pojos.WeblogEntryComment:getContent(...)@88: Addr_Set{null}, Inverse{null}
* org.apache.roller.weblogger.pojos.WeblogEntryComment:getEmail(...)@92: Addr_Set{null}, Inverse{null}
* org.apache.roller.weblogger.pojos.WeblogEntryComment:getName(...)@96: Addr_Set{null}, Inverse{null}
*/
75 boolean indexComments = WebloggerConfig.getBooleanProperty("search.index.comments", true);
76
77 String commentContent = "";
78 String commentEmail = "";
79 String commentName = "";
80 if (indexComments) {
81 List comments = data.getComments();
82 if (comments != null) {
83 StringBuffer commentEmailBuf = new StringBuffer();
84 StringBuffer commentContentBuf = new StringBuffer();
85 StringBuffer commentNameBuf = new StringBuffer();
86 for (Iterator cItr = comments.iterator(); cItr.hasNext();) {
87 WeblogEntryComment comment = (WeblogEntryComment) cItr.next();
88 if (comment.getContent() != null) {
89 commentContentBuf.append(comment.getContent());
90 commentContentBuf.append(",");
91 }
92 if (comment.getEmail() != null) {
93 commentEmailBuf.append(comment.getEmail());
94 commentEmailBuf.append(",");
95 }
96 if (comment.getName() != null) {
97 commentNameBuf.append(comment.getName());
98 commentNameBuf.append(",");
99 }
100 }
101 commentEmail = commentEmailBuf.toString();
102 commentContent = commentContentBuf.toString();
103 commentName = commentNameBuf.toString();
104 }
105 }
106
107 Document doc = new Document();
108
109 doc.add(Field.Keyword(FieldConstants.ID, data.getId()));
110
111 doc.add(Field.Keyword(FieldConstants.WEBSITE_HANDLE, data.getWebsite().getHandle()));
112
113 doc.add(Field.UnIndexed(FieldConstants.ANCHOR, data.getAnchor()));
114 doc.add(Field.Text(FieldConstants.USERNAME, data.getCreator().getUserName()));
115 doc.add(Field.Text(FieldConstants.TITLE, data.getTitle()));
116
117 // index the entry text, but don't store it - moved to end of block
118 doc.add(Field.UnStored(FieldConstants.CONTENT, data.getText()));
119
120 // store an abbreviated version of the entry text, but don't index
121 doc.add(Field.UnIndexed(FieldConstants.CONTENT_STORED,
122 Utilities.truncateNicely(Utilities.removeHTML(data.getText()), 240, 260, "...")));
123
124 doc.add(Field.Keyword(FieldConstants.UPDATED, data.getUpdateTime()
125 .toString()));
126 doc.add(Field.Keyword(FieldConstants.PUBLISHED, data.getPubTime()
127 .toString()));
128
129 // index Comments
130 doc.add(Field.UnStored(FieldConstants.C_CONTENT, commentContent));
131 doc.add(Field.UnStored(FieldConstants.C_EMAIL, commentEmail));
132 doc.add(Field.UnStored(FieldConstants.C_NAME, commentName));
133
134 doc.add(Field.UnStored(FieldConstants.CONSTANT, FieldConstants.CONSTANT_V));
135
136 // index Category
137 WeblogCategory categorydata = data.getCategory();
138 Field category = (categorydata == null)
139 ? Field.UnStored(FieldConstants.CATEGORY, "")
140 : Field.Text(FieldConstants.CATEGORY, categorydata.getName());
141 doc.add(category);
142
143 return doc;
144 }
145
146 protected IndexReader beginDeleting() {
147 try {
/*
P/P * Method: IndexReader beginDeleting()
*
* Preconditions:
* (soft) init'ed(this.reader)
* (soft) org/apache/roller/weblogger/business/search/IndexManagerImpl.mLogger != null
* (soft) this.manager != null
* (soft) init'ed(this.manager.fRAMindex)
* (soft) init'ed(this.manager.indexDir)
* (soft) init'ed(this.manager.useRAMIndex)
*
* Postconditions:
* init'ed(return_value)
* this.reader == return_value
*/
148 reader = IndexReader.open(manager.getIndexDirectory());
149 } catch (IOException e) {
150 }
151
152 return reader;
153 }
154
155 protected void endDeleting() {
/*
P/P * Method: void endDeleting()
*
* Preconditions:
* init'ed(this.reader)
* (soft) mLogger != null
*
* Test Vectors:
* this.reader: Addr_Set{null}, Inverse{null}
*/
156 if (reader != null) {
157 try {
158 reader.close();
159 } catch (IOException e) {
160 mLogger.error("ERROR closing reader");
161 }
162 }
163 }
164
165 protected IndexWriter beginWriting() {
166 try {
/*
P/P * Method: IndexWriter beginWriting()
*
* Preconditions:
* (soft) mLogger != null
* (soft) init'ed(this.writer)
* (soft) org/apache/roller/weblogger/business/search/IndexManagerImpl.mLogger != null
* (soft) this.manager != null
* (soft) init'ed(this.manager.fRAMindex)
* (soft) init'ed(this.manager.indexDir)
* (soft) init'ed(this.manager.useRAMIndex)
*
* Postconditions:
* return_value == One-of{&new IndexWriter(beginWriting#1), old this.writer}
* (soft) init'ed(return_value)
* this.writer == return_value
* new IndexWriter(beginWriting#1) num objects <= 1
*/
167 writer = new IndexWriter(manager.getIndexDirectory(), IndexManagerImpl.getAnalyzer(), false);
168 } catch (IOException e) {
169 mLogger.error("ERROR creating writer", e);
170 }
171
172 return writer;
173 }
174
175 protected void endWriting() {
/*
P/P * Method: void endWriting()
*
* Preconditions:
* init'ed(this.writer)
* (soft) mLogger != null
*
* Test Vectors:
* this.writer: Addr_Set{null}, Inverse{null}
*/
176 if (writer != null) {
177 try {
178 writer.close();
179 } catch (IOException e) {
180 mLogger.error("ERROR closing writer", e);
181 }
182 }
183 }
184
185 public void run() {
/*
P/P * Method: void run()
*/
186 doRun();
187 }
188
189 protected abstract void doRun();
190 }
SofCheck Inspector Build Version : 2.18479
| IndexOperation.java |
2009-Jan-02 14:25:06 |
| IndexOperation.class |
2009-Sep-04 03:12:30 |