1 package org.apache.lucene.index;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
33 public class TestForTooMuchCloning extends LuceneTestCase {
35 // Make sure we don't clone IndexInputs too frequently
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));
50 final Document doc = new Document();
51 doc.add(newField("field", sb.toString(), Field.Store.NO, Field.Index.ANALYZED));
54 final IndexReader r = w.getReader();
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);
61 final IndexSearcher s = new IndexSearcher(r);
63 // MTQ that matches all terms so the AUTO_REWRITE should
64 // cutover to filter rewrite and reuse a single DocsEnum
66 final TopDocs hits = s.search(new TermRangeQuery("field",
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);