Skip to content

Commit

Permalink
Merge pull request #266 from sixwaaaay/multiplex
Browse files Browse the repository at this point in the history
feat: comment multiplex
  • Loading branch information
sixwaaaay authored Aug 23, 2024
2 parents d4806e5 + 625cf47 commit ada06c4
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/comment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
mvn -q -Pnative compile -DskipTests
mvn -q -Pnative spring-boot:process-aot -DskipTests
- name: docker-compose up
run: docker-compose up -d
run: docker compose up -d
- name: Unit Test
run: |
mvn -q clean
Expand Down
84 changes: 58 additions & 26 deletions graal/comments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,29 @@ paths:
get:
summary: "GET comments/main"
operationId: "getMainCommentList"
tags:
- "comments"
security:
- { }
- BearerAuth: [ ]
- {}
- BearerAuth: []
parameters:
- name: "type"
in: "query"
required: true
schema:
oneOf:
- type: "string"
enum: [ "video", "post", "music" ]
- name: "belong_to"
in: "query"
required: true
schema:
type: "integer"
format: "int64"
type: "string"
- name: "page"
in: "query"
required: true
required: false
schema:
type: "integer"
format: "int64"
type: "string"
- name: "size"
in: "query"
required: false
Expand All @@ -50,35 +57,42 @@ paths:
"200":
description: "OK"
content:
'*/*':
"*/*":
schema:
$ref: "#/components/schemas/CommentResult"
/comments/reply:
get:
summary: "GET comments/reply"
operationId: "getReplyCommentList"
tags:
- "comments"
security:
- { }
- BearerAuth: [ ]
- {}
- BearerAuth: []
parameters:
- name: "type"
in: "query"
required: true
schema:
oneOf:
- type: "string"
enum: [ "video", "post", "music" ]
- name: "belong_to"
in: "query"
required: true
schema:
type: "integer"
format: "int64"
type: "string"
- name: "reply_to"
in: "query"
required: true
schema:
type: "integer"
format: "int64"
type: "string"
- name: "page"
in: "query"
required: true
required: false
schema:
type: "integer"
format: "int64"
type: "string"

- name: "size"
in: "query"
required: false
Expand All @@ -90,15 +104,17 @@ paths:
"200":
description: "OK"
content:
'*/*':
"*/*":
schema:
$ref: "#/components/schemas/ReplyResult"
/comments:
post:
summary: "POST comments"
operationId: "createComment"
tags:
- "comments"
security:
- BearerAuth: [ ]
- BearerAuth: []
requestBody:
content:
application/json:
Expand All @@ -109,15 +125,17 @@ paths:
"200":
description: "OK"
content:
'*/*':
"*/*":
schema:
$ref: "#/components/schemas/Comment"
/comments/{id}:
delete:
summary: "DELETE comments/{id}"
operationId: "deleteComment"
tags:
- "comments"
security:
- BearerAuth: [ ]
- BearerAuth: []
parameters:
- name: "id"
in: "path"
Expand All @@ -138,8 +156,10 @@ paths:
post:
summary: "POST comments/action/like/{id}"
operationId: "voteComment"
tags:
- "comments"
security:
- BearerAuth: [ ]
- BearerAuth: []
parameters:
- name: "id"
in: "path"
Expand All @@ -152,9 +172,11 @@ paths:
description: "OK"
delete:
summary: "DELETE comments/action/like/{id}"
tags:
- "comments"
operationId: "cancelVoteComment"
security:
- BearerAuth: [ ]
- BearerAuth: []
parameters:
- name: "id"
in: "path"
Expand All @@ -166,7 +188,7 @@ paths:
"200":
description: "OK"
security:
- BearerAuth: [ ]
- BearerAuth: []

components:
securitySchemes:
Expand Down Expand Up @@ -220,9 +242,11 @@ components:
reply_to:
type: "integer"
format: "int64"
belong_to:
refer_to:
type: "integer"
format: "int64"
belong_to:
type: "string"
created_at:
type: "string"
format: "date-time"
Expand Down Expand Up @@ -271,6 +295,14 @@ components:
reply_to:
type: "integer"
format: "int64"
refer_to:
type: "integer"
format: "int64"
belong_to:
type: "integer"
format: "int64"
format: "int64"
type:
type: "string"
oneOf:
- type: "string"
enum: [ "video", "post", "music" ]
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.sixwaaaay.sharingcomment.request.CommentRequest;
import io.sixwaaaay.sharingcomment.request.Principal;
import io.sixwaaaay.sharingcomment.service.CommentService;
import io.sixwaaaay.sharingcomment.util.ShardEnum;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
Expand All @@ -29,6 +30,8 @@
import java.time.ZoneOffset;
import java.util.Optional;

import static io.sixwaaaay.sharingcomment.util.ShardEnum.transformId;

@RestController
@RequestMapping("/comments")
@AllArgsConstructor
Expand All @@ -45,11 +48,13 @@ public class CommentController {
*/
@GetMapping("/main")
public CommentResult getMainCommentList(
@RequestParam("type") String type,
@RequestParam("belong_to") Long belongTo,
@RequestParam(value = "page") Optional<Long> id,
@RequestParam(value = "size", defaultValue = "10") Integer size
) {
var userId = Principal.currentUserId();
belongTo = transformId(belongTo, ShardEnum.getShard(type));
return commentService.getMainCommentList(belongTo, id.orElse(Long.MAX_VALUE), size, userId);
}

Expand All @@ -61,12 +66,14 @@ public CommentResult getMainCommentList(
*/
@GetMapping("/reply")
public ReplyResult getReplyCommentList(
@RequestParam("type") String type,
@RequestParam("belong_to") Long belongTo,
@RequestParam("reply_to") Long replyTo,
@RequestParam(value = "page", defaultValue = "0") long id,
@RequestParam(value = "size", defaultValue = "10") Integer size
) {
var userId = Principal.currentUserId();
belongTo = transformId(belongTo, ShardEnum.getShard(type));
return commentService.getReplyCommentList(belongTo, replyTo, id, size, userId);
}

Expand All @@ -80,7 +87,7 @@ public Comment createComment(@Valid @RequestBody CommentRequest request) {
var comment = new Comment();
var id = Principal.currentUserId();
comment.setUserId(id);
comment.setBelongTo(request.getBelongTo());
comment.setBelongTo(transformId(request.getBelongTo(), ShardEnum.getShard(request.getType())));
comment.setContent(request.getContent());
comment.setReferTo(request.getReferTo());
comment.setReplyTo(request.getReplyTo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
@Data
@Valid
public class CommentRequest {


/**
* The type of the comment.
*/
@NotNull
@JsonProperty("type")
private String type;

/**
* The content of the comment.
* It must be between 1 and 1000 characters long.
Expand Down Expand Up @@ -64,4 +73,9 @@ public class CommentRequest {
public boolean isValid() {
return (replyTo == null && referTo == null) || (replyTo != null && referTo != null);
}

@AssertTrue(message = "type must be one of 'chore', 'default', 'video', 'post', 'music'")
public boolean isValidType() {
return type.equals("chore") || type.equals("default") || type.equals("video") || type.equals("post") || type.equals("music");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2023-2024 sixwaaaay.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.sixwaaaay.sharingcomment.util;

import lombok.Getter;

public class ShardEnum {
// 最高位不使用, 次高位(62位) 到第60位(1位) 用于存储分片ID
// 共3位,八个分片
public static final long Chore = 0L; // aka 0b000L << 60;
public static final long SHARD_ID_0 = 0b001L << 60;
public static final long SHARD_ID_1 = 0b010L << 60;
public static final long SHARD_ID_2 = 0b011L << 60;
public static final long SHARD_ID_3 = 0b100L << 60;

public static Shard getShard(String shard) {
return switch (shard) {
case "chore" -> Shard.ChoreShard;
case "default" -> Shard.SHARD_0;
case "video" -> Shard.Video;
case "post" -> Shard.post;
case "music" -> Shard.music;
default -> throw new IllegalArgumentException("Unknown shard: " + shard);
};
}


@Getter
public enum Shard {
ChoreShard(Chore),
SHARD_0(SHARD_ID_0),
Video(SHARD_ID_1),
post(SHARD_ID_2),
music(SHARD_ID_3);

private final long shardId;

Shard(long shardId) {
this.shardId = shardId;
}
}

/**
* transform id to shard id
*
* @param id id
* @param shard shard
* @return the id embed shard id
*/
public static long transformId(long id, Shard shard) {
return shard.getShardId() | id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ class SharingCommentApplicationTests {
public void getMainCommentListTest() throws Exception {
var token = jwtUtil.generateToken("john", "111");
mockMvc.perform(MockMvcRequestBuilders.get("/comments/main")
.param("type","chore")
.param("belong_to", "1")
.param("size", "10")
.header("Authorization", "Bearer " + token))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
// without token
mockMvc.perform(MockMvcRequestBuilders.get("/comments/main")
.param("type","chore")
.param("belong_to", "1")
.param("size", "10"))
.andExpect(MockMvcResultMatchers.status().isOk())
Expand All @@ -65,6 +67,7 @@ public void getMainCommentListTest() throws Exception {
public void getReplyCommentListTest() throws Exception {
var token = jwtUtil.generateToken("n", "1111111");
mockMvc.perform(MockMvcRequestBuilders.get("/comments/reply")
.param("type", "chore")
.param("belong_to", "1")
.param("reply_to", "1")
.param("size", "10")
Expand All @@ -73,6 +76,7 @@ public void getReplyCommentListTest() throws Exception {
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));

mockMvc.perform(MockMvcRequestBuilders.get("/comments/reply")
.param("type","chore")
.param("belong_to", "1")
.param("reply_to", "1")
.param("size", "10"))
Expand All @@ -84,7 +88,7 @@ public void getReplyCommentListTest() throws Exception {
@Test
public void createCommentTest() throws Exception {
var token = jwtUtil.generateToken("n", "1111111");
var json = "{ \"content\": \"This is a test comment\", \"reply_to\": 1, \"belong_to\": 1, \"refer_to\": 1 }";
var json = "{ \"content\": \"This is a test comment\", \"reply_to\": 1, \"belong_to\": 1, \"refer_to\": 1, \"type\": \"chore\" }";
mockMvc.perform(MockMvcRequestBuilders.post("/comments")
.content(json)
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -101,7 +105,7 @@ public void createCommentTest() throws Exception {
@Test
public void deleteComments() throws Exception {
var token = jwtUtil.generateToken("n", "1");
var json = "{ \"content\": \"This is a test comment\", \"reply_to\": null, \"belong_to\": 1 }";
var json = "{ \"content\": \"This is a test comment\", \"reply_to\": null, \"belong_to\": 1, \"type\": \"chore\" }";
mockMvc.perform(MockMvcRequestBuilders.delete("/comments/21")
.content(json)
.contentType(MediaType.APPLICATION_JSON)
Expand Down

0 comments on commit ada06c4

Please sign in to comment.