[FIX] Correct both age based as count based vacuum cleaning.
authorRonald Portier <ronald@therp.nl>
Wed, 24 Oct 2012 14:01:19 +0000 (16:01 +0200)
committerRonald Portier <ronald@therp.nl>
Wed, 24 Oct 2012 14:01:19 +0000 (16:01 +0200)
lp bug: https://launchpad.net/bugs/1009014 fixed

bzr revid: ronald@therp.nl-20121024140119-hv72ffxmf1gvf2sj

openerp/osv/orm.py

index 6916f94..ca2a4fb 100644 (file)
@@ -5065,7 +5065,7 @@ class BaseModel(object):
     def _transient_clean_rows_older_than(self, cr, seconds):
         assert self._transient, "Model %s is not transient, it cannot be vacuumed!" % self._name
         '''Never delete rows used in last 5 minutes'''
-        seconds = max(seconds, 600)
+        seconds = max(seconds, 300)
         now_str = "(now() at time zone 'UTC')"
         cr.execute(
         "SELECT id FROM " + self._table + " WHERE"
@@ -5077,13 +5077,13 @@ class BaseModel(object):
 
     def _transient_clean_old_rows(self, cr, max_count):
         # Check how many rows we have in the table
-        cr.execute(
-            "SELECT count(*) as row_count FROM %s", (self._table, ))
+        sql_statement = "SELECT count(*) as row_count FROM %s" % (self._table, )
+        cr.execute(sql_statement)
         res = cr.fetchall()
         row_count = res[0][0]
         if  row_count <= max_count:
             return  # max not reached, nothing to do
-        self._transient_clean_rows_older_than(cr, 600)
+        self._transient_clean_rows_older_than(cr, 300)
 
     def _transient_vacuum(self, cr, uid, force=False):
         """Clean the transient records.
@@ -5093,12 +5093,20 @@ class BaseModel(object):
         Actual cleaning will happen only once every "_transient_check_time" calls.
         This means this method can be called frequently called (e.g. whenever
         a new record is created).
+        Example with both max_hours and max_count active:
+        Suppose max_hours = 0.2 (e.g. 12 minutes), max_count = 20, there are 55 rows in the
+        table, 10 created/changed in the last 5 minutes, an additional 12 created/changed between
+        5 and 10 minutes ago, the rest created/changed more then 12 minutes ago.
+        - age based vacuum will leave the 22 rows created/changed in the last 12 minutes
+        - count based vacuum will wipe out another 12 rows. Not just 2, otherwise each addition
+          would immediately cause the maximum to be reached again.
+        - the 10 rows that have been created/changed the last 5 minutes will NOT be deleted
         """
         assert self._transient, "Model %s is not transient, it cannot be vacuumed!" % self._name
-        _transient_check_time = 20  # arbitrary limit on vacuum executions
+        _transient_check_time = 20 # arbitrary limit on vacuum executions
         self._transient_check_count += 1
         if ((not force)
-        and (self._transient_check_count >= _transient_check_time)):
+        and (self._transient_check_count < _transient_check_time)):
             return True  # no vacuum cleaning this time
         self._transient_check_count = 0