<commit_before>package org.stagemonitor.core.instrument;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.pool.TypePool;
import org.stagemonitor.core.util.ExecutorUtils;

/**
 * This {@link net.bytebuddy.agent.builder.AgentBuilder.BinaryLocator} caches
 * {@link net.bytebuddy.description.type.TypeDescription}s and clears the cache every minute to avoid memory leaks.
 * <p/>
 * Class loader memory leaks are also avoided by using {@link WeakConcurrentMap}.
 */
public class AutoEvictingCachingBinaryLocator extends AgentBuilder.BinaryLocator.WithTypePoolCache {

	private final WeakConcurrentMap<ClassLoader, TypePool.CacheProvider> cacheProviders = new WeakConcurrentMap
			.WithInlinedExpunction<ClassLoader, TypePool.CacheProvider>();

	public AutoEvictingCachingBinaryLocator() {
		this(TypePool.Default.ReaderMode.EXTENDED);
	}

	public AutoEvictingCachingBinaryLocator(TypePool.Default.ReaderMode readerMode) {
		super(readerMode);
		Executors.newScheduledThreadPool(1, new ExecutorUtils.NamedThreadFactory("type-pool-cache-evicter")).scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				cacheProviders.clear();
				TimedElementMatcherDecorator.logMetrics();
			}
		}, 1, 1, TimeUnit.MINUTES);
	}

	@Override
	protected TypePool.CacheProvider locate(ClassLoader classLoader) {
		classLoader = classLoader == null ? BootstrapClassLoaderMarker.INSTANCE : classLoader;
		TypePool.CacheProvider cacheProvider = cacheProviders.get(classLoader);
		while (cacheProvider == null) {
			cacheProviders.putIfAbsent(classLoader, new TypePool.CacheProvider.Simple());
			cacheProvider = cacheProviders.get(classLoader);
		}
		return cacheProvider;
	}

	/**<commit_msg>First eviction after 5m<commit_after>package org.stagemonitor.core.instrument;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.pool.TypePool;
import org.stagemonitor.core.util.ExecutorUtils;

/**
 * This {@link net.bytebuddy.agent.builder.AgentBuilder.BinaryLocator} caches
 * {@link net.bytebuddy.description.type.TypeDescription}s and clears the cache every minute to avoid memory leaks.
 * <p/>
 * Class loader memory leaks are also avoided by using {@link WeakConcurrentMap}.
 */
public class AutoEvictingCachingBinaryLocator extends AgentBuilder.BinaryLocator.WithTypePoolCache {

	private final WeakConcurrentMap<ClassLoader, TypePool.CacheProvider> cacheProviders = new WeakConcurrentMap
			.WithInlinedExpunction<ClassLoader, TypePool.CacheProvider>();

	public AutoEvictingCachingBinaryLocator() {
		this(TypePool.Default.ReaderMode.EXTENDED);
	}

	public AutoEvictingCachingBinaryLocator(TypePool.Default.ReaderMode readerMode) {
		super(readerMode);
		Executors.newScheduledThreadPool(1, new ExecutorUtils.NamedThreadFactory("type-pool-cache-evicter")).scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				cacheProviders.clear();
				TimedElementMatcherDecorator.logMetrics();
			}
		}, 5, 1, TimeUnit.MINUTES);
	}

	@Override
	protected TypePool.CacheProvider locate(ClassLoader classLoader) {
		classLoader = classLoader == null ? BootstrapClassLoaderMarker.INSTANCE : classLoader;
		TypePool.CacheProvider cacheProvider = cacheProviders.get(classLoader);
		while (cacheProvider == null) {
			cacheProviders.putIfAbsent(classLoader, new TypePool.CacheProvider.Simple());
			cacheProvider = cacheProviders.get(classLoader);
		}
		return cacheProvider;
	}

	/**
{'fraction_non_alphanumeric': 0.09732620320855614, 'fraction_numerical': 0.0018716577540106951, 'mean_word_length': 6.1393129770992365, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3234219', 'n_tokens_mistral': 1171, 'n_tokens_neox': 1089, 'n_words': 205}