<commit_before>/* Copyright 2014 Yorba Foundation
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later).  See the COPYING file in this distribution.
 */

/**
 * IMAP database garbage collector.
 *
 * Currently the garbage collector reaps messages unlinked from the MessageLocationTable older than
 * a prescribed date.  It also removes their on-disk attachment files (in a transaction-safe manner)
 * and looks for empty directories in the attachment directory tree (caused by attachment files
 * being removed without deleting their parents).
 *
 * The garbage collector is designed to run in the background and in such a way that it can be
 * closed (even by application shutdown) and re-run later without the database going incoherent.
 *
 * In addition, GC can recommend when to perform a VACUUM on the database and perform that
 * operation for the caller.  Vacuuming locks the database for an extended period of time, and GC
 * attempts to determine when it's best to do that by tracking the number of messages reaped by
 * the garbage collector.  (Vacuuming is really only advantageous when lots of rows have been
 * deleted in the database; without garbage collection, Geary's database tends to merely grow in
 * size.)  Unlike garbage collection, vacuuming is not designed to run in the background and the
 * user should be presented a busy monitor while it's occurring.
 */

private class Geary.ImapDB.GC {
    // Maximum number of days between reaping runs.
    private const int REAP_DAYS_SPAN = 10;
    
    // Minimum number of days between vacuums.
    private const int VACUUM_DAYS_SPAN = 30;
    
    // Number of reaped messages since last vacuum indicating another vacuum should occur
    private const int VACUUM_WHEN_REAPED_REACHES = 10000;
    
    // Days old from today an unlinked email message must be to be reaped by the garbage collector
    private const int UNLINKED_DAYS = 30;
    
    // Amount of time to sleep between various database-bound GC iterations to give other
    // transactions a chance
    private const uint SLEEP_MSEC = 15;
    
    // Number of database operations to perform before sleeping (obviously this is a rough
    // approximation, as not all operations are the same cost)
    private const int OPS_PER_SLEEP_CYCLE = 10;
    
    // Number of files to reap from the DeleteAttachmentFileTable per iteration
    private const int REAP_ATTACHMENT_PER = 5;
    
    // Number of files to enumerate per time when walking a directory's children
    private const int ENUM_DIR_PER = 10;
    
    /**
     * Operation(s) recommended by {@link should_run_async}.
     */
    [Flag]
    public enum RecommendedOperation {
        /**
         * Indicates no garbage collection is recommended at this time.
         */
        NONE = 0,
        /*
         * Indicates the caller should run {@link reap_async} to potentially clean up unlinked
         * messages and files.
         */
        REAP,
        /**
         * Indicates the caller should run {@link vauum_async} to consolidate disk space and reduce
         * database fragmentation.
         */
        VACUUM
    }
    
    /**
     * Indicates the garbage collector is running.
     *
     * {@link run_async} will return immediately if called while running.
     */
    public bool is_running { get; private set; default = false; }
    
    private ImapDB.Database db;
    private int priority;
    private File data_dir;
    
    public GC(ImapDB.Database db, int priority) {
        this.db = db;<commit_msg>Fix code attribute to compile under Vala 0.28: Bug #741760<commit_after>/* Copyright 2014 Yorba Foundation
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later).  See the COPYING file in this distribution.
 */

/**
 * IMAP database garbage collector.
 *
 * Currently the garbage collector reaps messages unlinked from the MessageLocationTable older than
 * a prescribed date.  It also removes their on-disk attachment files (in a transaction-safe manner)
 * and looks for empty directories in the attachment directory tree (caused by attachment files
 * being removed without deleting their parents).
 *
 * The garbage collector is designed to run in the background and in such a way that it can be
 * closed (even by application shutdown) and re-run later without the database going incoherent.
 *
 * In addition, GC can recommend when to perform a VACUUM on the database and perform that
 * operation for the caller.  Vacuuming locks the database for an extended period of time, and GC
 * attempts to determine when it's best to do that by tracking the number of messages reaped by
 * the garbage collector.  (Vacuuming is really only advantageous when lots of rows have been
 * deleted in the database; without garbage collection, Geary's database tends to merely grow in
 * size.)  Unlike garbage collection, vacuuming is not designed to run in the background and the
 * user should be presented a busy monitor while it's occurring.
 */

private class Geary.ImapDB.GC {
    // Maximum number of days between reaping runs.
    private const int REAP_DAYS_SPAN = 10;
    
    // Minimum number of days between vacuums.
    private const int VACUUM_DAYS_SPAN = 30;
    
    // Number of reaped messages since last vacuum indicating another vacuum should occur
    private const int VACUUM_WHEN_REAPED_REACHES = 10000;
    
    // Days old from today an unlinked email message must be to be reaped by the garbage collector
    private const int UNLINKED_DAYS = 30;
    
    // Amount of time to sleep between various database-bound GC iterations to give other
    // transactions a chance
    private const uint SLEEP_MSEC = 15;
    
    // Number of database operations to perform before sleeping (obviously this is a rough
    // approximation, as not all operations are the same cost)
    private const int OPS_PER_SLEEP_CYCLE = 10;
    
    // Number of files to reap from the DeleteAttachmentFileTable per iteration
    private const int REAP_ATTACHMENT_PER = 5;
    
    // Number of files to enumerate per time when walking a directory's children
    private const int ENUM_DIR_PER = 10;
    
    /**
     * Operation(s) recommended by {@link should_run_async}.
     */
    [Flags]
    public enum RecommendedOperation {
        /**
         * Indicates no garbage collection is recommended at this time.
         */
        NONE = 0,
        /*
         * Indicates the caller should run {@link reap_async} to potentially clean up unlinked
         * messages and files.
         */
        REAP,
        /**
         * Indicates the caller should run {@link vauum_async} to consolidate disk space and reduce
         * database fragmentation.
         */
        VACUUM
    }
    
    /**
     * Indicates the garbage collector is running.
     *
     * {@link run_async} will return immediately if called while running.
     */
    public bool is_running { get; private set; default = false; }
    
    private ImapDB.Database db;
    private int priority;
    private File data_dir;
    
    public GC(ImapDB.Database db, int priority) {
        this.db = db;
{'fraction_non_alphanumeric': 0.052712100139082056, 'fraction_numerical': 0.008205841446453407, 'mean_word_length': 2.8392952482648157, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6325699', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1807, 'n_words': 902}