pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / index / TestSegmentInfo.java
1 package org.apache.lucene.index;
2
3 import java.io.IOException;
4 import java.util.Iterator;
5
6 import org.apache.lucene.analysis.MockAnalyzer;
7 import org.apache.lucene.document.Document;
8 import org.apache.lucene.document.Field;
9 import org.apache.lucene.document.Field.Index;
10 import org.apache.lucene.document.Field.Store;
11 import org.apache.lucene.document.Field.TermVector;
12 import org.apache.lucene.store.Directory;
13 import org.apache.lucene.util.LuceneTestCase;
14 import org.apache.lucene.util._TestUtil;
15
16 /**
17  * Licensed to the Apache Software Foundation (ASF) under one or more
18  * contributor license agreements.  See the NOTICE file distributed with
19  * this work for additional information regarding copyright ownership.
20  * The ASF licenses this file to You under the Apache License, Version 2.0
21  * (the "License"); you may not use this file except in compliance with
22  * the License.  You may obtain a copy of the License at
23  *
24  *     http://www.apache.org/licenses/LICENSE-2.0
25  *
26  * Unless required by applicable law or agreed to in writing, software
27  * distributed under the License is distributed on an "AS IS" BASIS,
28  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29  * See the License for the specific language governing permissions and
30  * limitations under the License.
31  */
32
33 public class TestSegmentInfo extends LuceneTestCase {
34
35   public void testSizeInBytesCache() throws Exception {
36     Directory dir = newDirectory();
37     IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy());
38     IndexWriter writer = new IndexWriter(dir, conf);
39     Document doc = new Document();
40     doc.add(new Field("a", "value", Store.YES, Index.ANALYZED));
41     writer.addDocument(doc);
42     writer.close();
43     
44     SegmentInfos sis = new SegmentInfos();
45     sis.read(dir);
46     SegmentInfo si = sis.info(0);
47     long sizeInBytesNoStore = si.sizeInBytes(false);
48     long sizeInBytesWithStore = si.sizeInBytes(true);
49     assertTrue("sizeInBytesNoStore=" + sizeInBytesNoStore + " sizeInBytesWithStore=" + sizeInBytesWithStore, sizeInBytesWithStore > sizeInBytesNoStore);
50     dir.close();
51   }
52   
53   // LUCENE-2584: calling files() by multiple threads could lead to ConcurrentModificationException
54   public void testFilesConcurrency() throws Exception {
55     Directory dir = newDirectory();
56     // Create many files
57     IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random));
58     IndexWriter writer = new IndexWriter(dir, conf);
59     Document doc = new Document();
60     doc.add(new Field("a", "b", Store.YES, Index.ANALYZED, TermVector.YES));
61     writer.addDocument(doc);
62     writer.close();
63     
64     SegmentInfos sis = new SegmentInfos();
65     sis.read(dir);
66     final SegmentInfo si = sis.info(0);
67     Thread[] threads = new Thread[_TestUtil.nextInt(random, 2, 5)];
68     for (int i = 0; i < threads.length; i++) {
69       threads[i] = new Thread() {
70         @Override
71         public void run() {
72           try {
73             // Verify that files() does not throw an exception and that the
74             // iteration afterwards succeeds.
75             Iterator<String> iter = si.files().iterator();
76             while (iter.hasNext()) iter.next();
77           } catch (IOException e) {
78             throw new RuntimeException(e);
79           }
80         }
81       };
82     }
83     
84     for (Thread t : threads) t.start();
85     for (Thread t : threads) t.join();
86     
87     dir.close();
88   }
89   
90 }