pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / index / TestForTooMuchCloning.java
1 package org.apache.lucene.index;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.util.*;
21
22 import org.apache.lucene.analysis.MockAnalyzer;
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.document.Field;
25 import org.apache.lucene.search.IndexSearcher;
26 import org.apache.lucene.search.TermRangeQuery;
27 import org.apache.lucene.search.TopDocs;
28 import org.apache.lucene.store.MockDirectoryWrapper;
29 import org.apache.lucene.util.BytesRef;
30 import org.apache.lucene.util.LuceneTestCase;
31 import org.apache.lucene.util._TestUtil;
32
33 public class TestForTooMuchCloning extends LuceneTestCase {
34
35   // Make sure we don't clone IndexInputs too frequently
36   // during merging:
37   public void test() throws Exception {
38     final MockDirectoryWrapper dir = newDirectory();
39     final TieredMergePolicy tmp = new TieredMergePolicy();
40     tmp.setMaxMergeAtOnce(2);
41     final RandomIndexWriter w = new RandomIndexWriter(random, dir,
42                                                       newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMaxBufferedDocs(2).setMergePolicy(tmp));
43     final int numDocs = 20;
44     for(int docs=0;docs<numDocs;docs++) {
45       StringBuilder sb = new StringBuilder();
46       for(int terms=0;terms<100;terms++) {
47         sb.append(_TestUtil.randomRealisticUnicodeString(random));
48         sb.append(' ');
49       }
50       final Document doc = new Document();
51       doc.add(newField("field", sb.toString(), Field.Store.NO, Field.Index.ANALYZED));
52       w.addDocument(doc);
53     }
54     final IndexReader r = w.getReader();
55     w.close();
56
57     final int cloneCount = dir.getInputCloneCount();
58     //System.out.println("merge clone count=" + cloneCount);
59     assertTrue("too many calls to IndexInput.clone during merging: " + dir.getInputCloneCount(), cloneCount < 500);
60
61     final IndexSearcher s = new IndexSearcher(r);
62
63     // MTQ that matches all terms so the AUTO_REWRITE should
64     // cutover to filter rewrite and reuse a single DocsEnum
65     // across all terms;
66     final TopDocs hits = s.search(new TermRangeQuery("field",
67                                                      "",
68                                                      "\uFFFF",
69                                                      true,
70                                                      true), 10);
71     assertTrue(hits.totalHits > 0);
72     final int queryCloneCount = dir.getInputCloneCount() - cloneCount;
73     //System.out.println("query clone count=" + queryCloneCount);
74     assertTrue("too many calls to IndexInput.clone during TermRangeQuery: " + queryCloneCount, queryCloneCount < 50);
75     s.close();
76     r.close();
77     dir.close();
78   }
79 }