|
| 1 | +package io.f1.backend.domain.stat.dao; |
| 2 | + |
| 3 | +import static java.util.Objects.requireNonNull; |
| 4 | + |
| 5 | +import io.f1.backend.domain.stat.dto.StatPageResponse; |
| 6 | +import io.f1.backend.domain.stat.dto.StatResponse; |
| 7 | +import io.f1.backend.domain.stat.dto.StatWithNickname; |
| 8 | +import io.f1.backend.domain.stat.dto.StatWithNicknameAndUserId; |
| 9 | +import jakarta.annotation.PostConstruct; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Objects; |
| 13 | +import java.util.Optional; |
| 14 | +import java.util.Set; |
| 15 | +import java.util.concurrent.atomic.AtomicInteger; |
| 16 | +import lombok.RequiredArgsConstructor; |
| 17 | +import lombok.extern.slf4j.Slf4j; |
| 18 | +import org.springframework.beans.factory.annotation.Autowired; |
| 19 | +import org.springframework.data.domain.Page; |
| 20 | +import org.springframework.data.domain.PageRequest; |
| 21 | +import org.springframework.data.domain.Pageable; |
| 22 | +import org.springframework.data.domain.Sort; |
| 23 | +import org.springframework.data.domain.Sort.Direction; |
| 24 | +import org.springframework.data.redis.core.HashOperations; |
| 25 | +import org.springframework.data.redis.core.RedisTemplate; |
| 26 | +import org.springframework.data.redis.core.ValueOperations; |
| 27 | +import org.springframework.data.redis.core.ZSetOperations; |
| 28 | +import org.springframework.data.redis.core.ZSetOperations.TypedTuple; |
| 29 | +import org.springframework.stereotype.Repository; |
| 30 | + |
| 31 | +@Slf4j |
| 32 | +@Repository |
| 33 | +@RequiredArgsConstructor |
| 34 | +public class StatRedisRepository { |
| 35 | + private static final String STAT_RANK = "stat:rank"; |
| 36 | + private static final String STAT_USER = "stat:user:%d"; |
| 37 | + private static final String STAT_NICKNAME = "stat:%s"; |
| 38 | + |
| 39 | + private final RedisTemplate<String, Object> redisTemplate; |
| 40 | + private ZSetOperations<String, Object> zSetOps; |
| 41 | + private HashOperations<String, Object, Object> hashOps; |
| 42 | + private ValueOperations<String, Object> valueOps; |
| 43 | + |
| 44 | + public void setup() { |
| 45 | + zSetOps = redisTemplate.opsForZSet(); |
| 46 | + hashOps = redisTemplate.opsForHash(); |
| 47 | + valueOps = redisTemplate.opsForValue(); |
| 48 | + } |
| 49 | + |
| 50 | + public void initialize(StatWithNicknameAndUserId stat) { |
| 51 | + String statUserKey = getStatUserKey(stat.userId()); |
| 52 | + String statNicknameKey = getStatNickname(stat.nickname()); |
| 53 | + |
| 54 | + // stat:user:{id} |
| 55 | + hashOps.put(statUserKey, "nickname", stat.nickname()); |
| 56 | + hashOps.put(statUserKey, "totalGames", stat.totalGames()); |
| 57 | + hashOps.put(statUserKey, "winningGames", stat.winningGames()); |
| 58 | + |
| 59 | + // stat:rank |
| 60 | + zSetOps.add(STAT_RANK, statUserKey, stat.score()); |
| 61 | + |
| 62 | + // stat:{nickname} |
| 63 | + valueOps.set(statNicknameKey, statUserKey); |
| 64 | + } |
| 65 | + |
| 66 | + public void removeUser(long userId) { |
| 67 | + String statUserKey = getStatUserKey(userId); |
| 68 | + String nickname = (String) hashOps.get(statUserKey,"nickname"); |
| 69 | + redisTemplate.delete(getStatUserKey(userId)); |
| 70 | + valueOps.getAndDelete(getStatNickname(nickname)); |
| 71 | + zSetOps.remove(STAT_RANK, statUserKey); |
| 72 | + } |
| 73 | + |
| 74 | + public StatPageResponse findAllStatsWithUser(Pageable pageable) { |
| 75 | + int page = pageable.getPageNumber(); |
| 76 | + int size = pageable.getPageSize(); |
| 77 | + long startIdx = (long) page * size; |
| 78 | + |
| 79 | + long totalCount = getTotalMemberCount(); |
| 80 | + int totalPages = (int) Math.ceil((double) totalCount / size); |
| 81 | + |
| 82 | + Set<TypedTuple<Object>> rankSet = getRankSet(startIdx, size); |
| 83 | + |
| 84 | + AtomicInteger rankCounter = new AtomicInteger((int) startIdx + 1); |
| 85 | + List<StatResponse> ranks = rankSet.stream() |
| 86 | + .map(rank -> convertToStatResponse(rank, rankCounter.getAndIncrement())) |
| 87 | + .toList(); |
| 88 | + |
| 89 | + return new StatPageResponse(totalPages, page + 1, rankSet.size(), ranks); |
| 90 | + } |
| 91 | + |
| 92 | + public Pageable getPageableFromNickname(String nickname, int pageSize) { |
| 93 | + String statUserKey = getStatUserKeyFromNickname(nickname); |
| 94 | + long rowNum = requireNonNull(zSetOps.reverseRank(STAT_RANK, statUserKey)) + 1; |
| 95 | + int pageNumber = rowNum > 0 ? (int) (rowNum / pageSize) : 0; |
| 96 | + return PageRequest.of(pageNumber, pageSize, Sort.by(Direction.DESC, "score")); |
| 97 | + } |
| 98 | + |
| 99 | + private long getTotalMemberCount() { |
| 100 | + long count = requireNonNull(zSetOps.zCard(STAT_RANK)); |
| 101 | + if (count == 0) { |
| 102 | + throw new NullPointerException("No member found in redis"); |
| 103 | + } |
| 104 | + return count; |
| 105 | + } |
| 106 | + |
| 107 | + private Set<TypedTuple<Object>> getRankSet(long startIdx, int size) { |
| 108 | + Set<TypedTuple<Object>> result = zSetOps.reverseRangeWithScores(STAT_RANK, startIdx, startIdx + size - 1); |
| 109 | + return requireNonNull(result); |
| 110 | + } |
| 111 | + |
| 112 | + public void updateRank(long userId, boolean win, int deltaScore) { |
| 113 | + String statUserKey = getStatUserKey(userId); |
| 114 | + |
| 115 | + zSetOps.incrementScore(STAT_RANK, getStatUserKey(userId), deltaScore); |
| 116 | + hashOps.increment(statUserKey, "totalGame", 1); |
| 117 | + if (win) { |
| 118 | + hashOps.increment(statUserKey, "winningGame", 1); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public void updateNickname(long userId, String newNickname) { |
| 123 | + String statUserKey = getStatUserKey(userId); |
| 124 | + valueOps.set(getStatNickname(newNickname), statUserKey); |
| 125 | + |
| 126 | + String oldNickname = (String) hashOps.get(statUserKey, "nickname"); |
| 127 | + valueOps.getAndDelete(getStatNickname(oldNickname)); |
| 128 | + |
| 129 | + hashOps.put(statUserKey, "nickname", newNickname); |
| 130 | + } |
| 131 | + |
| 132 | + private StatResponse convertToStatResponse(TypedTuple<Object> rank, int rankValue) { |
| 133 | + String statUserKey = (String) requireNonNull(rank.getValue()); |
| 134 | + Map<Object, Object> statUserMap = hashOps.entries(statUserKey); |
| 135 | + |
| 136 | + return new StatResponse( |
| 137 | + rankValue, |
| 138 | + (String) statUserMap.get("nickname"), |
| 139 | + (long) statUserMap.get("totalGames"), |
| 140 | + (long) statUserMap.get("winningGames"), |
| 141 | + requireNonNull(rank.getScore()).longValue() |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + private static String getStatUserKey(long userId) { |
| 146 | + return String.format(STAT_USER, userId); |
| 147 | + } |
| 148 | + |
| 149 | + private static String getStatNickname(String nickname) { |
| 150 | + return String.format(STAT_NICKNAME, nickname); |
| 151 | + } |
| 152 | + |
| 153 | + private String getStatUserKeyFromNickname(String nickname) { |
| 154 | + return (String) requireNonNull(valueOps.get(getStatNickname(nickname))); |
| 155 | + } |
| 156 | +} |
0 commit comments