<commit_before>package Meta.Model;

import Configuration.ConfigurationLoader;
import Configuration.JsonFileStore;
import Logging.Model.Logger;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.zip.GZIPOutputStream;

import static java.nio.file.StandardWatchEventKinds.*;

/**
 * @author <NAME>
 */
public class PatchKeeper {
    private static String resources = ConfigurationLoader.RESOURCES;
    private HashMap<String, PatchFile> files = new HashMap<>();
    private static PatchKeeper instance;
    private Logger logger;
    private PatchInfo patchInfo;
    private String name;
    private String version;
    private Vertx vertx;

    private PatchKeeper(Vertx vertx, Logger logger) {
        this.vertx = vertx;
        this.logger = logger;

        try {
            loadVersion();
            loadFiles();
            watchFiles();
            logger.patchLoaded(name, version);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void watchFiles() throws IOException {
        WatchService watcher = FileSystems.getDefault().newWatchService();
        WatchKey watchKey = Paths.get(resources).register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);

        vertx.setPeriodic(2500, handler -> {

            if (watchKey.pollEvents().size() != 0) {
                try {
                    logger.patchReloading(name, version);
                    synchronized (this) {
                        loadVersion();
                        loadFiles();
                    }
                    logger.patchReloaded(name, version);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    public static PatchKeeper instance(Vertx vertx, Logger logger) {
        if (instance == null) {
            instance = new PatchKeeper(vertx, logger);
        }
        return instance;
    }

    private void loadVersion() throws IOException {
        JsonObject version = JsonFileStore.readObject(resources + "/version.json");

        this.name = version.getString("name");
        this.version = version.getString("version");
    }

    private void loadFiles() throws IOException {
        Path path = Paths.get(resources);
        files.clear();

        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                File file = path.toFile();

                String filePath = path.toString().replace("resources", "").replace("\\", "/");
                long fileSize = file.length();<commit_msg>Updated: PatchKeeper only watches version.json for changes<commit_after>package Meta.Model;

import Configuration.ConfigurationLoader;
import Configuration.JsonFileStore;
import Logging.Model.Logger;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.zip.GZIPOutputStream;

import static java.nio.file.StandardWatchEventKinds.*;

/**
 * @author <NAME>
 */
public class PatchKeeper {
    private static final String RESOURCE_DIR = ConfigurationLoader.RESOURCES;
    private static final String VERSION_FILE = ConfigurationLoader.RESOURCES + "version.json";
    private HashMap<String, PatchFile> files = new HashMap<>();
    private static PatchKeeper instance;
    private Logger logger;
    private PatchInfo patchInfo;
    private String name;
    private String version;
    private Vertx vertx;

    private PatchKeeper(Vertx vertx, Logger logger) {
        this.vertx = vertx;
        this.logger = logger;

        try {
            loadVersion();
            loadFiles();
            watchFiles();
            logger.patchLoaded(name, version);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void watchFiles() throws IOException {
        WatchService watcher = FileSystems.getDefault().newWatchService();
        WatchKey watchKey = Paths.get(RESOURCE_DIR).register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);

        vertx.setPeriodic(2500, handler -> {

            for (WatchEvent<?> event : watchKey.pollEvents()) {
                if (VERSION_FILE.endsWith(event.context().toString())) {
                    try {
                        logger.patchReloading(name, version);
                        synchronized (this) {
                            loadVersion();
                            loadFiles();
                        }
                        logger.patchReloaded(name, version);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
    }

    public static PatchKeeper instance(Vertx vertx, Logger logger) {
        if (instance == null) {
            instance = new PatchKeeper(vertx, logger);
        }
        return instance;
    }

    private void loadVersion() throws IOException {
        JsonObject version = JsonFileStore.readObject(VERSION_FILE);

        this.name = version.getString("name");
        this.version = version.getString("version");
    }

    private void loadFiles() throws IOException {
        Path path = Paths.get(RESOURCE_DIR);
        files.clear();

        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                File file = path.toFile();

                String filePath = path.toString().replace("resources/", "");
                long fileSize = file.length();
{'fraction_non_alphanumeric': 0.09472049689440994, 'fraction_numerical': 0.0013975155279503106, 'mean_word_length': 2.215676485272092, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, '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': '4140804', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1588, 'n_words': 390}