Changeset 5023


Ignore:
Timestamp:
Jul 27, 2009, 1:14:07 PM (14 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #1351: Add a function that removes unwanted items from the database at regular intervals

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/config/dist/base.config

    r4827 r5023  
    5454db.raw-data-types      = /raw-data-types.xml
    5555db.batch-size          = 50
     56# Number of hours between cleanup of unused items in the database
     57# Set to 0 to disable (recommended for job agents)
     58db.cleanup.interval    = 24
    5659
    5760
  • trunk/doc/src/docbook/appendix/base.config.xml

    r4827 r5023  
    234234      </listitem>
    235235    </varlistentry>
     236
     237    <varlistentry>
     238      <term><property>db.cleanup.interval</property></term>
     239      <listitem>
     240        <para>
     241        Interval in hours between database cleanups. Set this to
     242        0 to disable (recommened for job agents). The default value
     243        is 24.
     244          </para>
     245      </listitem>
     246    </varlistentry>
    236247   
    237248    <varlistentry>
  • trunk/src/core/net/sf/basedb/core/Application.java

    r4889 r5023  
    505505        long milliSeconds = 60 * 1000 * sessionCacheTimeout;
    506506        getScheduler().schedule(new SessionControlCacheCleaner(), milliSeconds, milliSeconds, false);
     507       
     508        // Adding a task that cleans the database at regular intervals
     509        long cleanupInterval = 3600 * 1000 * Config.getInt("db.cleanup.interval", 24);
     510        if (cleanupInterval > 0)
     511        {
     512          getScheduler().schedule(new DbCleaner(), cleanupInterval, cleanupInterval, false);
     513        }
    507514       
    508515        // Adding a task that cleans the static cache at regular intervals
     
    10841091  }
    10851092 
     1093  private static class DbCleaner
     1094    extends TimerTask
     1095  {
     1096    /*
     1097      From the TimerTask class
     1098      -------------------------------------------
     1099    */
     1100    public void run()
     1101    {
     1102      log.info("Cleaning database");
     1103      // Stray any-to-any links
     1104      int numDeleted = AnyToAny.deleteStrayLinks(null);
     1105      log.info("Found " + numDeleted + " stray any-to-any links");
     1106     
     1107      // Item and project keys
     1108      numDeleted = ItemKey.deleteUnusedItemKeys();
     1109      log.info("Found " + numDeleted + " unused item keys");
     1110      numDeleted = ProjectKey.deleteUnusedProjectKeys();
     1111      log.info("Found " + numDeleted + " unused project keys");
     1112     
     1113      log.info("Finished cleaning of database");
     1114    }
     1115    // -------------------------------------------
     1116  }
     1117
     1118 
    10861119  private static class SecondaryStorageControllerTask
    10871120    extends TimerTask
  • trunk/src/core/net/sf/basedb/core/ItemKey.java

    r4889 r5023  
    148148
    149149  /**
     150    @deprecated Use {@link #deleteUnusedItemKeys()} instead which returns the number of
     151      deleted keys
     152  */
     153  public static synchronized void deleteUnusedKeys()
     154    throws BaseException
     155  {
     156    deleteUnusedItemKeys();
     157  }
     158
     159 
     160  /**
    150161    Delete all keys that are currently not used by any item. This method
    151162    is intended to be executed at regular intervals by a cleanup application.
    152     @throws BaseException If a unused key could not be deleted.
    153   */
    154   public static synchronized void deleteUnusedKeys()
     163    @return The number of deleted item keys
     164    @throws BaseException If a unused key could not be deleted.
     165    @since 2.13
     166  */
     167  public static synchronized int deleteUnusedItemKeys()
    155168    throws BaseException
    156169  {
    157170    org.hibernate.Session session = null;
    158171    org.hibernate.Transaction tx = null;
     172    int numDeleted = 0;
    159173    try
    160174    {
     
    179193      {
    180194        HibernateUtil.deleteData(session, ik);
     195        numDeleted++;
    181196      }
    182197      HibernateUtil.commit(tx);
     
    191206      if (session != null) HibernateUtil.close(session);
    192207    }
     208    return numDeleted;
    193209  }
    194210 
  • trunk/src/core/net/sf/basedb/core/ProjectKey.java

    r4889 r5023  
    122122
    123123  /**
     124    @deprecated Use {@link #deleteUnusedProjectKeys()} instead which returns the number of
     125      deleted keys
     126  */
     127  public static synchronized void deleteUnusedKeys()
     128    throws BaseException
     129  {
     130    deleteUnusedProjectKeys();
     131  }
     132
     133  /**
    124134    Delete all keys that are currently not used by any item. This method
    125135    is intended to be executed at regular intervals by a cleanup application.
    126136    @throws BaseException If there is some kind of error.
    127137  */
    128   public static synchronized void deleteUnusedKeys()
     138  public static synchronized int deleteUnusedProjectKeys()
    129139    throws BaseException
    130140  {
    131141    org.hibernate.Session session = null;
    132142    org.hibernate.Transaction tx = null;
     143    int numDeleted = 0;
    133144    try
    134145    {
     
    153164      {
    154165        HibernateUtil.deleteData(session, pk);
     166        numDeleted++;
    155167      }
    156168      HibernateUtil.commit(tx);
     
    165177      if (session != null) HibernateUtil.close(session);
    166178    }
     179    return numDeleted;
    167180  }
    168181 
  • trunk/src/test/TestItemKey.java

    r4889 r5023  
    6262    TestGroup.test_delete(groupId);
    6363
    64     test_delete_unused();
     64    test_delete_unused(2);
    6565
    6666    write("++Testing item key "+(ok ? "OK" : "Failed")+"\n");
     
    123123  }
    124124 
    125   static void test_delete_unused()
     125  static void test_delete_unused(int expected)
    126126  {
    127127    try
    128128    {
    129       ItemKey.deleteUnusedKeys();
    130       write("--Delete unused item keys OK");
     129      int numDeleted = ItemKey.deleteUnusedItemKeys();
     130      if (expected >= 0 && numDeleted != expected)
     131      {
     132        throw new BaseException("Unexpected number of unused item keys: " +
     133          numDeleted + "; expected " + expected);
     134      }
     135      write("--Delete unused item keys OK (" + expected + ")");
    131136    }
    132137    catch (Throwable ex)
  • trunk/src/test/TestProjectKey.java

    r4889 r5023  
    2222*/
    2323import net.sf.basedb.core.*;
     24
    2425import java.util.EnumSet;
    2526
     
    8081//    TestProject.test_delete(projectId2);
    8182
    82     test_delete_unused();
     83    test_delete_unused(2);
    8384
    8485    write("++Testing project key "+(ok ? "OK" : "Failed")+"\n");
     
    143144  }
    144145 
    145   static void test_delete_unused()
    146   {
    147     try
    148     {
    149       ProjectKey.deleteUnusedKeys();
    150       write("--Delete unused project keys OK");
     146  static void test_delete_unused(int expected)
     147  {
     148    try
     149    {
     150      int numDeleted = ProjectKey.deleteUnusedProjectKeys();
     151      if (expected >= 0 && numDeleted != expected)
     152      {
     153        throw new BaseException("Unexpected number of unused item keys: " +
     154          numDeleted + "; expected " + expected);
     155      }
     156      write("--Delete unused project keys OK (" + expected + ")");
    151157    }
    152158    catch (Throwable ex)
Note: See TracChangeset for help on using the changeset viewer.