* @see net.sf.logsaw.index.impl.ARunWithIndexWriter#doRunWithIndexWriter(org.apache.lucene.index.IndexWriter, net.sf.logsaw.core.framework.ILogResource)
*/
@Override
protected SynchronizationResult doRunWithIndexWriter(final IndexWriter writer,
final ILogResource log) throws CoreException {
ILogEntryCollector collector = new ALogEntryCollector(monitor) {
/* (non-Javadoc)
* @see net.sf.logsaw.core.framework.support.ALogEntryCollector#doCollect(net.sf.logsaw.core.model.LogEntry)
*/
@Override
protected boolean doCollect(final LogEntry entry)
throws IOException {
final Document doc = new Document();
if (latestEntryDate != null) {
Date d = entry.get(log.getDialect().getFieldProvider().getTimestampField());
if (!d.after(latestEntryDate)) {
// Skip entry because it was already indexed
return false;
}
}
// Setup visitor
ILogEntryFieldVisitor visitor = new ILogEntryFieldVisitor() {
/* (non-Javadoc)
* @see net.sf.logsaw.core.model.ILogEntryFieldVisitor#visit(net.sf.logsaw.core.model.StringLogEntryField)
*/
@Override
public void visit(StringLogEntryField fld) {
// Decide whether to analyze the field
if (fld.isAnalyzed()) {
doc.add(new TextField(fld.getKey(),
fld.toIndexedValue(entry.get(fld)),
Field.Store.YES));
} else {
doc.add(new StringField(fld.getKey(),
fld.toIndexedValue(entry.get(fld)),
Field.Store.YES));
}
}
/* (non-Javadoc)
* @see net.sf.logsaw.core.model.ILogEntryFieldVisitor#visit(net.sf.logsaw.core.model.LevelLogEntryField)
*/
@Override
public void visit(LevelLogEntryField fld) {
Level lvl = entry.get(fld);
Assert.isTrue(lvl.getValue() > 0, "Level value must be a positive integer"); //$NON-NLS-1$
doc.add(new IntField(
fld.getKey(), fld.toIndexedValue(lvl), Field.Store.YES));
}
/* (non-Javadoc)
* @see net.sf.logsaw.core.model.ILogEntryFieldVisitor#visit(net.sf.logsaw.core.model.DateLogEntryField)
*/
@Override
public void visit(DateLogEntryField fld) {
doc.add(new LongField(
fld.getKey(), fld.toIndexedValue(entry.get(fld)), Field.Store.YES));
}
};
for (ALogEntryField<?, ?> fld : log.getDialect().getFieldProvider().getAllFields()) {
if (entry.contains(fld)) {
fld.visit(visitor);
}
}
writer.addDocument(doc);
return true;
}
};
if (log.getDialect().getFieldProvider().getTimestampField() == null) {
// We have no barrier timestamp, so perform truncate to avoid duplicates
truncate(log, writer);
collector.addMessage(new Status(IStatus.INFO, IndexPlugin.PLUGIN_ID,
Messages.LuceneIndexService_info_autoTruncate_noTimestampField));
} else if (!hasDateComponent(log)) {
// The date format only contains time components, so perform truncate to avoid duplicates
truncate(log, writer);
collector.addMessage(new Status(IStatus.INFO, IndexPlugin.PLUGIN_ID,
Messages.LuceneIndexService_info_autoTruncate_noDateComponent));
}
// Perform synchronize
log.synchronize(collector, monitor);
return new SynchronizationResult(monitor.isCanceled(), collector.getTotalCollected(),
System.currentTimeMillis() - startTime, collector.getMessages());
}
};
return runnable.runWithIndexWriter(log, getAnalyzer(), getMatchVersion());
}