home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (TestSegmentInfo.java)

This example Lucene source code file (TestSegmentInfo.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lucene tags/keywords

directory, document, document, exception, field, indexwriter, indexwriter, indexwriterconfig, io, ioexception, segmentinfo, segmentinfos, segmentinfos, thread, thread, util

The Lucene TestSegmentInfo.java source code

package org.apache.lucene.index;

import java.io.IOException;
import java.util.Iterator;

import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.Field.TermVector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;

 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

public class TestSegmentInfo extends LuceneTestCase {

  public void testSizeInBytesCache() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy());
    IndexWriter writer = new IndexWriter(dir, conf);
    Document doc = new Document();
    doc.add(new Field("a", "value", Store.YES, Index.ANALYZED));
    SegmentInfos sis = new SegmentInfos();
    SegmentInfo si = sis.info(0);
    long sizeInBytesNoStore = si.sizeInBytes(false);
    long sizeInBytesWithStore = si.sizeInBytes(true);
    assertTrue("sizeInBytesNoStore=" + sizeInBytesNoStore + " sizeInBytesWithStore=" + sizeInBytesWithStore, sizeInBytesWithStore > sizeInBytesNoStore);
  // LUCENE-2584: calling files() by multiple threads could lead to ConcurrentModificationException
  public void testFilesConcurrency() throws Exception {
    Directory dir = newDirectory();
    // Create many files
    IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random));
    IndexWriter writer = new IndexWriter(dir, conf);
    Document doc = new Document();
    doc.add(new Field("a", "b", Store.YES, Index.ANALYZED, TermVector.YES));
    SegmentInfos sis = new SegmentInfos();
    final SegmentInfo si = sis.info(0);
    Thread[] threads = new Thread[_TestUtil.nextInt(random, 2, 5)];
    for (int i = 0; i < threads.length; i++) {
      threads[i] = new Thread() {
        public void run() {
          try {
            // Verify that files() does not throw an exception and that the
            // iteration afterwards succeeds.
            Iterator<String> iter = si.files().iterator();
            while (iter.hasNext()) iter.next();
          } catch (IOException e) {
            throw new RuntimeException(e);
    for (Thread t : threads) t.start();
    for (Thread t : threads) t.join();

Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene TestSegmentInfo.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.