package sorts;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Iterator;
import java.util.Map.Entry;

import org.apache.accumulo.core.Constants;
import org.apache.accumulo.core.client.BatchWriter;
import org.apache.accumulo.core.client.BatchWriterConfig;
import org.apache.accumulo.core.client.Connector;
import org.apache.accumulo.core.client.MutationsRejectedException;
import org.apache.accumulo.core.client.Scanner;
import org.apache.accumulo.core.client.TableNotFoundException;
import org.apache.accumulo.core.data.Key;
import org.apache.accumulo.core.data.Mutation;
import org.apache.accumulo.core.data.Range;
import org.apache.accumulo.core.data.Value;
import org.apache.hadoop.io.Text;

public class SortingMetadata {
  public static final Text EMPTY_TEXT = new Text("");
  public static final Text STATE_COLFAM = new Text("state");
  
  public enum State {
    LOADING,
    LOADED,
    ERROR,
    DELETING,
    UNKNOWN
  }
  
  public static State getState(SortableResult id) throws TableNotFoundException {
    checkNotNull(id);
    
    Connector con = id.connector();
    
    Scanner s = con.createScanner(id.metadataTable, Constants.NO_AUTHS);
    
    s.setRange(new Range(id.uuid()));
    
    s.fetchColumnFamily(STATE_COLFAM);
    
    Iterator<Entry<Key,Value>> iter = s.iterator();
    
    if (iter.hasNext()) {
      Entry<Key,Value> stateEntry = iter.next();
      return deserializeState(stateEntry.getValue());
    }
    
    return State.UNKNOWN;
  }
  
  public static void setState(SortableResult id, State state) throws TableNotFoundException, MutationsRejectedException {
    checkNotNull(id);
    checkNotNull(state);

    BatchWriter bw = null;
    try {
      bw = id.connector().createBatchWriter(id.metadataTable(), new BatchWriterConfig());
      Mutation m = new Mutation(id.uuid());
      m.put(STATE_COLFAM, EMPTY_TEXT, new Value(state.toString().getBytes()));
      
      bw.addMutation(m);
      bw.flush();
    } finally {
      if (null != bw) {
        bw.close();
      }
    }
  }
  
  public static void remove(SortableResult id) throws TableNotFoundException, MutationsRejectedException {
    checkNotNull(id);

    BatchWriter bw = null;
    try {
      bw = id.connector().createBatchWriter(id.metadataTable(), new BatchWriterConfig());
      Mutation m = new Mutation(id.uuid());
      m.putDelete(STATE_COLFAM, EMPTY_TEXT);
      
      bw.addMutation(m);
      bw.flush();
    } finally {
      if (null != bw) {
        bw.close();
      }
    }
  }
  
  public static State deserializeState(Value v) {
    return State.valueOf(v.toString());
  }
}

@@ -869,24 +869,1181 @@
 state%22);%0A  %0A
+  /**%0A   * A %7B@link State%7D determines the lifecycle phases of a %7B@link SortableResult%7D%0A   * in Accumulo.%0A   * %0A   * %3Cp%3E%0A   * %7B@code LOADING%7D means that new records are actively being loaded and queries %0A   * can start; however, only the columns specified as being indexed when the %7B@link SortableResult%7D%0A   * was defined can be guaranteed to exist. Meaning, calls to %7B@link Sorting#index(SortableResult, Iterable)%7D%0A   * will not block queries from running while the index is being updated. Obviously, queries in this%0A   * state are not guaranteed to be the column result set for a %7B@link SortableResult%7D%0A   * %0A   * %3Cp%3E%0A   *  %7B@code LOADED%7D means that the %7B@link Sorting%7D client writing results has completed. %0A   * %0A   * %3Cp%3E%0A   * %7B@code ERROR%7D means that there an error in the loading of the data for the given%0A   * %7B@link SortableResult%7D and processing has ceased.%0A   * %0A   * %3Cp%3E%0A   * %7B@code DELETING%7D means that a client has called %7B@link Sorting#delete(SortableResult)%7D%0A   * and the results are in the process of being deleted.%0A   * %0A   * %3Cp%3E%0A   * %7B@code UNKNOWN%7D means that the software is unaware of the given %7B@link SortableResult%7D%0A   *  %0A   * %0A   */%0A
   public enu

Add in some javadoc about State and its meaning