<commit_before>package com.psddev.dari.db;

import java.util.Date;
import java.util.List;

import com.psddev.dari.util.ObjectUtils;
import com.psddev.dari.util.PaginatedResult;
import com.psddev.dari.util.Profiler;

/** Times all database operations using {@link Profiler}. */
public class ProfilingDatabase extends ForwardingDatabase {

    /** Event name used to mark {@link Database#readAll} calls. */
    public static final String READ_ALL_EVENT_NAME = "Read All";

    /** Event name used to mark {@link Database#readAllGrouped} calls. */
    public static final String READ_ALL_GROUPED_EVENT_NAME = "Read All Grouped";

    /** Event name used to mark {@link Database#readCount} calls. */
    public static final String READ_COUNT_EVENT_NAME = "Read Count";

    /** Event name used to mark {@link Database#readFirst} calls. */
    public static final String READ_FIRST_EVENT_NAME = "Read First";

    /** Event name used to mark {@link Database#readIterable} calls. */
    public static final String READ_ITERABLE_EVENT_NAME = "Read Iterable";

    /** Event name used to mark {@link Database#readLastUpdate} calls. */
    public static final String READ_LAST_UPDATE_EVENT_NAME = "Read Last Update";

    /** Event name used to mark {@link Database#readPartial} calls. */
    public static final String READ_PARTIAL_EVENT_NAME = "Read Partial";

    /** Event name used to mark {@link Database#readPartialGrouped} calls. */
    public static final String READ_PARTIAL_GROUPED_EVENT_NAME = "Read Partial Grouped";

    /** Event name used to mark {@link Database#save} calls. */
    public static final String SAVE_EVENT_NAME = "Save";

    /** Event name used to mark {@link Database#saveUnsafely} calls. */
    public static final String SAVE_UNSAFELY_EVENT_NAME = "Save Unsafely";

    /** Event name used to mark {@link Database#index} calls. */
    public static final String INDEX_EVENT_NAME = "Index";

    /** Event name used to mark {@link Database#delete} calls. */
    public static final String DELETE_EVENT_NAME = "Delete";

    /** Event name used to mark {@link Database#deleteByQuery} calls. */
    public static final String DELETE_BY_QUERY_EVENT_NAME = "Delete By Query";

    // --- ForwardingDatabase support ---

    private void startQueryEvent(String event, Query<?> query) {
        StackTraceElement caller = null;
        StackTraceElement[] elements = new Throwable().getStackTrace();

        for (int i = 2, length = elements.length; i < length; ++ i) {
            StackTraceElement element = elements[i];
            String className = element.getClassName();
            Class<?> c = ObjectUtils.getClassByName(className);

            if (c == null ||
                    !(Database.class.isAssignableFrom(c) ||
                    Query.class.isAssignableFrom(c))) {
                caller = element;
                break;
            }
        }

        Object resolving = query.getOptions().get(State.REFERENCE_RESOLVING_QUERY_OPTION);
        if (resolving != null) {
            Profiler.Static.startThreadEvent("Resolving Fields", resolving, query.getOptions().get(State.REFERENCE_FIELD_QUERY_OPTION), caller);
        } else {
            Profiler.Static.startThreadEvent(event, caller, query);
        }
    }

    @Override
    public <T> List<T> readAll(Query<T> query) {
        List<T> result = null;

        try {
            startQueryEvent(READ_ALL_EVENT_NAME, query);
            result = super.readAll(query);
            return result;

        } finally {<commit_msg>Fixes ProfilingDatabase to ignore the caller discovery overhead.<commit_after>package com.psddev.dari.db;

import java.util.Date;
import java.util.List;

import com.psddev.dari.util.ObjectUtils;
import com.psddev.dari.util.PaginatedResult;
import com.psddev.dari.util.Profiler;

/** Times all database operations using {@link Profiler}. */
public class ProfilingDatabase extends ForwardingDatabase {

    /** Event name used to mark {@link Database#readAll} calls. */
    public static final String READ_ALL_EVENT_NAME = "Read All";

    /** Event name used to mark {@link Database#readAllGrouped} calls. */
    public static final String READ_ALL_GROUPED_EVENT_NAME = "Read All Grouped";

    /** Event name used to mark {@link Database#readCount} calls. */
    public static final String READ_COUNT_EVENT_NAME = "Read Count";

    /** Event name used to mark {@link Database#readFirst} calls. */
    public static final String READ_FIRST_EVENT_NAME = "Read First";

    /** Event name used to mark {@link Database#readIterable} calls. */
    public static final String READ_ITERABLE_EVENT_NAME = "Read Iterable";

    /** Event name used to mark {@link Database#readLastUpdate} calls. */
    public static final String READ_LAST_UPDATE_EVENT_NAME = "Read Last Update";

    /** Event name used to mark {@link Database#readPartial} calls. */
    public static final String READ_PARTIAL_EVENT_NAME = "Read Partial";

    /** Event name used to mark {@link Database#readPartialGrouped} calls. */
    public static final String READ_PARTIAL_GROUPED_EVENT_NAME = "Read Partial Grouped";

    /** Event name used to mark {@link Database#save} calls. */
    public static final String SAVE_EVENT_NAME = "Save";

    /** Event name used to mark {@link Database#saveUnsafely} calls. */
    public static final String SAVE_UNSAFELY_EVENT_NAME = "Save Unsafely";

    /** Event name used to mark {@link Database#index} calls. */
    public static final String INDEX_EVENT_NAME = "Index";

    /** Event name used to mark {@link Database#delete} calls. */
    public static final String DELETE_EVENT_NAME = "Delete";

    /** Event name used to mark {@link Database#deleteByQuery} calls. */
    public static final String DELETE_BY_QUERY_EVENT_NAME = "Delete By Query";

    // --- ForwardingDatabase support ---

    private void startQueryEvent(String event, Query<?> query) {
        StackTraceElement caller = null;

        Profiler.Static.pauseThreadEvent();

        try {
            StackTraceElement[] elements = new Throwable().getStackTrace();

            for (int i = 2, length = elements.length; i < length; ++ i) {
                StackTraceElement element = elements[i];
                String className = element.getClassName();
                Class<?> c = ObjectUtils.getClassByName(className);

                if (c == null ||
                        !(Database.class.isAssignableFrom(c) ||
                        Query.class.isAssignableFrom(c))) {
                    caller = element;
                    break;
                }
            }

        } finally {
            Profiler.Static.resumeThreadEvent();
        }

        Object resolving = query.getOptions().get(State.REFERENCE_RESOLVING_QUERY_OPTION);

        if (resolving != null) {
            Profiler.Static.startThreadEvent("Resolving Fields", resolving, query.getOptions().get(State.REFERENCE_FIELD_QUERY_OPTION), caller);

        } else {
            Profiler.Static.startThreadEvent(event, caller, query);
        }
    }

    @Override
    public <T> List<T> readAll(Query<T> query) {
        List<T> result = null;

        try {
            startQueryEvent(READ_ALL_EVENT_NAME, query);
            result = super.readAll(query);
            return result;

        } finally {
{'fraction_non_alphanumeric': 0.10316481709823264, 'fraction_numerical': 0.0002740101383751199, 'mean_word_length': 2.870625662778367, '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': '5560785', 'n_tokens_mistral': 2036, 'n_tokens_neox': 1963, 'n_words': 597}