<commit_before>package me.nallar.tickthreading.minecraft.storage;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

import cpw.mods.fml.common.FMLLog;
import net.minecraft.world.chunk.storage.RegionFile;

public class RegionFileCache {
	private final Map<Long, RegionFile> regionFileMap = new HashMap<Long, RegionFile>();
	private final File regionDir;
	private boolean closed = false;

	public RegionFileCache(File worldDir) {
		regionDir = new File(worldDir, "region").getAbsoluteFile();
		if (!regionDir.isDirectory() && !regionDir.mkdirs()) {
			throw new Error("Failed to create region directory: " + regionDir);
		}
	}

	private static long hash(int x, int z) {
		return (((long) x) << 32) | (z & 0xffffffffL);
	}

	public RegionFile get(int x, int z) {
		if (closed) {
			throw new IllegalStateException("RegionFileCache already closed");
		}

		long hash = hash(x, z);
		RegionFile regionFile = regionFileMap.get(hash);
		if (regionFile != null) {
			return regionFile;
		}
		synchronized (this) {
			regionFile = regionFileMap.get(hash);
			if (regionFile != null) {
				return regionFile;
			}
			if (regionFileMap.size() >= 256) {
				closeRegionFiles();
			}
			String location = "r." + x + '.' + z + ".mca";
			File regionFileFile = new File(regionDir, location);
			regionFile = RegionFileFixer.newRegionFile(regionFileFile);
			if (regionFileMap.put(hash, regionFile) != null) {
				throw new Error("Region file recreated concurrently. This should never happen.");
			}
			return regionFile;
		}
	}

	public synchronized void close() {
		closed = true;
		closeRegionFiles();
	}

	private synchronized void closeRegionFiles() {
		for (RegionFile regionFile : regionFileMap.values()) {
			try {
				regionFile.close();
			} catch (IOException e) {
				FMLLog.log(Level.WARNING, e, "Failed to close regionFile");
			}
		}
		regionFileMap.clear();
	}

	public DataOutputStream getChunkOutputStream(int x, int z) {
		return get(x >> 5, z >> 5).getChunkDataOutputStream(x & 31, z & 31);
	}

	public DataInputStream getChunkInputStream(int x, int z) {
		return get(x >> 5, z >> 5).getChunkDataInputStream(x & 31, z & 31);
	}
}<commit_msg>Prevent region file from being returned while closing.<commit_after>package me.nallar.tickthreading.minecraft.storage;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

import cpw.mods.fml.common.FMLLog;
import net.minecraft.world.chunk.storage.RegionFile;

public class RegionFileCache {
	private final Map<Long, RegionFile> regionFileMap = new HashMap<Long, RegionFile>();
	private final File regionDir;
	private boolean closed = false;
	private volatile boolean closing = false;

	public RegionFileCache(File worldDir) {
		regionDir = new File(worldDir, "region").getAbsoluteFile();
		if (!regionDir.isDirectory() && !regionDir.mkdirs()) {
			throw new Error("Failed to create region directory: " + regionDir);
		}
	}

	private static long hash(int x, int z) {
		return (((long) x) << 32) | (z & 0xffffffffL);
	}

	public RegionFile get(int x, int z) {
		if (closed) {
			throw new IllegalStateException("RegionFileCache already closed");
		}

		long hash = hash(x, z);
		RegionFile regionFile = regionFileMap.get(hash);
		if (!closing && regionFile != null) {
			return regionFile;
		}
		synchronized (this) {
			regionFile = regionFileMap.get(hash);
			if (regionFile != null) {
				return regionFile;
			}
			if (regionFileMap.size() >= 256) {
				closeRegionFiles();
			}
			String location = "r." + x + '.' + z + ".mca";
			File regionFileFile = new File(regionDir, location);
			regionFile = RegionFileFixer.newRegionFile(regionFileFile);
			if (regionFileMap.put(hash, regionFile) != null) {
				throw new Error("Region file recreated concurrently. This should never happen.");
			}
			return regionFile;
		}
	}

	public synchronized void close() {
		closed = true;
		closeRegionFiles();
	}

	private synchronized void closeRegionFiles() {
		closing = true;
		for (RegionFile regionFile : regionFileMap.values()) {
			try {
				regionFile.close();
			} catch (IOException e) {
				FMLLog.log(Level.WARNING, e, "Failed to close regionFile");
			}
		}
		regionFileMap.clear();
		closing = false;
	}

	public DataOutputStream getChunkOutputStream(int x, int z) {
		return get(x >> 5, z >> 5).getChunkDataOutputStream(x & 31, z & 31);
	}

	public DataInputStream getChunkInputStream(int x, int z) {
		return get(x >> 5, z >> 5).getChunkDataInputStream(x & 31, z & 31);
	}
}
{'fraction_non_alphanumeric': 0.12635149459402162, 'fraction_numerical': 0.007631969472122111, 'mean_word_length': 4.128260869565217, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '1794924', 'n_tokens_mistral': 1679, 'n_tokens_neox': 1480, 'n_words': 401}