<commit_before>package com.venky.cache;

import com.esotericsoftware.kryo.KryoException;
import com.venky.core.util.MultiException;
import com.venky.core.util.ObjectUtil;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public abstract class PersistentCache<K,V> extends Cache<K, V>{

	private static final long serialVersionUID = -1707252397643517532L;
	public PersistentCache() {
		this(Cache.MAX_ENTRIES_DEFAULT,Cache.PRUNE_FACTOR_DEFAULT);
	}
	public PersistentCache(int maxEntries,double pruneFactor) {
		super(maxEntries,pruneFactor);
	}

	public int size() {
		ensureOpen();
		return indexMap.size();
	}

	public boolean containsKey(Object key) {
		ensureOpen();
		return indexMap.containsKey(key);
	}
	@Override
	public boolean containsValue(Object value) {
		ensureOpen();
		for (K k : keySet()) {
			if (get(k).equals(value)) { 
				return true;
			}
		}
		return false;
	}
	@Override
	public Set<K> keySet(){
		ensureOpen();
		return new AbstractSet<K>() {
			
			
			@Override
			public Iterator<K> iterator() {
				Iterator<K> ki = new Iterator<K>() { 
					Iterator<K> keys = indexMap.keySet().iterator();

					@Override
					public boolean hasNext() {
						return keys.hasNext();
					}

					@Override
					public K next() {
						return keys.next();
					}
					
				};
				return ki;
			}

			@Override
			public int size() {
				return indexMap.size();
			} 
			
		};
	}
	@Override 
	public Set<V> values(){
		ensureOpen();
		Set<V> set = new AbstractSet<V>() {
			
			@Override
			public Iterator<V> iterator() {
				return new Iterator<V>() {
					Iterator<K> keys = indexMap.keySet().iterator();

					@Override
					public boolean hasNext() {
						return keys.hasNext();
					}

					@Override
					public V next() {
						return get(keys.next());
					}
				};
				
			}

			@Override
			public int size() {
				return indexMap.size();
			} 
			
		};
		return set;
	}
	@SuppressWarnings("unchecked")
	@Override
	public V get(Object key){
		ensureOpen();
		V v = null;
		if (super.containsKey(key)) {
			v = super.get(key);
		}else {
			synchronized (this) { 
				if (super.containsKey(key)) {
					v = super.get(key);
				}else {
					v = getPersistedValue((K)key);
					put((K)key, v, true);
				}
			}
		}
		return v;
	}
	public V put(K key,V value){
		return put(key,value,false);
	}
	private V put(K key,V value,boolean alreadyPersisted){
		ensureOpen();
		V ret = super.put(key,value);
		if (!alreadyPersisted){
			indexMap.remove(key);
		}

		if (!indexMap.containsKey(key)){
			if (isAutoPersist()) {
				persist(key,value);
			}else {
				indexMap.put(key, -1L);
			}
		}
		return ret;
	}

	@Override
	protected void evictKeys(List<K> keys){
		super.evictKeys(keys);
		flush();<commit_msg>Renamed flag to explain better<commit_after>package com.venky.cache;

import com.esotericsoftware.kryo.KryoException;
import com.venky.core.util.MultiException;
import com.venky.core.util.ObjectUtil;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public abstract class PersistentCache<K,V> extends Cache<K, V>{

	private static final long serialVersionUID = -1707252397643517532L;
	public PersistentCache() {
		this(Cache.MAX_ENTRIES_DEFAULT,Cache.PRUNE_FACTOR_DEFAULT);
	}
	public PersistentCache(int maxEntries,double pruneFactor) {
		super(maxEntries,pruneFactor);
	}

	public int size() {
		ensureOpen();
		return indexMap.size();
	}

	public boolean containsKey(Object key) {
		ensureOpen();
		return indexMap.containsKey(key);
	}
	@Override
	public boolean containsValue(Object value) {
		ensureOpen();
		for (K k : keySet()) {
			if (get(k).equals(value)) { 
				return true;
			}
		}
		return false;
	}
	@Override
	public Set<K> keySet(){
		ensureOpen();
		return new AbstractSet<K>() {
			
			
			@Override
			public Iterator<K> iterator() {
				Iterator<K> ki = new Iterator<K>() { 
					Iterator<K> keys = indexMap.keySet().iterator();

					@Override
					public boolean hasNext() {
						return keys.hasNext();
					}

					@Override
					public K next() {
						return keys.next();
					}
					
				};
				return ki;
			}

			@Override
			public int size() {
				return indexMap.size();
			} 
			
		};
	}
	@Override 
	public Set<V> values(){
		ensureOpen();
		Set<V> set = new AbstractSet<V>() {
			
			@Override
			public Iterator<V> iterator() {
				return new Iterator<V>() {
					Iterator<K> keys = indexMap.keySet().iterator();

					@Override
					public boolean hasNext() {
						return keys.hasNext();
					}

					@Override
					public V next() {
						return get(keys.next());
					}
				};
				
			}

			@Override
			public int size() {
				return indexMap.size();
			} 
			
		};
		return set;
	}
	@SuppressWarnings("unchecked")
	@Override
	public V get(Object key){
		ensureOpen();
		V v = null;
		if (super.containsKey(key)) {
			v = super.get(key);
		}else {
			synchronized (this) { 
				if (super.containsKey(key)) {
					v = super.get(key);
				}else {
					v = getPersistedValue((K)key);
					put((K)key, v, false);
				}
			}
		}
		return v;
	}
	public V put(K key,V value){
		return put(key,value,true);
	}
	private V put(K key,V value,boolean forcePersist){
		ensureOpen();
		V ret = super.put(key,value);
		if (forcePersist){
			indexMap.remove(key);
		}

		if (!indexMap.containsKey(key)){
			if (isAutoPersist()) {
				persist(key,value);
			}else {
				indexMap.put(key, -1L);
			}
		}
		return ret;
	}

	@Override
	protected void evictKeys(List<K> keys){
		super.evictKeys(keys);
		flush();
{'fraction_non_alphanumeric': 0.13890773405698778, 'fraction_numerical': 0.0067842605156037995, 'mean_word_length': 3.092297015961138, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '524061', 'n_tokens_mistral': 2544, 'n_tokens_neox': 2049, 'n_words': 447}