<commit_before>package pl.exsio.ck.model.dao;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.exception.ExceptionUtils;
import pl.exsio.ck.logging.presenter.LogPresenter;
import pl.exsio.ck.main.app.App;
import pl.exsio.ck.model.Entries;
import pl.exsio.ck.model.Entry;
import pl.exsio.ck.progress.presenter.ProgressPresenter;
import pl.exsio.ck.util.ArrayUtil;

public class StructuredlEntryDaoImpl extends SimpleEntryDaoImpl implements EntryDao {

    protected ProgressPresenter progress;

    public StructuredlEntryDaoImpl(LogPresenter log) {
        super(log);
    }

    public StructuredlEntryDaoImpl(LogPresenter log, String dbUrl) {
        super(log, dbUrl);
    }

    @Override
    protected Collection<Entry> saveCollection(Collection<Entry> entries, boolean updateExising) throws SQLException {
        this.showProgressBar("pracuję...");
        Map<String, List<Entry>> saveMap = this.createSaveMap(entries);
        for (String digest : saveMap.keySet()) {
            List<Entry> entriesGroup = saveMap.get(digest);
            String[] serials = Entries.getSerials(entriesGroup);
            List<String> existingSerials = this.getExistingSerialsFrom(serials);
            List<String> serialsToInsert = new ArrayList<>();
            List<String> serialsToUpdate = new ArrayList<>();
            for (String serial : serials) {
                if (!existingSerials.contains(serial)) {
                    serialsToInsert.add(serial);
                } else {
                    serialsToUpdate.add(serial);
                }
            }
            Entry entry = entriesGroup.iterator().next();
            this.performInserts(serialsToInsert, digest, entry);
            if (updateExising) {
                this.performUpdates(serialsToUpdate, digest, entry);
            }

        }
        this.conn.commit();
        this.hideProgressBar();
        return entries;
    }

    protected void performUpdates(List<String> serialsToUpdate, String digest, Entry entry) throws RuntimeException, SQLException {
        int entryId;
        if (!serialsToUpdate.isEmpty()) {
            entryId = this.obtainEntryIdForDigest(digest, entry);
            PreparedStatement pstmt = this.getStatement("update serials set entry_id = ? where serial_no = ?");
            for (String serial : serialsToUpdate) {
                pstmt.setInt(1, entryId);
                pstmt.setString(2, serial);
                pstmt.addBatch();
            }
            pstmt.executeBatch();
        }
    }

    protected void performInserts(List<String> serialsToInsert, String digest, Entry entry) throws SQLException, RuntimeException {
        int entryId;
        if (!serialsToInsert.isEmpty()) {
            entryId = this.obtainEntryIdForDigest(digest, entry);
            PreparedStatement pstmt = this.getStatement("insert into serials (serial_no, entry_id) values (?, ?)");
            for (String serial : serialsToInsert) {
                pstmt.setString(1, serial);
                pstmt.setInt(2, entryId);
                pstmt.addBatch();
            }
            pstmt.executeBatch();
        }
    }
<commit_msg>new Entry Dao<commit_after>package pl.exsio.ck.model.dao;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.exception.ExceptionUtils;
import pl.exsio.ck.logging.presenter.LogPresenter;
import pl.exsio.ck.main.app.App;
import pl.exsio.ck.model.Entries;
import pl.exsio.ck.model.Entry;
import pl.exsio.ck.progress.presenter.ProgressPresenter;
import pl.exsio.ck.util.ArrayUtil;

public class StructuredlEntryDaoImpl extends SimpleEntryDaoImpl implements EntryDao {

    protected ProgressPresenter progress;

    public StructuredlEntryDaoImpl(LogPresenter log) {
        super(log);
    }

    public StructuredlEntryDaoImpl(LogPresenter log, String dbUrl) {
        super(log, dbUrl);
    }

    @Override
    protected Collection<Entry> saveCollection(Collection<Entry> entries, boolean updateExising) throws SQLException {
        this.showProgressBar("pracuję...");
        Map<String, List<Entry>> saveMap = this.createSaveMap(entries);
        for (String digest : saveMap.keySet()) {
            List<Entry> entriesGroup = saveMap.get(digest);
            String[] serials = Entries.getSerials(entriesGroup);
            List<String> existingSerials = this.getExistingSerialsFrom(serials);
            List<String> serialsToInsert = new ArrayList<>();
            List<String> serialsToUpdate = new ArrayList<>();
            for (String serial : serials) {
                if (!existingSerials.contains(serial)) {
                    serialsToInsert.add(serial);
                } else {
                    serialsToUpdate.add(serial);
                }
            }
            int entryId = this.obtainEntryIdForDigest(digest, entriesGroup.iterator().next());
            this.performInserts(serialsToInsert, entryId);
            if (updateExising) {
                this.performUpdates(serialsToUpdate, entryId);
            }

        }
        this.conn.commit();
        this.hideProgressBar();
        return entries;
    }

    protected void performUpdates(List<String> serialsToUpdate, int entryId) throws RuntimeException, SQLException {
        if (!serialsToUpdate.isEmpty()) {
            PreparedStatement pstmt = this.getStatement("update serials set entry_id = ? where serial_no = ?");
            for (String serial : serialsToUpdate) {
                pstmt.setInt(1, entryId);
                pstmt.setString(2, serial);
                pstmt.addBatch();
            }
            pstmt.executeBatch();
        }
    }

    protected void performInserts(List<String> serialsToInsert, int entryId) throws SQLException, RuntimeException {
        if (!serialsToInsert.isEmpty()) {
            PreparedStatement pstmt = this.getStatement("insert into serials (serial_no, entry_id) values (?, ?)");
            for (String serial : serialsToInsert) {
                pstmt.setString(1, serial);
                pstmt.setInt(2, entryId);
                pstmt.addBatch();
            }
            pstmt.executeBatch();
        }
    }

{'fraction_non_alphanumeric': 0.10253335332034177, 'fraction_numerical': 0.00149902563333833, 'mean_word_length': 2.577479892761394, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '2895791', 'n_tokens_mistral': 1878, 'n_tokens_neox': 1852, 'n_words': 367}