<commit_before>/*******************************************************************************
 * 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.cache;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.util.Calendar;
import java.util.Map;

import org.eclipse.birt.data.engine.api.DataEngine;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.executor.IncreDataSetCacheObject;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;
import org.eclipse.birt.data.engine.impl.DataEngineSession;
import org.eclipse.birt.data.engine.impl.IIncreCacheDataSetDesign;

/**
 * 
 */
public class CacheUtil
{
	private static final int MAX_DIR_CREATION_ATTEMPT = 1000;
	private static final String PATH_SEP = File.separator;
	private static final String TEST_MEM_BUFFER_SIZE = "birt.data.engine.test.memcachesize";
	
	/**
	 * timestamp.data file will be used in incremental cache, while
	 * time.data file will be used in disk cache.
	 */
	private static final String TIME_DATA = "time.data";
	/**
	 * root directory for incremental cache, which should under the user
	 * specified cache directory.
	 */
	private static final String PS_ = "PS_"; 
	
	/**
	 * counters for creating unique temporary directory.
	 */
	private static IntegerHolder cacheCounter1 = new IntegerHolder( 0 );
	private static IntegerHolder cacheCounter2 = new IntegerHolder( 0 );
	
	private CacheUtil( )
	{
	}
	
	// --------------------service for SmartCache----------------------
	/**
	 * @return
	 */
	public static long computeMemoryBufferSize( Map appContext )
	{
		//here a simple assumption, that 1M memory can accommodate 2000 rows
		if ( appContext == null )
			return 10*1024*1024;
		if ( appContext.get( TEST_MEM_BUFFER_SIZE )!= null )
		{
			//For unit test.The unit is 1 byte.
			return populateMemBufferSize( appContext.get( TEST_MEM_BUFFER_SIZE ) );
		}
		
		//The unit is 1M.
		return populateMemBufferSize( appContext.get( DataEngine.MEMORY_BUFFER_SIZE )) * 1024 * 1024;
	}

	/**
	 * 
	 * @param propValue
	 * @return
	 */
	private static long populateMemBufferSize( Object propValue )
	{
		String targetBufferSize =  propValue == null
				? "10" : propValue
						.toString( );
		
		long memoryCacheSize = 10; 
		
		if ( targetBufferSize != null )
			memoryCacheSize = Long.parseLong( targetBufferSize );

		return memoryCacheSize == 0?1:memoryCacheSize;
	}
	
	// ------------------------service for DiskCache-------------------------

	public static String createTempRootDir( String tempDir )
	{
		String rootDirStr = null;

		// system default temp dir is used
		File tempDtEDir = null;
		tempDtEDir = new File(tempDir, "BirtDataTemp"
				+ System.currentTimeMillis() + cacheCounter1);
		cacheCounter1.add(1);
		int x = 0;
		while (tempDtEDir.exists())
		{
			x++;
			tempDtEDir = new File(tempDir, "BirtDataTemp"
					+ System.currentTimeMillis() + cacheCounter1 + "_" + x);
		}
		tempDtEDir.mkdirs();
		tempDtEDir.deleteOnExit();
		rootDirStr = getCanonicalPath( tempDtEDir );
		return rootDirStr;
	}

	/**
	 * @return session temp dir
	 * @throws DataException 
	 * @throws IOException 
	 */<commit_msg>Fix a temporary directory naming bug.<commit_after>/*******************************************************************************
 * 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.cache;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.util.Calendar;
import java.util.Map;

import org.eclipse.birt.data.engine.api.DataEngine;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.executor.IncreDataSetCacheObject;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;
import org.eclipse.birt.data.engine.impl.DataEngineSession;
import org.eclipse.birt.data.engine.impl.IIncreCacheDataSetDesign;

/**
 * 
 */
public class CacheUtil
{
	private static final int MAX_DIR_CREATION_ATTEMPT = 1000;
	private static final String PATH_SEP = File.separator;
	private static final String TEST_MEM_BUFFER_SIZE = "birt.data.engine.test.memcachesize";
	
	/**
	 * timestamp.data file will be used in incremental cache, while
	 * time.data file will be used in disk cache.
	 */
	private static final String TIME_DATA = "time.data";
	/**
	 * root directory for incremental cache, which should under the user
	 * specified cache directory.
	 */
	private static final String PS_ = "PS_"; 
	
	/**
	 * counters for creating unique temporary directory.
	 */
	private static IntegerHolder cacheCounter1 = new IntegerHolder( 0 );
	private static IntegerHolder cacheCounter2 = new IntegerHolder( 0 );
	
	private CacheUtil( )
	{
	}
	
	// --------------------service for SmartCache----------------------
	/**
	 * @return
	 */
	public static long computeMemoryBufferSize( Map appContext )
	{
		//here a simple assumption, that 1M memory can accommodate 2000 rows
		if ( appContext == null )
			return 10*1024*1024;
		if ( appContext.get( TEST_MEM_BUFFER_SIZE )!= null )
		{
			//For unit test.The unit is 1 byte.
			return populateMemBufferSize( appContext.get( TEST_MEM_BUFFER_SIZE ) );
		}
		
		//The unit is 1M.
		return populateMemBufferSize( appContext.get( DataEngine.MEMORY_BUFFER_SIZE )) * 1024 * 1024;
	}

	/**
	 * 
	 * @param propValue
	 * @return
	 */
	private static long populateMemBufferSize( Object propValue )
	{
		String targetBufferSize =  propValue == null
				? "10" : propValue
						.toString( );
		
		long memoryCacheSize = 10; 
		
		if ( targetBufferSize != null )
			memoryCacheSize = Long.parseLong( targetBufferSize );

		return memoryCacheSize == 0?1:memoryCacheSize;
	}
	
	// ------------------------service for DiskCache-------------------------

	public static String createTempRootDir( String tempDir )
	{
		String rootDirStr = null;

		// system default temp dir is used
		File tempDtEDir = null;
		tempDtEDir = new File(tempDir, "BirtDataTemp"
				+ System.currentTimeMillis() + cacheCounter1.intValue() );
		cacheCounter1.add(1);
		int x = 0;
		while (tempDtEDir.exists())
		{
			x++;
			tempDtEDir = new File(tempDir, "BirtDataTemp"
					+ System.currentTimeMillis() + cacheCounter1.intValue() + "_" + x);
		}
		tempDtEDir.mkdirs();
		tempDtEDir.deleteOnExit();
		rootDirStr = getCanonicalPath( tempDtEDir );
		return rootDirStr;
	}

	/**
	 * @return session temp dir
	 * @throws DataException 
	 * @throws IOException 
	 */
{'fraction_non_alphanumeric': 0.15910593836794074, 'fraction_numerical': 0.01428382489088745, 'mean_word_length': 3.6392638036809815, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6444541', 'n_tokens_mistral': 2459, 'n_tokens_neox': 2149, 'n_words': 595}