incubator-blur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amccu...@apache.org
Subject [14/19] Thrift re-package, sorry for the huge commit.
Date Thu, 23 May 2013 19:39:53 GMT
http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f63daff/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TNonblockingServer.java
----------------------------------------------------------------------
diff --git a/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TNonblockingServer.java b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TNonblockingServer.java
new file mode 100644
index 0000000..c786d42
--- /dev/null
+++ b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TNonblockingServer.java
@@ -0,0 +1,235 @@
+/*
+ * 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.
+ */
+
+
+package org.apache.blur.thirdparty.thrift_0_9_0.server;
+
+import org.apache.blur.thirdparty.thrift_0_9_0.transport.TNonblockingServerTransport;
+import org.apache.blur.thirdparty.thrift_0_9_0.transport.TNonblockingTransport;
+import org.apache.blur.thirdparty.thrift_0_9_0.transport.TTransportException;
+
+import java.io.IOException;
+import java.nio.channels.SelectionKey;
+import java.util.Iterator;
+
+/**
+ * A nonblocking TServer implementation. This allows for fairness amongst all
+ * connected clients in terms of invocations.
+ *
+ * This server is inherently single-threaded. If you want a limited thread pool
+ * coupled with invocation-fairness, see THsHaServer.
+ *
+ * To use this server, you MUST use a TFramedTransport at the outermost
+ * transport, otherwise this server will be unable to determine when a whole
+ * method call has been read off the wire. Clients must also use TFramedTransport.
+ */
+public class TNonblockingServer extends AbstractNonblockingServer {
+
+  public static class Args extends AbstractNonblockingServerArgs<Args> {
+    public Args(TNonblockingServerTransport transport) {
+      super(transport);
+    }
+  }
+
+  // Flag for stopping the server
+  private volatile boolean stopped_ = true;
+
+  private SelectAcceptThread selectAcceptThread_;
+
+  public TNonblockingServer(AbstractNonblockingServerArgs args) {
+    super(args);
+  }
+
+
+  /**
+   * Start the selector thread to deal with accepts and client messages.
+   *
+   * @return true if everything went ok, false if we couldn't start for some
+   * reason.
+   */
+  @Override
+  protected boolean startThreads() {
+    // start the selector
+    try {
+      selectAcceptThread_ = new SelectAcceptThread((TNonblockingServerTransport)serverTransport_);
+      stopped_ = false;
+      selectAcceptThread_.start();
+      return true;
+    } catch (IOException e) {
+      LOGGER.error("Failed to start selector thread!", e);
+      return false;
+    }
+  }
+
+  @Override
+  protected void waitForShutdown() {
+    joinSelector();
+  }
+
+  /**
+   * Block until the selector thread exits.
+   */
+  protected void joinSelector() {
+    // wait until the selector thread exits
+    try {
+      selectAcceptThread_.join();
+    } catch (InterruptedException e) {
+      // for now, just silently ignore. technically this means we'll have less of
+      // a graceful shutdown as a result.
+    }
+  }
+
+  /**
+   * Stop serving and shut everything down.
+   */
+  @Override
+  public void stop() {
+    stopped_ = true;
+    if (selectAcceptThread_ != null) {
+      selectAcceptThread_.wakeupSelector();
+    }
+  }
+
+  /**
+   * Perform an invocation. This method could behave several different ways
+   * - invoke immediately inline, queue for separate execution, etc.
+   */
+  @Override
+  protected boolean requestInvoke(FrameBuffer frameBuffer) {
+    frameBuffer.invoke();
+    return true;
+  }
+
+
+  public boolean isStopped() {
+    return selectAcceptThread_.isStopped();
+  }
+
+  /**
+   * The thread that will be doing all the selecting, managing new connections
+   * and those that still need to be read.
+   */
+  protected class SelectAcceptThread extends AbstractSelectThread {
+
+    // The server transport on which new client transports will be accepted
+    private final TNonblockingServerTransport serverTransport;
+
+    /**
+     * Set up the thread that will handle the non-blocking accepts, reads, and
+     * writes.
+     */
+    public SelectAcceptThread(final TNonblockingServerTransport serverTransport)
+    throws IOException {
+      this.serverTransport = serverTransport;
+      serverTransport.registerSelector(selector);
+    }
+
+    public boolean isStopped() {
+      return stopped_;
+    }
+
+    /**
+     * The work loop. Handles both selecting (all IO operations) and managing
+     * the selection preferences of all existing connections.
+     */
+    public void run() {
+      try {
+        while (!stopped_) {
+          select();
+          processInterestChanges();
+        }
+        for (SelectionKey selectionKey : selector.keys()) {
+          cleanupSelectionKey(selectionKey);
+        }
+      } catch (Throwable t) {
+        LOGGER.error("run() exiting due to uncaught error", t);
+      } finally {
+        stopped_ = true;
+      }
+    }
+
+    /**
+     * Select and process IO events appropriately:
+     * If there are connections to be accepted, accept them.
+     * If there are existing connections with data waiting to be read, read it,
+     * buffering until a whole frame has been read.
+     * If there are any pending responses, buffer them until their target client
+     * is available, and then send the data.
+     */
+    private void select() {
+      try {
+        // wait for io events.
+        selector.select();
+
+        // process the io events we received
+        Iterator<SelectionKey> selectedKeys = selector.selectedKeys().iterator();
+        while (!stopped_ && selectedKeys.hasNext()) {
+          SelectionKey key = selectedKeys.next();
+          selectedKeys.remove();
+
+          // skip if not valid
+          if (!key.isValid()) {
+            cleanupSelectionKey(key);
+            continue;
+          }
+
+          // if the key is marked Accept, then it has to be the server
+          // transport.
+          if (key.isAcceptable()) {
+            handleAccept();
+          } else if (key.isReadable()) {
+            // deal with reads
+            handleRead(key);
+          } else if (key.isWritable()) {
+            // deal with writes
+            handleWrite(key);
+          } else {
+            LOGGER.warn("Unexpected state in select! " + key.interestOps());
+          }
+        }
+      } catch (IOException e) {
+        LOGGER.warn("Got an IOException while selecting!", e);
+      }
+    }
+
+    /**
+     * Accept a new connection.
+     */
+    private void handleAccept() throws IOException {
+      SelectionKey clientKey = null;
+      TNonblockingTransport client = null;
+      try {
+        // accept the connection
+        client = (TNonblockingTransport)serverTransport.accept();
+        clientKey = client.registerSelector(selector, SelectionKey.OP_READ);
+
+        // add this key to the map
+        FrameBuffer frameBuffer = new FrameBuffer(client, clientKey,
+          SelectAcceptThread.this);
+        clientKey.attach(frameBuffer);
+      } catch (TTransportException tte) {
+        // something went wrong accepting.
+        LOGGER.warn("Exception trying to accept!", tte);
+        tte.printStackTrace();
+        if (clientKey != null) cleanupSelectionKey(clientKey);
+        if (client != null) client.close();
+      }
+    }
+  } // SelectAcceptThread
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f63daff/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TServer.java
----------------------------------------------------------------------
diff --git a/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TServer.java b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TServer.java
new file mode 100644
index 0000000..6d6cd03
--- /dev/null
+++ b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TServer.java
@@ -0,0 +1,165 @@
+/*
+ * 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.
+ */
+
+package org.apache.blur.thirdparty.thrift_0_9_0.server;
+
+import org.apache.blur.thirdparty.thrift_0_9_0.TProcessor;
+import org.apache.blur.thirdparty.thrift_0_9_0.TProcessorFactory;
+import org.apache.blur.thirdparty.thrift_0_9_0.protocol.TBinaryProtocol;
+import org.apache.blur.thirdparty.thrift_0_9_0.protocol.TProtocolFactory;
+import org.apache.blur.thirdparty.thrift_0_9_0.transport.TServerTransport;
+import org.apache.blur.thirdparty.thrift_0_9_0.transport.TTransportFactory;
+
+/**
+ * Generic interface for a Thrift server.
+ *
+ */
+public abstract class TServer {
+
+  public static class Args extends AbstractServerArgs<Args> {
+    public Args(TServerTransport transport) {
+      super(transport);
+    }
+  }
+
+  public static abstract class AbstractServerArgs<T extends AbstractServerArgs<T>> {
+    final TServerTransport serverTransport;
+    TProcessorFactory processorFactory;
+    TTransportFactory inputTransportFactory = new TTransportFactory();
+    TTransportFactory outputTransportFactory = new TTransportFactory();
+    TProtocolFactory inputProtocolFactory = new TBinaryProtocol.Factory();
+    TProtocolFactory outputProtocolFactory = new TBinaryProtocol.Factory();
+
+    public AbstractServerArgs(TServerTransport transport) {
+      serverTransport = transport;
+    }
+
+    public T processorFactory(TProcessorFactory factory) {
+      this.processorFactory = factory;
+      return (T) this;
+    }
+
+    public T processor(TProcessor processor) {
+      this.processorFactory = new TProcessorFactory(processor);
+      return (T) this;
+    }
+
+    public T transportFactory(TTransportFactory factory) {
+      this.inputTransportFactory = factory;
+      this.outputTransportFactory = factory;
+      return (T) this;
+    }
+
+    public T inputTransportFactory(TTransportFactory factory) {
+      this.inputTransportFactory = factory;
+      return (T) this;
+    }
+
+    public T outputTransportFactory(TTransportFactory factory) {
+      this.outputTransportFactory = factory;
+      return (T) this;
+    }
+
+    public T protocolFactory(TProtocolFactory factory) {
+      this.inputProtocolFactory = factory;
+      this.outputProtocolFactory = factory;
+      return (T) this;
+    }
+
+    public T inputProtocolFactory(TProtocolFactory factory) {
+      this.inputProtocolFactory = factory;
+      return (T) this;
+    }
+
+    public T outputProtocolFactory(TProtocolFactory factory) {
+      this.outputProtocolFactory = factory;
+      return (T) this;
+    }
+  }
+
+  /**
+   * Core processor
+   */
+  protected TProcessorFactory processorFactory_;
+
+  /**
+   * Server transport
+   */
+  protected TServerTransport serverTransport_;
+
+  /**
+   * Input Transport Factory
+   */
+  protected TTransportFactory inputTransportFactory_;
+
+  /**
+   * Output Transport Factory
+   */
+  protected TTransportFactory outputTransportFactory_;
+
+  /**
+   * Input Protocol Factory
+   */
+  protected TProtocolFactory inputProtocolFactory_;
+
+  /**
+   * Output Protocol Factory
+   */
+  protected TProtocolFactory outputProtocolFactory_;
+
+  private boolean isServing;
+
+  protected TServerEventHandler eventHandler_;
+
+  protected TServer(AbstractServerArgs args) {
+    processorFactory_ = args.processorFactory;
+    serverTransport_ = args.serverTransport;
+    inputTransportFactory_ = args.inputTransportFactory;
+    outputTransportFactory_ = args.outputTransportFactory;
+    inputProtocolFactory_ = args.inputProtocolFactory;
+    outputProtocolFactory_ = args.outputProtocolFactory;
+  }
+
+  /**
+   * The run method fires up the server and gets things going.
+   */
+  public abstract void serve();
+
+  /**
+   * Stop the server. This is optional on a per-implementation basis. Not
+   * all servers are required to be cleanly stoppable.
+   */
+  public void stop() {}
+
+  public boolean isServing() {
+    return isServing;
+  }
+
+  protected void setServing(boolean serving) {
+    isServing = serving;
+  }
+
+  public void setServerEventHandler(TServerEventHandler eventHandler) {
+    eventHandler_ = eventHandler;
+  }
+
+  public TServerEventHandler getEventHandler() {
+    return eventHandler_;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f63daff/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TServerEventHandler.java
----------------------------------------------------------------------
diff --git a/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TServerEventHandler.java b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TServerEventHandler.java
new file mode 100644
index 0000000..d0ebfe7
--- /dev/null
+++ b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TServerEventHandler.java
@@ -0,0 +1,59 @@
+/*
+ * 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.
+ */
+
+package org.apache.blur.thirdparty.thrift_0_9_0.server;
+
+import org.apache.blur.thirdparty.thrift_0_9_0.protocol.TProtocol;
+import org.apache.blur.thirdparty.thrift_0_9_0.transport.TTransport;
+
+/**
+ * Interface that can handle events from the server core. To
+ * use this you should subclass it and implement the methods that you care
+ * about. Your subclass can also store local data that you may care about,
+ * such as additional "arguments" to these methods (stored in the object
+ * instance's state).
+ */
+public interface TServerEventHandler {
+
+  /**
+   * Called before the server begins.
+   */
+  void preServe();
+
+  /**
+   * Called when a new client has connected and is about to being processing.
+   */
+  ServerContext createContext(TProtocol input,
+                              TProtocol output);
+
+  /**
+   * Called when a client has finished request-handling to delete server
+   * context.
+   */
+  void deleteContext(ServerContext serverContext,
+                             TProtocol input,
+                             TProtocol output);
+
+  /**
+   * Called when a client is about to call the processor.
+   */
+  void processContext(ServerContext serverContext,
+                              TTransport inputTransport, TTransport outputTransport);
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f63daff/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TServlet.java
----------------------------------------------------------------------
diff --git a/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TServlet.java b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TServlet.java
new file mode 100644
index 0000000..e7daf58
--- /dev/null
+++ b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TServlet.java
@@ -0,0 +1,119 @@
+package org.apache.blur.thirdparty.thrift_0_9_0.server;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Map;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.blur.thirdparty.thrift_0_9_0.TException;
+import org.apache.blur.thirdparty.thrift_0_9_0.TProcessor;
+import org.apache.blur.thirdparty.thrift_0_9_0.protocol.TProtocol;
+import org.apache.blur.thirdparty.thrift_0_9_0.protocol.TProtocolFactory;
+import org.apache.blur.thirdparty.thrift_0_9_0.transport.TIOStreamTransport;
+import org.apache.blur.thirdparty.thrift_0_9_0.transport.TTransport;
+
+/**
+ * Servlet implementation class ThriftServer
+ */
+public class TServlet extends HttpServlet {
+
+  private final TProcessor processor;
+
+  private final TProtocolFactory inProtocolFactory;
+
+  private final TProtocolFactory outProtocolFactory;
+
+  private final Collection<Map.Entry<String, String>> customHeaders;
+
+  /**
+   * @see HttpServlet#HttpServlet()
+   */
+  public TServlet(TProcessor processor, TProtocolFactory inProtocolFactory,
+      TProtocolFactory outProtocolFactory) {
+    super();
+    this.processor = processor;
+    this.inProtocolFactory = inProtocolFactory;
+    this.outProtocolFactory = outProtocolFactory;
+    this.customHeaders = new ArrayList<Map.Entry<String, String>>();
+  }
+
+  /**
+   * @see HttpServlet#HttpServlet()
+   */
+  public TServlet(TProcessor processor, TProtocolFactory protocolFactory) {
+    this(processor, protocolFactory, protocolFactory);
+  }
+
+  /**
+   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
+   *      response)
+   */
+  @Override
+  protected void doPost(HttpServletRequest request, HttpServletResponse response)
+      throws ServletException, IOException {
+
+    TTransport inTransport = null;
+    TTransport outTransport = null;
+
+    try {
+      response.setContentType("application/x-thrift");
+
+      if (null != this.customHeaders) {
+        for (Map.Entry<String, String> header : this.customHeaders) {
+          response.addHeader(header.getKey(), header.getValue());
+        }
+      }
+      InputStream in = request.getInputStream();
+      OutputStream out = response.getOutputStream();
+
+      TTransport transport = new TIOStreamTransport(in, out);
+      inTransport = transport;
+      outTransport = transport;
+
+      TProtocol inProtocol = inProtocolFactory.getProtocol(inTransport);
+      TProtocol outProtocol = outProtocolFactory.getProtocol(outTransport);
+
+      processor.process(inProtocol, outProtocol);
+      out.flush();
+    } catch (TException te) {
+      throw new ServletException(te);
+    }
+  }
+
+  /**
+   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
+   *      response)
+   */
+  protected void doGet(HttpServletRequest request, HttpServletResponse response)
+      throws ServletException, IOException {
+    doPost(request, response);
+  }
+
+  public void addCustomHeader(final String key, final String value) {
+    this.customHeaders.add(new Map.Entry<String, String>() {
+      public String getKey() {
+        return key;
+      }
+
+      public String getValue() {
+        return value;
+      }
+
+      public String setValue(String value) {
+        return null;
+      }
+    });
+  }
+
+  public void setCustomHeaders(Collection<Map.Entry<String, String>> headers) {
+    this.customHeaders.clear();
+    this.customHeaders.addAll(headers);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f63daff/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TSimpleServer.java
----------------------------------------------------------------------
diff --git a/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TSimpleServer.java b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TSimpleServer.java
new file mode 100644
index 0000000..bad4a60
--- /dev/null
+++ b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TSimpleServer.java
@@ -0,0 +1,120 @@
+/*
+ * 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.
+ */
+
+package org.apache.blur.thirdparty.thrift_0_9_0.server;
+
+import org.apache.blur.thirdparty.thrift_0_9_0.TException;
+import org.apache.blur.thirdparty.thrift_0_9_0.TProcessor;
+import org.apache.blur.thirdparty.thrift_0_9_0.protocol.TProtocol;
+import org.apache.blur.thirdparty.thrift_0_9_0.transport.TTransport;
+import org.apache.blur.thirdparty.thrift_0_9_0.transport.TTransportException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Simple singlethreaded server for testing.
+ *
+ */
+public class TSimpleServer extends TServer {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(TSimpleServer.class.getName());
+
+  private boolean stopped_ = false;
+
+  public TSimpleServer(AbstractServerArgs args) {
+    super(args);
+  }
+
+  public void serve() {
+    stopped_ = false;
+    try {
+      serverTransport_.listen();
+    } catch (TTransportException ttx) {
+      LOGGER.error("Error occurred during listening.", ttx);
+      return;
+    }
+
+    // Run the preServe event
+    if (eventHandler_ != null) {
+      eventHandler_.preServe();
+    }
+
+    setServing(true);
+
+    while (!stopped_) {
+      TTransport client = null;
+      TProcessor processor = null;
+      TTransport inputTransport = null;
+      TTransport outputTransport = null;
+      TProtocol inputProtocol = null;
+      TProtocol outputProtocol = null;
+      ServerContext connectionContext = null;
+      try {
+        client = serverTransport_.accept();
+        if (client != null) {
+          processor = processorFactory_.getProcessor(client);
+          inputTransport = inputTransportFactory_.getTransport(client);
+          outputTransport = outputTransportFactory_.getTransport(client);
+          inputProtocol = inputProtocolFactory_.getProtocol(inputTransport);
+          outputProtocol = outputProtocolFactory_.getProtocol(outputTransport);
+          if (eventHandler_ != null) {
+            connectionContext = eventHandler_.createContext(inputProtocol, outputProtocol);
+          }
+          while (true) {
+            if (eventHandler_ != null) {
+              eventHandler_.processContext(connectionContext, inputTransport, outputTransport);
+            }
+            if(!processor.process(inputProtocol, outputProtocol)) {
+              break;
+            }
+          }
+        }
+      } catch (TTransportException ttx) {
+        // Client died, just move on
+      } catch (TException tx) {
+        if (!stopped_) {
+          LOGGER.error("Thrift error occurred during processing of message.", tx);
+        }
+      } catch (Exception x) {
+        if (!stopped_) {
+          LOGGER.error("Error occurred during processing of message.", x);
+        }
+      }
+
+      if (eventHandler_ != null) {
+        eventHandler_.deleteContext(connectionContext, inputProtocol, outputProtocol);
+      }
+
+      if (inputTransport != null) {
+        inputTransport.close();
+      }
+
+      if (outputTransport != null) {
+        outputTransport.close();
+      }
+
+    }
+    setServing(false);
+  }
+
+  public void stop() {
+    stopped_ = true;
+    serverTransport_.interrupt();
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f63daff/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TThreadPoolServer.java
----------------------------------------------------------------------
diff --git a/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TThreadPoolServer.java b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TThreadPoolServer.java
new file mode 100644
index 0000000..fc744f1
--- /dev/null
+++ b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TThreadPoolServer.java
@@ -0,0 +1,231 @@
+/*
+ * 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.
+ */
+
+package org.apache.blur.thirdparty.thrift_0_9_0.server;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.SynchronousQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.blur.thirdparty.thrift_0_9_0.TException;
+import org.apache.blur.thirdparty.thrift_0_9_0.TProcessor;
+import org.apache.blur.thirdparty.thrift_0_9_0.protocol.TProtocol;
+import org.apache.blur.thirdparty.thrift_0_9_0.transport.TServerTransport;
+import org.apache.blur.thirdparty.thrift_0_9_0.transport.TTransport;
+import org.apache.blur.thirdparty.thrift_0_9_0.transport.TTransportException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Server which uses Java's built in ThreadPool management to spawn off
+ * a worker pool that
+ *
+ */
+public class TThreadPoolServer extends TServer {
+  private static final Logger LOGGER = LoggerFactory.getLogger(TThreadPoolServer.class.getName());
+
+  public static class Args extends AbstractServerArgs<Args> {
+    public int minWorkerThreads = 5;
+    public int maxWorkerThreads = Integer.MAX_VALUE;
+    public ExecutorService executorService;
+    public int stopTimeoutVal = 60;
+    public TimeUnit stopTimeoutUnit = TimeUnit.SECONDS;
+
+    public Args(TServerTransport transport) {
+      super(transport);
+    }
+
+    public Args minWorkerThreads(int n) {
+      minWorkerThreads = n;
+      return this;
+    }
+
+    public Args maxWorkerThreads(int n) {
+      maxWorkerThreads = n;
+      return this;
+    }
+
+    public Args executorService(ExecutorService executorService) {
+      this.executorService = executorService;
+      return this;
+    }
+  }
+
+  // Executor service for handling client connections
+  private ExecutorService executorService_;
+
+  // Flag for stopping the server
+  private volatile boolean stopped_;
+
+  private final TimeUnit stopTimeoutUnit;
+
+  private final long stopTimeoutVal;
+
+  public TThreadPoolServer(Args args) {
+    super(args);
+
+    stopTimeoutUnit = args.stopTimeoutUnit;
+    stopTimeoutVal = args.stopTimeoutVal;
+
+    executorService_ = args.executorService != null ?
+        args.executorService : createDefaultExecutorService(args);
+  }
+
+  private static ExecutorService createDefaultExecutorService(Args args) {
+    SynchronousQueue<Runnable> executorQueue =
+      new SynchronousQueue<Runnable>();
+    return new ThreadPoolExecutor(args.minWorkerThreads,
+                                  args.maxWorkerThreads,
+                                  60,
+                                  TimeUnit.SECONDS,
+                                  executorQueue);
+  }
+
+
+  public void serve() {
+    try {
+      serverTransport_.listen();
+    } catch (TTransportException ttx) {
+      LOGGER.error("Error occurred during listening.", ttx);
+      return;
+    }
+
+    // Run the preServe event
+    if (eventHandler_ != null) {
+      eventHandler_.preServe();
+    }
+
+    stopped_ = false;
+    setServing(true);
+    while (!stopped_) {
+      int failureCount = 0;
+      try {
+        TTransport client = serverTransport_.accept();
+        WorkerProcess wp = new WorkerProcess(client);
+        executorService_.execute(wp);
+      } catch (TTransportException ttx) {
+        if (!stopped_) {
+          ++failureCount;
+          LOGGER.warn("Transport error occurred during acceptance of message.", ttx);
+        }
+      }
+    }
+
+    executorService_.shutdown();
+
+    // Loop until awaitTermination finally does return without a interrupted
+    // exception. If we don't do this, then we'll shut down prematurely. We want
+    // to let the executorService clear it's task queue, closing client sockets
+    // appropriately.
+    long timeoutMS = stopTimeoutUnit.toMillis(stopTimeoutVal);
+    long now = System.currentTimeMillis();
+    while (timeoutMS >= 0) {
+      try {
+        executorService_.awaitTermination(timeoutMS, TimeUnit.MILLISECONDS);
+        break;
+      } catch (InterruptedException ix) {
+        long newnow = System.currentTimeMillis();
+        timeoutMS -= (newnow - now);
+        now = newnow;
+      }
+    }
+    setServing(false);
+  }
+
+  public void stop() {
+    stopped_ = true;
+    serverTransport_.interrupt();
+  }
+
+  private class WorkerProcess implements Runnable {
+
+    /**
+     * Client that this services.
+     */
+    private TTransport client_;
+
+    /**
+     * Default constructor.
+     *
+     * @param client Transport to process
+     */
+    private WorkerProcess(TTransport client) {
+      client_ = client;
+    }
+
+    /**
+     * Loops on processing a client forever
+     */
+    public void run() {
+      TProcessor processor = null;
+      TTransport inputTransport = null;
+      TTransport outputTransport = null;
+      TProtocol inputProtocol = null;
+      TProtocol outputProtocol = null;
+
+      TServerEventHandler eventHandler = null;
+      ServerContext connectionContext = null;
+
+      try {
+        processor = processorFactory_.getProcessor(client_);
+        inputTransport = inputTransportFactory_.getTransport(client_);
+        outputTransport = outputTransportFactory_.getTransport(client_);
+        inputProtocol = inputProtocolFactory_.getProtocol(inputTransport);
+        outputProtocol = outputProtocolFactory_.getProtocol(outputTransport);	  
+
+        eventHandler = getEventHandler();
+        if (eventHandler != null) {
+          connectionContext = eventHandler.createContext(inputProtocol, outputProtocol);
+        }
+        // we check stopped_ first to make sure we're not supposed to be shutting
+        // down. this is necessary for graceful shutdown.
+        while (true) {
+
+            if (eventHandler != null) {
+              eventHandler.processContext(connectionContext, inputTransport, outputTransport);
+            }
+
+            if(stopped_ || !processor.process(inputProtocol, outputProtocol)) {
+              break;
+            }
+        }
+      } catch (TTransportException ttx) {
+        // Assume the client died and continue silently
+      } catch (TException tx) {
+        LOGGER.error("Thrift error occurred during processing of message.", tx);
+      } catch (Exception x) {
+        LOGGER.error("Error occurred during processing of message.", x);
+      }
+
+      if (eventHandler != null) {
+        eventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol);
+      }
+
+      if (inputTransport != null) {
+        inputTransport.close();
+      }
+
+      if (outputTransport != null) {
+        outputTransport.close();
+      }
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f63daff/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TThreadedSelectorServer.java
----------------------------------------------------------------------
diff --git a/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TThreadedSelectorServer.java b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TThreadedSelectorServer.java
new file mode 100644
index 0000000..c96da82
--- /dev/null
+++ b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/server/TThreadedSelectorServer.java
@@ -0,0 +1,649 @@
+/*
+ * 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.
+ */
+
+package org.apache.blur.thirdparty.thrift_0_9_0.server;
+
+import java.io.IOException;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.nio.channels.spi.SelectorProvider;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.blur.thirdparty.thrift_0_9_0.transport.TNonblockingServerTransport;
+import org.apache.blur.thirdparty.thrift_0_9_0.transport.TNonblockingTransport;
+import org.apache.blur.thirdparty.thrift_0_9_0.transport.TTransportException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A Half-Sync/Half-Async server with a separate pool of threads to handle
+ * non-blocking I/O. Accepts are handled on a single thread, and a configurable
+ * number of nonblocking selector threads manage reading and writing of client
+ * connections. A synchronous worker thread pool handles processing of requests.
+ * 
+ * Performs better than TNonblockingServer/THsHaServer in multi-core
+ * environments when the the bottleneck is CPU on the single selector thread
+ * handling I/O. In addition, because the accept handling is decoupled from
+ * reads/writes and invocation, the server has better ability to handle back-
+ * pressure from new connections (e.g. stop accepting when busy).
+ * 
+ * Like TNonblockingServer, it relies on the use of TFramedTransport.
+ */
+public class TThreadedSelectorServer extends AbstractNonblockingServer {
+  private static final Logger LOGGER = LoggerFactory.getLogger(TThreadedSelectorServer.class.getName());
+
+  public static class Args extends AbstractNonblockingServerArgs<Args> {
+
+    /** The number of threads for selecting on already-accepted connections */
+    public int selectorThreads = 2;
+    /**
+     * The size of the executor service (if none is specified) that will handle
+     * invocations. This may be set to 0, in which case invocations will be
+     * handled directly on the selector threads (as is in TNonblockingServer)
+     */
+    private int workerThreads = 5;
+    /** Time to wait for server to stop gracefully */
+    private int stopTimeoutVal = 60;
+    private TimeUnit stopTimeoutUnit = TimeUnit.SECONDS;
+    /** The ExecutorService for handling dispatched requests */
+    private ExecutorService executorService = null;
+    /**
+     * The size of the blocking queue per selector thread for passing accepted
+     * connections to the selector thread
+     */
+    private int acceptQueueSizePerThread = 4;
+
+    /**
+     * Determines the strategy for handling new accepted connections.
+     */
+    public static enum AcceptPolicy {
+      /**
+       * Require accepted connection registration to be handled by the executor.
+       * If the worker pool is saturated, further accepts will be closed
+       * immediately. Slightly increases latency due to an extra scheduling.
+       */
+      FAIR_ACCEPT,
+      /**
+       * Handle the accepts as fast as possible, disregarding the status of the
+       * executor service.
+       */
+      FAST_ACCEPT
+    }
+
+    private AcceptPolicy acceptPolicy = AcceptPolicy.FAST_ACCEPT;
+
+    public Args(TNonblockingServerTransport transport) {
+      super(transport);
+    }
+
+    public Args selectorThreads(int i) {
+      selectorThreads = i;
+      return this;
+    }
+
+    public int getSelectorThreads() {
+      return selectorThreads;
+    }
+
+    public Args workerThreads(int i) {
+      workerThreads = i;
+      return this;
+    }
+
+    public int getWorkerThreads() {
+      return workerThreads;
+    }
+
+    public int getStopTimeoutVal() {
+      return stopTimeoutVal;
+    }
+
+    public Args stopTimeoutVal(int stopTimeoutVal) {
+      this.stopTimeoutVal = stopTimeoutVal;
+      return this;
+    }
+
+    public TimeUnit getStopTimeoutUnit() {
+      return stopTimeoutUnit;
+    }
+
+    public Args stopTimeoutUnit(TimeUnit stopTimeoutUnit) {
+      this.stopTimeoutUnit = stopTimeoutUnit;
+      return this;
+    }
+
+    public ExecutorService getExecutorService() {
+      return executorService;
+    }
+
+    public Args executorService(ExecutorService executorService) {
+      this.executorService = executorService;
+      return this;
+    }
+
+    public int getAcceptQueueSizePerThread() {
+      return acceptQueueSizePerThread;
+    }
+
+    public Args acceptQueueSizePerThread(int acceptQueueSizePerThread) {
+      this.acceptQueueSizePerThread = acceptQueueSizePerThread;
+      return this;
+    }
+
+    public AcceptPolicy getAcceptPolicy() {
+      return acceptPolicy;
+    }
+
+    public Args acceptPolicy(AcceptPolicy acceptPolicy) {
+      this.acceptPolicy = acceptPolicy;
+      return this;
+    }
+
+    public void validate() {
+      if (selectorThreads <= 0) {
+        throw new IllegalArgumentException("selectorThreads must be positive.");
+      }
+      if (workerThreads < 0) {
+        throw new IllegalArgumentException("workerThreads must be non-negative.");
+      }
+      if (acceptQueueSizePerThread <= 0) {
+        throw new IllegalArgumentException("acceptQueueSizePerThread must be positive.");
+      }
+    }
+  }
+
+  // Flag for stopping the server
+  private volatile boolean stopped_ = true;
+
+  // The thread handling all accepts
+  private AcceptThread acceptThread;
+
+  // Threads handling events on client transports
+  private final Set<SelectorThread> selectorThreads = new HashSet<SelectorThread>();
+
+  // This wraps all the functionality of queueing and thread pool management
+  // for the passing of Invocations from the selector thread(s) to the workers
+  // (if any).
+  private final ExecutorService invoker;
+
+  private final Args args;
+
+  /**
+   * Create the server with the specified Args configuration
+   */
+  public TThreadedSelectorServer(Args args) {
+    super(args);
+    args.validate();
+    invoker = args.executorService == null ? createDefaultExecutor(args) : args.executorService;
+    this.args = args;
+  }
+
+  /**
+   * Start the accept and selector threads running to deal with clients.
+   * 
+   * @return true if everything went ok, false if we couldn't start for some
+   *         reason.
+   */
+  @Override
+  protected boolean startThreads() {
+    try {
+      for (int i = 0; i < args.selectorThreads; ++i) {
+        selectorThreads.add(new SelectorThread(args.acceptQueueSizePerThread));
+      }
+      acceptThread = new AcceptThread((TNonblockingServerTransport) serverTransport_,
+        createSelectorThreadLoadBalancer(selectorThreads));
+      stopped_ = false;
+      for (SelectorThread thread : selectorThreads) {
+        thread.start();
+      }
+      acceptThread.start();
+      return true;
+    } catch (IOException e) {
+      LOGGER.error("Failed to start threads!", e);
+      return false;
+    }
+  }
+
+  /**
+   * Joins the accept and selector threads and shuts down the executor service.
+   */
+  @Override
+  protected void waitForShutdown() {
+    try {
+      joinThreads();
+    } catch (InterruptedException e) {
+      // Non-graceful shutdown occurred
+      LOGGER.error("Interrupted while joining threads!", e);
+    }
+    gracefullyShutdownInvokerPool();
+  }
+
+  protected void joinThreads() throws InterruptedException {
+    // wait until the io threads exit
+    acceptThread.join();
+    for (SelectorThread thread : selectorThreads) {
+      thread.join();
+    }
+  }
+
+  /**
+   * Stop serving and shut everything down.
+   */
+  @Override
+  public void stop() {
+    stopped_ = true;
+
+    // Stop queuing connect attempts asap
+    stopListening();
+
+    if (acceptThread != null) {
+      acceptThread.wakeupSelector();
+    }
+    if (selectorThreads != null) {
+      for (SelectorThread thread : selectorThreads) {
+        if (thread != null)
+          thread.wakeupSelector();
+      }
+    }
+  }
+
+  protected void gracefullyShutdownInvokerPool() {
+    // try to gracefully shut down the executor service
+    invoker.shutdown();
+
+    // Loop until awaitTermination finally does return without a interrupted
+    // exception. If we don't do this, then we'll shut down prematurely. We want
+    // to let the executorService clear it's task queue, closing client sockets
+    // appropriately.
+    long timeoutMS = args.stopTimeoutUnit.toMillis(args.stopTimeoutVal);
+    long now = System.currentTimeMillis();
+    while (timeoutMS >= 0) {
+      try {
+        invoker.awaitTermination(timeoutMS, TimeUnit.MILLISECONDS);
+        break;
+      } catch (InterruptedException ix) {
+        long newnow = System.currentTimeMillis();
+        timeoutMS -= (newnow - now);
+        now = newnow;
+      }
+    }
+  }
+
+  /**
+   * We override the standard invoke method here to queue the invocation for
+   * invoker service instead of immediately invoking. If there is no thread
+   * pool, handle the invocation inline on this thread
+   */
+  @Override
+  protected boolean requestInvoke(FrameBuffer frameBuffer) {
+    Runnable invocation = getRunnable(frameBuffer);
+    if (invoker != null) {
+      try {
+        invoker.execute(invocation);
+        return true;
+      } catch (RejectedExecutionException rx) {
+        LOGGER.warn("ExecutorService rejected execution!", rx);
+        return false;
+      }
+    } else {
+      // Invoke on the caller's thread
+      invocation.run();
+      return true;
+    }
+  }
+
+  protected Runnable getRunnable(FrameBuffer frameBuffer) {
+    return new Invocation(frameBuffer);
+  }
+
+  /**
+   * Helper to create the invoker if one is not specified
+   */
+  protected static ExecutorService createDefaultExecutor(Args options) {
+    return (options.workerThreads > 0) ? Executors.newFixedThreadPool(options.workerThreads) : null;
+  }
+
+  private static BlockingQueue<TNonblockingTransport> createDefaultAcceptQueue(int queueSize) {
+    if (queueSize == 0) {
+      // Unbounded queue
+      return new LinkedBlockingQueue<TNonblockingTransport>();
+    }
+    return new ArrayBlockingQueue<TNonblockingTransport>(queueSize);
+  }
+
+  /**
+   * The thread that selects on the server transport (listen socket) and accepts
+   * new connections to hand off to the IO selector threads
+   */
+  protected class AcceptThread extends Thread {
+
+    // The listen socket to accept on
+    private final TNonblockingServerTransport serverTransport;
+    private final Selector acceptSelector;
+
+    private final SelectorThreadLoadBalancer threadChooser;
+
+    /**
+     * Set up the AcceptThead
+     * 
+     * @throws IOException
+     */
+    public AcceptThread(TNonblockingServerTransport serverTransport,
+        SelectorThreadLoadBalancer threadChooser) throws IOException {
+      this.serverTransport = serverTransport;
+      this.threadChooser = threadChooser;
+      this.acceptSelector = SelectorProvider.provider().openSelector();
+      this.serverTransport.registerSelector(acceptSelector);
+    }
+
+    /**
+     * The work loop. Selects on the server transport and accepts. If there was
+     * a server transport that had blocking accepts, and returned on blocking
+     * client transports, that should be used instead
+     */
+    public void run() {
+      try {
+        while (!stopped_) {
+          select();
+        }
+      } catch (Throwable t) {
+        LOGGER.error("run() exiting due to uncaught error", t);
+      } finally {
+        // This will wake up the selector threads
+        TThreadedSelectorServer.this.stop();
+      }
+    }
+
+    /**
+     * If the selector is blocked, wake it up.
+     */
+    public void wakeupSelector() {
+      acceptSelector.wakeup();
+    }
+
+    /**
+     * Select and process IO events appropriately: If there are connections to
+     * be accepted, accept them.
+     */
+    private void select() {
+      try {
+        // wait for connect events.
+        acceptSelector.select();
+
+        // process the io events we received
+        Iterator<SelectionKey> selectedKeys = acceptSelector.selectedKeys().iterator();
+        while (!stopped_ && selectedKeys.hasNext()) {
+          SelectionKey key = selectedKeys.next();
+          selectedKeys.remove();
+
+          // skip if not valid
+          if (!key.isValid()) {
+            continue;
+          }
+
+          if (key.isAcceptable()) {
+            handleAccept();
+          } else {
+            LOGGER.warn("Unexpected state in select! " + key.interestOps());
+          }
+        }
+      } catch (IOException e) {
+        LOGGER.warn("Got an IOException while selecting!", e);
+      }
+    }
+
+    /**
+     * Accept a new connection.
+     */
+    private void handleAccept() {
+      final TNonblockingTransport client = doAccept();
+      if (client != null) {
+        // Pass this connection to a selector thread
+        final SelectorThread targetThread = threadChooser.nextThread();
+
+        if (args.acceptPolicy == Args.AcceptPolicy.FAST_ACCEPT || invoker == null) {
+          doAddAccept(targetThread, client);
+        } else {
+          // FAIR_ACCEPT
+          try {
+            invoker.submit(new Runnable() {
+              public void run() {
+                doAddAccept(targetThread, client);
+              }
+            });
+          } catch (RejectedExecutionException rx) {
+            LOGGER.warn("ExecutorService rejected accept registration!", rx);
+            // close immediately
+            client.close();
+          }
+        }
+      }
+    }
+
+    private TNonblockingTransport doAccept() {
+      try {
+        return (TNonblockingTransport) serverTransport.accept();
+      } catch (TTransportException tte) {
+        // something went wrong accepting.
+        LOGGER.warn("Exception trying to accept!", tte);
+        return null;
+      }
+    }
+
+    private void doAddAccept(SelectorThread thread, TNonblockingTransport client) {
+      if (!thread.addAcceptedConnection(client)) {
+        client.close();
+      }
+    }
+  } // AcceptThread
+
+  /**
+   * The SelectorThread(s) will be doing all the selecting on accepted active
+   * connections.
+   */
+  protected class SelectorThread extends AbstractSelectThread {
+
+    // Accepted connections added by the accept thread.
+    private final BlockingQueue<TNonblockingTransport> acceptedQueue;
+
+    /**
+     * Set up the SelectorThread with an unbounded queue for incoming accepts.
+     * 
+     * @throws IOException
+     *           if a selector cannot be created
+     */
+    public SelectorThread() throws IOException {
+      this(new LinkedBlockingQueue<TNonblockingTransport>());
+    }
+
+    /**
+     * Set up the SelectorThread with an bounded queue for incoming accepts.
+     * 
+     * @throws IOException
+     *           if a selector cannot be created
+     */
+    public SelectorThread(int maxPendingAccepts) throws IOException {
+      this(createDefaultAcceptQueue(maxPendingAccepts));
+    }
+
+    /**
+     * Set up the SelectorThread with a specified queue for connections.
+     * 
+     * @param acceptedQueue
+     *          The BlockingQueue implementation for holding incoming accepted
+     *          connections.
+     * @throws IOException
+     *           if a selector cannot be created.
+     */
+    public SelectorThread(BlockingQueue<TNonblockingTransport> acceptedQueue) throws IOException {
+      this.acceptedQueue = acceptedQueue;
+    }
+
+    /**
+     * Hands off an accepted connection to be handled by this thread. This
+     * method will block if the queue for new connections is at capacity.
+     * 
+     * @param accepted
+     *          The connection that has been accepted.
+     * @return true if the connection has been successfully added.
+     */
+    public boolean addAcceptedConnection(TNonblockingTransport accepted) {
+      try {
+        acceptedQueue.put(accepted);
+      } catch (InterruptedException e) {
+        LOGGER.warn("Interrupted while adding accepted connection!", e);
+        return false;
+      }
+      selector.wakeup();
+      return true;
+    }
+
+    /**
+     * The work loop. Handles selecting (read/write IO), dispatching, and
+     * managing the selection preferences of all existing connections.
+     */
+    public void run() {
+      try {
+        while (!stopped_) {
+          select();
+          processAcceptedConnections();
+          processInterestChanges();
+        }
+        for (SelectionKey selectionKey : selector.keys()) {
+          cleanupSelectionKey(selectionKey);
+        }
+      } catch (Throwable t) {
+        LOGGER.error("run() exiting due to uncaught error", t);
+      } finally {
+        // This will wake up the accept thread and the other selector threads
+        TThreadedSelectorServer.this.stop();
+      }
+    }
+
+    /**
+     * Select and process IO events appropriately: If there are existing
+     * connections with data waiting to be read, read it, buffering until a
+     * whole frame has been read. If there are any pending responses, buffer
+     * them until their target client is available, and then send the data.
+     */
+    private void select() {
+      try {
+        // wait for io events.
+        selector.select();
+
+        // process the io events we received
+        Iterator<SelectionKey> selectedKeys = selector.selectedKeys().iterator();
+        while (!stopped_ && selectedKeys.hasNext()) {
+          SelectionKey key = selectedKeys.next();
+          selectedKeys.remove();
+
+          // skip if not valid
+          if (!key.isValid()) {
+            cleanupSelectionKey(key);
+            continue;
+          }
+
+          if (key.isReadable()) {
+            // deal with reads
+            handleRead(key);
+          } else if (key.isWritable()) {
+            // deal with writes
+            handleWrite(key);
+          } else {
+            LOGGER.warn("Unexpected state in select! " + key.interestOps());
+          }
+        }
+      } catch (IOException e) {
+        LOGGER.warn("Got an IOException while selecting!", e);
+      }
+    }
+
+    private void processAcceptedConnections() {
+      // Register accepted connections
+      while (!stopped_) {
+        TNonblockingTransport accepted = acceptedQueue.poll();
+        if (accepted == null) {
+          break;
+        }
+        registerAccepted(accepted);
+      }
+    }
+
+    private void registerAccepted(TNonblockingTransport accepted) {
+      SelectionKey clientKey = null;
+      try {
+        clientKey = accepted.registerSelector(selector, SelectionKey.OP_READ);
+
+        FrameBuffer frameBuffer = new FrameBuffer(accepted, clientKey, SelectorThread.this);
+        clientKey.attach(frameBuffer);
+      } catch (IOException e) {
+        LOGGER.warn("Failed to register accepted connection to selector!", e);
+        if (clientKey != null) {
+          cleanupSelectionKey(clientKey);
+        }
+        accepted.close();
+      }
+    }
+  } // SelectorThread
+
+  /**
+   * Creates a SelectorThreadLoadBalancer to be used by the accept thread for
+   * assigning newly accepted connections across the threads.
+   */
+  protected SelectorThreadLoadBalancer createSelectorThreadLoadBalancer(Collection<? extends SelectorThread> threads) {
+    return new SelectorThreadLoadBalancer(threads);
+  }
+
+  /**
+   * A round robin load balancer for choosing selector threads for new
+   * connections.
+   */
+  protected class SelectorThreadLoadBalancer {
+    private final Collection<? extends SelectorThread> threads;
+    private Iterator<? extends SelectorThread> nextThreadIterator;
+
+    public <T extends SelectorThread> SelectorThreadLoadBalancer(Collection<T> threads) {
+      if (threads.isEmpty()) {
+        throw new IllegalArgumentException("At least one selector thread is required");
+      }
+      this.threads = Collections.unmodifiableList(new ArrayList<T>(threads));
+      nextThreadIterator = this.threads.iterator();
+    }
+
+    public SelectorThread nextThread() {
+      // Choose a selector thread (round robin)
+      if (!nextThreadIterator.hasNext()) {
+        nextThreadIterator = threads.iterator();
+      }
+      return nextThreadIterator.next();
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f63daff/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/AutoExpandingBuffer.java
----------------------------------------------------------------------
diff --git a/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/AutoExpandingBuffer.java b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/AutoExpandingBuffer.java
new file mode 100644
index 0000000..6922317
--- /dev/null
+++ b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/AutoExpandingBuffer.java
@@ -0,0 +1,52 @@
+/*
+ * 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.
+ */
+package org.apache.blur.thirdparty.thrift_0_9_0.transport;
+
+/**
+ * Helper class that wraps a byte[] so that it can expand and be reused. Users
+ * should call resizeIfNecessary to make sure the buffer has suitable capacity,
+ * and then use the array as needed. Note that the internal array will grow at a
+ * rate slightly faster than the requested capacity with the (untested)
+ * objective of avoiding expensive buffer allocations and copies.
+ */
+public class AutoExpandingBuffer {
+  private byte[] array;
+
+  private final double growthCoefficient;
+
+  public AutoExpandingBuffer(int initialCapacity, double growthCoefficient) {
+    if (growthCoefficient < 1.0) {
+      throw new IllegalArgumentException("Growth coefficient must be >= 1.0");
+    }
+    array = new byte[initialCapacity];
+    this.growthCoefficient = growthCoefficient;
+  }
+
+  public void resizeIfNecessary(int size) {
+    if (array.length < size) {
+      byte[] newBuf = new byte[(int)(size * growthCoefficient)];
+      System.arraycopy(array, 0, newBuf, 0, array.length);
+      array = newBuf;
+    }
+  }
+
+  public byte[] array() {
+    return array;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f63daff/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/AutoExpandingBufferReadTransport.java
----------------------------------------------------------------------
diff --git a/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/AutoExpandingBufferReadTransport.java b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/AutoExpandingBufferReadTransport.java
new file mode 100644
index 0000000..9b9d82e
--- /dev/null
+++ b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/AutoExpandingBufferReadTransport.java
@@ -0,0 +1,86 @@
+/*
+ * 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.
+ */
+package org.apache.blur.thirdparty.thrift_0_9_0.transport;
+
+import org.apache.commons.lang.NotImplementedException;
+
+/**
+ * TTransport for reading from an AutoExpandingBuffer.
+ */
+public class AutoExpandingBufferReadTransport extends TTransport {
+
+  private final AutoExpandingBuffer buf;
+
+  private int pos = 0;
+  private int limit = 0;
+
+  public AutoExpandingBufferReadTransport(int initialCapacity, double overgrowthCoefficient) {
+    this.buf = new AutoExpandingBuffer(initialCapacity, overgrowthCoefficient);
+  }
+
+  public void fill(TTransport inTrans, int length) throws TTransportException {
+    buf.resizeIfNecessary(length);
+    inTrans.readAll(buf.array(), 0, length);
+    pos = 0;
+    limit = length;
+  }
+
+  @Override
+  public void close() {}
+
+  @Override
+  public boolean isOpen() { return true; }
+
+  @Override
+  public void open() throws TTransportException {}
+
+  @Override
+  public final int read(byte[] target, int off, int len) throws TTransportException {
+    int amtToRead = Math.min(len, getBytesRemainingInBuffer());
+    System.arraycopy(buf.array(), pos, target, off, amtToRead);
+    consumeBuffer(amtToRead);
+    return amtToRead;
+  }
+
+  @Override
+  public void write(byte[] buf, int off, int len) throws TTransportException {
+    throw new NotImplementedException();
+  }
+
+  @Override
+  public final void consumeBuffer(int len) {
+    pos += len;
+  }
+
+  @Override
+  public final byte[] getBuffer() {
+    return buf.array();
+  }
+
+  @Override
+  public final int getBufferPosition() {
+    return pos;
+  }
+
+  @Override
+  public final int getBytesRemainingInBuffer() {
+    return limit - pos;
+  }
+}
+  
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f63daff/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/AutoExpandingBufferWriteTransport.java
----------------------------------------------------------------------
diff --git a/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/AutoExpandingBufferWriteTransport.java b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/AutoExpandingBufferWriteTransport.java
new file mode 100644
index 0000000..3b81697
--- /dev/null
+++ b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/AutoExpandingBufferWriteTransport.java
@@ -0,0 +1,68 @@
+/*
+ * 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.
+ */
+package org.apache.blur.thirdparty.thrift_0_9_0.transport;
+
+import org.apache.commons.lang.NotImplementedException;
+
+/**
+ * TTransport for writing to an AutoExpandingBuffer.
+ */
+public final class AutoExpandingBufferWriteTransport extends TTransport {
+
+  private final AutoExpandingBuffer buf;
+  private int pos;
+
+  public AutoExpandingBufferWriteTransport(int initialCapacity, double growthCoefficient) {
+    this.buf = new AutoExpandingBuffer(initialCapacity, growthCoefficient);
+    this.pos = 0;
+  }
+
+  @Override
+  public void close() {}
+
+  @Override
+  public boolean isOpen() {return true;}
+
+  @Override
+  public void open() throws TTransportException {}
+
+  @Override
+  public int read(byte[] buf, int off, int len) throws TTransportException {
+    throw new NotImplementedException();
+  }
+
+  @Override
+  public void write(byte[] toWrite, int off, int len) throws TTransportException {
+    buf.resizeIfNecessary(pos + len);
+    System.arraycopy(toWrite, off, buf.array(), pos, len);
+    pos += len;
+  }
+
+  public AutoExpandingBuffer getBuf() {
+    return buf;
+  }
+
+  public int getPos() {
+    return pos;
+  }
+
+  public void reset() {
+    pos = 0;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f63daff/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/TFastFramedTransport.java
----------------------------------------------------------------------
diff --git a/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/TFastFramedTransport.java b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/TFastFramedTransport.java
new file mode 100644
index 0000000..d16ce08
--- /dev/null
+++ b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/TFastFramedTransport.java
@@ -0,0 +1,188 @@
+/*
+ * 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.
+ */
+package org.apache.blur.thirdparty.thrift_0_9_0.transport;
+
+/**
+ * This transport is wire compatible with {@link TFramedTransport}, but makes 
+ * use of reusable, expanding read and write buffers in order to avoid
+ * allocating new byte[]s all the time. Since the buffers only expand, you
+ * should probably only use this transport if your messages are not too variably
+ * large, unless the persistent memory cost is not an issue.
+ * 
+ * This implementation is NOT threadsafe.
+ */
+public class TFastFramedTransport extends TTransport {
+
+  public static class Factory extends TTransportFactory {
+    private final int initialCapacity;
+    private final int maxLength;
+
+    public Factory() {
+      this(DEFAULT_BUF_CAPACITY, DEFAULT_MAX_LENGTH);
+    }
+
+    public Factory(int initialCapacity) {
+      this(initialCapacity, DEFAULT_MAX_LENGTH);
+    }
+
+    public Factory(int initialCapacity, int maxLength) {
+      this.initialCapacity = initialCapacity;
+      this.maxLength = maxLength;
+    }
+
+    @Override
+    public TTransport getTransport(TTransport trans) {
+      return new TFastFramedTransport(trans,
+          initialCapacity,
+          maxLength);
+    }
+  }
+
+  /**
+   * How big should the default read and write buffers be?
+   */
+  public static final int DEFAULT_BUF_CAPACITY = 1024;
+  /**
+   * How big is the largest allowable frame? Defaults to 16MB.
+   */
+  public static final int DEFAULT_MAX_LENGTH = 16384000;
+
+  private final TTransport underlying;
+  private final AutoExpandingBufferWriteTransport writeBuffer;
+  private final AutoExpandingBufferReadTransport readBuffer;
+  private final byte[] i32buf = new byte[4];
+  private final int maxLength;
+
+  /**
+   * Create a new {@link TFastFramedTransport}. Use the defaults
+   * for initial buffer size and max frame length.
+   * @param underlying Transport that real reads and writes will go through to.
+   */
+  public TFastFramedTransport(TTransport underlying) {
+    this(underlying, DEFAULT_BUF_CAPACITY, DEFAULT_MAX_LENGTH);
+  }
+
+  /**
+   * Create a new {@link TFastFramedTransport}. Use the specified
+   * initial buffer capacity and the default max frame length.
+   * @param underlying Transport that real reads and writes will go through to.
+   * @param initialBufferCapacity The initial size of the read and write buffers.
+   * In practice, it's not critical to set this unless you know in advance that
+   * your messages are going to be very large.
+   */
+  public TFastFramedTransport(TTransport underlying, int initialBufferCapacity) {
+    this(underlying, initialBufferCapacity, DEFAULT_MAX_LENGTH);
+  }
+
+  /**
+   * 
+   * @param underlying Transport that real reads and writes will go through to.
+   * @param initialBufferCapacity The initial size of the read and write buffers.
+   * In practice, it's not critical to set this unless you know in advance that
+   * your messages are going to be very large. (You can pass
+   * TFramedTransportWithReusableBuffer.DEFAULT_BUF_CAPACITY if you're only
+   * using this constructor because you want to set the maxLength.)
+   * @param maxLength The max frame size you are willing to read. You can use
+   * this parameter to limit how much memory can be allocated.
+   */
+  public TFastFramedTransport(TTransport underlying, int initialBufferCapacity, int maxLength) {
+    this.underlying = underlying;
+    this.maxLength = maxLength;
+    writeBuffer = new AutoExpandingBufferWriteTransport(initialBufferCapacity, 1.5);
+    readBuffer = new AutoExpandingBufferReadTransport(initialBufferCapacity, 1.5);
+  }
+
+  @Override
+  public void close() {
+    underlying.close();
+  }
+
+  @Override
+  public boolean isOpen() {
+    return underlying.isOpen();
+  }
+
+  @Override
+  public void open() throws TTransportException {
+    underlying.open();
+  }
+
+  @Override
+  public int read(byte[] buf, int off, int len) throws TTransportException {
+    int got = readBuffer.read(buf, off, len);
+    if (got > 0) {
+      return got;
+    }
+
+    // Read another frame of data
+    readFrame();
+
+    return readBuffer.read(buf, off, len);
+  }
+
+  private void readFrame() throws TTransportException {
+    underlying.readAll(i32buf , 0, 4);
+    int size = TFramedTransport.decodeFrameSize(i32buf);
+
+    if (size < 0) {
+      throw new TTransportException("Read a negative frame size (" + size + ")!");
+    }
+
+    if (size > maxLength) {
+      throw new TTransportException("Frame size (" + size + ") larger than max length (" + maxLength + ")!");
+    }
+
+    readBuffer.fill(underlying, size);
+  }
+
+  @Override
+  public void write(byte[] buf, int off, int len) throws TTransportException {
+    writeBuffer.write(buf, off, len);
+  }
+
+  @Override
+  public void consumeBuffer(int len) {
+    readBuffer.consumeBuffer(len);
+  }
+
+  @Override
+  public void flush() throws TTransportException {
+    int length = writeBuffer.getPos();
+    TFramedTransport.encodeFrameSize(length, i32buf);
+    underlying.write(i32buf, 0, 4);
+    underlying.write(writeBuffer.getBuf().array(), 0, length);
+    writeBuffer.reset();
+    underlying.flush();
+  }
+
+  @Override
+  public byte[] getBuffer() {
+    return readBuffer.getBuffer();
+  }
+
+  @Override
+  public int getBufferPosition() {
+    return readBuffer.getBufferPosition();
+  }
+
+  @Override
+  public int getBytesRemainingInBuffer() {
+    return readBuffer.getBytesRemainingInBuffer();
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9f63daff/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/TFileProcessor.java
----------------------------------------------------------------------
diff --git a/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/TFileProcessor.java b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/TFileProcessor.java
new file mode 100644
index 0000000..09c6bb6
--- /dev/null
+++ b/src/blur-thrift/src/main/java/org/apache/blur/thirdparty/thrift_0_9_0/transport/TFileProcessor.java
@@ -0,0 +1,130 @@
+/*
+ * 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.
+ */
+
+package org.apache.blur.thirdparty.thrift_0_9_0.transport;
+
+import org.apache.blur.thirdparty.thrift_0_9_0.TException;
+import org.apache.blur.thirdparty.thrift_0_9_0.TProcessor;
+import org.apache.blur.thirdparty.thrift_0_9_0.protocol.TProtocol;
+import org.apache.blur.thirdparty.thrift_0_9_0.protocol.TProtocolFactory;
+
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.RandomAccessFile;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.IOException;
+
+/**
+ * FileProcessor: helps in processing files generated by TFileTransport.
+ * Port of original cpp implementation
+ *
+ * @author Joydeep Sen Sarma <jssarma@facebook.com>
+ */
+public class TFileProcessor {
+
+  private TProcessor processor_;
+  private TProtocolFactory inputProtocolFactory_;
+  private TProtocolFactory outputProtocolFactory_;
+  private TFileTransport inputTransport_;
+  private TTransport outputTransport_;
+
+  public TFileProcessor(TProcessor processor, TProtocolFactory protocolFactory,
+                        TFileTransport inputTransport,
+                        TTransport outputTransport) {
+    processor_ = processor;
+    inputProtocolFactory_ = outputProtocolFactory_ = protocolFactory;
+    inputTransport_ = inputTransport;
+    outputTransport_ = outputTransport;
+  }
+
+  public TFileProcessor(TProcessor processor, 
+                        TProtocolFactory inputProtocolFactory,
+                        TProtocolFactory outputProtocolFactory,
+                        TFileTransport inputTransport,
+                        TTransport outputTransport) {
+    processor_ = processor;
+    inputProtocolFactory_ = inputProtocolFactory;
+    outputProtocolFactory_ = outputProtocolFactory;
+    inputTransport_ = inputTransport;
+    outputTransport_ = outputTransport;
+  }
+
+  private void processUntil(int lastChunk) throws TException {
+    TProtocol ip = inputProtocolFactory_.getProtocol(inputTransport_);
+    TProtocol op = outputProtocolFactory_.getProtocol(outputTransport_);
+    int curChunk = inputTransport_.getCurChunk();
+
+    try {
+      while (lastChunk >= curChunk) {
+        processor_.process(ip, op);
+        int newChunk = inputTransport_.getCurChunk();
+        curChunk = newChunk;
+      } 
+    } catch (TTransportException e) {
+      // if we are processing the last chunk - we could have just hit EOF
+      // on EOF - trap the error and stop processing.
+      if(e.getType() != TTransportException.END_OF_FILE)
+        throw e;
+      else {
+        return;
+      }
+    }
+  }
+
+  /**
+   * Process from start to last chunk both inclusive where chunks begin from 0
+
+   * @param startChunkNum first chunk to be processed
+   * @param endChunkNum last chunk to be processed
+   */
+  public void processChunk(int startChunkNum, int endChunkNum) throws TException {
+    int numChunks = inputTransport_.getNumChunks();
+    if(endChunkNum < 0)
+      endChunkNum += numChunks;
+
+    if(startChunkNum < 0)
+      startChunkNum += numChunks;
+
+    if(endChunkNum < startChunkNum)
+      throw new TException("endChunkNum " + endChunkNum + " is less than " + startChunkNum);
+
+    inputTransport_.seekToChunk(startChunkNum);
+    processUntil(endChunkNum);
+  }
+
+  /**
+   * Process a single chunk
+   *
+   * @param chunkNum chunk to be processed
+   */
+  public void processChunk(int chunkNum) throws TException {
+    processChunk(chunkNum, chunkNum);
+  }
+
+  /**
+   * Process a current chunk
+   */
+  public void processChunk() throws TException {
+    processChunk(inputTransport_.getCurChunk());
+  }
+}


Mime
View raw message