<commit_before>package net.ripe.db.whois.scheduler.task.grs;

import net.ripe.db.whois.common.DateTimeProvider;
import net.ripe.db.whois.common.collect.CollectionHelper;
import net.ripe.db.whois.common.dao.RpslObjectInfo;
import net.ripe.db.whois.common.dao.RpslObjectUpdateInfo;
import net.ripe.db.whois.common.dao.jdbc.JdbcRpslObjectOperations;
import net.ripe.db.whois.common.dao.jdbc.domain.ObjectTypeIds;
import net.ripe.db.whois.common.domain.CIString;
import net.ripe.db.whois.common.rpsl.ObjectType;
import net.ripe.db.whois.common.rpsl.RpslObject;
import net.ripe.db.whois.common.source.IllegalSourceException;
import net.ripe.db.whois.common.source.Source;
import net.ripe.db.whois.common.source.SourceContext;
import org.slf4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.CheckForNull;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import static net.ripe.db.whois.common.dao.jdbc.JdbcRpslObjectOperations.*;

class GrsDao {
    private final Logger logger;
    private final DateTimeProvider dateTimeProvider;
    private final CIString sourceName;
    private final SourceContext sourceContext;

    private JdbcTemplate masterJdbcTemplate;
    private JdbcTemplate slaveJdbcTemplate;

    GrsDao(final Logger logger, final DateTimeProvider dateTimeProvider, final CIString sourceName, final SourceContext sourceContext) {
        this.logger = logger;
        this.dateTimeProvider = dateTimeProvider;
        this.sourceName = sourceName;
        this.sourceContext = sourceContext;
    }

    private void ensureInitialized() {
        if (masterJdbcTemplate == null) {
            try {
                masterJdbcTemplate = sourceContext.getSourceConfiguration(Source.master(sourceName)).getJdbcTemplate();
                slaveJdbcTemplate = sourceContext.getSourceConfiguration(Source.slave(sourceName)).getJdbcTemplate();
                JdbcRpslObjectOperations.sanityCheck(masterJdbcTemplate);
                JdbcRpslObjectOperations.sanityCheck(slaveJdbcTemplate);
            } catch (IllegalSourceException e) {
                throw new IllegalArgumentException(String.format("Source not configured: %s", e.getSource()));
            }
        }
    }

    void cleanDatabase() {
        ensureInitialized();
        JdbcRpslObjectOperations.truncateTables(masterJdbcTemplate);
    }

    List<Integer> getCurrentObjectIds() {
        ensureInitialized();
        return slaveJdbcTemplate.queryForList("" +
                "SELECT object_id " +
                "FROM last " +
                "WHERE sequence_id != 0",
                Integer.class);
    }

    @CheckForNull
    GrsObjectInfo get(final int objectId) {
        ensureInitialized();
        return CollectionHelper.uniqueResult(masterJdbcTemplate.query("" +
                "SELECT object_id, sequence_id, object " +
                "  FROM last " +
                "  WHERE object_id = ?" +<commit_msg>upgraded sanity checks for better exception protection<commit_after>package net.ripe.db.whois.scheduler.task.grs;

import net.ripe.db.whois.common.DateTimeProvider;
import net.ripe.db.whois.common.collect.CollectionHelper;
import net.ripe.db.whois.common.dao.RpslObjectInfo;
import net.ripe.db.whois.common.dao.RpslObjectUpdateInfo;
import net.ripe.db.whois.common.dao.jdbc.JdbcRpslObjectOperations;
import net.ripe.db.whois.common.dao.jdbc.domain.ObjectTypeIds;
import net.ripe.db.whois.common.domain.CIString;
import net.ripe.db.whois.common.rpsl.ObjectType;
import net.ripe.db.whois.common.rpsl.RpslObject;
import net.ripe.db.whois.common.source.IllegalSourceException;
import net.ripe.db.whois.common.source.Source;
import net.ripe.db.whois.common.source.SourceContext;
import org.slf4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.CheckForNull;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import static net.ripe.db.whois.common.dao.jdbc.JdbcRpslObjectOperations.*;

class GrsDao {
    private final Logger logger;
    private final DateTimeProvider dateTimeProvider;
    private final CIString sourceName;
    private final SourceContext sourceContext;

    private JdbcTemplate masterJdbcTemplate;
    private JdbcTemplate slaveJdbcTemplate;

    GrsDao(final Logger logger, final DateTimeProvider dateTimeProvider, final CIString sourceName, final SourceContext sourceContext) {
        this.logger = logger;
        this.dateTimeProvider = dateTimeProvider;
        this.sourceName = sourceName;
        this.sourceContext = sourceContext;
    }

    private void ensureInitialized() {
        if (masterJdbcTemplate == null) {
            try {
                JdbcTemplate masterJdbcTemplate = sourceContext.getSourceConfiguration(Source.master(sourceName)).getJdbcTemplate();
                JdbcTemplate slaveJdbcTemplate = sourceContext.getSourceConfiguration(Source.slave(sourceName)).getJdbcTemplate();
                JdbcRpslObjectOperations.sanityCheck(masterJdbcTemplate);
                JdbcRpslObjectOperations.sanityCheck(slaveJdbcTemplate);
                this.masterJdbcTemplate = masterJdbcTemplate;
                this.slaveJdbcTemplate = slaveJdbcTemplate;
            } catch (IllegalSourceException e) {
                throw new IllegalArgumentException(String.format("Source not configured: %s", e.getSource()));
            }
        }
    }

    void cleanDatabase() {
        ensureInitialized();
        JdbcRpslObjectOperations.truncateTables(masterJdbcTemplate);
    }

    List<Integer> getCurrentObjectIds() {
        ensureInitialized();
        return slaveJdbcTemplate.queryForList("" +
                "SELECT object_id " +
                "FROM last " +
                "WHERE sequence_id != 0",
                Integer.class);
    }

    @CheckForNull
    GrsObjectInfo get(final int objectId) {
        ensureInitialized();
        return CollectionHelper.uniqueResult(masterJdbcTemplate.query("" +
                "SELECT object_id, sequence_id, object " +
                "  FROM last " +
                "  WHERE object_id = ?" +
{'fraction_non_alphanumeric': 0.09202168861347793, 'fraction_numerical': 0.0006196746707978311, 'mean_word_length': 3.335795836131632, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2407851', 'n_tokens_mistral': 1871, 'n_tokens_neox': 1841, 'n_words': 310}