1 package org.apache.lucene.index;
3 import java.io.IOException;
4 import java.util.Iterator;
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;
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
24 * http://www.apache.org/licenses/LICENSE-2.0
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.
33 public class TestSegmentInfo extends LuceneTestCase {
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);
44 SegmentInfos sis = new SegmentInfos();
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);
53 // LUCENE-2584: calling files() by multiple threads could lead to ConcurrentModificationException
54 public void testFilesConcurrency() throws Exception {
55 Directory dir = newDirectory();
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);
64 SegmentInfos sis = new SegmentInfos();
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() {
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);
84 for (Thread t : threads) t.start();
85 for (Thread t : threads) t.join();