Skip to content

Commit

Permalink
[Enhancement] Optimize MongoDB Framework Performance close #833 (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
caochengxiang authored Apr 26, 2024
2 parents 7d74352 + f6bbefc commit 25d0904
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package run.mone.controller;


import com.xiaomi.youpin.docean.anno.ModelAttribute;

import com.xiaomi.youpin.docean.anno.RequestMapping;
import com.xiaomi.youpin.docean.anno.RequestParam;
import com.xiaomi.youpin.docean.mvc.ContextHolder;
Expand Down Expand Up @@ -46,6 +49,14 @@ public T getById(@RequestParam("id") String id) {
return datastore.find(this.clazz).filter(Filters.eq("id", id)).first();
}



@RequestMapping(path = "/getByIdAndUid", method = "get")
public T getByIdAndUid(@ModelAttribute("user") User user, @RequestParam("id") String id) {
return datastore.find(this.clazz).filter(Filters.and(Filters.eq("id", id), Filters.eq("uid", user.getId()))).first();
}


//按id删除(class)
@Auth
@RequestMapping(path = "/deleteById", method = "get")
Expand All @@ -54,10 +65,9 @@ public boolean deleteById(@RequestParam("id") String id) {
return true;
}

@Auth(role = "user")
@RequestMapping(path = "/deleteByIdAndUid", method = "get")
public boolean deleteByIdAndUid(@RequestParam("id") String id) {
User user = getCurrentUser();

@RequestMapping(path = "/deleteByIdAndUid")
public boolean deleteByIdAndUid(@ModelAttribute("user") User user, String id) {
this.datastore.find(this.clazz).filter(Filters.and(Filters.eq("id", id), Filters.eq("uid", user.getId()))).delete();
return true;
}
Expand Down
12 changes: 10 additions & 2 deletions jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,16 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo
}
Throwable unwrapThrowable = ExceptionUtil.unwrapThrowable(ex);
result.setCode(500);
int httpCode = 200;
if (ex instanceof DoceanException de) {
int code = de.getCode();
if (0 != code) {
result.setCode(code);
httpCode = code;
}
}
result.setMessage(unwrapThrowable.getMessage());
response.writeAndFlush(context, gson.toJson(result));
response.writeAndFlush(context, gson.toJson(result), httpCode);
});
}

Expand All @@ -293,7 +301,7 @@ private Object[] getMethodParams(MvcContext context, MvcRequest request, HttpReq
Object obj = context.session().getAttribute(name);
//提取失败,用户需要登录
if (null == obj) {
throw new DoceanException("You are required to log in first.");
throw new DoceanException("You are required to log in first.", 401);
}
return obj;
});
Expand Down
18 changes: 14 additions & 4 deletions jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/Get.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.xiaomi.youpin.docean.anno.ModelAttribute;
import com.xiaomi.youpin.docean.anno.RequestParam;
import com.xiaomi.youpin.docean.mvc.util.RequestUtils;
import io.netty.handler.codec.http.QueryStringDecoder;

import java.lang.annotation.Annotation;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -38,21 +41,28 @@ public static JsonElement getParams(HttpRequestMethod method, String uri, MvcCon
JsonObject res = new JsonObject();
params.entrySet().forEach(it -> res.addProperty(it.getKey(), it.getValue()));
context.setParams(res);
JsonArray resArray = new JsonArray();

//get 只允许第一个参数是session user
Parameter[] methodParameters = method.getMethod().getParameters();
if (methodParameters.length > 0 && (methodParameters[0].getAnnotation(ModelAttribute.class) != null)) {
resArray.add(RequestUtils.createSessionJsonObject(methodParameters[0].getAnnotation(ModelAttribute.class).value()));
}

JsonArray array = new JsonArray();
Annotation[][] anns = method.getMethod().getParameterAnnotations();
Arrays.stream(anns).forEach(it -> {
if (it.length > 0) {
RequestParam param = getRequestParam(it);
String name = param.value();
if (!params.containsKey(name)) {
array.add("");
resArray.add("");
} else {
array.add(params.get(name));
resArray.add(params.get(name));
}
}
});
return array;

return resArray;
}

private static RequestParam getRequestParam(Annotation[] annos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ public void writeAndFlush(MvcContext context, String message) {
responseStatus = HttpResponseStatus.valueOf(Integer.valueOf(status));
}
writeAndFlush(context, responseStatus, message);

writeAndFlush(context, message, HttpResponseStatus.OK.code());
}

public void writeAndFlush(MvcContext context, String message, int code) {
HttpResponseStatus responseStatus = HttpResponseStatus.valueOf(code);
String status = context.getResHeaders().get("x-status");
if (null != status) {
responseStatus = HttpResponseStatus.valueOf(Integer.valueOf(status));
}
writeAndFlush(context, responseStatus, message);
}

public void clear() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import com.xiaomi.youpin.docean.anno.ModelAttribute;
import com.xiaomi.youpin.docean.mvc.httpmethod.HttpMethodUtils;
import com.xiaomi.youpin.docean.mvc.util.GsonUtils;
import com.xiaomi.youpin.docean.mvc.util.RequestUtils;

import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;

import java.lang.reflect.Parameter;
import java.util.ArrayList;
Expand All @@ -49,7 +56,7 @@ public static JsonArray getParams(HttpRequestMethod method, byte[] data, MvcCont
if (null == arguments) {
Arrays.stream(methodParameters).forEach(it -> {
if (it.getAnnotation(ModelAttribute.class) != null) {
arrayRes.add(obj(it.getAnnotation(ModelAttribute.class).value()));
arrayRes.add(RequestUtils.createSessionJsonObject(it.getAnnotation(ModelAttribute.class).value()));
}
});
return arrayRes;
Expand All @@ -60,7 +67,7 @@ public static JsonArray getParams(HttpRequestMethod method, byte[] data, MvcCont
AtomicInteger i = new AtomicInteger(0);
Arrays.stream(methodParameters).forEach(it -> {
if (it.getAnnotation(ModelAttribute.class) != null) {
arrayRes.add(obj(it.getAnnotation(ModelAttribute.class).value()));
arrayRes.add(RequestUtils.createSessionJsonObject(it.getAnnotation(ModelAttribute.class).value()));
} else {
arrayRes.add(list.get(i.get()));
i.incrementAndGet();
Expand All @@ -72,7 +79,7 @@ public static JsonArray getParams(HttpRequestMethod method, byte[] data, MvcCont
if (arguments.isJsonObject()) {
Arrays.stream(methodParameters).forEach(it -> {
if (it.getAnnotation(ModelAttribute.class) != null) {
arrayRes.add(obj(it.getAnnotation(ModelAttribute.class).value()));
arrayRes.add(RequestUtils.createSessionJsonObject(it.getAnnotation(ModelAttribute.class).value()));
} else {
arrayRes.add(arguments.getAsJsonObject());
}
Expand All @@ -83,7 +90,7 @@ public static JsonArray getParams(HttpRequestMethod method, byte[] data, MvcCont
if (arguments.isJsonPrimitive()) {
Arrays.stream(methodParameters).forEach(it -> {
if (it.getAnnotation(ModelAttribute.class) != null) {
arrayRes.add(obj(it.getAnnotation(ModelAttribute.class).value()));
arrayRes.add(RequestUtils.createSessionJsonObject(it.getAnnotation(ModelAttribute.class).value()));
} else {
arrayRes.add(arguments.getAsJsonPrimitive());
}
Expand Down Expand Up @@ -112,6 +119,7 @@ public static JsonArray getParams(HttpRequestMethod method, byte[] data, MvcCont
}

return arrayRes;

}

private static JsonObject obj(String name) {
Expand All @@ -121,4 +129,6 @@ private static JsonObject obj(String name) {
return obj;
}



}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xiaomi.youpin.docean.mvc.util;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.xiaomi.youpin.docean.config.HttpServerConfig;
import com.xiaomi.youpin.docean.mvc.HttpRequestUtils;
import com.xiaomi.youpin.docean.mvc.upload.Upload;
Expand All @@ -18,8 +19,6 @@
*/
public abstract class RequestUtils {

private static Gson gson = new Gson();

public static byte[] getData(HttpServerConfig config, String uri, FullHttpRequest request) {
return getData(config, uri, request, null);
}
Expand Down Expand Up @@ -49,4 +48,11 @@ public static Map<String, String> headers(FullHttpRequest request) {
return request.headers().entries().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e, n) -> n));
}

public static JsonObject createSessionJsonObject(String name) {
JsonObject obj = new JsonObject();
obj.addProperty("__type__", "session");
obj.addProperty("__name__", name);
return obj;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@

package com.xiaomi.youpin.docean.exception;

import lombok.Getter;

/**
* @author [email protected]
* @date 2020/6/20
*/
public class DoceanException extends RuntimeException {

@Getter
private int code;

public DoceanException(Throwable ex) {
super(ex);
}
Expand All @@ -33,4 +38,9 @@ public DoceanException() {
public DoceanException(String m) {
super(m);
}

public DoceanException(String message, int code) {
super(message);
this.code = code;
}
}

0 comments on commit 25d0904

Please sign in to comment.