<commit_before>package de.eric_wiltfang.dictionary;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.sql.*;
import java.util.EnumSet;
import java.util.Vector;

import de.eric_wiltfang.dictionary.DictionaryEvent.DictionaryEventType;
import de.eric_wiltfang.dictionary.Exporter.ExporterSettings;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

import java.util.UUID;

public class Dictionary {
	private DictionarySettings settings;
	private Path workingDirectory;
	private Connection connection;
	
	private Vector<DictionaryListener> listeners; 

	private Dictionary() throws IOException {
		listeners = new Vector<>();
		settings = new DictionarySettings();
		try {
			workingDirectory = Files.createTempDirectory("dict");
		} catch (IOException e) {
			throw new IOException("Couldn't create temporary directory: " + e);
		}
	}
	/** 
	 *	Since temporary files are used for the Dictionary, cleanup() has to be called
	 *  once the Dictionary will not be used anymore, so that no files remain to clutter
	 *  the user's system 
     */
	public void cleanup() throws IOException {
		try {
			connection.close();

			File[] files = workingDirectory.toFile().listFiles();
			for (File f : files) {
				f.delete();
			}

			workingDirectory.toFile().delete();
		} catch(Exception e) {
			throw new IOException("Couldn't clean up: " + e);
		}
	}

	private void connectDatabase() throws IOException {
		try {
			connection = DriverManager.getConnection("jdbc:h2:" + workingDirectory + "/db", "sa", "");
		} catch (SQLException e) {
			throw new IOException("Couldn't connect to Database: " + e);
		}
	}
	private void init() throws IOException, SQLException {
		connectDatabase();
		Statement s = connection.createStatement();
		s.execute("create table entry (" +
		          "    entry_id bigint not null auto_increment," +
		          "    word varchar(255)," +
		          "    definition varchar(10000)," +
		          "    notes varchar(10000)," +
		          "    category varchar(127)," +
		          "    tags array," +
		          "    primary key (entry_id)" +
		          ");");
	}
	/**
	 * Loads dictionary files 
	 */
	private void load(File from) throws IOException {
		try {
			ZipFile zip = new ZipFile(from);
			zip.extractAll(workingDirectory.toString());
			
			InputStreamReader reader = new InputStreamReader(new FileInputStream(workingDirectory + "/settings.json"), Charset.forName("UTF-8"));
			settings.load(reader);
			
			connectDatabase();
		} catch(Exception e) {
			throw new IOException("Coulnd't load file: " + e);
		}
	}
	/**
	 * Saves dictionary files to disk
	 */
	public void save(File target) throws IOException, Exception {
		try {
			connection.close();
		} catch(Exception e) {
			throw new Exception("Coulnd't release database for saving: " + e);
		}
		if (Files.exists(target.toPath())) {
			try {
				Files.delete(target.toPath());
			} catch (IOException e) {
				throw new IOException("Coulnd't save; File " + target + " already exists and can't be deleted: " + e);
			}
		}
		try {
			OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(workingDirectory + "/settings.json"), Charset.forName("UTF-8"));
			settings.save(writer);
			writer.close();
			
			ZipFile zip = new ZipFile(target);

			ZipParameters parameters = new ZipParameters();
			parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);<commit_msg>Now writing the version of the save in a seperate file inside the archive.<commit_after>package de.eric_wiltfang.dictionary;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.sql.*;
import java.util.EnumSet;
import java.util.Vector;

import de.eric_wiltfang.dictionary.DictionaryEvent.DictionaryEventType;
import de.eric_wiltfang.dictionary.Exporter.ExporterSettings;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

import java.util.UUID;

public class Dictionary {
	private DictionarySettings settings;
	private Path workingDirectory;
	private Connection connection;
	
	private Vector<DictionaryListener> listeners; 

	private Dictionary() throws IOException {
		listeners = new Vector<>();
		settings = new DictionarySettings();
		try {
			workingDirectory = Files.createTempDirectory("dict");
		} catch (IOException e) {
			throw new IOException("Couldn't create temporary directory: " + e);
		}
	}
	/** 
	 *	Since temporary files are used for the Dictionary, cleanup() has to be called
	 *  once the Dictionary will not be used anymore, so that no files remain to clutter
	 *  the user's system 
     */
	public void cleanup() throws IOException {
		try {
			connection.close();

			File[] files = workingDirectory.toFile().listFiles();
			for (File f : files) {
				f.delete();
			}

			workingDirectory.toFile().delete();
		} catch(Exception e) {
			throw new IOException("Couldn't clean up: " + e);
		}
	}

	private void connectDatabase() throws IOException {
		try {
			connection = DriverManager.getConnection("jdbc:h2:" + workingDirectory + "/db", "sa", "");
		} catch (SQLException e) {
			throw new IOException("Couldn't connect to Database: " + e);
		}
	}
	private void init() throws IOException, SQLException {
		connectDatabase();
		Statement s = connection.createStatement();
		s.execute("create table entry (" +
		          "    entry_id bigint not null auto_increment," +
		          "    word varchar(255)," +
		          "    definition varchar(10000)," +
		          "    notes varchar(10000)," +
		          "    category varchar(127)," +
		          "    tags array," +
		          "    primary key (entry_id)" +
		          ");");
	}
	/**
	 * Loads dictionary files 
	 */
	private void load(File from) throws IOException {
		try {
			ZipFile zip = new ZipFile(from);
			zip.extractAll(workingDirectory.toString());
			
			InputStreamReader reader = new InputStreamReader(new FileInputStream(workingDirectory + "/settings.json"), Charset.forName("UTF-8"));
			settings.load(reader);
			
			connectDatabase();
		} catch(Exception e) {
			throw new IOException("Coulnd't load file: " + e);
		}
	}
	/**
	 * Saves dictionary files to disk
	 */
	public void save(File target) throws IOException, Exception {
		try {
			connection.close();
		} catch(Exception e) {
			throw new Exception("Coulnd't release database for saving: " + e);
		}
		if (Files.exists(target.toPath())) {
			try {
				Files.delete(target.toPath());
			} catch (IOException e) {
				throw new IOException("Coulnd't save; File " + target + " already exists and can't be deleted: " + e);
			}
		}
		try {
			OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(workingDirectory + "/VERSION"), Charset.forName("UTF-8"));
			writer.write("1.0");
			writer.close();
			
			writer = new OutputStreamWriter(new FileOutputStream(workingDirectory + "/settings.json"), Charset.forName("UTF-8"));
			settings.save(writer);
			writer.close();
			
			ZipFile zip = new ZipFile(target);

			ZipParameters parameters = new ZipParameters();
			parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
{'fraction_non_alphanumeric': 0.12024393477396261, 'fraction_numerical': 0.006761235582659419, 'mean_word_length': 3.6857142857142855, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6979315', 'n_tokens_mistral': 2475, 'n_tokens_neox': 2137, 'n_words': 572}