package org.apache.lucene.index.sorter;

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

import org.apache.lucene.index.AtomicReader;
import org.apache.lucene.search.SortField;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.TestUtil;
import org.apache.lucene.util.TestUtil;
import org.junit.BeforeClass;

public class SortingAtomicReaderTest extends SorterTestBase {
  
  @BeforeClass
  public static void beforeClassSortingAtomicReaderTest() throws Exception {
    // build the mapping from the reader, since we deleted documents, some of
    // them might have disappeared from the index (e.g. if an entire segment is
    // dropped b/c all its docs are deleted)
    final int[] values = new int[reader.maxDoc()];
    for (int i = 0; i < reader.maxDoc(); i++) {
      values[i] = Integer.valueOf(reader.document(i).get(ID_FIELD));
    }
    final Sorter.DocComparator comparator = new Sorter.DocComparator() {
      @Override
      public int compare(int docID1, int docID2) {
        final int v1 = values[docID1];
        final int v2 = values[docID2];
        return v1 < v2 ? -1 : v1 == v2 ? 0 : 1;
      }
    };

    final Sorter.DocMap docMap = Sorter.sort(reader.maxDoc(), comparator);
    // Sorter.compute also sorts the values
    sortedValues = new Integer[reader.maxDoc()];
    for (int i = 0; i < reader.maxDoc(); ++i) {
      sortedValues[docMap.oldToNew(i)] = values[i];
    }
    if (VERBOSE) {
      System.out.println("docMap: " + docMap);
      System.out.println("sortedValues: " + Arrays.toString(sortedValues));
    }
    
    // TODO: what is this doing? like a no-op sort?
    reader = SortingAtomicReader.wrap(reader, docMap);
    
    if (VERBOSE) {
      System.out.print("mapped-deleted-docs: ");
      Bits mappedLiveDocs = reader.getLiveDocs();
      for (int i = 0; i < mappedLiveDocs.length(); i++) {
        if (!mappedLiveDocs.get(i)) {
          System.out.print(i + " ");
        }
      }
      System.out.println();
    }
    
    TestUtil.checkReader(reader);
  }

}

@@ -841,36 +841,8 @@
 */%0A%0A
-import java.io.IOException;%0A
 impo
@@ -894,28 +894,70 @@
 e.index.
-AtomicReader
+NumericDocValues;%0Aimport org.apache.lucene.search.Sort
 ;%0Aimport
@@ -1073,48 +1073,8 @@
 il;%0A
-import org.apache.lucene.util.TestUtil;%0A
 impo
@@ -1265,781 +1265,335 @@
     
-// build the mapping from the reader, since we deleted documents, some of%0A    // them might have disappeared from the index (e.g. if an entire segment is%0A    // dropped b/c all its docs are deleted)%0A    final int%5B%5D values = new int%5Breader.maxDoc()%5D;%0A    for (int i = 0; i %3C reader.maxDoc(); i++) %7B%0A      values%5Bi%5D = Integer.valueOf(reader.document(i).get(ID
+%0A    // sort the index by id (as integer, in NUMERIC_DV
 _FIELD)
-);
 %0A    
-%7D%0A    final Sorter.DocComparator comparator = new Sorter.DocComparator() %7B%0A      @Override%0A      public int compare(int docID1, int docID2) %7B%0A        final int v1 = values%5BdocID1%5D;%0A        final int v2 = values%5BdocID2%5D;%0A        return v1 %3C v2 ? -1 : v1 == v2 ? 0 : 1;%0A      %7D%0A    %7D;%0A%0A    final Sorter.DocMap docMap = Sorter.sort(reader.maxDoc(), comparator);%0A    // Sorter.compute also sorts the values
+Sort sort = new Sort(new SortField(NUMERIC_DV_FIELD, SortField.Type.INT));%0A    final Sorter.DocMap docMap = new SortSorter(sort).sort(reader);%0A %0A    // Sorter.compute also sorts the values%0A    NumericDocValues dv = reader.getNumericDocValues(NUMERIC_DV_FIELD);
 %0A   
@@ -1731,17 +1731,22 @@
 %5D = 
-values%5Bi%5D
+(int)dv.get(i)
 ;%0A  
@@ -1913,52 +1913,62 @@
  // 
-TODO: what is this doing? like a no-op sort?
+sort the index by id (as integer, in NUMERIC_DV_FIELD)
 %0A   
@@ -2010,22 +2010,20 @@
 reader, 
-docMap
+sort
 );%0A    %0A

simplify this test