<commit_before>/*
 ** 2013 August 10
 **
 ** The author disclaims copyright to this source code.  In place of
 ** a legal notice, here is a blessing:
 **    May you do good and not evil.
 **    May you find forgiveness for yourself and forgive others.
 **    May you share freely, never taking more than you give.
 */
package info.ata4.unity.rtti;

import info.ata4.io.DataReader;
import info.ata4.io.DataWriter;
import info.ata4.io.socket.IOSocket;
import info.ata4.io.socket.Sockets;
import info.ata4.log.LogUtils;
import info.ata4.unity.asset.AssetFile;
import info.ata4.unity.asset.FieldTypeNode;
import info.ata4.unity.asset.ObjectInfo;
import info.ata4.unity.asset.VersionInfo;
import info.ata4.unity.util.ClassID;
import info.ata4.unity.util.UnityVersion;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.nio.file.StandardOpenOption.WRITE;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;

/**
 *
 * @author <NAME> <barracuda415 at yahoo.de>
 */
public class FieldTypeDatabase {
    
    private static final Logger L = LogUtils.getLogger();
    
    public static final int VERSION = 1;
    public static final String FILENAME = "structdb.dat";
    
    private static FieldTypeDatabase instance;

    public static FieldTypeDatabase getInstance() {
        if (instance == null) {
            instance = new FieldTypeDatabase();
            instance.load();
        }
        return instance;
    }
    
    private HashMap<Pair<Integer, UnityVersion>, FieldTypeNode> nodeMap = new HashMap<>();
    private Path dbFile;
    
    private FieldTypeDatabase() {
        // get database path based on the path to the current .jar file
        try {
            dbFile = Paths.get(FieldTypeDatabase.class.getProtectionDomain()
                        .getCodeSource().getLocation().toURI()).resolveSibling(FILENAME);
        } catch (URISyntaxException ex) {
            L.log(Level.WARNING, "Can't resolve database path", ex);
        }
    }
    
    public Map<Pair<Integer, UnityVersion>, FieldTypeNode> getFieldTypeMap() {
        return Collections.unmodifiableMap(nodeMap);
    }
    
    public void load() {
        L.info("Loading type database");
        
        // read database file, external or internal otherwise
        InputStream is = null;
        
        try {
            if (dbFile != null && Files.exists(dbFile)) {
                is = Files.newInputStream(dbFile);
            } else {
                is = getClass().getResourceAsStream("/resources/" + FILENAME);
            }
            
            if (is == null) {
                throw new IOException("Type database file not found");
            }
            
            load(is);
        } catch (IOException ex) {
            L.log(Level.SEVERE, "Can't open type database", ex);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    
    public void load(InputStream is) {
        try (IOSocket socket = Sockets.forInputStream(new BufferedInputStream(is))) {
            DataReader in = new DataReader(socket);

            // read header
            int dbVersion = in.readInt();

            if (dbVersion != VERSION) {
                throw new RuntimeException("Wrong database version");
            }

            // read field node table
            int fieldNodeSize = in.readInt();
            List<FieldTypeNode> fieldNodes = new ArrayList<>(fieldNodeSize);

            for (int i = 0; i < fieldNodeSize; i++) {
                FieldTypeNode fieldNode = new FieldTypeNode();
                fieldNode.read(in);
                fieldNodes.add(fieldNode);
            }

            // read version string table
            int versionSize = in.readInt();
            List<UnityVersion> versions = new ArrayList<>(versionSize);

            for (int i = 0; i < versionSize; i++) {
                versions.add(new UnityVersion(in.readStringNull()));
            }

            // read mapping data
            int fieldNodeKeySize = in.readInt();

            for (int i = 0; i < fieldNodeKeySize; i++) {
                int index = in.readInt();
                int classID = in.readInt();
                int revisionIndex = in.readInt();
                
                UnityVersion version = versions.get(revisionIndex);
                FieldTypeNode fieldNode = fieldNodes.get(index);

                addNode(classID, version, fieldNode);
            }
        } catch (IOException ex) {
            L.log(Level.SEVERE, "Can't read type database", ex);
        }
    }
    
    public void save() {
        L.info("Saving type database");
        
        try (
            OutputStream os = Files.newOutputStream(dbFile, WRITE, CREATE, TRUNCATE_EXISTING)
        ) {
            save(os);
        } catch (IOException ex) {
            L.log(Level.SEVERE, "Can't open type database", ex);
        }
    }
    <commit_msg>Reduced log level for loading/saving logs<commit_after>/*
 ** 2013 August 10
 **
 ** The author disclaims copyright to this source code.  In place of
 ** a legal notice, here is a blessing:
 **    May you do good and not evil.
 **    May you find forgiveness for yourself and forgive others.
 **    May you share freely, never taking more than you give.
 */
package info.ata4.unity.rtti;

import info.ata4.io.DataReader;
import info.ata4.io.DataWriter;
import info.ata4.io.socket.IOSocket;
import info.ata4.io.socket.Sockets;
import info.ata4.log.LogUtils;
import info.ata4.unity.asset.AssetFile;
import info.ata4.unity.asset.FieldTypeNode;
import info.ata4.unity.asset.ObjectInfo;
import info.ata4.unity.asset.VersionInfo;
import info.ata4.unity.util.ClassID;
import info.ata4.unity.util.UnityVersion;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.nio.file.StandardOpenOption.WRITE;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;

/**
 *
 * @author <NAME> <barracuda415 at yahoo.de>
 */
public class FieldTypeDatabase {
    
    private static final Logger L = LogUtils.getLogger();
    
    public static final int VERSION = 1;
    public static final String FILENAME = "structdb.dat";
    
    private static FieldTypeDatabase instance;

    public static FieldTypeDatabase getInstance() {
        if (instance == null) {
            instance = new FieldTypeDatabase();
            instance.load();
        }
        return instance;
    }
    
    private HashMap<Pair<Integer, UnityVersion>, FieldTypeNode> nodeMap = new HashMap<>();
    private Path dbFile;
    
    private FieldTypeDatabase() {
        // get database path based on the path to the current .jar file
        try {
            dbFile = Paths.get(FieldTypeDatabase.class.getProtectionDomain()
                        .getCodeSource().getLocation().toURI()).resolveSibling(FILENAME);
        } catch (URISyntaxException ex) {
            L.log(Level.WARNING, "Can't resolve database path", ex);
        }
    }
    
    public Map<Pair<Integer, UnityVersion>, FieldTypeNode> getFieldTypeMap() {
        return Collections.unmodifiableMap(nodeMap);
    }
    
    public void load() {
        L.fine("Loading type database");
        
        // read database file, external or internal otherwise
        InputStream is = null;
        
        try {
            if (dbFile != null && Files.exists(dbFile)) {
                is = Files.newInputStream(dbFile);
            } else {
                is = getClass().getResourceAsStream("/resources/" + FILENAME);
            }
            
            if (is == null) {
                throw new IOException("Type database file not found");
            }
            
            load(is);
        } catch (IOException ex) {
            L.log(Level.SEVERE, "Can't open type database", ex);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    
    public void load(InputStream is) {
        try (IOSocket socket = Sockets.forInputStream(new BufferedInputStream(is))) {
            DataReader in = new DataReader(socket);

            // read header
            int dbVersion = in.readInt();

            if (dbVersion != VERSION) {
                throw new RuntimeException("Wrong database version");
            }

            // read field node table
            int fieldNodeSize = in.readInt();
            List<FieldTypeNode> fieldNodes = new ArrayList<>(fieldNodeSize);

            for (int i = 0; i < fieldNodeSize; i++) {
                FieldTypeNode fieldNode = new FieldTypeNode();
                fieldNode.read(in);
                fieldNodes.add(fieldNode);
            }

            // read version string table
            int versionSize = in.readInt();
            List<UnityVersion> versions = new ArrayList<>(versionSize);

            for (int i = 0; i < versionSize; i++) {
                versions.add(new UnityVersion(in.readStringNull()));
            }

            // read mapping data
            int fieldNodeKeySize = in.readInt();

            for (int i = 0; i < fieldNodeKeySize; i++) {
                int index = in.readInt();
                int classID = in.readInt();
                int revisionIndex = in.readInt();
                
                UnityVersion version = versions.get(revisionIndex);
                FieldTypeNode fieldNode = fieldNodes.get(index);

                addNode(classID, version, fieldNode);
            }
        } catch (IOException ex) {
            L.log(Level.SEVERE, "Can't read type database", ex);
        }
    }
    
    public void save() {
        L.fine("Saving type database");
        
        try (
            OutputStream os = Files.newOutputStream(dbFile, WRITE, CREATE, TRUNCATE_EXISTING)
        ) {
            save(os);
        } catch (IOException ex) {
            L.log(Level.SEVERE, "Can't open type database", ex);
        }
    }
    
{'fraction_non_alphanumeric': 0.10418149466192171, 'fraction_numerical': 0.004804270462633452, 'mean_word_length': 2.19800853485064, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 25, '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': '6712314', 'n_tokens_mistral': 3180, 'n_tokens_neox': 3078, 'n_words': 797}