Author: tmortagne
Date: 2008-01-14 15:10:19 +0100 (Mon, 14 Jan 2008)
New Revision: 6808
Modified:
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/AttachmentData.java
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/IndexRebuilder.java
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/IndexUpdater.java
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/LucenePluginApi.java
Log:
Some codestyle improvement.
Modified:
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/AttachmentData.java
===================================================================
---
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/AttachmentData.java
2008-01-14 11:10:10 UTC (rev 6807)
+++
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/AttachmentData.java
2008-01-14 14:10:19 UTC (rev 6808)
@@ -34,7 +34,7 @@
* Holds all data but the content of an attachment to be indexed. The content
is retrieved at
* indexing time, which should save us some memory especially when rebuilding
an index for a big
* wiki.
- *
+ *
* @version $Id: $
*/
public class AttachmentData extends IndexData
@@ -69,22 +69,22 @@
final XWikiContext context)
{
super(attachment.getDoc(), context);
+
setModificationDate(attachment.getDate());
setAuthor(attachment.getAuthor());
setSize(attachment.getFilesize());
setFilename(attachment.getFilename());
}
- /**
- * @see IndexData#addDataToLuceneDocument
- */
- public void addDataToLuceneDocument(Document luceneDoc, XWikiDocument doc,
XWikiContext context)
+ public void addDataToLuceneDocument(Document luceneDoc, XWikiDocument doc,
+ XWikiContext context)
{
super.addDataToLuceneDocument(luceneDoc, doc, context);
if (filename != null) {
- luceneDoc
- .add(new Field(IndexFields.FILENAME, filename, Field.Store.YES,
- Field.Index.TOKENIZED));
+ luceneDoc.add(new Field(IndexFields.FILENAME,
+ filename,
+ Field.Store.YES,
+ Field.Index.TOKENIZED));
}
}
@@ -122,14 +122,14 @@
/**
* overridden to append the filename
- *
+ *
* @see IndexData#getId()
*/
public String getId()
{
return new
StringBuffer(super.getId()).append(".file.").append(filename).toString();
}
-
+
/**
* @return a string containing the result of [EMAIL PROTECTED]
IndexData#getFullText} plus the full text
* content of this attachment, as far as it could be extracted.
@@ -142,34 +142,32 @@
if (contentText != null) {
retval.append(" ").append(contentText).toString();
}
+
return retval.toString();
}
- /**
- * @param doc
- * @param context
- * @return
- */
private String getContentAsText(XWikiDocument doc, XWikiContext context)
{
String contentText = null;
+
try {
XWikiAttachment att = doc.getAttachment(filename);
- if (LOG.isDebugEnabled()) {
- LOG.debug("have attachment for filename " + filename + ": " +
att);
- }
+
+ LOG.debug("have attachment for filename " + filename + ": " + att);
+
byte[] content = att.getContent(context);
if (filename != null) {
String[] nameParts = filename.split("\\.");
if (nameParts.length > 1) {
- contentText = TextExtractor.getText(content, (String)
MIMETYPES
- .get(nameParts[nameParts.length - 1].toLowerCase()));
+ contentText =
+ TextExtractor.getText(content, (String) MIMETYPES
+ .get(nameParts[nameParts.length -
1].toLowerCase()));
}
}
} catch (Exception e) {
LOG.error("error getting content of attachment", e);
- e.printStackTrace();
}
+
return contentText;
}
}
Modified:
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/IndexRebuilder.java
===================================================================
---
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/IndexRebuilder.java
2008-01-14 11:10:10 UTC (rev 6807)
+++
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/IndexRebuilder.java
2008-01-14 14:10:19 UTC (rev 6808)
@@ -51,6 +51,7 @@
public IndexRebuilder(IndexUpdater indexUpdater, XWikiContext context)
{
this.indexUpdater = indexUpdater;
+
if (indexUpdater.needInitialBuild) {
LOG.info("Initializing Lucene search index, " +
this.rebuildIndex(context)
+ " documents found");
@@ -62,13 +63,12 @@
* for re-addition to the index.
*
* @param context
- * @return total number of documents and attachments successfully added to
the indexer queue,
- * -1 when errors occured.
- * TODO: give more detailed results
+ * @return total number of documents and attachments successfully added to
the indexer queue, -1
+ * when errors occured. TODO: give more detailed results
*/
public int rebuildIndex(XWikiContext context)
{
- indexUpdater.cleanIndex();
+ this.indexUpdater.cleanIndex();
int retval = 0;
Collection wikiServers;
XWiki xwiki = context.getWiki();
@@ -109,6 +109,7 @@
protected int indexWiki(String wikiName, XWikiContext context)
{
LOG.info("reading content of wiki " + wikiName);
+
int retval = 0;
XWiki xwiki = context.getWiki();
@@ -122,8 +123,7 @@
try {
docNames = xwiki.getStore().searchDocumentsNames("", context);
} catch (XWikiException e1) {
- LOG.error("error getting document names for wiki " + wikiName);
- e1.printStackTrace();
+ LOG.error("error getting document names for wiki " + wikiName,
e1);
return -1;
}
@@ -134,16 +134,15 @@
try {
document = xwiki.getDocument(docName, context);
} catch (XWikiException e2) {
- LOG.error("error fetching document " + wikiName + ":" +
docName);
- e2.printStackTrace();
+ LOG.error("error fetching document " + wikiName + ":" +
docName, e2);
continue;
}
if (document != null) {
- indexUpdater.add(document, context);
+ this.indexUpdater.add(document, context);
retval++;
retval += addTranslationsOfDocument(document, context);
- retval += indexUpdater.addAttachmentsOfDocument(document,
context);
+ retval +=
this.indexUpdater.addAttachmentsOfDocument(document, context);
retval += addObjectsOfDocument(document, context);
} else {
LOG
@@ -165,23 +164,20 @@
private int addObjectsOfDocument(XWikiDocument document, XWikiContext
wikiContext)
{
int retval = 0;
+
Map xwikiObjects = document.getxWikiObjects();
if (document.hasElement(XWikiDocument.HAS_OBJECTS)) {
retval += xwikiObjects.size();
- indexUpdater.addObject(document, wikiContext);
+ this.indexUpdater.addObject(document, wikiContext);
}
return retval;
}
- /**
- * @param document
- * @param wikiContext
- * @throws XWikiException
- */
protected int addTranslationsOfDocument(XWikiDocument document,
XWikiContext wikiContext)
{
int retval = 0;
+
List translations;
try {
translations = document.getTranslationList(wikiContext);
@@ -195,28 +191,24 @@
for (Iterator iter = translations.iterator(); iter.hasNext();) {
String lang = (String) iter.next();
try {
- indexUpdater.add(document.getTranslatedDocument(lang,
wikiContext), wikiContext);
+ this.indexUpdater.add(document.getTranslatedDocument(lang,
wikiContext), wikiContext);
retval++;
} catch (XWikiException e1) {
LOG.error("error getting translated document for document "
- + document.getFullName() + " and language " + lang);
- e1.printStackTrace();
+ + document.getFullName() + " and language " + lang, e1);
}
}
return retval;
}
- /**
- * @param context
- * @return
- */
private Collection findWikiServers(XWikiContext context)
{
List retval = Collections.EMPTY_LIST;
+
try {
retval = context.getWiki().getVirtualWikisDatabaseNames(context);
-
+
if (!retval.contains(context.getMainXWiki())) {
retval.add(context.getMainXWiki());
}
Modified:
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/IndexUpdater.java
===================================================================
---
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/IndexUpdater.java
2008-01-14 11:10:10 UTC (rev 6807)
+++
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/IndexUpdater.java
2008-01-14 14:10:19 UTC (rev 6808)
@@ -95,23 +95,19 @@
{
MDC.put("url", "index updating thread");
- while (!exit) {
- if (queue.isEmpty()) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("IndexUpdater: queue empty, nothing to do");
- }
+ while (!this.exit) {
+ if (this.queue.isEmpty()) {
+ LOG.debug("IndexUpdater: queue empty, nothing to do");
} else {
- if (LOG.isDebugEnabled()) {
- LOG.debug("IndexUpdater: documents in queue, start
indexing");
- }
+ LOG.debug("IndexUpdater: documents in queue, start indexing");
Map toIndex = new HashMap();
List toDelete = new ArrayList();
try {
openSearcher();
- while (!queue.isEmpty()) {
- IndexData data = queue.remove();
+ while (!this.queue.isEmpty()) {
+ IndexData data = this.queue.remove();
List oldDocs = getOldIndexDocIds(data);
if (oldDocs != null) {
for (int i = 0; i < oldDocs.size(); i++) {
@@ -125,6 +121,7 @@
}
}
}
+
String id = data.getId();
LOG.debug("Adding " + id + " to index list");
if (toIndex.containsKey(id)) {
@@ -136,7 +133,6 @@
}
} catch (Exception e) {
LOG.error("error preparing index queue", e);
- e.printStackTrace();
} finally {
closeSearcher();
}
@@ -149,7 +145,6 @@
LOG.info("deleted " + nb + " docs from lucene index");
} catch (Exception e) {
LOG.error("error deleting previous documents", e);
- e.printStackTrace();
} finally {
closeSearcher();
}
@@ -157,62 +152,70 @@
// Let's index
try {
LOG.info("indexing " + toIndex.size() + " docs to lucene
index");
- int nb = 0;
+
XWikiContext context = (XWikiContext) this.context.clone();
context.getWiki().getStore().cleanUp(context);
openWriter(false);
- Iterator docIt = toIndex.keySet().iterator();
- while (docIt.hasNext()) {
- String id = (String) docIt.next();
+
+ int nb = 0;
+ for (Iterator entryIt = toIndex.entrySet().iterator();
entryIt.hasNext();) {
+ Map.Entry entry = (Map.Entry) entryIt.next();
+
+ String id = (String) entry.getKey();
+ IndexData data = (IndexData) entry.getValue();
+
+ System.out.println("###################### " + id);
+
try {
- IndexData data = (IndexData) toIndex.get(id);
- XWikiDocument doc =
xwiki.getDocument(data.getFullName(), context);
- if ((data.getLanguage() != null) &&
(!data.getLanguage().equals("")))
+ XWikiDocument doc =
+ this.xwiki.getDocument(data.getFullName(),
context);
+
+ if (data.getLanguage() != null &&
!data.getLanguage().equals("")) {
doc =
doc.getTranslatedDocument(data.getLanguage(), context);
+ }
+
addToIndex(data, doc, context);
- nb++;
+ ++nb;
} catch (Exception e) {
LOG.error("error indexing document " + id, e);
- e.printStackTrace();
}
}
+
LOG.info("indexed " + nb + " docs to lucene index");
} catch (Exception e) {
LOG.error("error indexing documents", e);
- e.printStackTrace();
} finally {
- context.getWiki().getStore().cleanUp(context);
+ this.context.getWiki().getStore().cleanUp(this.context);
closeWriter();
}
plugin.openSearchers();
}
try {
- Thread.sleep(indexingInterval);
+ Thread.sleep(this.indexingInterval);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
- xwiki.getStore().cleanUp(context);
+ this.xwiki.getStore().cleanUp(this.context);
MDC.remove("url");
}
private synchronized void closeSearcher()
{
try {
- if (searcher != null) {
- searcher.close();
+ if (this.searcher != null) {
+ this.searcher.close();
}
- if (reader != null) {
- reader.close();
+ if (this.reader != null) {
+ this.reader.close();
}
} catch (IOException e) {
LOG.error("error closing index searcher", e);
- e.printStackTrace();
} finally {
- searcher = null;
- reader = null;
+ this.searcher = null;
+ this.reader = null;
}
}
@@ -223,11 +226,10 @@
private synchronized void openSearcher()
{
try {
- reader = IndexReader.open(indexDir);
- searcher = new IndexSearcher(reader);
+ this.reader = IndexReader.open(this.indexDir);
+ this.searcher = new IndexSearcher(this.reader);
} catch (IOException e) {
LOG.error("error opening index searcher", e);
- e.printStackTrace();
}
}
@@ -237,108 +239,92 @@
private int deleteOldDocs(List oldDocs)
{
int nb = 0;
-
+
for (Iterator iter = oldDocs.iterator(); iter.hasNext();) {
Integer id = (Integer) iter.next();
- if (LOG.isDebugEnabled()) {
- LOG.debug("delete doc " + id);
- }
+ LOG.debug("delete doc " + id);
+
try {
- reader.deleteDocument(id.intValue());
+ this.reader.deleteDocument(id.intValue());
nb++;
} catch (IOException e1) {
LOG.error("error deleting doc " + id, e1);
- e1.printStackTrace();
}
}
-
+
return nb;
}
- /**
- * @param data
- * @return
- */
private List getOldIndexDocIds(IndexData data)
{
List retval = new ArrayList(3);
Query query = data.buildQuery();
try {
- Hits hits = searcher.search(query);
+ Hits hits = this.searcher.search(query);
for (int i = 0; i < hits.length(); i++) {
retval.add(new Integer(hits.id(i)));
}
} catch (Exception e) {
LOG.error("error looking for old versions of document " + data + "
with query "
+ query, e);
- e.printStackTrace();
}
-
+
return retval;
}
- /**
- *
- */
private void openWriter(boolean create)
{
if (writer != null) {
LOG.error("Writer already open and createWriter called");
return;
}
-
+
try {
// fix for windows by Daniel Cortes:
FSDirectory f = FSDirectory.getDirectory(indexDir, false);
writer = new IndexWriter(f, analyzer, create);
// writer = new IndexWriter (indexDir, analyzer, create);
writer.setUseCompoundFile(true);
- if (LOG.isDebugEnabled()) {
- LOG.debug("successfully opened index writer : " + indexDir);
- }
+
+ LOG.debug("successfully opened index writer : " + indexDir);
} catch (IOException e) {
LOG.error("IOException when opening Lucene Index for writing at "
+ indexDir, e);
}
}
- /**
- *
- */
private void closeWriter()
{
- if (writer == null) {
+ if (this.writer == null) {
LOG.error("Writer not open and closeWriter called");
return;
}
+
try {
- writer.optimize();
+ this.writer.optimize();
} catch (IOException e1) {
LOG.error("Exception caught when optimizing Index", e1);
}
+
try {
- writer.close();
+ this.writer.close();
} catch (Exception e) {
LOG.error("Exception caught when closing IndexWriter", e);
}
- writer = null;
- if (LOG.isDebugEnabled()) {
- LOG.debug("closed writer.");
- }
+
+ this.writer = null;
+
+ LOG.debug("closed writer.");
}
- /**
- * @param doc
- * @throws IOException
- */
private void addToIndex(IndexData data, XWikiDocument doc, XWikiContext
context)
throws IOException
{
- if (LOG.isDebugEnabled()) {
- LOG.debug("addToIndex: " + data);
- }
+ LOG.debug("addToIndex: " + data);
+
org.apache.lucene.document.Document luceneDoc = new
org.apache.lucene.document.Document();
data.addDataToLuceneDocument(luceneDoc, doc, context);
Field fld = null;
+
// collecting all the fields for using up in search
for (Enumeration e = luceneDoc.fields(); e.hasMoreElements();) {
fld = (Field) e.nextElement();
@@ -346,7 +332,8 @@
fields.add(fld.name());
}
}
- writer.addDocument(luceneDoc);
+
+ this.writer.addDocument(luceneDoc);
}
/**
@@ -365,48 +352,45 @@
this.analyzer = analyzer;
}
- /**
- * @param config
- */
public synchronized void init(Properties config, LucenePlugin plugin,
XWikiContext context)
{
this.xwiki = context.getWiki();
- this.context = (XWikiContext)context.clone();
+ this.context = (XWikiContext) context.clone();
this.context.setDatabase(this.context.getMainXWiki());
this.plugin = plugin;
// take the first configured index dir as the one for writing
// String[] indexDirs =
- // StringUtils.split(config.getProperty(LucenePlugin.PROP_INDEX_DIR),
" ,");
+ // StringUtils.split(config.getProperty(LucenePlugin.PROP_INDEX_DIR), "
+ // ,");
String[] indexDirs = StringUtils.split(plugin.getIndexDirs(), ",");
if (indexDirs != null && indexDirs.length > 0) {
this.indexDir = indexDirs[0];
File f = new File(indexDir);
if (!f.isDirectory()) {
f.mkdirs();
- needInitialBuild = true;
+ this.needInitialBuild = true;
}
if (!IndexReader.indexExists(f)) {
- needInitialBuild = true;
+ this.needInitialBuild = true;
}
}
- indexingInterval =
+
+ this.indexingInterval =
1000 * Integer.parseInt(config
.getProperty(LucenePlugin.PROP_INDEXING_INTERVAL, "300"));
- // Note: There's no need to open the Searcher here (with a call to
openSearcher()) as each
+ // Note: There's no need to open the Searcher here (with a call to
+ // openSearcher()) as each
// task needing it will open it itself.
}
- /**
- *
- */
public void cleanIndex()
{
LOG.info("trying to clear index for rebuilding");
+
while (writer != null) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("waiting for existing index writer to close");
- }
+ LOG.debug("waiting for existing index writer to close");
+
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
@@ -414,41 +398,31 @@
e.printStackTrace();
}
}
-
+
synchronized (this) {
openWriter(true);
closeWriter();
}
}
- /**
- * @param document
- */
public void add(XWikiDocument document, XWikiContext context)
{
- queue.add(new DocumentData(document, context));
-
+ this.queue.add(new DocumentData(document, context));
+
if (document.hasElement(XWikiDocument.HAS_OBJECTS)) {
addObject(document, context);
}
}
- /**
- * @param document
- * @param context
- */
public void addObject(XWikiDocument document, XWikiContext context)
{
- queue.add(new ObjectData(document, context));
+ this.queue.add(new ObjectData(document, context));
}
- /**
- * @param attachment
- */
public void add(XWikiDocument document, XWikiAttachment attachment,
XWikiContext context)
{
if (document != null && attachment != null && context != null) {
- queue.add(new AttachmentData(document, attachment, context));
+ this.queue.add(new AttachmentData(document, attachment, context));
} else {
LOG.error("invalid parameters given to add: " + document + ", " +
attachment + ", "
+ context);
@@ -458,6 +432,7 @@
public int addAttachmentsOfDocument(XWikiDocument document, XWikiContext
context)
{
int retval = 0;
+
final List attachmentList = document.getAttachmentList();
retval += attachmentList.size();
for (Iterator attachmentIter = attachmentList.iterator();
attachmentIter.hasNext();) {
@@ -468,6 +443,7 @@
LOG.error("error retrieving attachment of document " +
document.getFullName(), e);
}
}
+
return retval;
}
@@ -481,11 +457,9 @@
public void notify(XWikiNotificationRule rule, XWikiDocument newDoc,
XWikiDocument oldDoc,
int event, XWikiContext context)
{
- if (LOG.isDebugEnabled()) {
- LOG.debug("notify from XWikiDocChangeNotificationInterface,
event=" + event
- + ", newDoc=" + newDoc + " oldDoc=" + oldDoc);
- }
-
+ LOG.debug("notify from XWikiDocChangeNotificationInterface, event=" +
event + ", newDoc="
+ + newDoc + " oldDoc=" + oldDoc);
+
try {
add(newDoc, context);
} catch (Exception e) {
@@ -503,9 +477,8 @@
XWikiContext context)
{
if ("upload".equals(action)) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("upload action notification for doc " +
doc.getName());
- }
+ LOG.debug("upload action notification for doc " + doc.getName());
+
try {
// Retrieve the latest version (with the file just attached)
XWikiDocument basedoc =
context.getWiki().getDocument(doc.getFullName(), context);
@@ -528,6 +501,6 @@
public long getQueueSize()
{
- return queue.getSize();
+ return this.queue.getSize();
}
}
Modified:
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/LucenePluginApi.java
===================================================================
---
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/LucenePluginApi.java
2008-01-14 11:10:10 UTC (rev 6807)
+++
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/LucenePluginApi.java
2008-01-14 14:10:19 UTC (rev 6808)
@@ -170,7 +170,7 @@
return null;
}
- /*
+ /**
* @return the number of documents in the queue
*/
public long getQueueSize()
_______________________________________________
notifications mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/notifications