<commit_before>package org.apache.lucene.index;

/**
 * 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,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.io.IOException;
import java.util.List;

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.index.CheckIndex.Status;
import org.apache.lucene.index.CheckIndex.Status.SegmentInfoStatus;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.codecs.Codec;
import org.apache.lucene.index.codecs.CodecProvider;
import org.apache.lucene.index.codecs.mocksep.MockSepCodec;
import org.apache.lucene.index.codecs.simpletext.SimpleTextCodec;
import org.apache.lucene.index.codecs.standard.StandardCodec;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;
import org.junit.Test;

/**
 * 
 *
 */
public class TestPerFieldCodecSupport extends LuceneTestCase {

  private IndexWriter newWriter(Directory dir, IndexWriterConfig conf)
      throws IOException {
    LogDocMergePolicy logByteSizeMergePolicy = new LogDocMergePolicy();
    logByteSizeMergePolicy.setUseCompoundFile(false); // make sure we use plain
    // files
    conf.setMergePolicy(logByteSizeMergePolicy);

    final IndexWriter writer = new IndexWriter(dir, conf);
    return writer;
  }

  private void addDocs(IndexWriter writer, int numDocs) throws IOException {
    for (int i = 0; i < numDocs; i++) {
      Document doc = new Document();
      doc.add(newField("content", "aaa", Field.Store.NO, Field.Index.ANALYZED));
      writer.addDocument(doc);
    }
  }

  private void addDocs2(IndexWriter writer, int numDocs) throws IOException {
    for (int i = 0; i < numDocs; i++) {
      Document doc = new Document();
      doc.add(newField("content", "bbb", Field.Store.NO, Field.Index.ANALYZED));
      writer.addDocument(doc);
    }
  }

  private void addDocs3(IndexWriter writer, int numDocs) throws IOException {
    for (int i = 0; i < numDocs; i++) {
      Document doc = new Document();
      doc.add(newField("content", "ccc", Field.Store.NO, Field.Index.ANALYZED));
      doc.add(newField("id", "" + i, Field.Store.YES, Field.Index.ANALYZED));
      writer.addDocument(doc);
    }
  }

  /*
   * Test is hetrogenous index segements are merge sucessfully
   */
  @Test
  public void testMergeUnusedPerFieldCodec() throws IOException {
    Directory dir = newDirectory();
    CodecProvider provider = new MockCodecProvider();
    IndexWriterConfig iwconf = newIndexWriterConfig(TEST_VERSION_CURRENT,
        new MockAnalyzer()).setOpenMode(OpenMode.CREATE).setCodecProvider(
        provider);
    IndexWriter writer = newWriter(dir, iwconf);
    addDocs(writer, 10);
    writer.commit();
    addDocs3(writer, 10);
    writer.commit();
    addDocs2(writer, 10);
    writer.commit();
    assertEquals(30, writer.maxDoc());
    _TestUtil.checkIndex(dir, provider);
    writer.optimize();
    assertEquals(30, writer.maxDoc());
    writer.close();
    dir.close();
  }

  /*
   * Test is hetrogenous index segements are merge sucessfully
   */
  @Test
  public void testChangeCodecAndMerge() throws IOException {
    Directory dir = newDirectory();
    CodecProvider provider = new MockCodecProvider();
    IndexWriterConfig iwconf = newIndexWriterConfig(TEST_VERSION_CURRENT,
        new MockAnalyzer()).setOpenMode(OpenMode.CREATE).setCodecProvider(
        provider);
    IndexWriter writer = newWriter(dir, iwconf);
    addDocs(writer, 10);
    writer.commit();
    assertQuery(new Term("content", "aaa"), dir, 10, provider);
    addDocs3(writer, 10);
    writer.commit();
    writer.close();

    assertQuery(new Term("content", "ccc"), dir, 10, provider);
    assertQuery(new Term("content", "aaa"), dir, 10, provider);
    assertCodecPerField(_TestUtil.checkIndex(dir, provider), "content",
        provider.lookup("MockSep"));

    iwconf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer())
        .setOpenMode(OpenMode.APPEND).setCodecProvider(provider);
    ((LogMergePolicy) iwconf.getMergePolicy()).setUseCompoundFile(false);
    ((LogMergePolicy) iwconf.getMergePolicy()).setUseCompoundDocStore(false);

    provider = new MockCodecProvider2(); // uses standard for field content
    iwconf.setCodecProvider(provider);
    writer = newWriter(dir, iwconf);
    // swap in new codec for currently written segments
    addDocs2(writer, 10);
    writer.commit();
    Codec origContentCodec = provider.lookup("MockSep");
    Codec newContentCodec = provider.lookup("Standard");
    assertHybridCodecPerField(_TestUtil.checkIndex(dir, provider), "content",
        origContentCodec, origContentCodec, newContentCodec);
    assertEquals(30, writer.maxDoc());
    assertQuery(new Term("content", "bbb"), dir, 10, provider);
    assertQuery(new Term("content", "ccc"), dir, 10, provider);
    assertQuery(new Term("content", "aaa"), dir, 10, provider);
<commit_msg>LUCENE-2758: fix intermittent fail<commit_after>package org.apache.lucene.index;

/**
 * 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,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.io.IOException;
import java.util.List;

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.index.CheckIndex.Status;
import org.apache.lucene.index.CheckIndex.Status.SegmentInfoStatus;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.codecs.Codec;
import org.apache.lucene.index.codecs.CodecProvider;
import org.apache.lucene.index.codecs.mocksep.MockSepCodec;
import org.apache.lucene.index.codecs.simpletext.SimpleTextCodec;
import org.apache.lucene.index.codecs.standard.StandardCodec;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;
import org.junit.Test;

/**
 * 
 *
 */
public class TestPerFieldCodecSupport extends LuceneTestCase {

  private IndexWriter newWriter(Directory dir, IndexWriterConfig conf)
      throws IOException {
    LogDocMergePolicy logByteSizeMergePolicy = new LogDocMergePolicy();
    logByteSizeMergePolicy.setUseCompoundFile(false); // make sure we use plain
    // files
    conf.setMergePolicy(logByteSizeMergePolicy);

    final IndexWriter writer = new IndexWriter(dir, conf);
    return writer;
  }

  private void addDocs(IndexWriter writer, int numDocs) throws IOException {
    for (int i = 0; i < numDocs; i++) {
      Document doc = new Document();
      doc.add(newField("content", "aaa", Field.Store.NO, Field.Index.ANALYZED));
      writer.addDocument(doc);
    }
  }

  private void addDocs2(IndexWriter writer, int numDocs) throws IOException {
    for (int i = 0; i < numDocs; i++) {
      Document doc = new Document();
      doc.add(newField("content", "bbb", Field.Store.NO, Field.Index.ANALYZED));
      writer.addDocument(doc);
    }
  }

  private void addDocs3(IndexWriter writer, int numDocs) throws IOException {
    for (int i = 0; i < numDocs; i++) {
      Document doc = new Document();
      doc.add(newField("content", "ccc", Field.Store.NO, Field.Index.ANALYZED));
      doc.add(newField("id", "" + i, Field.Store.YES, Field.Index.ANALYZED));
      writer.addDocument(doc);
    }
  }

  /*
   * Test is hetrogenous index segements are merge sucessfully
   */
  @Test
  public void testMergeUnusedPerFieldCodec() throws IOException {
    Directory dir = newDirectory();
    CodecProvider provider = new MockCodecProvider();
    IndexWriterConfig iwconf = newIndexWriterConfig(TEST_VERSION_CURRENT,
        new MockAnalyzer()).setOpenMode(OpenMode.CREATE).setCodecProvider(
        provider);
    IndexWriter writer = newWriter(dir, iwconf);
    addDocs(writer, 10);
    writer.commit();
    addDocs3(writer, 10);
    writer.commit();
    addDocs2(writer, 10);
    writer.commit();
    assertEquals(30, writer.maxDoc());
    _TestUtil.checkIndex(dir, provider);
    writer.optimize();
    assertEquals(30, writer.maxDoc());
    writer.close();
    dir.close();
  }

  /*
   * Test is hetrogenous index segements are merge sucessfully
   */
  @Test
  public void testChangeCodecAndMerge() throws IOException {
    Directory dir = newDirectory();
    CodecProvider provider = new MockCodecProvider();
    IndexWriterConfig iwconf = newIndexWriterConfig(TEST_VERSION_CURRENT,
             new MockAnalyzer()).setOpenMode(OpenMode.CREATE).setCodecProvider(provider);
    iwconf.setMaxBufferedDocs(IndexWriterConfig.DISABLE_AUTO_FLUSH);
    ((LogMergePolicy) iwconf.getMergePolicy()).setMergeFactor(10);
    IndexWriter writer = newWriter(dir, iwconf);

    addDocs(writer, 10);
    writer.commit();
    assertQuery(new Term("content", "aaa"), dir, 10, provider);
    addDocs3(writer, 10);
    writer.commit();
    writer.close();

    assertQuery(new Term("content", "ccc"), dir, 10, provider);
    assertQuery(new Term("content", "aaa"), dir, 10, provider);
    assertCodecPerField(_TestUtil.checkIndex(dir, provider), "content",
        provider.lookup("MockSep"));

    iwconf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer())
        .setOpenMode(OpenMode.APPEND).setCodecProvider(provider);
    ((LogMergePolicy) iwconf.getMergePolicy()).setUseCompoundFile(false);
    ((LogMergePolicy) iwconf.getMergePolicy()).setUseCompoundDocStore(false);
    ((LogMergePolicy) iwconf.getMergePolicy()).setMergeFactor(10);
    iwconf.setMaxBufferedDocs(IndexWriterConfig.DISABLE_AUTO_FLUSH);

    provider = new MockCodecProvider2(); // uses standard for field content
    iwconf.setCodecProvider(provider);
    writer = newWriter(dir, iwconf);
    // swap in new codec for currently written segments
    addDocs2(writer, 10);
    writer.commit();
    Codec origContentCodec = provider.lookup("MockSep");
    Codec newContentCodec = provider.lookup("Standard");
    assertHybridCodecPerField(_TestUtil.checkIndex(dir, provider), "content",
        origContentCodec, origContentCodec, newContentCodec);
    assertEquals(30, writer.maxDoc());
    assertQuery(new Term("content", "bbb"), dir, 10, provider);
    assertQuery(new Term("content", "ccc"), dir, 10, provider);
    assertQuery(new Term("content", "aaa"), dir, 10, provider);

{'fraction_non_alphanumeric': 0.11446977205153618, 'fraction_numerical': 0.007928642220019821, 'mean_word_length': 4.310964912280702, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '419210', 'n_tokens_mistral': 3666, 'n_tokens_neox': 3549, 'n_words': 884}