/*
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * Modified by
 * @author  Data Virtuality GmbH
 * @date    $Date$
 *  
 */
// https://issues.jboss.org/browse/TEIID-2153
// https://issues.jboss.org/browse/TEIID-2171
// https://issues.jboss.org/browse/TEIID-2359
// https://issues.jboss.org/browse/TEIID-2149
// https://issues.jboss.org/browse/TEIID-2358
// https://issues.jboss.org/browse/TEIID-2410
// https://issues.jboss.org/browse/TEIID-2271
// https://issues.jboss.org/browse/TEIID-2467
// https://issues.jboss.org/browse/TEIID-2429
// https://issues.jboss.org/browse/TEIID-2363
// https://issues.jboss.org/browse/TEIID-2160
// https://issues.jboss.org/browse/TEIID-2461
// https://issues.jboss.org/browse/TEIID-2797
// https://issues.jboss.org/browse/TEIID-2326
// https://issues.jboss.org/browse/TEIID-1557
// https://issues.jboss.org/browse/TEIID-2628
// https://issues.jboss.org/browse/TEIID-2496
// https://issues.jboss.org/browse/TEIID-2499
// https://issues.jboss.org/browse/TEIID-2582
// https://issues.jboss.org/browse/TEIID-2672
// https://issues.jboss.org/browse/TEIID-2626
// https://issues.jboss.org/browse/TEIID-2707
// https://issues.jboss.org/browse/TEIID-2900
// https://issues.jboss.org/browse/TEIID-2902
// https://issues.jboss.org/browse/TEIID-2457
// https://issues.jboss.org/browse/TEIID-2942
// https://github.com/teiid/teiid/commit/f840ed8283aad9cbe8d3019467a88d951d695659
// https://issues.jboss.org/browse/TEIID-2482
// https://issues.jboss.org/browse/TEIID-2346
// https://issues.jboss.org/browse/TEIID-2430
// https://issues.jboss.org/browse/TEIID-2731
// https://issues.jboss.org/browse/TEIID-2747
// https://issues.jboss.org/browse/TEIID-2946
// https://issues.jboss.org/browse/TEIID-3568
// https://issues.jboss.org/browse/TEIID-4812
package org.teiid.query.processor.relational;

import java.util.Collections;
import java.util.List;

import org.teiid.common.buffer.BufferManager.TupleSourceType;
import org.teiid.common.buffer.IndexedTupleSource;
import org.teiid.common.buffer.TupleBuffer;
import org.teiid.common.buffer.TupleSource;
import org.teiid.core.TeiidComponentException;
import org.teiid.core.TeiidProcessingException;
import org.teiid.core.util.Assertion;
import org.teiid.query.processor.BatchCollector;
import org.teiid.query.processor.BatchIterator;
import org.teiid.query.processor.relational.MergeJoinStrategy.SortOption;
import org.teiid.query.processor.relational.SortUtility.Mode;
import org.teiid.query.sql.lang.OrderBy;
import org.teiid.query.QueryPlugin;


class SourceState {

	enum ImplicitBuffer {
		NONE, FULL, ON_MARK
	}
	
    private RelationalNode source;
    private List expressions;
    private BatchCollector collector;
    private TupleBuffer buffer;
    private List<TupleBuffer> buffers;
    private List<Object> outerVals;
    private IndexedTupleSource iterator;
    private int[] expressionIndexes;
    private List currentTuple;
    private int maxProbeMatch = 1;
    private boolean distinct;
    private ImplicitBuffer implicitBuffer = ImplicitBuffer.FULL;
    boolean open;
    
    private SortUtility sortUtility;
	private boolean limited;
    
    public SourceState(RelationalNode source, List expressions) {
        this.source = source;
        this.expressions = expressions;
        List elements = source.getElements();
        this.outerVals = Collections.nCopies(elements.size(), null);
        this.expressionIndexes = getExpressionIndecies(expressions, elements);
    }
    
    public RelationalNode getSource() {
		return source;
	}
    
    public void setImplicitBuffer(ImplicitBuffer implicitBuffer) {
		this.implicitBuffer = implicitBuffer;
	}
    
    public ImplicitBuffer getImplicitBuffer() {
		return implicitBuffer;
	}
    
    static int[] getExpressionIndecies(List expressions,
                                        List elements) {
        if (expressions == null) {
            return new int[0];
        }
        int[] indecies = new int[expressions.size()];
        for (int i = 0; i < expressions.size(); i++) {
            indecies[i] = elements.indexOf(expressions.get(i));
            assert indecies[i] != -1;
        }
        return indecies;
    }
    
    TupleBuffer createSourceTupleBuffer() throws TeiidComponentException {
    	return this.source.getBufferManager().createTupleBuffer(source.getElements(), source.getConnectionID(), TupleSourceType.PROCESSOR);
    }
    
    public List saveNext() throws TeiidComponentException, TeiidProcessingException {
        this.currentTuple = this.getIterator().nextTuple();
        return currentTuple;
    }
    
    public void reset() throws TeiidComponentException, TeiidProcessingException {
        this.getIterator().reset();
        this.getIterator().mark();
        this.currentTuple = null;
    }
    
    public void close() {
    	closeBuffer();
    	if (buffers != null) {
	    	for (TupleBuffer tb : buffers) {
	    		tb.remove();
	    	}
    	}
    	this.buffers = null;
        this.open = false;
        if (this.sortUtility != null) {
        	this.sortUtility.remove();
        	this.sortUtility = null;
        }
    }

	private void closeBuffer() {
		if (this.buffer != null) {
            this.buffer.remove();
            this.buffer = null;
        }
        if (this.iterator != null) {
			this.iterator.closeSource();
        	this.iterator = null;
        }
        this.currentTuple = null;
	}

    public int getRowCount() throws TeiidComponentException, TeiidProcessingException {
    	return this.getTupleBuffer().getRowCount();
    }
    
    /**
     * Uses the prefetch logic to determine an incremental row count
     */
    public boolean rowCountLE(long count) throws TeiidComponentException, TeiidProcessingException {
    	if (buffer != null) {
    		return buffer.getRowCount() <= count;
    	}
    	if (iterator != null || this.sortUtility != null) {
    		throw new IllegalStateException();
    	}
    	while (buffer == null) {
    		if (getIncrementalRowCount(true) > count) {
    			return false;
    		}
		    prefetch(count + 1);
    	}
		return buffer.getRowCount() <= count;
    }

    IndexedTupleSource getIterator() throws TeiidComponentException, TeiidProcessingException {
        if (this.iterator == null) {
        	if (this.buffer != null) {
                iterator = buffer.createIndexedTupleSource();
            } else {
                // return a TupleBatch tuplesource iterator
                BatchIterator bi = new BatchIterator(this.source);
                if (this.collector != null) {
                	bi.setBuffer(this.collector.getTupleBuffer(), implicitBuffer == ImplicitBuffer.ON_MARK);
                	if (implicitBuffer == ImplicitBuffer.NONE) {
                		bi.getBuffer().setForwardOnly(true);
                	}
                	this.collector = null;
                } else if (implicitBuffer != ImplicitBuffer.NONE) {
                	bi.setBuffer(createSourceTupleBuffer(), implicitBuffer == ImplicitBuffer.ON_MARK);
                }
                this.iterator = bi;
            }
        }
        return this.iterator;
    }
    
    /**
     * Pro-actively pull batches for later use.
     * There are unfortunately quite a few cases to cover here.
     */
    protected void prefetch(long limit) throws TeiidComponentException, TeiidProcessingException {
    	if (!open) {
    		return;
    	}
    	if (this.buffer == null) {
    		if (this.sortUtility != null) {
    			return;
    		}
    		if (this.iterator != null) {
    			((BatchIterator)this.iterator).readAhead(limit);
    			return;
    		}
    		if (source.hasBuffer(true)) {
    			this.buffer = source.getBuffer(-1);
    			return;
    		}
	    	if (collector == null) {
	            collector = new BatchCollector(source, source.getBufferManager(), source.getContext(), false);
	    	}
	    	if (collector.getTupleBuffer() != null && collector.getTupleBuffer().getManagedRowCount() >= limit) {
	    		return;
	    	}
	        this.buffer = collector.collectTuples(true);
    	}
    }

    public List<Object> getOuterVals() {
        return this.outerVals;
    }

    public List getCurrentTuple() {
        return this.currentTuple;
    }

    public int[] getExpressionIndexes() {
        return this.expressionIndexes;
    }
    
    void setMaxProbeMatch(int maxProbeMatch) {
        this.maxProbeMatch = maxProbeMatch;
    }

    int getMaxProbeMatch() {
        return maxProbeMatch;
    }

    public TupleBuffer getTupleBuffer() throws TeiidComponentException, TeiidProcessingException {
        if (this.buffer == null) {
        	if (this.iterator instanceof BatchIterator) {
        		throw new AssertionError(QueryPlugin.Util.gs(QueryPlugin.Event.DVCORE301257));
        	}
    		if (source.hasBuffer(true)) {
    			this.buffer = source.getBuffer(-1);
    			Assertion.assertTrue(this.buffer.isFinal());
    			return this.buffer;
    		}
        	if (collector == null) {
                collector = new BatchCollector(source, source.getBufferManager(), source.getContext(), false);
            }
            this.buffer = collector.collectTuples();
        } 
        return this.buffer;
    }

    /**
     * @return true if the join expressions are a distinct set
     */
    public boolean isExpresssionDistinct() {
        return this.distinct;
    }

    public void markExpressionsDistinct(boolean distinct) {
        this.distinct |= distinct;
    }
    
    public void sort(SortOption sortOption) throws TeiidComponentException, TeiidProcessingException {
    	if (sortOption == SortOption.ALREADY_SORTED) {
    		return;
    	}
    	if (this.sortUtility == null) {
    		TupleSource ts = null;
    		if (source.hasBuffer(true)) {
			    this.buffer = getTupleBuffer();
    		} else if (this.buffer == null && this.collector != null) {
    			if (sortOption == SortOption.NOT_SORTED) {
    				//pass the buffer and the source
    				this.buffer = this.collector.getTupleBuffer();
    				ts = new BatchCollector.BatchProducerTupleSource(this.source, this.buffer.getRowCount() + 1);
    			} else {
    				//fully read
    				this.buffer = this.collector.collectTuples();
    			}
    		}
    		if (this.buffer != null) {
    			this.buffer.setForwardOnly(true);
    		} else {
    			ts = new BatchIterator(this.source);
    		}
		    this.sortUtility = new SortUtility(ts, expressions, Collections.nCopies(expressions.size(), OrderBy.ASC), 
		    		sortOption == SortOption.SORT_DISTINCT?Mode.DUP_REMOVE_SORT:Mode.SORT, this.source.getBufferManager(), this.source.getConnectionID(), source.getElements());
		    this.markExpressionsDistinct(sortOption == SortOption.SORT_DISTINCT && expressions.size() == this.getOuterVals().size());
		    if (this.buffer != null) {
		    	this.sortUtility.setWorkingBuffer(this.buffer);
		    }
		}
    	TupleBuffer sorted = null;
    	if (sortOption == SortOption.NOT_SORTED) {
    		if (this.buffers != null || sortUtility.isDoneReading()) {
    			return;
    		}
    		this.buffers = sortUtility.onePassSort(limited);
    		if (this.buffers.size() != 1 || !sortUtility.isDoneReading()) {
        		nextBuffer();
        		return;
    		}
    		sorted = this.buffers.get(0);
    		this.buffers = null;
    	} else { 
    		sorted = sortUtility.sort();
    	}
    	//only remove the buffer if this is the first time through
    	if (this.buffer != null && this.buffer != sorted) {
    		this.buffer.remove();
    	}
		this.buffer = sorted;
        this.markExpressionsDistinct(sortUtility.isDistinct());
    }
    
    public boolean hasBuffer() {
    	return this.buffer != null || this.source.hasBuffer(true);
    }
    
    public boolean nextBuffer() throws TeiidComponentException, TeiidProcessingException {
    	this.closeBuffer();
    	if (this.buffers == null || this.buffers.isEmpty()) {
    		if (!sortUtility.isDoneReading()) {
    			this.buffers = sortUtility.onePassSort(limited);
    			return nextBuffer();
    		}
    		return false;
    	}
    	this.buffer = this.buffers.remove(this.buffers.size() - 1);
    	this.buffer.setForwardOnly(false);
    	this.resetState();
    	return true;
    }

    /**
     * return the iterator to a fresh state
     */
	public void resetState() {
		if (this.iterator != null) {
			this.iterator.reset();
			this.iterator.setPosition(1);
		}
		this.currentTuple = null;
		this.maxProbeMatch = 1;
	}
	
	public void setMaxProbePosition() throws TeiidComponentException, TeiidProcessingException {
		this.getIterator().setPosition(this.getMaxProbeMatch());
		this.currentTuple = null;
	}

	public int getIncrementalRowCount(boolean low) {
		if (this.buffer != null) {
			return this.buffer.getRowCount();
		}
		if (this.collector != null) {
			return this.collector.getTupleBuffer().getRowCount();
		}
		if (sortUtility == null) {
			if (this.iterator instanceof BatchIterator) {
				TupleBuffer tb = ((BatchIterator)this.iterator).getBuffer();
				if (tb != null) {
					return tb.getRowCount();
				}
				//TODO: should estimate the rows
			}
			//TODO: should estimate the rows based upon what is being fed into the sort
		}
		return low?0:Integer.MAX_VALUE;
	}
	
	public SortUtility getSortUtility() {
		return sortUtility;
	}

	public void isLimited(boolean hasLimit) {
		this.limited = hasLimit;
	}
    
}
