/**
 * 
 */
package edu.jhu.hlt.rebar.accumulo;

import org.apache.accumulo.core.cli.BatchWriterOpts;
import org.apache.accumulo.core.client.AccumuloException;
import org.apache.accumulo.core.client.AccumuloSecurityException;
import org.apache.accumulo.core.client.BatchWriter;
import org.apache.accumulo.core.client.Connector;
import org.apache.accumulo.core.client.Instance;
import org.apache.accumulo.core.client.TableNotFoundException;
import org.apache.accumulo.core.client.ZooKeeperInstance;
import org.apache.thrift.TDeserializer;
import org.apache.thrift.TSerializer;
import org.apache.thrift.protocol.TBinaryProtocol;

import edu.jhu.hlt.rebar.Configuration;
import edu.jhu.hlt.rebar.Constants;
import edu.jhu.hlt.rebar.RebarException;

/**
 * @author max
 * 
 */
public abstract class AbstractAccumuloClient implements AutoCloseable {

  protected final Connector conn;
  protected final RebarTableOps tableOps;
  protected final TSerializer serializer;
  protected final TDeserializer deserializer;
  
  protected static final String colFamilyRoot = "root";
  protected static final String colFamilyAnnotations = "anno";
  protected static final String documentTableName = "documents";
  protected static final BatchWriterOpts defaultBwOpts;
  
  protected BatchWriter bw;
  
  protected final static long MAX_WRITE_MEMORY = 1024L * 1024 * 5; // 5 mb
  protected final static long MAX_WRITE_LATENCY_MS = 5L; // 5 msec
  protected final static int NUM_WRITE_THREADS = 5;
  protected final static int NUM_DELETER_QUERY_THREADS = 5;
  protected final static int NUM_QUERY_THREADS = 8;
  
  static {
    defaultBwOpts = new BatchWriterOpts();
    defaultBwOpts.batchLatency = MAX_WRITE_LATENCY_MS;
    defaultBwOpts.batchMemory = MAX_WRITE_MEMORY;
    defaultBwOpts.batchThreads = NUM_WRITE_THREADS;
    defaultBwOpts.batchTimeout = 3000L;
  }

  /**
   * @throws RebarException 
   * 
   */
  public AbstractAccumuloClient() throws RebarException {
    this(Constants.getConnector());
  }

  public AbstractAccumuloClient(Connector conn) throws RebarException {
    this.conn = conn;
    this.tableOps = new RebarTableOps(this.conn);
    this.tableOps.createTableIfNotExists(documentTableName);
    this.serializer = new TSerializer(new TBinaryProtocol.Factory());
    this.deserializer = new TDeserializer(new TBinaryProtocol.Factory());
    try {
      this.bw = this.conn.createBatchWriter(documentTableName, defaultBwOpts.getBatchWriterConfig());
    } catch (TableNotFoundException e) {
      throw new RebarException(e);
    }
  }
  
  public abstract void flush() throws RebarException;
}

@@ -99,132 +99,8 @@
 ts;%0A
-import org.apache.accumulo.core.client.AccumuloException;%0Aimport org.apache.accumulo.core.client.AccumuloSecurityException;%0A
 impo
@@ -143,24 +143,24 @@
 atchWriter;%0A
+
 import org.a
@@ -201,57 +201,8 @@
 or;%0A
-import org.apache.accumulo.core.client.Instance;%0A
 impo
@@ -264,66 +264,8 @@
 on;%0A
-import org.apache.accumulo.core.client.ZooKeeperInstance;%0A
 impo
@@ -394,48 +394,8 @@
 l;%0A%0A
-import edu.jhu.hlt.rebar.Configuration;%0A
 impo
@@ -739,192 +739,8 @@
 %0A  %0A
-  protected static final String colFamilyRoot = %22root%22;%0A  protected static final String colFamilyAnnotations = %22anno%22;%0A  protected static final String documentTableName = %22documents%22;%0A
   pr
@@ -1721,33 +1721,45 @@
 tExists(
-documentTableName
+Constants.DOCUMENT_TABLE_NAME
 );%0A    t
@@ -1955,25 +1955,37 @@
 ter(
-documentTableName
+Constants.DOCUMENT_TABLE_NAME
 , de

tweak some magic numbers