Copy rows to a set of backup tables, then purge rows.
    :param timestamp: Archive rows older than this timestamp
    :return:
429997
def _archive_jobs(timestamp):
    '''
    
    '''
    source_tables = ['jids',
                     'salt_returns',
                     'salt_events']

    with _get_serv() as cursor:
        target_tables = {}
        for table_name in source_tables:
            try:
                tmp_table_name = table_name + '_archive'
                sql = 'create table IF NOT exists {0} (LIKE {1})'.format(tmp_table_name, table_name)
                cursor.execute(sql)
                cursor.execute('COMMIT')
                target_tables[table_name] = tmp_table_name
            except psycopg2.DatabaseError as err:
                error = err.args
                sys.stderr.write(six.text_type(error))
                cursor.execute("ROLLBACK")
                raise err

        try:
            sql = 'insert into {0} select * from {1} where jid in (select distinct jid from salt_returns where alter_time < %s)'.format(target_tables['jids'], 'jids')
            cursor.execute(sql, (timestamp,))
            cursor.execute('COMMIT')
        except psycopg2.DatabaseError as err:
            error = err.args
            sys.stderr.write(six.text_type(error))
            cursor.execute("ROLLBACK")
            raise err
        except Exception as e:
            log.error(e)
            raise

        try:
            sql = 'insert into {0} select * from {1} where alter_time < %s'.format(target_tables['salt_returns'], 'salt_returns')
            cursor.execute(sql, (timestamp,))
            cursor.execute('COMMIT')
        except psycopg2.DatabaseError as err:
            error = err.args
            sys.stderr.write(six.text_type(error))
            cursor.execute("ROLLBACK")
            raise err

        try:
            sql = 'insert into {0} select * from {1} where alter_time < %s'.format(target_tables['salt_events'], 'salt_events')
            cursor.execute(sql, (timestamp,))
            cursor.execute('COMMIT')
        except psycopg2.DatabaseError as err:
            error = err.args
            sys.stderr.write(six.text_type(error))
            cursor.execute("ROLLBACK")
            raise err

    return _purge_jobs(timestamp)
953930
def purge_stale_services(argv=None):
    
    argv = argv or sys.argv
    arg_parser = argparse.ArgumentParser(
            prog=os.path.basename(argv[0]), description=(
                'doublethink-purge-stale-services: utility to periodically '
                'purge stale entries from the "services" table.'))

    arg_parser.add_argument(
            "-d", "--rethinkdb-db", required=True, dest="database",
            help="A RethinkDB database containing a 'services' table")

    arg_parser.add_argument("-s", "--rethinkdb-servers",
        metavar="SERVERS", dest="servers", default='localhost',
        help="rethinkdb servers, e.g. db0.foo.org,db0.foo.org:38015,db1.foo.org")

    arg_parser.add_argument(
        '-v', '--verbose', dest='log_level', action='store_const',
        default=logging.INFO, const=logging.DEBUG, help=(
                'verbose logging'))
    args = arg_parser.parse_args(argv[1:])
    logging.basicConfig(
            stream=sys.stdout, level=args.log_level, format=(
                '%(asctime)s %(process)d %(levelname)s %(threadName)s '
                '%(name)s.%(funcName)s(%(filename)s:%(lineno)d) %(message)s'))

    args.servers = [srv.strip() for srv in args.servers.split(",")]

    rethinker = doublethink.Rethinker(servers=args.servers, db=args.database)
    registry = doublethink.services.ServiceRegistry(rethinker)
    registry.purge_stale_services()
    return 0
314068
def clean_old_jobs():
    '''
    
    '''
    if __opts__.get('keep_jobs', False) and int(__opts__.get('keep_jobs', 0)) > 0:
        try:
            with _get_serv() as cur:
                sql = 'select date_sub(now(), interval {0} hour) as stamp;'.format(__opts__['keep_jobs'])
                cur.execute(sql)
                rows = cur.fetchall()
                stamp = rows[0][0]

            if __opts__.get('archive_jobs', False):
                _archive_jobs(stamp)
            else:
                _purge_jobs(stamp)
        except MySQLdb.Error as e:
            log.error('Mysql returner was unable to get timestamp for purge/archive of jobs')
            log.error(six.text_type(e))
            raise salt.exceptions.SaltRunnerError(six.text_type(e))
255555
def cleanup_full_hashes(self, keep_expired_for=(60 * 60 * 12)):
        """"""
        q = '''DELETE FROM full_hash WHERE expires_at < datetime(current_timestamp, '-{} SECONDS')
        '''
        log.info('Cleaning up full_hash entries expired more than {} seconds ago.'.format(keep_expired_for))
        with self.get_cursor() as dbc:
            dbc.execute(q.format(int(keep_expired_for)))
343292
def Copy(self,
           old_urn,
           new_urn,
           age=NEWEST_TIME,
           limit=None,
           update_timestamps=False):
    """"""
    new_urn = rdfvalue.RDFURN(new_urn)

    if update_timestamps and age != NEWEST_TIME:
      raise ValueError(
          "Can't update timestamps unless reading the latest version.")

    values = {}
    for predicate, value, ts in data_store.DB.ResolvePrefix(
        old_urn,
        AFF4_PREFIXES,
        timestamp=self.ParseAgeSpecification(age),
        limit=limit):
      if update_timestamps:
        values.setdefault(predicate, []).append((value, None))
      else:
        values.setdefault(predicate, []).append((value, ts))

    if values:
      with data_store.DB.GetMutationPool() as pool:
        pool.MultiSet(new_urn, values, replace=False)
        self._UpdateChildIndex(new_urn, pool)
708865
def archive_insert_data(self, data_dump):
        '''
        
        '''
        with self.session as session:
            try:
                data = [self.tables.archive(**entry) for entry in data_dump]

                session.add_all(data)
                session.commit()
            except SQLAlchemyError as exc:
                session.rollback()
                print_exc()
                raise IOError(exc)
507117
def reap_expired_sessions
      @timestamps.each do |session_id,stamp|
        delete_session(session_id) if (stamp + @session_ttl) < Time.now 
      end
      GC.start
    end
901877
private function cleanupTimestamp($timestamp): void
    {
        $key = sprintf('%s:%s', self::AT_QUEUE_NAME, $timestamp);

        if ($this->client->llen($key)) {
            $this->client->del([$key]);
            $this->client->zrem(self::AT_QUEUE_NAME, $timestamp);
        }
    }
1287899
private long updateBaseTime(final long timestamp) {
    // We force the starting timestamp to be on a MAX_TIMESPAN boundary
    // so that all TSDs create rows with the same base time. Otherwise
    // we'd need to coordinate TSDs to avoid creating rows that cover
    // overlapping time periods.
    final long base_time = timestamp - (timestamp % Const.MAX_TIMESPAN);
    // Clone the row key since we're going to change it. We must clone it
    // because the HBase client may still hold a reference to it in its
    // internal datastructures.
    row = Arrays.copyOf(row, row.length);
    Bytes.setInt(row, (int) base_time, Const.SALT_WIDTH() + tsdb.metrics.width());
    RowKey.prefixKeyWithSalt(row); // in case the timestamp will be involved in
                                   // salting later
    tsdb.scheduleForCompaction(row, (int) base_time);
    return base_time;
  }
239731
private void expireOrphanSessions(long timestamp) {
    // Iterate through registered sessions.
    for (RaftSession session : raft.getSessions().getSessions()) {
      if (session.getService().deleted() && session.isTimedOut(timestamp)) {
        logger.debug("Orphaned session expired in {} milliseconds: {}", timestamp - session.getLastUpdated(), session);
        session = raft.getSessions().removeSession(session.sessionId());
        if (session != null) {
          session.expire();
        }
      }
    }
  }
1156717
def polymorphic_update_timestamp(session, flush_context, instances):
    
    for obj in session.dirty:
        if not isinstance(obj, Entity):
            continue
        state = sa.inspect(obj)
        history = state.attrs["updated_at"].history
        if not any((history.added, history.deleted)):
            obj.updated_at = datetime.utcnow()
236490
private void commitDeletes(DBTransaction dbTran, long timestamp) {
        Map<String, Set<ByteBuffer>> rowDeleteMap = CassandraTransaction.getRowDeletionMap(dbTran);
        if (rowDeleteMap.size() == 0) {
            return;
        }
        
        // Iterate through all ColumnFamilies
        for (String colFamName : rowDeleteMap.keySet()) {
            // Delete each row in this key set.
            Set<ByteBuffer> rowKeySet = rowDeleteMap.get(colFamName);
            for (ByteBuffer rowKey : rowKeySet) {
                removeRow(timestamp, rowKey, new ColumnPath(colFamName));
            }
        }
    }
681567
public void updateTable(boolean dedup, String procedure, long partition, long timestamp,
            long invocations, long minOUT, long maxOUT, long avgOUT)
    {
        ProcOutputRow in = new ProcOutputRow(procedure, partition, timestamp,
            invocations, minOUT, maxOUT, avgOUT);
        ProcOutputRow exists = m_rowsTable.ceiling(in);
        if (exists != null && in.procedure.equals(exists.procedure)) {
            exists.updateWith(dedup, in);
        } else {
            m_rowsTable.add(in);
        }
    }
1303580
def upsert(self, doc, namespace, timestamp):
        
        database, coll = self._db_and_collection(namespace)

        meta_collection_name = self._get_meta_collection(namespace)

        self.meta_database[meta_collection_name].replace_one(
            {self.id_field: doc["_id"], "ns": namespace},
            {self.id_field: doc["_id"], "_ts": timestamp, "ns": namespace},
            upsert=True,
        )

        self.mongo[database][coll].replace_one({"_id": doc["_id"]}, doc, upsert=True)
1137476
def bulk_upsert(self, docs, namespace, timestamp):
        
        for doc in docs:
            self.upsert(doc, namespace, timestamp)
66698
public void updateTable(boolean dedup, String procedure, long partition, long timestamp,
            long invocations, long minIN, long maxIN, long avgIN)
    {
        ProcInputRow in = new ProcInputRow(procedure, partition, timestamp,
            invocations, minIN, maxIN, avgIN);
        ProcInputRow exists = m_rowsTable.ceiling(in);
        if (exists != null && in.procedure.equals(exists.procedure)) {
            exists.updateWith(dedup, in);
        } else {
            m_rowsTable.add(in);
        }
    }
1042329
def copy(self, tx_ins=None, tx_outs=None, lock_time=None,
             expiry_height=None, tx_joinsplits=None, joinsplit_pubkey=None,
             joinsplit_sig=None):
        '''
        
        '''
        return OverwinterTx(
            tx_ins=tx_ins if tx_ins is not None else self.tx_ins,
            tx_outs=tx_outs if tx_outs is not None else self.tx_outs,
            lock_time=(lock_time if lock_time is not None
                       else self.lock_time),
            expiry_height=(expiry_height if expiry_height is not None
                           else self.expiry_height),
            tx_joinsplits=(tx_joinsplits if tx_joinsplits is not None
                           else self.tx_joinsplits),
            joinsplit_pubkey=(joinsplit_pubkey if joinsplit_pubkey is not None
                              else self.joinsplit_pubkey),
            joinsplit_sig=(joinsplit_sig if joinsplit_sig is not None
                           else self.joinsplit_sig))