/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/
package org.eclipse.birt.data.engine.executor;

import org.eclipse.birt.data.engine.api.DataEngineContext;
import org.eclipse.birt.data.engine.api.IBaseDataSetDesign;

/**
 * 
 */
class DataSetCacheUtil
{
	
	/**
	 * @return
	 */
	static boolean needsToCache( IBaseDataSetDesign dataSetDesign,
			int cacheOption, int alwaysCacheRowCount )
	{
		if ( dataSetDesign == null )
			return false;

		if ( cacheOption == DataEngineContext.CACHE_USE_DISABLE )
		{
			return false;
		}
		else if ( cacheOption == DataEngineContext.CACHE_USE_ALWAYS )
		{
			if ( alwaysCacheRowCount == 0 )
				return false;
		}
		else if ( dataSetDesign.getCacheRowCount( ) == 0 )
		{
			return false;
		}

		return true;
	}
	
	/**
	 * @return
	 */
	static int getCacheRowCount( int cacheOption, int alwaysCacheRowCount,
			int cacheRowCount )
	{
		if ( cacheOption == DataEngineContext.CACHE_USE_ALWAYS )
		{
			if ( alwaysCacheRowCount <= 0 )
				return Integer.MAX_VALUE;
			else
				return alwaysCacheRowCount;
		}
		else if ( cacheOption == DataEngineContext.CACHE_USE_DISABLE )
		{
			return Integer.MAX_VALUE;
		}
		else
		{
			if ( cacheRowCount == -1 )
				return Integer.MAX_VALUE;
			else
				return cacheRowCount;
		}
	}
	
}
