'Trim metrics that are older than settings.FULL_DURATION and
purge old metrics.'

def vacuum(self, i, namespace, duration):

    begin = time()
    unique_metrics = list(self.redis_conn.smembers((namespace + 'unique_metrics')))
    keys_per_processor = (len(unique_metrics) / settings.ROOMBA_PROCESSES)
    assigned_max = (i * keys_per_processor)
    assigned_min = (assigned_max - keys_per_processor)
    assigned_keys = range(assigned_min, assigned_max)
    assigned_metrics = [unique_metrics[index] for index in assigned_keys]
    euthanized = 0
    blocked = 0
    for i in xrange(len(assigned_metrics)):
        self.check_if_parent_is_alive()
        pipe = self.redis_conn.pipeline()
        now = time()
        key = assigned_metrics[i]
        try:
            pipe.watch(key)
            raw_series = pipe.get(key)
            unpacker = Unpacker(use_list=False)
            unpacker.feed(raw_series)
            timeseries = sorted([unpacked for unpacked in unpacker])
            pipe.multi()
            try:
                if (not isinstance(timeseries[0], TupleType)):
                    if (timeseries[0] < (now - duration)):
                        pipe.delete(key)
                        pipe.srem((namespace + 'unique_metrics'), key)
                        pipe.execute()
                        euthanized += 1
                    continue
            except IndexError:
                continue
            if (timeseries[(-1)][0] < (now - duration)):
                pipe.delete(key)
                pipe.srem((namespace + 'unique_metrics'), key)
                pipe.execute()
                euthanized += 1
                continue
            temp = set()
            temp_add = temp.add
            delta = (now - duration)
            trimmed = [tuple for tuple in timeseries if ((tuple[0] > delta) and (tuple[0] not in temp) and (not temp_add(tuple[0])))]
            if (len(trimmed) > 0):
                btrimmed = packb(trimmed)
                if (len(trimmed) <= 15):
                    value = btrimmed[1:]
                elif (len(trimmed) <= 65535):
                    value = btrimmed[3:]
                else:
                    value = btrimmed[5:]
                pipe.set(key, value)
            else:
                pipe.delete(key)
                pipe.srem((namespace + 'unique_metrics'), key)
                euthanized += 1
            pipe.execute()
        except WatchError:
            blocked += 1
            assigned_metrics.append(key)
        except Exception as e:
            pipe.delete(key)
            pipe.srem((namespace + 'unique_metrics'), key)
            pipe.execute()
            euthanized += 1
            logger.info(e)
            logger.info(('Euthanizing    ' + key))
        finally:
            pipe.reset()
    logger.info(('operated    on    %s    in    %f    seconds' % (namespace, (time() - begin))))
    logger.info(('%s    keyspace    is    %d' % (namespace, (len(assigned_metrics) - euthanized))))
    logger.info(('blocked    %d    times' % blocked))
    logger.info(('euthanized    %d    geriatric    keys' % euthanized))
    if ((time() - begin) < 30):
        logger.info('sleeping    due    to    low    run    time...')
        sleep(10)
