pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / index / Test2BPostings.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.io.IOException;
21
22 import org.apache.lucene.analysis.MockAnalyzer;
23 import org.apache.lucene.analysis.TokenStream;
24 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.document.Field;
27 import org.apache.lucene.index.FieldInfo.IndexOptions;
28 import org.apache.lucene.store.MockDirectoryWrapper;
29 import org.apache.lucene.util.LuceneTestCase;
30 import org.apache.lucene.util._TestUtil;
31
32 /**
33  * Test indexes ~82M docs with 26 terms each, so you get > Integer.MAX_VALUE terms/docs pairs
34  * @lucene.experimental
35  */
36 public class Test2BPostings extends LuceneTestCase {
37
38   @Nightly
39   public void test() throws Exception {
40
41     MockDirectoryWrapper dir = newFSDirectory(_TestUtil.getTempDir("2BPostings"));
42     dir.setThrottling(MockDirectoryWrapper.Throttling.NEVER);
43     dir.setCheckIndexOnClose(false); // don't double-checkindex
44     
45     IndexWriter w = new IndexWriter(dir,
46         new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
47         .setMaxBufferedDocs(IndexWriterConfig.DISABLE_AUTO_FLUSH)
48         .setRAMBufferSizeMB(256.0)
49         .setMergeScheduler(new ConcurrentMergeScheduler())
50         .setMergePolicy(newLogMergePolicy(false, 10))
51         .setOpenMode(IndexWriterConfig.OpenMode.CREATE));
52
53     MergePolicy mp = w.getConfig().getMergePolicy();
54     if (mp instanceof LogByteSizeMergePolicy) {
55      // 1 petabyte:
56      ((LogByteSizeMergePolicy) mp).setMaxMergeMB(1024*1024*1024);
57     }
58
59     Document doc = new Document();
60     Field field = new Field("field", new MyTokenStream());
61     field.setIndexOptions(IndexOptions.DOCS_ONLY);
62     field.setOmitNorms(true);
63     doc.add(field);
64     
65     final int numDocs = (Integer.MAX_VALUE / 26) + 1;
66     for (int i = 0; i < numDocs; i++) {
67       w.addDocument(doc);
68       if (VERBOSE && i % 100000 == 0) {
69         System.out.println(i + " of " + numDocs + "...");
70       }
71     }
72     w.forceMerge(1);
73     w.close();
74     CheckIndex ci = new CheckIndex(dir);
75     if (VERBOSE) {
76       ci.setInfoStream(System.out);
77     }
78     ci.checkIndex();
79     dir.close();
80   }
81   
82   public static final class MyTokenStream extends TokenStream {
83     private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
84     private final char buffer[];
85     int index;
86
87     public MyTokenStream() {
88       termAtt.setLength(1);
89       buffer = termAtt.buffer();
90     }
91     
92     @Override
93     public boolean incrementToken() throws IOException {
94       if (index <= 'z') {
95         buffer[0] = (char) index++;
96         return true;
97       }
98       return false;
99     }
100     
101     @Override
102     public void reset() throws IOException {
103       index = 'a';
104     }
105   }
106 }