<commit_before>package vg.civcraft.mc.citadel.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.Callable;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.inventory.meta.ItemMeta;
import vg.civcraft.mc.citadel.Citadel;
import vg.civcraft.mc.citadel.reinforcementtypes.ReinforcementType;
import vg.civcraft.mc.citadel.reinforcementtypes.ReinforcementTypeManager;
import vg.civcraft.mc.civmodcore.CivModCorePlugin;
import vg.civcraft.mc.civmodcore.dao.ManagedDatasource;
import vg.civcraft.mc.civmodcore.locations.chunkmeta.XZWCoord;
import vg.civcraft.mc.civmodcore.locations.chunkmeta.block.BlockBasedChunkMeta;
import vg.civcraft.mc.civmodcore.locations.chunkmeta.block.table.TableBasedBlockChunkMeta;
import vg.civcraft.mc.civmodcore.locations.chunkmeta.block.table.TableStorageEngine;
import vg.civcraft.mc.civmodcore.locations.global.WorldIDManager;

public class CitadelDAO extends TableStorageEngine<Reinforcement> {

	private boolean batchMode;
	private List<List<ReinforcementTuple>> batches;

	public CitadelDAO(Logger logger, ManagedDatasource db) {
		super(logger, db);
		this.batchMode = false;
	}

	public void setBatchMode(boolean batch) {
		this.batchMode = batch;
		batches = new ArrayList<>();
		for (int i = 0; i < 3; i++) {
			batches.add(new ArrayList<>());
		}
	}

	public void cleanupBatches() {
		long currentTime = System.currentTimeMillis();
		try (Connection conn = db.getConnection();
			 PreparedStatement deletePlant = conn.prepareStatement(
					 "delete from ctdl_reinforcements where chunk_x = ? and chunk_z = ? and world_id = ? and "
							 + "x_offset = ? and y = ? and z_offset = ?;");) {
			conn.setAutoCommit(false);
			for (ReinforcementTuple rein : batches.get(2)) {
				setDeleteDataStatement(deletePlant, rein.rein, rein.coord);
				deletePlant.addBatch();
			}
			logger.info("Batch 2: " + (System.currentTimeMillis() - currentTime) + " ms");
			logger.info("Batch 2 Size: " + batches.get(2).size());
			batches.get(2).clear();
			deletePlant.executeBatch();
			conn.setAutoCommit(true);
			logger.info("Batch 2 Finish: " + (System.currentTimeMillis() - currentTime) + " ms");
		} catch (SQLException e) {
			logger.log(Level.SEVERE, "Failed to delete plant from db: ", e);
		}
		try (Connection conn = db.getConnection();
			 PreparedStatement insertPlant = conn.prepareStatement(
					 "insert into ctdl_reinforcements (chunk_x, chunk_z, world_id, x_offset, y, z_offset, type_id, "
							 + "health, group_id, insecure, creation_time) values(?,?,?, ?,?,?, ?,?,?,?,?);");) {
			conn.setAutoCommit(false);
			for (ReinforcementTuple rein : batches.get(0)) {
				setInsertDataStatement(insertPlant, rein.rein, rein.coord);
				insertPlant.addBatch();
			}
			logger.info("Batch 0: " + (System.currentTimeMillis() - currentTime) + " ms");
			logger.info("Batch 0 Size: " + batches.get(0).size());
			batches.get(0).clear();
			insertPlant.executeBatch();
			conn.setAutoCommit(true);
			logger.info("Batch 0 Finish: " + (System.currentTimeMillis() - currentTime) + " ms");
		} catch (SQLException e) {
			logger.log(Level.SEVERE, "Failed to insert plant into db: ", e);
		}
		try (Connection conn = db.getConnection();
			 PreparedStatement updatePlant = conn.prepareStatement("update ctdl_reinforcements set type_id = ?, health = ?, group_id = ?, insecure = ?, creation_time = ? where "
					 + "chunk_x = ? and chunk_z = ? and world_id = ? and x_offset = ? and y = ? and z_offset = ?;");) {
			conn.setAutoCommit(false);
			for (ReinforcementTuple rein : batches.get(1)) {
				setUpdateDataStatement(updatePlant, rein.rein, rein.coord);
				updatePlant.addBatch();
			}
			logger.info("Batch 1: " + (System.currentTimeMillis() - currentTime) + " ms");
			logger.info("Batch 1 Size: " + batches.get(1).size());
			batches.get(1).clear();
			updatePlant.executeBatch();
			conn.setAutoCommit(true);
			logger.info("Batch 1 Finish: " + (System.currentTimeMillis() - currentTime) + " ms");
		} catch (SQLException e) {
			logger.log(Level.SEVERE, "Failed to update plant in db: ", e);
		}
	}

	@Override
	public void registerMigrations() {
		db.registerMigration(15, false,
				"CREATE TABLE IF NOT EXISTS reinforcement (rein_id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,"
						+ "x int(11) NOT NULL, y int(11) NOT NULL, z int(11) NOT NULL, chunk_x int(11) NOT NULL, chunk_z int(11) NOT NULL,"
						+ "world varchar(255) NOT NULL, material_id int(11) NOT NULL, durability varchar(10) NOT NULL, insecure tinyint(1) NOT NULL,"
						+ "group_id int(11) NOT NULL, maturation_time int(11) NOT NULL, rein_type_id int(11) NOT NULL, lore varchar(255) DEFAULT NULL,"
						+ "acid_time int(11) NOT NULL)");
		db.registerMigration(16, false, new Callable<Boolean>() {

			@Override
			public Boolean call() throws Exception {
				try (Connection insertConn = db.getConnection();
						PreparedStatement selectRein = insertConn.prepareStatement(<commit_msg>update names<commit_after>package vg.civcraft.mc.citadel.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.Callable;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.inventory.meta.ItemMeta;
import vg.civcraft.mc.citadel.Citadel;
import vg.civcraft.mc.citadel.reinforcementtypes.ReinforcementType;
import vg.civcraft.mc.citadel.reinforcementtypes.ReinforcementTypeManager;
import vg.civcraft.mc.civmodcore.CivModCorePlugin;
import vg.civcraft.mc.civmodcore.dao.ManagedDatasource;
import vg.civcraft.mc.civmodcore.locations.chunkmeta.XZWCoord;
import vg.civcraft.mc.civmodcore.locations.chunkmeta.block.BlockBasedChunkMeta;
import vg.civcraft.mc.civmodcore.locations.chunkmeta.block.table.TableBasedBlockChunkMeta;
import vg.civcraft.mc.civmodcore.locations.chunkmeta.block.table.TableStorageEngine;
import vg.civcraft.mc.civmodcore.locations.global.WorldIDManager;

public class CitadelDAO extends TableStorageEngine<Reinforcement> {

	private boolean batchMode;
	private List<List<ReinforcementTuple>> batches;

	public CitadelDAO(Logger logger, ManagedDatasource db) {
		super(logger, db);
		this.batchMode = false;
	}

	public void setBatchMode(boolean batch) {
		this.batchMode = batch;
		batches = new ArrayList<>();
		for (int i = 0; i < 3; i++) {
			batches.add(new ArrayList<>());
		}
	}

	public void cleanupBatches() {
		long currentTime = System.currentTimeMillis();
		try (Connection conn = db.getConnection();
			 PreparedStatement deleteRein = conn.prepareStatement(
					 "delete from ctdl_reinforcements where chunk_x = ? and chunk_z = ? and world_id = ? and "
							 + "x_offset = ? and y = ? and z_offset = ?;");) {
			conn.setAutoCommit(false);
			for (ReinforcementTuple rein : batches.get(2)) {
				setDeleteDataStatement(deleteRein, rein.rein, rein.coord);
				deleteRein.addBatch();
			}
			logger.info("Batch 2: " + (System.currentTimeMillis() - currentTime) + " ms");
			logger.info("Batch 2 Size: " + batches.get(2).size());
			batches.get(2).clear();
			deleteRein.executeBatch();
			conn.setAutoCommit(true);
			logger.info("Batch 2 Finish: " + (System.currentTimeMillis() - currentTime) + " ms");
		} catch (SQLException e) {
			logger.log(Level.SEVERE, "Failed to delete reinforcement from db: ", e);
		}
		try (Connection conn = db.getConnection();
			 PreparedStatement insertRein = conn.prepareStatement(
					 "insert into ctdl_reinforcements (chunk_x, chunk_z, world_id, x_offset, y, z_offset, type_id, "
							 + "health, group_id, insecure, creation_time) values(?,?,?, ?,?,?, ?,?,?,?,?);");) {
			conn.setAutoCommit(false);
			for (ReinforcementTuple rein : batches.get(0)) {
				setInsertDataStatement(insertRein, rein.rein, rein.coord);
				insertRein.addBatch();
			}
			logger.info("Batch 0: " + (System.currentTimeMillis() - currentTime) + " ms");
			logger.info("Batch 0 Size: " + batches.get(0).size());
			batches.get(0).clear();
			insertRein.executeBatch();
			conn.setAutoCommit(true);
			logger.info("Batch 0 Finish: " + (System.currentTimeMillis() - currentTime) + " ms");
		} catch (SQLException e) {
			logger.log(Level.SEVERE, "Failed to insert reinforcement into db: ", e);
		}
		try (Connection conn = db.getConnection();
			 PreparedStatement updateRein = conn.prepareStatement("update ctdl_reinforcements set type_id = ?, health = ?, group_id = ?, insecure = ?, creation_time = ? where "
					 + "chunk_x = ? and chunk_z = ? and world_id = ? and x_offset = ? and y = ? and z_offset = ?;");) {
			conn.setAutoCommit(false);
			for (ReinforcementTuple rein : batches.get(1)) {
				setUpdateDataStatement(updateRein, rein.rein, rein.coord);
				updateRein.addBatch();
			}
			logger.info("Batch 1: " + (System.currentTimeMillis() - currentTime) + " ms");
			logger.info("Batch 1 Size: " + batches.get(1).size());
			batches.get(1).clear();
			updateRein.executeBatch();
			conn.setAutoCommit(true);
			logger.info("Batch 1 Finish: " + (System.currentTimeMillis() - currentTime) + " ms");
		} catch (SQLException e) {
			logger.log(Level.SEVERE, "Failed to update reinforcement in db: ", e);
		}
	}

	@Override
	public void registerMigrations() {
		db.registerMigration(15, false,
				"CREATE TABLE IF NOT EXISTS reinforcement (rein_id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,"
						+ "x int(11) NOT NULL, y int(11) NOT NULL, z int(11) NOT NULL, chunk_x int(11) NOT NULL, chunk_z int(11) NOT NULL,"
						+ "world varchar(255) NOT NULL, material_id int(11) NOT NULL, durability varchar(10) NOT NULL, insecure tinyint(1) NOT NULL,"
						+ "group_id int(11) NOT NULL, maturation_time int(11) NOT NULL, rein_type_id int(11) NOT NULL, lore varchar(255) DEFAULT NULL,"
						+ "acid_time int(11) NOT NULL)");
		db.registerMigration(16, false, new Callable<Boolean>() {

			@Override
			public Boolean call() throws Exception {
				try (Connection insertConn = db.getConnection();
						PreparedStatement selectRein = insertConn.prepareStatement(
{'fraction_non_alphanumeric': 0.13616134835716315, 'fraction_numerical': 0.01041568033330177, 'mean_word_length': 4.588359788359789, 'pattern_counts': {'":': 0, '<': 17, '<?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': '7389349', 'n_tokens_mistral': 3961, 'n_tokens_neox': 3482, 'n_words': 752}