/*
 * 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.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.FilteredTermsEnum;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.analysis.MockAnalyzer;
import org.apache.lucene.tests.analysis.MockTokenizer;
import org.apache.lucene.tests.index.RandomIndexWriter;
import org.apache.lucene.tests.search.CheckHits;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.tests.util.TestUtil;
import org.apache.lucene.util.AttributeSource;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.StringHelper;

/**
 * Create an index with random unicode terms Generates random prefix queries, and validates against
 * a simple impl.
 */
public class TestPrefixRandom extends LuceneTestCase {
  private IndexSearcher searcher;
  private IndexReader reader;
  private Directory dir;

  @Override
  public void setUp() throws Exception {
    super.setUp();
    dir = newDirectory();
    RandomIndexWriter writer =
        new RandomIndexWriter(
            random(),
            dir,
            newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.KEYWORD, false))
                .setMaxBufferedDocs(TestUtil.nextInt(random(), 50, 1000)));

    Document doc = new Document();
    Field field = newStringField("field", "", Field.Store.NO);
    doc.add(field);

    int num = atLeast(1000);
    for (int i = 0; i < num; i++) {
      field.setStringValue(TestUtil.randomUnicodeString(random(), 10));
      writer.addDocument(doc);
    }
    reader = writer.getReader();
    searcher = newSearcher(reader);
    writer.close();
  }

  @Override
  public void tearDown() throws Exception {
    reader.close();
    dir.close();
    super.tearDown();
  }

  /** a stupid prefix query that just blasts thru the terms */
  private static class DumbPrefixQuery extends MultiTermQuery {
    private final BytesRef prefix;

    DumbPrefixQuery(Term term) {
      super(term.field(), CONSTANT_SCORE_BLENDED_REWRITE);
      prefix = term.bytes();
    }

    @Override
    protected TermsEnum getTermsEnum(Terms terms, AttributeSource atts) throws IOException {
      return new SimplePrefixTermsEnum(terms.iterator(), prefix);
    }

    private static class SimplePrefixTermsEnum extends FilteredTermsEnum {
      private final BytesRef prefix;

      private SimplePrefixTermsEnum(TermsEnum tenum, BytesRef prefix) {
        super(tenum);
        this.prefix = prefix;
        setInitialSeekTerm(new BytesRef(""));
      }

      @Override
      protected AcceptStatus accept(BytesRef term) throws IOException {
        return StringHelper.startsWith(term, prefix) ? AcceptStatus.YES : AcceptStatus.NO;
      }
    }

    @Override
    public String toString(String field) {
      return field.toString() + ":" + prefix.toString();
    }

    @Override
    public void visit(QueryVisitor visitor) {}

    @Override
    public boolean equals(Object obj) {
      if (super.equals(obj) == false) {
        return false;
      }
      final DumbPrefixQuery that = (DumbPrefixQuery) obj;
      return prefix.equals(that.prefix);
    }
  }

  /** test a bunch of random prefixes */
  public void testPrefixes() throws Exception {
    int num = atLeast(100);
    for (int i = 0; i < num; i++) assertSame(TestUtil.randomUnicodeString(random(), 5));
  }

  /** check that the # of hits is the same as from a very simple prefixquery implementation. */
  private void assertSame(String prefix) throws IOException {
    PrefixQuery smart = new PrefixQuery(new Term("field", prefix));
    DumbPrefixQuery dumb = new DumbPrefixQuery(new Term("field", prefix));

    TopDocs smartDocs = searcher.search(smart, 25);
    TopDocs dumbDocs = searcher.search(dumb, 25);
    CheckHits.checkEqual(smart, smartDocs.scoreDocs, dumbDocs.scoreDocs);
  }
}
