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 org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;

/**
 * Tests {@link Terms#getSumDocFreq()}
 * @lucene.experimental
 */
public class TestSumDocFreq extends LuceneTestCase {
  
  public void testSumDocFreq() throws Exception {
    final int numDocs = atLeast(500);
    
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random, dir);
    
    Document doc = new Document();
    Field field1 = newField("foo", "", TextField.TYPE_UNSTORED);
    Field field2 = newField("bar", "", TextField.TYPE_UNSTORED);
    doc.add(field1);
    doc.add(field2);
    for (int i = 0; i < numDocs; i++) {
      char ch1 = (char) _TestUtil.nextInt(random, 'a', 'z');
      char ch2 = (char) _TestUtil.nextInt(random, 'a', 'z');
      field1.setValue("" + ch1 + " " + ch2);
      ch1 = (char) _TestUtil.nextInt(random, 'a', 'z');
      ch2 = (char) _TestUtil.nextInt(random, 'a', 'z');
      field2.setValue("" + ch1 + " " + ch2);
      writer.addDocument(doc);
    }
    
    IndexReader ir = writer.getReader();
    writer.close();
    
    assertSumDocFreq(ir);    
    ir.close();
    
    /* nocommit: fix this to use IW to delete documents
    ir = IndexReader.open(dir);
    int numDeletions = atLeast(20);
    for (int i = 0; i < numDeletions; i++) {
      ir.deleteDocument(random.nextInt(ir.maxDoc()));
    }
    ir.close();
    
    IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
    w.forceMerge(1);
    w.close();
    
    ir = IndexReader.open(dir);
    assertSumDocFreq(ir);
    ir.close();
    */

    dir.close();
  }
  
  private void assertSumDocFreq(IndexReader ir) throws Exception {
    // compute sumDocFreq across all fields
    Fields fields = MultiFields.getFields(ir);
    FieldsEnum fieldEnum = fields.iterator();
    String f = null;
    while ((f = fieldEnum.next()) != null) {
      Terms terms = fields.terms(f);
      long sumDocFreq = terms.getSumDocFreq();
      if (sumDocFreq == -1) {
        if (VERBOSE) {
          System.out.println("skipping field: " + f + ", codec does not support sumDocFreq");
        }
        continue;
      }
      
      long computedSumDocFreq = 0;
      TermsEnum termsEnum = terms.iterator(null);
      while (termsEnum.next() != null) {
        computedSumDocFreq += termsEnum.docFreq();
      }
      assertEquals(computedSumDocFreq, sumDocFreq);
    }
  }
}

@@ -835,56 +835,8 @@
 */%0A%0A
-import org.apache.lucene.analysis.MockAnalyzer;%0A
 impo
@@ -912,24 +912,71 @@
 ment.Field;%0A
+import org.apache.lucene.document.StringField;%0A
 import org.a
@@ -1497,24 +1497,86 @@
 Document();%0A
+    Field id = newField(%22id%22, %22%22, StringField.TYPE_UNSTORED);%0A
     Field fi
@@ -1689,24 +1689,41 @@
 _UNSTORED);%0A
+    doc.add(id);%0A
     doc.add(
@@ -1788,24 +1788,51 @@
 ocs; i++) %7B%0A
+      id.setValue(%22%22 + i);%0A
       char c
@@ -2230,28 +2230,8 @@
 ();%0A
-    writer.close();%0A
     
@@ -2286,96 +2286,8 @@
    %0A
-    /* nocommit: fix this to use IW to delete documents%0A    ir = IndexReader.open(dir);%0A
     
@@ -2369,17 +2369,21 @@
 %7B%0A      
-i
+write
 r.delete
@@ -2390,17 +2390,38 @@
 Document
-(
+s(new Term(%22id%22, %22%22 + 
 random.n
@@ -2431,18 +2431,15 @@
 Int(
-ir.maxDoc(
+numDocs
 )));
@@ -2453,142 +2453,14 @@
     
-ir.close();%0A    %0A    IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));%0A    w
+writer
 .for
@@ -2476,16 +2476,21 @@
 );%0A    w
+riter
 .close()
@@ -2574,16 +2574,8 @@
 ();%0A
-    */%0A%0A
     

fix test