/*
 * 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.
 */

package org.apache.lucene.search;

import java.io.IOException;

import org.apache.lucene.document.Field;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.FixedBitSet;



/**
 *
 */
public class TestFilteredSearch extends LuceneTestCase {

  private static final String FIELD = "category";
  
  public void testFilteredSearch() throws IOException {
    boolean enforceSingleSegment = true;
    Directory directory = newDirectory();
    int[] filterBits = {1, 36};
    SimpleDocIdSetFilter filter = new SimpleDocIdSetFilter(filterBits);
    IndexWriter writer = new IndexWriter(directory, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random())).setMergePolicy(newLogMergePolicy()));
    searchFiltered(writer, directory, filter, enforceSingleSegment);
    // run the test on more than one segment
    enforceSingleSegment = false;
    // reset - it is stateful
    filter.reset();
    writer.close();
    writer = new IndexWriter(directory, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random())).setOpenMode(OpenMode.CREATE).setMaxBufferedDocs(10).setMergePolicy(newLogMergePolicy()));
    // we index 60 docs - this will create 6 segments
    searchFiltered(writer, directory, filter, enforceSingleSegment);
    writer.close();
    directory.close();
  }

  public void searchFiltered(IndexWriter writer, Directory directory, Filter filter, boolean fullMerge) {
    try {
      for (int i = 0; i < 60; i++) {//Simple docs
        Document doc = new Document();
        doc.add(newStringField(FIELD, Integer.toString(i), Field.Store.YES));
        writer.addDocument(doc);
      }
      if (fullMerge) {
        writer.forceMerge(1);
      }
      writer.close();

      BooleanQuery booleanQuery = new BooleanQuery();
      booleanQuery.add(new TermQuery(new Term(FIELD, "36")), BooleanClause.Occur.SHOULD);
     
     
      IndexReader reader = DirectoryReader.open(directory);
      IndexSearcher indexSearcher = newSearcher(reader);
      ScoreDoc[] hits = indexSearcher.search(booleanQuery, filter, 1000).scoreDocs;
      assertEquals("Number of matched documents", 1, hits.length);
      reader.close();
    }
    catch (IOException e) {
      fail(e.getMessage());
    }
    
  }
 
  public static final class SimpleDocIdSetFilter extends Filter {
    private final int[] docs;
    private int index;
    
    public SimpleDocIdSetFilter(int[] docs) {
      this.docs = docs;
    }

    @Override
    public DocIdSet getDocIdSet(AtomicReaderContext context, Bits acceptDocs) {
      assertNull("acceptDocs should be null, as we have an index without deletions", acceptDocs);
      final FixedBitSet set = new FixedBitSet(context.reader().maxDoc());
      int docBase = context.docBase;
      final int limit = docBase+context.reader().maxDoc();
      for (;index < docs.length; index++) {
        final int docId = docs[index];
        if(docId > limit)
          break;
        if (docId >= docBase) {
          set.set(docId-docBase);
        }
      }
      return set.cardinality() == 0 ? null:set;
    }
    
    public void reset(){
      index = 0;
    }
  }

}

@@ -2126,58 +2126,8 @@
 se;%0A
-    // reset - it is stateful%0A    filter.reset();%0A
     
@@ -2623,26 +2623,33 @@
 ge) 
-%7B%0A    try
+throws IOException
  %7B%0A    
- 
- 
 for 
@@ -2694,18 +2694,16 @@
 s%0A      
-  
 Document
@@ -2731,18 +2731,16 @@
 ;%0A      
- 
- 
 doc.add(
@@ -2803,26 +2803,24 @@
 ES));%0A      
-  
 writer.addDo
@@ -2832,30 +2832,26 @@
 t(doc);%0A    
- 
- 
 %7D%0A
-  
     if (full
@@ -2861,26 +2861,24 @@
 ge) %7B%0A      
- 
- 
 writer.force
@@ -2891,26 +2891,22 @@
 1);%0A    
-  
 %7D%0A    
- 
- 
 writer.c
@@ -2914,18 +2914,16 @@
 ose();%0A%0A
-  
     Bool
@@ -2970,18 +2970,16 @@
 ();%0A    
- 
- 
 booleanQ
@@ -3066,26 +3066,24 @@
  %0A     %0A    
-  
 IndexReader 
@@ -3128,18 +3128,16 @@
 y);%0A    
- 
- 
 IndexSea
@@ -3179,18 +3179,16 @@
 eader);%0A
-  
     Scor
@@ -3261,26 +3261,24 @@
 reDocs;%0A    
- 
- 
 assertEquals
@@ -3330,18 +3330,16 @@
 h);%0A    
-  
 reader.c
@@ -3350,81 +3350,8 @@
 ();%0A
-    %7D%0A    catch (IOException e) %7B%0A      fail(e.getMessage());%0A    %7D%0A    %0A
   %7D%0A
@@ -3452,31 +3452,8 @@
 cs;%0A
-    private int index;%0A
     
@@ -3903,16 +3903,27 @@
    for (
+int index=0
 ;index %3C
@@ -3996,24 +3996,25 @@
       if
+ 
 (docId %3E
  limit)%0A
@@ -4009,61 +4009,34 @@
 Id %3E
- limit)%0A          break;%0A        if (docId %3E= docBase
+= docBase && docId %3C limit
 ) %7B%0A
@@ -4145,61 +4145,8 @@
   %7D%0A
-    %0A    public void reset()%7B%0A      index = 0;%0A    %7D%0A
   %7D%0A

fix test bug