Skip to content

Commit

Permalink
[Enhancement] Optimize MongoDB Framework Performance close #833
Browse files Browse the repository at this point in the history
  • Loading branch information
goodjava committed Apr 26, 2024
1 parent d41a220 commit 8546322
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 +47,12 @@ 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 +61,8 @@ 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 @@ -19,10 +19,10 @@
import com.google.common.collect.Lists;
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;
Expand All @@ -49,7 +49,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 +60,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 +72,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 +83,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 @@ -114,11 +114,6 @@ public static JsonArray getParams(HttpRequestMethod method, byte[] data, MvcCont
return arrayRes;
}

private static JsonObject obj(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
@@ -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 8546322

Please sign in to comment.