<commit_before>package com.ironz.binaryprefs.fetch;

import com.ironz.binaryprefs.file.transaction.FileTransaction;
import com.ironz.binaryprefs.file.transaction.TransactionElement;
import com.ironz.binaryprefs.lock.LockFactory;
import com.ironz.binaryprefs.serialization.SerializerFactory;
import com.ironz.binaryprefs.task.FutureBarrier;
import com.ironz.binaryprefs.task.TaskExecutor;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.locks.Lock;

public final class NoOpFetchStrategy implements FetchStrategy {

    private final Lock readLock;
    private final TaskExecutor taskExecutor;
    private final FileTransaction fileTransaction;
    private final SerializerFactory serializerFactory;

    public NoOpFetchStrategy(LockFactory lockFactory,
                             TaskExecutor taskExecutor,
                             FileTransaction fileTransaction,
                             SerializerFactory serializerFactory) {
        this.readLock = lockFactory.getReadLock();
        this.taskExecutor = taskExecutor;
        this.fileTransaction = fileTransaction;
        this.serializerFactory = serializerFactory;
    }

    @Override
    public Object getValue(String key, Object defValue) {
        return getInternal(key, defValue);
    }

    @Override
    public Map<String, Object> getAll() {
        return getAllInternal();
    }

    @Override
    public boolean contains(String key) {
        return containsInternal(key);
    }

    private Object getInternal(final String key, Object defValue) {
        readLock.lock();
        fileTransaction.lock();
        try {
            FutureBarrier barrier = taskExecutor.submit(new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                    return fetchObject(key);
                }
            });
            return barrier.completeBlockingWihResult(defValue);
        } finally {
            readLock.unlock();
            fileTransaction.unlock();
        }
    }

    private Map<String, Object> getAllInternal() {
        readLock.lock();
        try {
            fileTransaction.lock();
            Set<String> names = fileTransaction.fetchNames();
            HashMap<String, Object> clone = new HashMap<>(names.size());
            for (String candidate : names) {
                Object o = getInternal(candidate);
                clone.put(candidate, o);
            }
            return Collections.unmodifiableMap(clone);
        } finally {
            fileTransaction.unlock();
            readLock.unlock();<commit_msg>file lock for all element fetch has been moved up<commit_after>package com.ironz.binaryprefs.fetch;

import com.ironz.binaryprefs.file.transaction.FileTransaction;
import com.ironz.binaryprefs.file.transaction.TransactionElement;
import com.ironz.binaryprefs.lock.LockFactory;
import com.ironz.binaryprefs.serialization.SerializerFactory;
import com.ironz.binaryprefs.task.FutureBarrier;
import com.ironz.binaryprefs.task.TaskExecutor;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.locks.Lock;

public final class NoOpFetchStrategy implements FetchStrategy {

    private final Lock readLock;
    private final TaskExecutor taskExecutor;
    private final FileTransaction fileTransaction;
    private final SerializerFactory serializerFactory;

    public NoOpFetchStrategy(LockFactory lockFactory,
                             TaskExecutor taskExecutor,
                             FileTransaction fileTransaction,
                             SerializerFactory serializerFactory) {
        this.readLock = lockFactory.getReadLock();
        this.taskExecutor = taskExecutor;
        this.fileTransaction = fileTransaction;
        this.serializerFactory = serializerFactory;
    }

    @Override
    public Object getValue(String key, Object defValue) {
        return getInternal(key, defValue);
    }

    @Override
    public Map<String, Object> getAll() {
        return getAllInternal();
    }

    @Override
    public boolean contains(String key) {
        return containsInternal(key);
    }

    private Object getInternal(final String key, Object defValue) {
        readLock.lock();
        fileTransaction.lock();
        try {
            FutureBarrier barrier = taskExecutor.submit(new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                    return fetchObject(key);
                }
            });
            return barrier.completeBlockingWihResult(defValue);
        } finally {
            readLock.unlock();
            fileTransaction.unlock();
        }
    }

    private Map<String, Object> getAllInternal() {
        readLock.lock();
        fileTransaction.lock();
        try {
            Set<String> names = fileTransaction.fetchNames();
            HashMap<String, Object> clone = new HashMap<>(names.size());
            for (String candidate : names) {
                Object o = getInternal(candidate);
                clone.put(candidate, o);
            }
            return Collections.unmodifiableMap(clone);
        } finally {
            fileTransaction.unlock();
            readLock.unlock();
{'fraction_non_alphanumeric': 0.083364072298045, 'fraction_numerical': 0.0, 'mean_word_length': 2.4366286438529783, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '3142103', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1319, 'n_words': 298}