Skip to content

Commit 240804e

Browse files
committed
chore(memorystore-valkey-leaderboard): lint
1 parent dd5e8cc commit 240804e

File tree

15 files changed

+910
-901
lines changed

15 files changed

+910
-901
lines changed

memorystore/valkey/leaderboard/app/src/main/java/app/DataController.java

Lines changed: 132 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -16,155 +16,155 @@
1616

1717
package app;
1818

19+
import java.util.ArrayList;
20+
import java.util.List;
1921
import org.springframework.stereotype.Controller;
2022
import redis.clients.jedis.Jedis;
2123
import redis.clients.jedis.resps.Tuple;
22-
import java.util.ArrayList;
23-
import java.util.List;
2424

2525
@Controller
2626
public class DataController {
2727

28-
/** Repository for persisting leaderboard entries. */
29-
private final LeaderboardRepository leaderboardRepository;
30-
31-
/** Redis client for caching leaderboard data. */
32-
private final Jedis jedis;
33-
34-
/**
35-
* Constructs a new DataController.
36-
*
37-
* @param redisClient Redis client for caching
38-
* @param repository Repository for persistence
39-
*/
40-
public DataController(final Jedis redisClient,
41-
final LeaderboardRepository repository) {
42-
this.leaderboardRepository = repository;
43-
this.jedis = redisClient;
28+
/** Repository for persisting leaderboard entries. */
29+
private final LeaderboardRepository leaderboardRepository;
30+
31+
/** Redis client for caching leaderboard data. */
32+
private final Jedis jedis;
33+
34+
/**
35+
* Constructs a new DataController.
36+
*
37+
* @param redisClient Redis client for caching
38+
* @param repository Repository for persistence
39+
*/
40+
public DataController(final Jedis redisClient,
41+
final LeaderboardRepository repository) {
42+
this.leaderboardRepository = repository;
43+
this.jedis = redisClient;
44+
}
45+
46+
/**
47+
* Get the leaderboard entries starting from the given position.
48+
*
49+
* @param position The starting position of the entries to search.
50+
* @param orderBy The order of the entries.
51+
* @param pageSize The number of entries to return.
52+
* @param username The username to check the rank of.
53+
* @return The leaderboard entries.
54+
*/
55+
public LeaderboardResponse getLeaderboard(
56+
final long position,
57+
final OrderByType orderBy,
58+
final long pageSize,
59+
final String username) {
60+
String cacheKey = Global.LEADERBOARD_ENTRIES_KEY;
61+
long maxPosition = position + pageSize - 1;
62+
63+
// Initialize the cache if it's empty
64+
boolean cacheUpdated = this.initializeCache();
65+
66+
// Set the cache status for the front end
67+
int cacheStatus = cacheUpdated
68+
? FromCacheType.FROM_DB.getValue()
69+
: FromCacheType.FULL_CACHE.getValue();
70+
71+
// If we have a username, search for the user's rank
72+
if (username != null) {
73+
Long userRank = jedis.zrevrank(cacheKey, username);
74+
if (userRank != null) {
75+
long pos = userRank;
76+
long maxPos = userRank + pageSize - 1;
77+
78+
return new LeaderboardResponse(
79+
getEntries(cacheKey, pos, maxPos, true),
80+
cacheStatus);
81+
}
4482
}
4583

46-
/**
47-
* Get the leaderboard entries starting from the given position.
48-
*
49-
* @param position The starting position of the entries to search.
50-
* @param orderBy The order of the entries.
51-
* @param pageSize The number of entries to return.
52-
* @param username The username to check the rank of.
53-
* @return The leaderboard entries.
54-
*/
55-
public LeaderboardResponse getLeaderboard(
56-
final long position,
57-
final OrderByType orderBy,
58-
final long pageSize,
59-
final String username) {
60-
String cacheKey = Global.LEADERBOARD_ENTRIES_KEY;
61-
long maxPosition = position + pageSize - 1;
62-
63-
// Initialize the cache if it's empty
64-
boolean cacheUpdated = this.initializeCache();
65-
66-
// Set the cache status for the front end
67-
int cacheStatus = cacheUpdated
68-
? FromCacheType.FROM_DB.getValue()
69-
: FromCacheType.FULL_CACHE.getValue();
70-
71-
// If we have a username, search for the user's rank
72-
if (username != null) {
73-
Long userRank = jedis.zrevrank(cacheKey, username);
74-
if (userRank != null) {
75-
long pos = userRank;
76-
long maxPos = userRank + pageSize - 1;
77-
78-
return new LeaderboardResponse(
79-
getEntries(cacheKey, pos, maxPos, true),
80-
cacheStatus);
81-
}
82-
}
83-
84-
// Get the leaderboard entries depending on the order
85-
List<LeaderboardEntry> leaderboardList = getEntries(
86-
cacheKey, position, maxPosition,
87-
orderBy == OrderByType.HIGH_TO_LOW);
88-
89-
return new LeaderboardResponse(leaderboardList, cacheStatus);
84+
// Get the leaderboard entries depending on the order
85+
List<LeaderboardEntry> leaderboardList = getEntries(
86+
cacheKey, position, maxPosition,
87+
orderBy == OrderByType.HIGH_TO_LOW);
88+
89+
return new LeaderboardResponse(leaderboardList, cacheStatus);
90+
}
91+
92+
private List<LeaderboardEntry> getEntries(
93+
final String cacheKey,
94+
final long position,
95+
final long maxPosition,
96+
final boolean isDescending) {
97+
// Define an object
98+
List<Tuple> entries = new ArrayList<>();
99+
100+
// Use zrevrangeWithScores to get the entries in descending order
101+
if (isDescending) {
102+
entries = new ArrayList<>(
103+
jedis.zrevrangeWithScores(cacheKey, position, maxPosition));
90104
}
91105

92-
private List<LeaderboardEntry> getEntries(
93-
final String cacheKey,
94-
final long position,
95-
final long maxPosition,
96-
final boolean isDescending) {
97-
// Define an object
98-
List<Tuple> entries = new ArrayList<>();
99-
100-
// Use zrevrangeWithScores to get the entries in descending order
101-
if (isDescending) {
102-
entries = new ArrayList<>(
103-
jedis.zrevrangeWithScores(cacheKey, position, maxPosition));
104-
}
105-
106-
// If zrangeWithScores is used, the entries are in ascending order
107-
if (!isDescending) {
108-
entries = new ArrayList<>(
109-
jedis.zrangeWithScores(cacheKey, position, maxPosition));
110-
}
111-
112-
List<LeaderboardEntry> newEntries = new ArrayList<>();
113-
for (int i = 0; i < entries.size(); i++) {
114-
Tuple e = entries.get(i);
115-
116-
// Calculate overall position
117-
long overallPosition = position + i;
118-
if (!isDescending) {
119-
overallPosition = jedis.zcard(cacheKey) - overallPosition - 1;
120-
}
121-
122-
newEntries.add(
123-
new LeaderboardEntry(
124-
e.getElement(), e.getScore(), overallPosition
125-
)
126-
);
127-
}
128-
129-
return newEntries;
106+
// If zrangeWithScores is used, the entries are in ascending order
107+
if (!isDescending) {
108+
entries = new ArrayList<>(
109+
jedis.zrangeWithScores(cacheKey, position, maxPosition));
130110
}
131111

132-
private boolean initializeCache() {
133-
if (this.jedis.zcard(Global.LEADERBOARD_ENTRIES_KEY) > 0) {
134-
return false;
135-
}
112+
List<LeaderboardEntry> newEntries = new ArrayList<>();
113+
for (int i = 0; i < entries.size(); i++) {
114+
Tuple e = entries.get(i);
115+
116+
// Calculate overall position
117+
long overallPosition = position + i;
118+
if (!isDescending) {
119+
overallPosition = jedis.zcard(cacheKey) - overallPosition - 1;
120+
}
121+
122+
newEntries.add(
123+
new LeaderboardEntry(
124+
e.getElement(), e.getScore(), overallPosition
125+
)
126+
);
127+
}
128+
129+
return newEntries;
130+
}
136131

137-
List<LeaderboardEntry> entries = this.leaderboardRepository
138-
.getEntries();
132+
private boolean initializeCache() {
133+
if (this.jedis.zcard(Global.LEADERBOARD_ENTRIES_KEY) > 0) {
134+
return false;
135+
}
139136

140-
if (!entries.isEmpty()) {
141-
for (LeaderboardEntry entry : entries) {
142-
this.jedis.zadd(
143-
Global.LEADERBOARD_ENTRIES_KEY,
144-
entry.getScore(),
145-
entry.getUsername());
146-
}
147-
}
137+
List<LeaderboardEntry> entries = this.leaderboardRepository
138+
.getEntries();
148139

149-
return true;
140+
if (!entries.isEmpty()) {
141+
for (LeaderboardEntry entry : entries) {
142+
this.jedis.zadd(
143+
Global.LEADERBOARD_ENTRIES_KEY,
144+
entry.getScore(),
145+
entry.getUsername());
146+
}
150147
}
151148

152-
/**
153-
* Creates or updates a leaderboard entry with the given username and score.
154-
* Only updates the entry if the new score is higher than the current score.
155-
*
156-
* @param username The username of the entry
157-
* @param score The score to set
158-
*/
159-
public void createOrUpdate(final String username, final Double score) {
160-
// See if score is higher than the current score
161-
Double currentScore = this.jedis.zscore(
162-
Global.LEADERBOARD_ENTRIES_KEY, username);
163-
if (currentScore != null && currentScore >= score) {
164-
return;
165-
}
166-
167-
this.leaderboardRepository.update(username, score);
168-
this.jedis.zadd(Global.LEADERBOARD_ENTRIES_KEY, score, username);
149+
return true;
150+
}
151+
152+
/**
153+
* Creates or updates a leaderboard entry with the given username and score.
154+
* Only updates the entry if the new score is higher than the current score.
155+
*
156+
* @param username The username of the entry
157+
* @param score The score to set
158+
*/
159+
public void createOrUpdate(final String username, final Double score) {
160+
// See if score is higher than the current score
161+
Double currentScore = this.jedis.zscore(
162+
Global.LEADERBOARD_ENTRIES_KEY, username);
163+
if (currentScore != null && currentScore >= score) {
164+
return;
169165
}
166+
167+
this.leaderboardRepository.update(username, score);
168+
this.jedis.zadd(Global.LEADERBOARD_ENTRIES_KEY, score, username);
169+
}
170170
}

memorystore/valkey/leaderboard/app/src/main/java/app/FromCacheType.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,29 @@
1818

1919
/** Cache state indicator for leaderboard data. */
2020
public enum FromCacheType {
21-
/** Data freshly loaded from database. */
22-
FROM_DB(0),
23-
/** Data served from cache. */
24-
FULL_CACHE(1);
21+
/** Data freshly loaded from database. */
22+
FROM_DB(0),
23+
/** Data served from cache. */
24+
FULL_CACHE(1);
2525

26-
/** Numeric value for the cache state. */
27-
private final int value;
26+
/** Numeric value for the cache state. */
27+
private final int value;
2828

29-
/**
30-
* Constructor.
31-
*
32-
* @param valueParam Numeric value for this state
33-
*/
34-
FromCacheType(final int valueParam) {
35-
this.value = valueParam;
36-
}
29+
/**
30+
* Constructor.
31+
*
32+
* @param valueParam Numeric value for this state
33+
*/
34+
FromCacheType(final int valueParam) {
35+
this.value = valueParam;
36+
}
3737

38-
/**
39-
* Gets the numeric value.
40-
*
41-
* @return The value for this state
42-
*/
43-
public int getValue() {
44-
return value;
45-
}
38+
/**
39+
* Gets the numeric value.
40+
*
41+
* @return The value for this state
42+
*/
43+
public int getValue() {
44+
return value;
45+
}
4646
}

memorystore/valkey/leaderboard/app/src/main/java/app/Global.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
public final class Global {
2020

21-
/** Private constructor to prevent instantiation. */
22-
private Global() {
23-
throw new UnsupportedOperationException();
24-
}
21+
/** Private constructor to prevent instantiation. */
22+
private Global() {
23+
throw new UnsupportedOperationException();
24+
}
2525

26-
/** Key for accessing leaderboard entries in responses. */
27-
public static final String LEADERBOARD_ENTRIES_KEY = "entries";
26+
/** Key for accessing leaderboard entries in responses. */
27+
public static final String LEADERBOARD_ENTRIES_KEY = "entries";
2828
}

memorystore/valkey/leaderboard/app/src/main/java/app/HomeController.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
@Controller
2424
public class HomeController {
2525

26-
/**
27-
* Handles requests to the home page.
28-
*
29-
* @param model view model
30-
* @return view name for home page
31-
*/
32-
@GetMapping("/")
33-
public String home(final Model model) {
34-
return "index";
35-
}
26+
/**
27+
* Handles requests to the home page.
28+
*
29+
* @param model view model
30+
* @return view name for home page
31+
*/
32+
@GetMapping("/")
33+
public String home(final Model model) {
34+
return "index";
35+
}
3636
}

0 commit comments

Comments
 (0)