<commit_before>/**
 * Copyright (C) 2011
 *   <NAME> <<EMAIL>>
 *   <NAME> <<EMAIL>>
 *
 * with contributions from
 * 	konstantin-ba@github,<NAME> (<EMAIL>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.flapdoodle.embed.process.store;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import de.flapdoodle.embed.process.distribution.Distribution;
import de.flapdoodle.embed.process.runtime.ProcessControl;

public class CachingArtifactStore implements IArtifactStore {

	private static Logger _logger = Logger.getLogger(CachingArtifactStore.class.getName());

	private final IArtifactStore _delegate;

	Object _lock=new Object();
	
	HashMap<Distribution, FileWithCounter> _distributionFiles = new HashMap<Distribution, FileWithCounter>();

	public CachingArtifactStore(IArtifactStore delegate) {
		_delegate = delegate;
		ProcessControl.addShutdownHook(new CacheCleaner());
		
		ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
		executor.scheduleAtFixedRate(new RemoveUnused(), 10, 10, TimeUnit.SECONDS);
	}

	@Override
	public boolean checkDistribution(Distribution distribution) throws IOException {
		return _delegate.checkDistribution(distribution);
	}

	@Override
	public File extractExe(Distribution distribution) throws IOException {
		
		FileWithCounter fileWithCounter;
		
		synchronized (_lock) {
			fileWithCounter = _distributionFiles.get(distribution);
			if (fileWithCounter == null) {
				_logger.fine("cache NOT found for "+distribution);
				fileWithCounter=new FileWithCounter(distribution);
				_distributionFiles.put(distribution, fileWithCounter);
			} else {
				_logger.fine("cache found for "+distribution);
			}
		}
		
		return fileWithCounter.use();<commit_msg>- properly hutdown ExecutorService<commit_after>/**
 * Copyright (C) 2011
 *   <NAME> <<EMAIL>>
 *   <NAME> <<EMAIL>>
 *
 * with contributions from
 * 	konstantin-ba@github,<NAME> (<EMAIL>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.flapdoodle.embed.process.store;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import de.flapdoodle.embed.process.distribution.Distribution;
import de.flapdoodle.embed.process.runtime.ProcessControl;

public class CachingArtifactStore implements IArtifactStore {

	private static Logger _logger = Logger.getLogger(CachingArtifactStore.class.getName());

	private final IArtifactStore _delegate;

	private final ScheduledExecutorService executor;
	
	Object _lock=new Object();
	
	HashMap<Distribution, FileWithCounter> _distributionFiles = new HashMap<Distribution, FileWithCounter>();

	public CachingArtifactStore(IArtifactStore delegate) {
		_delegate = delegate;
		ProcessControl.addShutdownHook(new CacheCleaner());
		
		executor = Executors.newSingleThreadScheduledExecutor();
		executor.scheduleAtFixedRate(new RemoveUnused(), 10, 10, TimeUnit.SECONDS);
	}
	
	public void finalize(){
	    executor.shutdownNow();
	}

	@Override
	public boolean checkDistribution(Distribution distribution) throws IOException {
		return _delegate.checkDistribution(distribution);
	}

	@Override
	public File extractExe(Distribution distribution) throws IOException {
		
		FileWithCounter fileWithCounter;
		
		synchronized (_lock) {
			fileWithCounter = _distributionFiles.get(distribution);
			if (fileWithCounter == null) {
				_logger.fine("cache NOT found for "+distribution);
				fileWithCounter=new FileWithCounter(distribution);
				_distributionFiles.put(distribution, fileWithCounter);
			} else {
				_logger.fine("cache found for "+distribution);
			}
		}
		
		return fileWithCounter.use();
{'fraction_non_alphanumeric': 0.09212898057280192, 'fraction_numerical': 0.004806729421189666, 'mean_word_length': 5.075425790754258, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '4942105', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1339, 'n_words': 389}