<commit_before>package carrot2.demo.cache;

import java.util.LinkedList;
import java.util.WeakHashMap;

import org.apache.log4j.Logger;

import com.dawidweiss.carrot.core.local.impl.ClustersConsumerOutputComponent.Result;

/**
 * A cache based on weak references. However, last few requests
 * are always stored (hard references).
 * 
 * @author <NAME>
 */
final class WeakRawDocumentsCache implements RawDocumentsCache {
    private final Logger logger = Logger.getLogger(WeakRawDocumentsCache.class); 

    private final WeakHashMap cache = new WeakHashMap();
    private final LinkedList hardCache;
    private final int hardCacheSize;

    public WeakRawDocumentsCache(int hardCacheSize) {
        this.hardCacheSize = hardCacheSize;
        this.hardCache = new LinkedList();
    }
    
    public synchronized Result get(CacheEntry entry) {
        final Result result = (Result) cache.get(entry); 
        if (result != null) {
            logger.info("Returning cached data for: " + entry);
            updateRecent(entry);
        }
        return result;
    }

    public synchronized void put(CacheEntry entry, Result result) {
        logger.info("Saving cached data for: " + entry);
        cache.put(entry, result);
        updateRecent(entry);
    }

    private void updateRecent(CacheEntry entry) {
        hardCache.remove(entry);
        hardCache.addFirst(entry);
        while (hardCache.size() > hardCacheSize) {
            hardCache.removeLast();
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Hard cache: " + hardCache);
        }
    }
}<commit_msg>Bugfix: when updating the hard references list, a new key object was used (the old one was then flushed from the weak hashmap by the GC and caused 'not-in-cache' failure).<commit_after>package carrot2.demo.cache;

import java.util.LinkedList;
import java.util.WeakHashMap;

import org.apache.log4j.Logger;

import com.dawidweiss.carrot.core.local.impl.ClustersConsumerOutputComponent.Result;

/**
 * A cache based on weak references. However, last few requests
 * are always stored (hard references).
 * 
 * @author <NAME>
 */
final class WeakRawDocumentsCache implements RawDocumentsCache {
    private final Logger logger = Logger.getLogger(WeakRawDocumentsCache.class); 

    private final WeakHashMap cache = new WeakHashMap();
    private final LinkedList hardCache;
    private final int hardCacheSize;

    public WeakRawDocumentsCache(int hardCacheSize) {
        this.hardCacheSize = hardCacheSize;
        this.hardCache = new LinkedList();
    }
    
    public synchronized Result get(CacheEntry entry) {
        final Result result = (Result) cache.get(entry); 
        if (result != null) {
            logger.info("Returning cached data for: " + entry);
            updateRecent(entry);
        } else {
            logger.info("Not in cache: " + entry);
        }
        return result;
    }

    public synchronized void put(CacheEntry entry, Result result) {
        logger.info("Saving cached data for: " + entry);
        cache.put(entry, result);
        updateRecent(entry);
    }

    private void updateRecent(CacheEntry entry) {
        final int index = hardCache.indexOf(entry);
        if (index >= 0) {
            final CacheEntry cachedKey = (CacheEntry) hardCache.remove(index);
            hardCache.addFirst(cachedKey);
        } else {
            hardCache.addFirst(entry);
            while (hardCache.size() > hardCacheSize) {
                hardCache.removeLast();
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Hard cache: " + hardCache);
        }
    }
}
{'fraction_non_alphanumeric': 0.09632272228320526, 'fraction_numerical': 0.0013721185510428102, 'mean_word_length': 2.7231869254341166, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '6207375', 'n_tokens_mistral': 1012, 'n_tokens_neox': 970, 'n_words': 267}