Index: lucene/CHANGES.txt =================================================================== --- lucene/CHANGES.txt (revision 1418093) +++ lucene/CHANGES.txt (working copy) @@ -128,6 +128,11 @@ rule files in the ICU RuleBasedBreakIterator format. (Shawn Heisey, Robert Muir, Steve Rowe) +* LUCENE-4590: Added WriteEnwikiLineDocTask - a benchmark task for writing + Wikipedia category pages and non-category pages into separate line files. + extractWikipedia.alg was changed to use this task, so now it creates two + files. (Doron Cohen) + API Changes * LUCENE-4399: Deprecated AppendingCodec. Lucene's term dictionaries Index: lucene/benchmark/conf/extractWikipedia.alg =================================================================== --- lucene/benchmark/conf/extractWikipedia.alg (revision 1418093) +++ lucene/benchmark/conf/extractWikipedia.alg (working copy) @@ -41,4 +41,4 @@ # ------------------------------------------------------------------------------------- # Process all documents, appending each one to the line file: -{WriteLineDoc() > : * +{WriteEnwikiLineDoc() > : * Index: lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/WriteLineDocTaskTest.java =================================================================== --- lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/WriteLineDocTaskTest.java (revision 1418093) +++ lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/WriteLineDocTaskTest.java (working copy) @@ -188,7 +188,7 @@ } } - private void assertHeaderLine(String line) { + static void assertHeaderLine(String line) { assertTrue("First line should be a header line",line.startsWith(WriteLineDocTask.FIELDS_HEADER_INDICATOR)); } Index: lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/WriteEnwikiLineDocTaskTest.java =================================================================== --- lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/WriteEnwikiLineDocTaskTest.java (revision 0) +++ lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/WriteEnwikiLineDocTaskTest.java (working copy) @@ -0,0 +1,114 @@ +package org.apache.lucene.benchmark.byTask.tasks; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.lucene.benchmark.BenchmarkTestCase; +import org.apache.lucene.benchmark.byTask.PerfRunData; +import org.apache.lucene.benchmark.byTask.feeds.DocMaker; +import org.apache.lucene.benchmark.byTask.utils.Config; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.StringField; + +/** Tests the functionality of {@link WriteEnwikiLineDocTask}. */ +public class WriteEnwikiLineDocTaskTest extends BenchmarkTestCase { + + + // class has to be public so that Class.forName.newInstance() will work + /** Interleaves category docs with regular docs */ + public static final class WriteLineCategoryDocMaker extends DocMaker { + + AtomicInteger flip = new AtomicInteger(0); + + @Override + public Document makeDocument() throws Exception { + boolean isCategory = (flip.incrementAndGet() % 2 == 0); + Document doc = new Document(); + doc.add(new StringField(BODY_FIELD, "body text", Field.Store.NO)); + doc.add(new StringField(TITLE_FIELD, isCategory ? "Category:title text" : "title text", Field.Store.NO)); + doc.add(new StringField(DATE_FIELD, "date text", Field.Store.NO)); + return doc; + } + + } + + private PerfRunData createPerfRunData(File file, String docMakerName) throws Exception { + Properties props = new Properties(); + props.setProperty("doc.maker", docMakerName); + props.setProperty("line.file.out", file.getAbsolutePath()); + props.setProperty("directory", "RAMDirectory"); // no accidental FS dir. + Config config = new Config(props); + return new PerfRunData(config); + } + + private void doReadTest(File file, String expTitle, + String expDate, String expBody) throws Exception { + doReadTest(2, file, expTitle, expDate, expBody); + File categoriesFile = WriteEnwikiLineDocTask.categoriesLineFile(file); + doReadTest(2, categoriesFile, "Category:"+expTitle, expDate, expBody); + } + + private void doReadTest(int n, File file, String expTitle, String expDate, String expBody) throws Exception { + InputStream in = new FileInputStream(file); + BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8")); + try { + String line = br.readLine(); + WriteLineDocTaskTest.assertHeaderLine(line); + for (int i=0; i threadBuffer = new ThreadLocal(); - private ThreadLocal threadNormalizer = new ThreadLocal(); - private final String[] fieldsToWrite;; + protected final String fname; + private final PrintWriter lineFileOut; + private final DocMaker docMaker; + private final ThreadLocal threadBuffer = new ThreadLocal(); + private final ThreadLocal threadNormalizer = new ThreadLocal(); + private final String[] fieldsToWrite; private final boolean[] sufficientFields; private final boolean checkSufficientFields; + public WriteLineDocTask(PerfRunData runData) throws Exception { super(runData); Config config = runData.getConfig(); - String fname = config.get("line.file.out", null); + fname = config.get("line.file.out", null); if (fname == null) { throw new IllegalArgumentException("line.file.out must be set"); } @@ -129,13 +130,13 @@ } } - writeHeader(); + writeHeader(lineFileOut); } /** - * Write a header to the lines file - indicating how to read the file later + * Write header to the lines file - indicating how to read the file later. */ - private void writeHeader() { + protected void writeHeader(PrintWriter out) { StringBuilder sb = threadBuffer.get(); if (sb == null) { sb = new StringBuilder(); @@ -146,7 +147,7 @@ for (String f : fieldsToWrite) { sb.append(SEP).append(f); } - lineFileOut.println(sb.toString()); + out.println(sb.toString()); } @Override @@ -181,12 +182,20 @@ if (sufficient) { sb.setLength(sb.length()-1); // remove redundant last separator // lineFileOut is a PrintWriter, which synchronizes internally in println. - lineFileOut.println(sb.toString()); + lineFileOut(doc).println(sb.toString()); } return 1; } + /** + * Selects output line file by written doc. + * Default: original output line file. + */ + protected PrintWriter lineFileOut(Document doc) { + return lineFileOut; + } + @Override public void close() throws Exception { lineFileOut.close();