<commit_before>package net.yeputons.spbau.spring2016.torrent.client;

import net.yeputons.spbau.spring2016.torrent.FileDescription;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public class ClientState implements Serializable {
    private static final long serialVersionUID = 2L;

    private final String downloadsDir;
    private final Map<Integer, FileDescription> files = new HashMap<>();
    private transient Map<Integer, RandomAccessFile> filesOpened;

    public ClientState(Path downloadsDir) {
        this.downloadsDir = downloadsDir.toAbsolutePath().toString();
    }

    Map<Integer, FileDescription> getFiles() {
        return files;
    }

    public FileDescription getFileDescription(int id) {
        synchronized (files) {
            return files.get(id);
        }
    }

    public RandomAccessFile getFile(int id) throws IOException {
        synchronized (files) {
            FileDescription description = getFileDescription(id);
            if (description == null) {
                throw new RuntimeException("Requested to open unknown file with id " + id);
            }
            if (filesOpened == null) {
                filesOpened = new HashMap<>();
            }
            RandomAccessFile result = filesOpened.get(id);
            if (result == null) {
                result = new RandomAccessFile(
                        Paths.get(downloadsDir, description.getEntry().getName()).toFile(),
                        "rw");
                filesOpened.put(id, result);
            }
            result.setLength(description.getEntry().getSize());
            return result;
        }
    }
}
<commit_msg>ClientState: make it use concurrent maps to reduce amount of syncs<commit_after>package net.yeputons.spbau.spring2016.torrent.client;

import net.yeputons.spbau.spring2016.torrent.FileDescription;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class ClientState implements Serializable {
    private static final long serialVersionUID = 3L;

    private final String downloadsDir;
    private final ConcurrentMap<Integer, FileDescription> files = new ConcurrentHashMap<>();
    private transient ConcurrentMap<Integer, RandomAccessFile> filesOpened;

    public ClientState(Path downloadsDir) {
        this.downloadsDir = downloadsDir.toAbsolutePath().toString();
    }

    Map<Integer, FileDescription> getFiles() {
        return files;
    }

    public FileDescription getFileDescription(int id) {
        return files.get(id);
    }

    public RandomAccessFile getFile(int id) throws IOException {
        FileDescription description = getFileDescription(id);
        if (description == null) {
            throw new RuntimeException("Requested to open unknown file with id " + id);
        }
        if (filesOpened == null) {
            synchronized (this) {
                if (filesOpened == null) {
                    filesOpened = new ConcurrentHashMap<>();
                }
            }
        }
        RandomAccessFile result;
        synchronized (description) {
            result = filesOpened.get(id);
            if (result == null) {
                result = new RandomAccessFile(
                        Paths.get(downloadsDir, description.getEntry().getName()).toFile(),
                        "rw");
                filesOpened.put(id, result);
            }
        }
        synchronized (result) {
            result.setLength(description.getEntry().getSize());
        }
        return result;
    }
}

{'fraction_non_alphanumeric': 0.09369322605761744, 'fraction_numerical': 0.004671684401764859, 'mean_word_length': 2.4349376114081998, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6543563', 'n_tokens_mistral': 1020, 'n_tokens_neox': 989, 'n_words': 238}