Skip to content

Commit bbc7cfc

Browse files
committed
feat: refactor type to use enum
1 parent ff6eae9 commit bbc7cfc

File tree

3 files changed

+34
-43
lines changed

3 files changed

+34
-43
lines changed

graal/src/main/java/io/sixwaaaay/sharingcomment/controller/CommentController.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import java.time.ZoneOffset;
3131
import java.util.Optional;
3232

33-
import static io.sixwaaaay.sharingcomment.util.ShardEnum.transformId;
34-
3533
@RestController
3634
@RequestMapping("/comments")
3735
@AllArgsConstructor
@@ -48,13 +46,13 @@ public class CommentController {
4846
*/
4947
@GetMapping("/main")
5048
public CommentResult getMainCommentList(
51-
@RequestParam("type") String type,
49+
@RequestParam("type") ShardEnum.Shard type,
5250
@RequestParam("belong_to") Long belongTo,
5351
@RequestParam(value = "page") Optional<Long> id,
5452
@RequestParam(value = "size", defaultValue = "10") Integer size
5553
) {
5654
var userId = Principal.currentUserId();
57-
belongTo = transformId(belongTo, ShardEnum.getShard(type));
55+
belongTo = type.transform(belongTo);
5856
return commentService.getMainCommentList(belongTo, id.orElse(Long.MAX_VALUE), size, userId);
5957
}
6058

@@ -66,14 +64,14 @@ public CommentResult getMainCommentList(
6664
*/
6765
@GetMapping("/reply")
6866
public ReplyResult getReplyCommentList(
69-
@RequestParam("type") String type,
67+
@RequestParam("type") ShardEnum.Shard type,
7068
@RequestParam("belong_to") Long belongTo,
7169
@RequestParam("reply_to") Long replyTo,
7270
@RequestParam(value = "page", defaultValue = "0") long id,
7371
@RequestParam(value = "size", defaultValue = "10") Integer size
7472
) {
7573
var userId = Principal.currentUserId();
76-
belongTo = transformId(belongTo, ShardEnum.getShard(type));
74+
belongTo = type.transform(belongTo);
7775
return commentService.getReplyCommentList(belongTo, replyTo, id, size, userId);
7876
}
7977

@@ -83,11 +81,14 @@ public ReplyResult getReplyCommentList(
8381
* @return the created comment
8482
*/
8583
@PostMapping
86-
public Comment createComment(@Valid @RequestBody CommentRequest request) {
84+
public Comment createComment(
85+
@Valid @RequestBody CommentRequest request
86+
) {
8787
var comment = new Comment();
8888
var id = Principal.currentUserId();
8989
comment.setUserId(id);
90-
comment.setBelongTo(transformId(request.getBelongTo(), ShardEnum.getShard(request.getType())));
90+
var belongTo = request.getType().transform(request.getBelongTo());
91+
comment.setBelongTo(belongTo);
9192
comment.setContent(request.getContent());
9293
comment.setReferTo(request.getReferTo());
9394
comment.setReplyTo(request.getReplyTo());

graal/src/main/java/io/sixwaaaay/sharingcomment/request/CommentRequest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package io.sixwaaaay.sharingcomment.request;
1515

1616
import com.fasterxml.jackson.annotation.JsonProperty;
17+
import io.sixwaaaay.sharingcomment.util.ShardEnum;
1718
import jakarta.validation.Valid;
1819
import jakarta.validation.constraints.AssertTrue;
1920
import jakarta.validation.constraints.NotNull;
@@ -34,7 +35,7 @@ public class CommentRequest {
3435
*/
3536
@NotNull
3637
@JsonProperty("type")
37-
private String type;
38+
private ShardEnum.Shard type;
3839

3940
/**
4041
* The content of the comment.
@@ -73,9 +74,4 @@ public class CommentRequest {
7374
public boolean isValid() {
7475
return (replyTo == null && referTo == null) || (replyTo != null && referTo != null);
7576
}
76-
77-
@AssertTrue(message = "type must be one of 'chore', 'default', 'video', 'post', 'music'")
78-
public boolean isValidType() {
79-
return type.equals("chore") || type.equals("default") || type.equals("video") || type.equals("post") || type.equals("music");
80-
}
8177
}

graal/src/main/java/io/sixwaaaay/sharingcomment/util/ShardEnum.java

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,46 @@
1313

1414
package io.sixwaaaay.sharingcomment.util;
1515

16-
import lombok.Getter;
16+
import com.fasterxml.jackson.annotation.JsonProperty;
1717

1818
public class ShardEnum {
19-
// 最高位不使用, 次高位(62位) 到第60位(1位) 用于存储分片ID
20-
// 共3位,八个分片
19+
/*
20+
the highest bit is not used,
21+
the second-highest bit (62) to the 60th bit is used to store the shard ID.
22+
in total, 3 bits, so there are at most 8 shards.
23+
*/
2124
public static final long Chore = 0L; // aka 0b000L << 60;
2225
public static final long SHARD_ID_0 = 0b001L << 60;
2326
public static final long SHARD_ID_1 = 0b010L << 60;
2427
public static final long SHARD_ID_2 = 0b011L << 60;
2528
public static final long SHARD_ID_3 = 0b100L << 60;
2629

27-
public static Shard getShard(String shard) {
28-
return switch (shard) {
29-
case "chore" -> Shard.ChoreShard;
30-
case "default" -> Shard.SHARD_0;
31-
case "video" -> Shard.Video;
32-
case "post" -> Shard.post;
33-
case "music" -> Shard.music;
34-
default -> throw new IllegalArgumentException("Unknown shard: " + shard);
35-
};
36-
}
37-
38-
39-
@Getter
4030
public enum Shard {
41-
ChoreShard(Chore),
42-
SHARD_0(SHARD_ID_0),
43-
Video(SHARD_ID_1),
31+
@JsonProperty("chore")
32+
chore(Chore),
33+
@JsonProperty("default")
34+
SHARD(SHARD_ID_0),
35+
@JsonProperty("video")
36+
video(SHARD_ID_1),
37+
@JsonProperty("post")
4438
post(SHARD_ID_2),
39+
@JsonProperty("music")
4540
music(SHARD_ID_3);
4641

4742
private final long shardId;
4843

4944
Shard(long shardId) {
5045
this.shardId = shardId;
5146
}
52-
}
5347

54-
/**
55-
* transform id to shard id
56-
*
57-
* @param id id
58-
* @param shard shard
59-
* @return the id embed shard id
60-
*/
61-
public static long transformId(long id, Shard shard) {
62-
return shard.getShardId() | id;
48+
/**
49+
* transform id to shard id
50+
*
51+
* @param id id
52+
* @return the id embed shard id
53+
*/
54+
public long transform(long id) {
55+
return this.shardId | id;
56+
}
6357
}
6458
}

0 commit comments

Comments
 (0)