/**
 * 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 storm.trident.spout;

import backtype.storm.task.TopologyContext;
import storm.trident.topology.TransactionAttempt;
import backtype.storm.tuple.Fields;
import java.io.Serializable;
import java.util.Map;
import storm.trident.operation.TridentCollector;


public interface ITridentSpout<T> extends Serializable {
    interface BatchCoordinator<X> {
        /**
         * Create metadata for this particular transaction id which has never
         * been emitted before. The metadata should contain whatever is necessary
         * to be able to replay the exact batch for the transaction at a later point.
         * 
         * The metadata is stored in Zookeeper.
         * 
         * Storm uses the Kryo serializations configured in the component configuration 
         * for this spout to serialize and deserialize the metadata.
         * 
         * @param txid The id of the transaction.
         * @param prevMetadata The metadata of the previous transaction
         * @param currMetadata The metadata for this transaction the last time it was initialized.
         *                     null if this is the first attempt
         * @return the metadata for this new transaction
         */
        X initializeTransaction(long txid, X prevMetadata, X currMetadata);
        
        void success(long txid);
        
        boolean isReady(long txid);
        
        /**
         * Release any resources from this coordinator.
         */
        void close();
    }
    
    interface Emitter<X> {
        /**
         * Emit a batch for the specified transaction attempt and metadata for the transaction. The metadata
         * was created by the Coordinator in the initializeTranaction method. This method must always emit
         * the same batch of tuples across all tasks for the same transaction id.
         * 
         */
        void emitBatch(TransactionAttempt tx, X coordinatorMeta, TridentCollector collector);
        
        /**
         * This attempt committed successfully, so all state for this commit and before can be safely cleaned up.
         */
        void success(TransactionAttempt tx);
        
        /**
         * Release any resources held by this emitter.
         */
        void close();
    }
    
    /**
     * The coordinator for a TransactionalSpout runs in a single thread and indicates when batches
     * of tuples should be emitted and when transactions should commit. The Coordinator that you provide 
     * in a TransactionalSpout provides metadata for each transaction so that the transactions can be replayed.
     */
    BatchCoordinator<T> getCoordinator(String txStateId, Map conf, TopologyContext context);

    /**
     * The emitter for a TransactionalSpout runs as many tasks across the cluster. Emitters are responsible for
     * emitting batches of tuples for a transaction and must ensure that the same batch of tuples is always
     * emitted for the same transaction id.
     */    
    Emitter<T> getEmitter(String txStateId, Map conf, TopologyContext context); 
    
    Map<String, Object> getComponentConfiguration();
    Fields getOutputFields();
}

@@ -1507,142 +1507,141 @@
 ses 
-the Kryo serializations configured in the component configuration %0A         * for this spout to serialize and deserialize the metadata
+JSON encoding to store the metadata.  Only simple types%0A         * such as numbers, booleans, strings, lists, and maps should be used
 .%0A  
@@ -2081,24 +2081,25 @@
 adata);%0A
+%0A
         
 %0A       
@@ -2082,32 +2082,225 @@
 data);%0A%0A        
+/**%0A         * This attempt committed successfully, so all state for this commit and before can be safely cleaned up.%0A         *%0A         * @param txid transaction id that completed%0A         */
 %0A        void su
@@ -2313,32 +2313,33 @@
 long txid);%0A
+%0A
         
 %0A        boo
@@ -2318,32 +2318,253 @@
 txid);%0A%0A        
+/**%0A         * hint to Storm if the spout is ready for the transaction id%0A         *%0A         * @param txid the id of the transaction%0A         * @return true, if the spout is ready for the given transaction id%0A         */
 %0A        boolean
@@ -2917,16 +2917,17 @@
 lizeTran
+s
 action m
@@ -3047,32 +3047,197 @@
  id.%0A         * 
+@param tx transaction id%0A         * @param coordinatorMeta metadata from the coordinator defining this transaction%0A         * @param collector output tuple collector
 %0A         */%0A   
@@ -3454,32 +3454,113 @@
 ely cleaned up.%0A
+         * @param tx attempt object containing transaction id and attempt number%0A
          */%0A    
@@ -3827,23 +3827,16 @@
  batches
-%0A     *
  of tupl
@@ -3837,16 +3837,23 @@
 f tuples
+%0A     *
  should 
@@ -3866,44 +3866,8 @@
 tted
- and when transactions should commit
 . Th
@@ -3897,24 +3897,16 @@
 provide 
-%0A     * 
 in a Tra
@@ -3947,16 +3947,23 @@
 for each
+%0A     *
  transac
@@ -4007,25 +4007,451 @@
 replayed
-.%0A     */
+ in case of failure.%0A     *%0A     * Two instances are requested, one on the master batch coordinator where isReady() is called, and an instance%0A     * in the coordinator bolt which is used for all other operations. The two instances do not necessarily share a%0A     * worker JVM.%0A     *%0A     * @param txStateId stream id%0A     * @param conf Storm config map%0A     * @param context topology context%0A     * @return spout coordinator instance
 %0A    Bat
@@ -4819,13 +4819,347 @@
    *
-/
+%0A 
     
+* All emitter tasks get the same transaction metadata. The topology context parameter contains the instance%0A     * task id that can be used to distribute the work across the tasks.%0A     *%0A     * @param txStateId stream id%0A     * @param conf Storm config map%0A     * @param context topology context%0A     * @return spout emitter%0A     */
 %0A   

Improve documentation including incorrect Kryo serialization framework doc