'trim using Cassandra\'s tombstones black magic
retrieve the WRITETIME of the last item we want to keep
then delete everything written after that
this is still pretty inefficient since it needs to retrieve
length amount of items
WARNING: since activities created using Batch share the same timestamp
trim can trash up to (batch_size - 1) more activities than requested'

def trim(self, key, length, batch_interface=None):

    query = "SELECT    WRITETIME(%s)    as    wt    FROM    %s.%s    WHERE    feed_id='%s'    ORDER    BY    activity_id    DESC    LIMIT    %s;"
    trim_col = [c for c in self.model._columns.keys() if (c not in self.model._primary_keys.keys())][0]
    parameters = (trim_col, self.model._get_keyspace(), self.column_family_name, key, (length + 1))
    results = execute((query % parameters))
    results_length = (len(results.current_rows) if hasattr(results, 'current_rows') else len(results))
    if (results_length < length):
        return
    trim_ts = ((results[(-1)]['wt'] + results[(-2)]['wt']) // 2)
    delete_query = "DELETE    FROM    %s.%s    USING    TIMESTAMP    %s    WHERE    feed_id='%s';"
    delete_params = (self.model._get_keyspace(), self.column_family_name, trim_ts, key)
    execute((delete_query % delete_params))
