<commit_before>package railo.runtime.type.scope.storage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import railo.commons.lang.RandomUtil;
import railo.commons.lang.SizeOf;
import railo.commons.lang.StringUtil;
import railo.runtime.PageContext;
import railo.runtime.config.Config;
import railo.runtime.dump.DumpData;
import railo.runtime.dump.DumpProperties;
import railo.runtime.engine.ThreadLocalPageContext;
import railo.runtime.exp.PageException;
import railo.runtime.listener.ApplicationContext;
import railo.runtime.type.Collection;
import railo.runtime.type.Sizeable;
import railo.runtime.type.Struct;
import railo.runtime.type.StructImpl;
import railo.runtime.type.dt.DateTime;
import railo.runtime.type.dt.DateTimeImpl;
import railo.runtime.type.util.CollectionUtil;
import railo.runtime.type.util.KeyConstants;
import railo.runtime.type.util.StructSupport;
import railo.runtime.type.util.StructUtil;

public abstract class StorageScopeImpl extends StructSupport implements StorageScope,Sizeable {

	public static Collection.Key CFID=KeyConstants._cfid;
	public static Collection.Key CFTOKEN=KeyConstants._cftoken;
	public static Collection.Key URLTOKEN=KeyConstants._urltoken;
	public static Collection.Key LASTVISIT=KeyConstants._lastvisit;
	public static Collection.Key HITCOUNT=KeyConstants._hitcount;
	public static Collection.Key TIMECREATED=KeyConstants._timecreated;
	public static Collection.Key SESSION_ID=KeyConstants._sessionid;


	private static int _id=0;
	private int id=0;

	private static final long serialVersionUID = 7874930250042576053L;
	private static Set<Collection.Key> FIX_KEYS=new HashSet<Collection.Key>();
	static {
		FIX_KEYS.add(CFID);
		FIX_KEYS.add(CFTOKEN);
		FIX_KEYS.add(URLTOKEN);
		FIX_KEYS.add(LASTVISIT);
		FIX_KEYS.add(HITCOUNT);
		FIX_KEYS.add(TIMECREATED);
	}
	

	protected static Set<Collection.Key> ignoreSet=new HashSet<Collection.Key>();
	static {
		ignoreSet.add(CFID);
		ignoreSet.add(CFTOKEN);
		ignoreSet.add(URLTOKEN);
	}
	
	
	protected boolean isinit=true;
	protected Struct sct;
	protected long lastvisit;
	protected DateTime _lastvisit;
	protected int hitcount=0;
	protected DateTime timecreated;
	private boolean hasChanges=false;
	private String strType;
	private int type;
	private long timeSpan=-1;
	private String storage;
	private Map<String, String> tokens; 
	
	
	/**
	 * Constructor of the class
	 * @param sct
	 * @param timecreated
	 * @param _lastvisit
	 * @param lastvisit
	 * @param hitcount
	 */
	public StorageScopeImpl(Struct sct, DateTime timecreated, DateTime _lastvisit, long lastvisit, int hitcount,String strType,int type) {
		this.sct=sct;
		this.timecreated=timecreated;
		if(_lastvisit==null)	this._lastvisit=timecreated;
		else 					this._lastvisit=_lastvisit;
		
		if(lastvisit==-1) 		this.lastvisit=this._lastvisit.getTime();
		else 					this.lastvisit=lastvisit;

		this.hitcount=hitcount;
		this.strType=strType;
		this.type=type;
        id=++_id;
	}
	
	/**
	 * Constructor of the class
	 * @param other
	 * @param deepCopy
	 */
	public StorageScopeImpl(StorageScopeImpl other, boolean deepCopy) {
		this.sct=(Struct)other.sct.duplicate(deepCopy);
		this.timecreated=other.timecreated;
		this._lastvisit=other._lastvisit;
		this.hitcount=other.hitcount;
		this.isinit=other.isinit;
		this.lastvisit=other.lastvisit;
		this.strType=other.strType;
		this.type=other.type;
		this.timeSpan=other.timeSpan;
        id=++_id;
	}

	/**
	 * @see railo.runtime.type.scope.Scope#initialize(railo.runtime.PageContext)
	 */
	public void touchBeforeRequest(PageContext pc) {
		
		hasChanges=false;
		setTimeSpan(pc);
		
		
		//lastvisit=System.currentTimeMillis();
		if(sct==null) sct=new StructImpl();
		sct.setEL(KeyConstants._cfid, pc.getCFID());
		sct.setEL(KeyConstants._cftoken, pc.getCFToken());
		sct.setEL(URLTOKEN, pc.getURLToken());
		sct.setEL(LASTVISIT, _lastvisit);
		_lastvisit=new DateTimeImpl(pc.getConfig());
		lastvisit=System.currentTimeMillis();
		
		if(type==SCOPE_CLIENT){
			sct.setEL(HITCOUNT, new Double(hitcount++));
		}
		else {
			sct.setEL(SESSION_ID, pc.getApplicationContext().getName()+"_"+pc.getCFID()+"_"+pc.getCFToken());
		}
		sct.setEL(TIMECREATED, timecreated);
	}

	public void resetEnv(PageContext pc){
		_lastvisit=new DateTimeImpl(pc.getConfig());
		timecreated=new DateTimeImpl(pc.getConfig());
		touchBeforeRequest(pc);
		
	}
	
	void setTimeSpan(PageContext pc) {
		ApplicationContext ac=pc.getApplicationContext();
		this.timeSpan=getType()==SCOPE_SESSION?
				ac.getSessionTimeout().getMillis():
				ac.getClientTimeout().getMillis();
	}
	

	/**
	 * @see railo.runtime.type.scope.storage.StorageScope#setMaxInactiveInterval(int)
	 */
	@Override
	public void setMaxInactiveInterval(int interval) {
		this.timeSpan=interval*1000L;
	}

	/**
	 * @see railo.runtime.type.scope.storage.StorageScope#getMaxInactiveInterval()
	 */
	@Override
	public int getMaxInactiveInterval() {
		return (int)(this.timeSpan/1000L);
	}
	
	
	

	/**
	 * @see railo.runtime.type.scope.Scope#isInitalized()
	 */
	public final boolean isInitalized() {
		return isinit;
	}
	
	/**
	 * @see railo.runtime.type.scope.Scope#initialize(railo.runtime.PageContext)
	 */
	public final void initialize(PageContext pc) {
		// StorageScopes need only request initialisation no global init, they are not reused;
	}
	
	/**
	 * @see railo.runtime.type.SharedScope#release(railo.runtime.PageContext)
	 */
	public void touchAfterRequest(PageContext pc) {
		<commit_msg>cleaned some unnecessary comments<commit_after>package railo.runtime.type.scope.storage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import railo.commons.lang.RandomUtil;
import railo.commons.lang.SizeOf;
import railo.commons.lang.StringUtil;
import railo.runtime.PageContext;
import railo.runtime.config.Config;
import railo.runtime.dump.DumpData;
import railo.runtime.dump.DumpProperties;
import railo.runtime.engine.ThreadLocalPageContext;
import railo.runtime.exp.PageException;
import railo.runtime.listener.ApplicationContext;
import railo.runtime.type.Collection;
import railo.runtime.type.Sizeable;
import railo.runtime.type.Struct;
import railo.runtime.type.StructImpl;
import railo.runtime.type.dt.DateTime;
import railo.runtime.type.dt.DateTimeImpl;
import railo.runtime.type.util.CollectionUtil;
import railo.runtime.type.util.KeyConstants;
import railo.runtime.type.util.StructSupport;
import railo.runtime.type.util.StructUtil;

public abstract class StorageScopeImpl extends StructSupport implements StorageScope,Sizeable {

	public static Collection.Key CFID=KeyConstants._cfid;
	public static Collection.Key CFTOKEN=KeyConstants._cftoken;
	public static Collection.Key URLTOKEN=KeyConstants._urltoken;
	public static Collection.Key LASTVISIT=KeyConstants._lastvisit;
	public static Collection.Key HITCOUNT=KeyConstants._hitcount;
	public static Collection.Key TIMECREATED=KeyConstants._timecreated;
	public static Collection.Key SESSION_ID=KeyConstants._sessionid;


	private static int _id=0;
	private int id=0;

	private static final long serialVersionUID = 7874930250042576053L;
	private static Set<Collection.Key> FIX_KEYS=new HashSet<Collection.Key>();
	static {
		FIX_KEYS.add(CFID);
		FIX_KEYS.add(CFTOKEN);
		FIX_KEYS.add(URLTOKEN);
		FIX_KEYS.add(LASTVISIT);
		FIX_KEYS.add(HITCOUNT);
		FIX_KEYS.add(TIMECREATED);
	}
	

	protected static Set<Collection.Key> ignoreSet=new HashSet<Collection.Key>();
	static {
		ignoreSet.add(CFID);
		ignoreSet.add(CFTOKEN);
		ignoreSet.add(URLTOKEN);
	}
	
	
	protected boolean isinit=true;
	protected Struct sct;
	protected long lastvisit;
	protected DateTime _lastvisit;
	protected int hitcount=0;
	protected DateTime timecreated;
	private boolean hasChanges=false;
	private String strType;
	private int type;
	private long timeSpan=-1;
	private String storage;
	private Map<String, String> tokens; 
	
	
	/**
	 * Constructor of the class
	 * @param sct
	 * @param timecreated
	 * @param _lastvisit
	 * @param lastvisit
	 * @param hitcount
	 */
	public StorageScopeImpl(Struct sct, DateTime timecreated, DateTime _lastvisit, long lastvisit, int hitcount,String strType,int type) {
		this.sct=sct;
		this.timecreated=timecreated;
		if(_lastvisit==null)	this._lastvisit=timecreated;
		else 					this._lastvisit=_lastvisit;
		
		if(lastvisit==-1) 		this.lastvisit=this._lastvisit.getTime();
		else 					this.lastvisit=lastvisit;

		this.hitcount=hitcount;
		this.strType=strType;
		this.type=type;
        id=++_id;
	}
	
	/**
	 * Constructor of the class
	 * @param other
	 * @param deepCopy
	 */
	public StorageScopeImpl(StorageScopeImpl other, boolean deepCopy) {
		this.sct=(Struct)other.sct.duplicate(deepCopy);
		this.timecreated=other.timecreated;
		this._lastvisit=other._lastvisit;
		this.hitcount=other.hitcount;
		this.isinit=other.isinit;
		this.lastvisit=other.lastvisit;
		this.strType=other.strType;
		this.type=other.type;
		this.timeSpan=other.timeSpan;
        id=++_id;
	}

	/**
	 * @see railo.runtime.type.scope.Scope#initialize(railo.runtime.PageContext)
	 */
	public void touchBeforeRequest(PageContext pc) {
		
		hasChanges=false;
		setTimeSpan(pc);
		
		
		//lastvisit=System.currentTimeMillis();
		if(sct==null) sct=new StructImpl();
		sct.setEL(KeyConstants._cfid, pc.getCFID());
		sct.setEL(KeyConstants._cftoken, pc.getCFToken());
		sct.setEL(URLTOKEN, pc.getURLToken());
		sct.setEL(LASTVISIT, _lastvisit);
		_lastvisit=new DateTimeImpl(pc.getConfig());
		lastvisit=System.currentTimeMillis();
		
		if(type==SCOPE_CLIENT){
			sct.setEL(HITCOUNT, new Double(hitcount++));
		}
		else {
			sct.setEL(SESSION_ID, pc.getApplicationContext().getName()+"_"+pc.getCFID()+"_"+pc.getCFToken());
		}
		sct.setEL(TIMECREATED, timecreated);
	}

	public void resetEnv(PageContext pc){
		_lastvisit=new DateTimeImpl(pc.getConfig());
		timecreated=new DateTimeImpl(pc.getConfig());
		touchBeforeRequest(pc);
		
	}
	
	void setTimeSpan(PageContext pc) {
		ApplicationContext ac=pc.getApplicationContext();
		this.timeSpan=getType()==SCOPE_SESSION?
				ac.getSessionTimeout().getMillis():
				ac.getClientTimeout().getMillis();
	}
	
	@Override
	public void setMaxInactiveInterval(int interval) {
		this.timeSpan=interval*1000L;
	}

	@Override
	public int getMaxInactiveInterval() {
		return (int)(this.timeSpan/1000L);
	}
	
	@Override
	public final boolean isInitalized() {
		return isinit;
	}
	
	@Override
	public final void initialize(PageContext pc) {
		// StorageScopes need only request initialisation no global init, they are not reused;
	}
	
	@Override
	public void touchAfterRequest(PageContext pc) {
		
{'fraction_non_alphanumeric': 0.11434395848776872, 'fraction_numerical': 0.005930318754633061, 'mean_word_length': 5.17095483133219, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5530913', 'n_tokens_mistral': 3819, 'n_tokens_neox': 3555, 'n_words': 646}