<commit_before>package io.minimum.minecraft.superbvote.storage;

import com.google.common.base.Preconditions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.minimum.minecraft.superbvote.SuperbVote;
import io.minimum.minecraft.superbvote.util.PlayerVotes;
import io.minimum.minecraft.superbvote.votes.Vote;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;

public class JsonVoteStorage implements VoteStorage {
    private static final int VERSION = 2;

    private final Map<UUID, PlayerRecord> voteCounts = new HashMap<>();
    private final ReadWriteLock rwl = new ReentrantReadWriteLock();
    private final Gson gson = new Gson();
    private final Path saveTo;

    public JsonVoteStorage(File file) throws IOException {
        Preconditions.checkNotNull(file, "file");
        saveTo = file.toPath();

        if (!file.exists()) file.createNewFile();

        boolean needMigrate = false;
        try (Reader reader = Files.newBufferedReader(saveTo)) {
            VotingFile vf = gson.fromJson(reader, VotingFile.class);
            if (vf == null || vf.records == null || vf.records.isEmpty()) {
                needMigrate = true;
            } else {
                voteCounts.putAll(vf.records);
            }
        }

        if (needMigrate) {
            Map<UUID, Integer> votesToMigrate = null;
            try (Reader reader = Files.newBufferedReader(saveTo)) {
                votesToMigrate = gson.fromJson(reader, new TypeToken<Map<UUID, Integer>>() {
                }.getType());
            }
            if (votesToMigrate != null) {
                VotingFile vf = migrateOldVersion(votesToMigrate);
                voteCounts.putAll(vf.records);
            }
        }
    }

    private VotingFile migrateOldVersion(Map<UUID, Integer> votes) throws IOException {
        // We'll need to convert old records then<commit_msg>Fix bug where SuperbVote would always try to convert votes.json, even a new-style file.<commit_after>package io.minimum.minecraft.superbvote.storage;

import com.google.common.base.Preconditions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.minimum.minecraft.superbvote.SuperbVote;
import io.minimum.minecraft.superbvote.util.PlayerVotes;
import io.minimum.minecraft.superbvote.votes.Vote;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;

public class JsonVoteStorage implements VoteStorage {
    private static final int VERSION = 2;

    private final Map<UUID, PlayerRecord> voteCounts = new HashMap<>();
    private final ReadWriteLock rwl = new ReentrantReadWriteLock();
    private final Gson gson = new Gson();
    private final Path saveTo;

    public JsonVoteStorage(File file) throws IOException {
        Preconditions.checkNotNull(file, "file");
        saveTo = file.toPath();

        if (!file.exists()) file.createNewFile();

        boolean needMigrate = false;
        try (Reader reader = Files.newBufferedReader(saveTo)) {
            VotingFile vf = gson.fromJson(reader, VotingFile.class);
            if (vf == null || vf.records == null) {
                needMigrate = true;
            } else {
                voteCounts.putAll(vf.records);
            }
        }

        if (needMigrate) {
            Map<UUID, Integer> votesToMigrate = null;
            try (Reader reader = Files.newBufferedReader(saveTo)) {
                votesToMigrate = gson.fromJson(reader, new TypeToken<Map<UUID, Integer>>() {
                }.getType());
            }
            if (votesToMigrate != null) {
                VotingFile vf = migrateOldVersion(votesToMigrate);
                voteCounts.putAll(vf.records);
            }
        }
    }

    private VotingFile migrateOldVersion(Map<UUID, Integer> votes) throws IOException {
        // We'll need to convert old records then
{'fraction_non_alphanumeric': 0.10296067167476801, 'fraction_numerical': 0.0004418912947414936, 'mean_word_length': 2.8925193465176267, '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': '4825937', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1322, 'n_words': 295}