def cache_last_stolen(team_id: int, current_round: int, pipe: Pipeline) -> None: """ Caches stolen flags from "flag_lifetime" rounds. Just adds commands to pipeline stack, don't forget to execute afterwards. :param team_id: attacker team id :param current_round: current round :param pipe: redis connection to add command to """ game_config = game.get_current_game_config() with utils.db_cursor() as (_, curs): curs.execute( _SELECT_LAST_STOLEN_TEAM_FLAGS_QUERY, { 'round': current_round - game_config.flag_lifetime, 'attacker_id': team_id, }, ) flags = curs.fetchall() key = CacheKeys.team_stolen_flags(team_id) pipe.delete(key) if flags: pipe.sadd(key, *(flag[0] for flag in flags))
def _populate( self, pipeline: Pipeline, iterable: Iterable[JSONTypes] = tuple(), ) -> None: encoded_values = {self._encode(value) for value in iterable} if encoded_values: # pragma: no cover pipeline.multi() pipeline.sadd(self.key, *encoded_values)
def index_text( pipe: Pipeline, cfg: CollectionConfig, doc_id: str, text: str): """Index text from text field""" tokens = tokenize(text, cfg.transl_tbl, cfg.stop_words) if len(tokens) == 0: return pipe.sadd(f'{cfg.name}/text_tokens', *tokens) for tok in tokens: index_pats(pipe, cfg, tok) pipe.sadd( key_token( cfg.name, tok), doc_id)
def cache_tasks(pipe: Pipeline) -> None: """ Put active tasks table data from database to cache. Just adds commands to pipeline stack don't forget to execute afterwards. """ with utils.db_cursor(dict_cursor=True) as (_, curs): curs.execute(models.Task.get_select_active_query()) tasks = curs.fetchall() tasks = list(models.Task.from_dict(task) for task in tasks) key = CacheKeys.tasks() pipe.delete(key) if tasks: pipe.sadd(key, *(task.to_json() for task in tasks))
def cache_teams(pipe: Pipeline) -> None: """ Put "teams" table data from database to cache. Just adds commands to pipeline stack, don't forget to execute afterwards. """ with utils.db_cursor(dict_cursor=True) as (_, curs): curs.execute(models.Team.get_select_active_query()) teams = curs.fetchall() teams = list(models.Team.from_dict(team) for team in teams) key = CacheKeys.teams() pipe.delete(key) if teams: pipe.sadd(key, *[team.to_json() for team in teams]) for team in teams: pipe.set(CacheKeys.team_by_token(team.token), team.id)
def index_pats( pipe: Pipeline, cfg: CollectionConfig, tok: str ): """indexing starting and ending patterns""" if len(tok) >= 2: pipe.sadd(f'{cfg.name}/s_pat/{tok[0]}{tok[1]}', tok) pipe.sadd(f'{cfg.name}/e_pat/{tok[-2]}{tok[-1]}', tok) if len(tok) >= 3: pipe.sadd(f'{cfg.name}/s_pat/{tok[0]}?{tok[2]}', tok) pipe.sadd(f'{cfg.name}/s_pat/?{tok[1]}{tok[2]}', tok) pipe.sadd(f'{cfg.name}/e_pat/{tok[-2]}?{tok[-1]}', tok) pipe.sadd(f'{cfg.name}/e_pat/{tok[-3]}{tok[-2]}?', tok) if len(tok) >= 4: pipe.sadd(f'{cfg.name}/s_pat/{tok[0]}??{tok[3]}', tok) pipe.sadd(f'{cfg.name}/s_pat/?{tok[1]}?{tok[3]}', tok) pipe.sadd(f'{cfg.name}/e_pat/{tok[-4]}??{tok[-1]}', tok) pipe.sadd(f'{cfg.name}/e_pat/{tok[-4]}?{tok[-2]}?', tok)