<commit_before>package org.quickcached;

import java.util.Arrays;
import java.util.List;
import org.quickserver.net.server.*;

import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.quickcached.mem.MemoryWarningSystem;

import org.quickserver.net.ServerHook;

public class PrepareHook implements ServerHook {
	private static final Logger logger = Logger.getLogger(PrepareHook.class.getName());
	
	private QuickServer quickserver;

	public String info() {
		return "Init Server Hook to setup cache.";
	}

	public void initHook(QuickServer quickserver) {
		this.quickserver = quickserver;
	}

	public boolean handleEvent(int event) {
		if(event==ServerHook.PRE_STARTUP) {
			Map config = quickserver.getConfig().getApplicationConfiguration();
			try {
				CommandHandler.init(config);
			} catch(Exception e) {
				logger.log(Level.WARNING, "Error: "+e, e);
			}

			try {
				String enableStatsReportStr = (String) config.get("ENABLE_STATS_REPORT");
				boolean enableStatsReport = false;

				if("true".equals(enableStatsReportStr)) {
					enableStatsReport = true;
				}

				String statsReportWriteIntervalStr = (String) config.get("STATS_REPORT_WRITE_INTERVAL");
				if(statsReportWriteIntervalStr!=null) {
					StatsReportGenerator.setWriteInterval(Integer.parseInt(statsReportWriteIntervalStr));
				}

				String entriesToLog  = (String) config.get("ENTRIES_TO_LOG");
				if(entriesToLog!=null) {
					List entriesToLogList = Arrays.asList(entriesToLog.split(","));
					entriesToLogList.remove(" ");
					StatsReportGenerator.setEntriesToLog(entriesToLogList);
				}

				if(enableStatsReport) {
					StatsReportGenerator.start(quickserver);
				}
				
				String gcCallOnLowMemoryPercentStr = (String) config.get("GC_CALL_ON_LOW_MEMORY_PERCENT");
				if(gcCallOnLowMemoryPercentStr!=null && gcCallOnLowMemoryPercentStr.trim().length()!=0) {
					try {
						final int gcCallOnLowMemoryPercent = (int) (
								Double.parseDouble(gcCallOnLowMemoryPercentStr) * 100);
						final int gcCallOnLowMemoryPoolIntervalMin = Integer.parseInt((String) config.get(
								"GC_CALL_ON_LOW_MEMORY_POOL_INTERVAL_MIN"));
						
						Thread t = new Thread() {
							public void run() {
								logger.info("Started..");
								int gcCallOnLowMemoryPoolInterval = gcCallOnLowMemoryPoolIntervalMin * 1000 * 60;
								while(true) {
									try {
										sleep(gcCallOnLowMemoryPoolInterval);
									} catch (InterruptedException ex) {
										Logger.getLogger(StatsReportGenerator.class.getName()).log(
												Level.WARNING, "Error", ex);
										break;
									}
									int permemuse = MemoryWarningSystem.getMemUsedPercentage();
									if(permemuse > gcCallOnLowMemoryPercent) {
										logger.fine("MemUsedPercentage: "+permemuse+", calling flush..");
										System.gc();
										CommandHandler.gcCalls++;
									}
								}
								logger.info("Done");
							}
						};
						t.setName("GCCallOnLowMemoryPool-Thread");<commit_msg>text change<commit_after>package org.quickcached;

import java.util.Arrays;
import java.util.List;
import org.quickserver.net.server.*;

import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.quickcached.mem.MemoryWarningSystem;

import org.quickserver.net.ServerHook;

public class PrepareHook implements ServerHook {
	private static final Logger logger = Logger.getLogger(PrepareHook.class.getName());
	
	private QuickServer quickserver;

	public String info() {
		return "Init Server Hook to setup cache.";
	}

	public void initHook(QuickServer quickserver) {
		this.quickserver = quickserver;
	}

	public boolean handleEvent(int event) {
		if(event==ServerHook.PRE_STARTUP) {
			Map config = quickserver.getConfig().getApplicationConfiguration();
			try {
				CommandHandler.init(config);
			} catch(Exception e) {
				logger.log(Level.WARNING, "Error: "+e, e);
			}

			try {
				String enableStatsReportStr = (String) config.get("ENABLE_STATS_REPORT");
				boolean enableStatsReport = false;

				if("true".equals(enableStatsReportStr)) {
					enableStatsReport = true;
				}

				String statsReportWriteIntervalStr = (String) config.get("STATS_REPORT_WRITE_INTERVAL");
				if(statsReportWriteIntervalStr!=null) {
					StatsReportGenerator.setWriteInterval(Integer.parseInt(statsReportWriteIntervalStr));
				}

				String entriesToLog  = (String) config.get("ENTRIES_TO_LOG");
				if(entriesToLog!=null) {
					List entriesToLogList = Arrays.asList(entriesToLog.split(","));
					entriesToLogList.remove(" ");
					StatsReportGenerator.setEntriesToLog(entriesToLogList);
				}

				if(enableStatsReport) {
					StatsReportGenerator.start(quickserver);
				}
				
				String gcCallOnLowMemoryPercentStr = (String) config.get("GC_CALL_ON_LOW_MEMORY_PERCENT");
				if(gcCallOnLowMemoryPercentStr!=null && gcCallOnLowMemoryPercentStr.trim().length()!=0) {
					try {
						final int gcCallOnLowMemoryPercent = (int) (
								Double.parseDouble(gcCallOnLowMemoryPercentStr) * 100);
						final int gcCallOnLowMemoryPoolIntervalMin = Integer.parseInt((String) config.get(
								"GC_CALL_ON_LOW_MEMORY_POOL_INTERVAL_MIN"));
						
						Thread t = new Thread() {
							public void run() {
								logger.info("Started..");
								int gcCallOnLowMemoryPoolInterval = gcCallOnLowMemoryPoolIntervalMin * 1000 * 60;
								while(true) {
									try {
										sleep(gcCallOnLowMemoryPoolInterval);
									} catch (InterruptedException ex) {
										Logger.getLogger(StatsReportGenerator.class.getName()).log(
												Level.WARNING, "Error", ex);
										break;
									}
									int permemuse = MemoryWarningSystem.getMemUsedPercentage();
									if(permemuse > gcCallOnLowMemoryPercent) {
										logger.fine("MemUsedPercentage: "+permemuse+", calling gc..");
										System.gc();
										CommandHandler.gcCalls++;
									}
								}
								logger.info("Done");
							}
						};
						t.setName("GCCallOnLowMemoryPool-Thread");
{'fraction_non_alphanumeric': 0.11098150308281952, 'fraction_numerical': 0.0033327778703549408, 'mean_word_length': 3.5957120980091886, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3388161', 'n_tokens_mistral': 2393, 'n_tokens_neox': 1784, 'n_words': 298}