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.util.BytesRef;

/**
 * A per-document byte[] with presorted values.
 * <p>
 * Per-Document values in a SortedDocValues are deduplicated, dereferenced,
 * and sorted into a dictionary of unique values. A pointer to the
 * dictionary value (ordinal) can be retrieved for each document. Ordinals
 * are dense and in increasing sorted order.
 */
public abstract class SortedDocValues extends BinaryDocValues {
  
  /** Sole constructor. (For invocation by subclass 
   * constructors, typically implicit.) */
  protected SortedDocValues() {}

  /**
   * Returns the ordinal for the specified docID.
   * @param  docID document ID to lookup
   * @return ordinal for the document: this is dense, starts at 0, then
   *         increments by 1 for the next value in sorted order. 
   */
  public abstract int getOrd(int docID);

  /** Retrieves the value for the specified ordinal.
   * @param ord ordinal to lookup
   * @param result will be populated with the ordinal's value
   * @see #getOrd(int) 
   */
  public abstract void lookupOrd(int ord, BytesRef result);

  /**
   * Returns the number of unique values.
   * @return number of unique values in this SortedDocValues. This is
   *         also equivalent to one plus the maximum ordinal.
   */
  public abstract int getValueCount();

  @Override
  public void get(int docID, BytesRef result) {
    int ord = getOrd(docID);
    if (ord == -1) {
      result.bytes = MISSING;
      result.length = 0;
      result.offset = 0;
    } else {
      lookupOrd(ord, result);
    }
  }

  /** An empty SortedDocValues which returns empty bytes for every document */
  public static final SortedDocValues EMPTY = new SortedDocValues() {
    @Override
    public int getOrd(int docID) {
      return 0;
    }

    @Override
    public void lookupOrd(int ord, BytesRef result) {
      result.length = 0;
    }

    @Override
    public int getValueCount() {
      return 1;
    }
  };

  /** If {@code key} exists, returns its ordinal, else
   *  returns {@code -insertionPoint-1}, like {@code
   *  Arrays.binarySearch}.
   *
   *  @param key Key to look up
   *  @param spare Spare BytesRef
   **/
  // nocommit: what does spare mean? its no spare: because people rely upon its return value!
  // if its 'result' then the parameter and javadoc needs changing, otherwise things need fixing
  // unconditionally set its value to "NONSENSE" bytes to see the bugs!
  public int lookupTerm(BytesRef key) {
    BytesRef spare = new BytesRef();
    int low = 0;
    int high = getValueCount()-1;

    while (low <= high) {
      int mid = (low + high) >>> 1;
      lookupOrd(mid, spare);
      int cmp = spare.compareTo(key);

      if (cmp < 0) {
        low = mid + 1;
      } else if (cmp > 0) {
        high = mid - 1;
      } else {
        return mid; // key found
      }
    }

    return -(low + 1);  // key not found.
  }
}

@@ -2964,307 +2964,10 @@
    *
-  @param spare Spare BytesRef%0A   **/%0A  // nocommit: what does spare mean? its no spare: because people rely upon its return value!%0A  // if its 'result' then the parameter and javadoc needs changing, otherwise things need fixing%0A  // unconditionally set its value to %22NONSENSE%22 bytes to see the bugs!
+*/
 %0A  p

fix javadocs/nocommit