Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Official and Stable ORM Framework for MongoDB close(#829) #830

Merged
merged 13 commits into from
Apr 19, 2024
Merged
51 changes: 51 additions & 0 deletions jcommon/ai/aws/src/main/java/run/mone/AwsClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package run.mone;

import com.google.gson.Gson;
import org.json.JSONObject;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity;
import software.amazon.awssdk.identity.spi.IdentityProvider;
import software.amazon.awssdk.identity.spi.ResolveIdentityRequest;
import software.amazon.awssdk.identity.spi.internal.DefaultAwsCredentialsIdentity;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient;
import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelRequest;
import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelResponse;

import java.util.concurrent.CompletableFuture;

/**
* @author goodjava@qq.com
* @date 2024/4/12 13:35
*/
public class AwsClient {

private static final Gson gson = new Gson();


public static ResponsePayload call(JSONObject payload, Region region, String modelId, Key key) {
BedrockRuntimeClient client = BedrockRuntimeClient.builder()
.credentialsProvider(new IdentityProvider<>() {
@Override
public Class<AwsCredentialsIdentity> identityType() {
return AwsCredentialsIdentity.class;
}

public CompletableFuture<AwsCredentialsIdentity> resolveIdentity(ResolveIdentityRequest request) {
return CompletableFuture.completedFuture(DefaultAwsCredentialsIdentity.builder().accessKeyId(key.getKeyId()).secretAccessKey(key.getKey()).build());
}
})
.region(region)
.build();

InvokeModelRequest request = InvokeModelRequest.builder()
.contentType("application/json")
.body(SdkBytes.fromUtf8String(payload.toString()))
.modelId(modelId)
.build();

InvokeModelResponse resp = client.invokeModel(request);
String str = new String(resp.body().asByteArray());
return gson.fromJson(str, ResponsePayload.class);
}
}
4 changes: 3 additions & 1 deletion jcommon/ai/aws/src/main/java/run/mone/ModelEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ public enum ModelEnum {


Sonnet("anthropic.claude-3-sonnet-20240229-v1:0"),
Haiku("anthropic.claude-3-haiku-20240307-v1:0");
Haiku("anthropic.claude-3-haiku-20240307-v1:0"),
Opus("anthropic.claude-3-opus-20240229-v1:0");



public String modelName;
Expand Down
11 changes: 11 additions & 0 deletions jcommon/docean-plugin/docean-plugin-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,30 @@
<artifactId>docean-plugin-mongodb</artifactId>
<version>1.5.0-jdk21-SNAPSHOT</version>
<dependencies>

<dependency>
<groupId>run.mone</groupId>
<artifactId>docean-plugin-config</artifactId>
</dependency>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.8.0</version>
</dependency>

<dependency>
<groupId>run.mone</groupId>
<artifactId>catPlugin</artifactId>
<version>1.6.0-jdk21-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>dev.morphia.morphia</groupId>
<artifactId>morphia-core</artifactId>
<version>2.4.13</version>
</dependency>


</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.mongodb.client.MongoDatabase;
import com.xiaomi.youpin.cat.CatPlugin;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -73,6 +74,10 @@ public MongoCollection<Document> getCollection(String collectionName) {
return db.getCollection(collectionName);
}

public <T> MongoCollection<T> getCollection(String collectionName, Class<T> clazz) {
return db.getCollection(collectionName, clazz);
}

public void insert(String collectionName, Document doc) {
CatPlugin cat = new CatPlugin("insert", catEnabled, CAT_TYPE);
boolean success = true;
Expand Down Expand Up @@ -111,29 +116,25 @@ public void insertMany(String collectionName, List<Document> docList) {
}

public Document findFirst(String collectionName) {
CatPlugin cat = new CatPlugin("findFirst", catEnabled, CAT_TYPE);
boolean success = true;
cat.before(null);
try {
MongoCollection<Document> collection = this.getCollection(collectionName);
return collection.find().first();
} catch (MongoException e) {
success = false;
logger.error(e.getMessage(), e);
} catch (Exception e) {
success = false;
logger.error(e.getMessage(), e);
} finally {
cat.after(success);
}
return null;
MongoCollection<Document> collection = this.getCollection(collectionName);
return collection.find().first();
}

public Document findFirst(String collectionName, Bson filter) {
MongoCollection<Document> collection = this.getCollection(collectionName);
return collection.find(filter).first();
}

public <T> T findFirst(String collectionName, Bson filter, Class<T> clazz) {
MongoCollection<T> collection = this.getCollection(collectionName, clazz);
return collection.find(filter).first();
}

public List<Document> findAll(String collectionName, Document doc) {
public List<Document> findAll(String collectionName, Bson filter) {
try {
MongoCollection<Document> collection = this.getCollection(collectionName);
List<Document> res = new ArrayList<>();
for (Document cur : collection.find(doc)) {
for (Document cur : collection.find(filter)) {
res.add(cur);
}
return res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@

package com.xiaomi.youpin.docean.plugin.mongodb;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.xiaomi.youpin.docean.Ioc;
import com.xiaomi.youpin.docean.anno.DOceanPlugin;
import com.xiaomi.youpin.docean.plugin.IPlugin;
import com.xiaomi.youpin.docean.plugin.config.Config;
import dev.morphia.Datastore;
import dev.morphia.Morphia;
import dev.morphia.mapping.Mapper;
import dev.morphia.mapping.MapperOptions;
import lombok.extern.slf4j.Slf4j;

import java.util.Set;
Expand All @@ -42,10 +48,23 @@ public void init(Set<? extends Class<?>> classSet, Ioc ioc) {
mongoDb.setCatEnabled(config.get("mongodb.cat.enabled", "false").equals("true"));
mongoDb.init();
ioc.putBean(mongoDb);


MongoClient mongoClient = MongoClients.create(mongoDb.getMongoDbClient());
Datastore datastore = Morphia.createDatastore(mongoClient, mongoDb.getMongoDatabase());


String packagePath = config.get("mongodb.package", "run.mone.bo");
datastore.getMapper().mapPackage(packagePath);
datastore.ensureIndexes();


ioc.putBean(Datastore.class.getName(), datastore);
}

@Override
public String version() {
return "0.0.1:2020-07-04:zheng.xucn@outlook.com";
return "0.0.1:2020-07-04:goodjava@qq.com";
}

}
1 change: 0 additions & 1 deletion jcommon/docean/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
<groupId>run.mone</groupId>
<artifactId>easy</artifactId>
<version>1.6.0-jdk21-SNAPSHOT</version>
<!-- <version>1.6.0-jdk21-SNAPSHOT</version>-->
</dependency>
<dependency>
<groupId>cglib</groupId>
Expand Down
45 changes: 41 additions & 4 deletions jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.xiaomi.youpin.docean;

import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.xiaomi.youpin.docean.anno.RequestMapping;
Expand All @@ -41,10 +42,12 @@
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Locale;
import java.util.Optional;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
* @author goodjava@qq.com
Expand Down Expand Up @@ -138,11 +141,32 @@ private void registerControllerMethods(Bean bean) {
hrm.setObj(bean.getObj());
hrm.setMethod(m);
hrm.setHttpMethod(rm.method());
hrm.setGenericSuperclassTypeArguments(getGenericSuperclassTypeArguments(bean.getClazz()));
ioc.publishEvent(new Event(EventType.initController, path));
requestMethodMap.put(path, hrm);
}));
}

public static Map<String, Class> getGenericSuperclassTypeArguments(Class clazz) {
List<String> list = Arrays.stream(clazz.getSuperclass().getTypeParameters()).map(it -> it.getName()).collect(Collectors.toList());
if (list.size() == 0) {
return Maps.newHashMap();
}
Map<String, Class> map = new HashMap<>();
Type genericSuperclass = clazz.getGenericSuperclass();
if (genericSuperclass instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
Type[] typeArguments = parameterizedType.getActualTypeArguments();
for (int i = 0; i < typeArguments.length; i++) {
Type argument = typeArguments[i];
if (argument instanceof Class<?>) {
map.put(list.get(i), (Class) argument);
}
}
}
return map;
}


private static final class LazyHolder {
private static final Mvc ins = new Mvc(Ioc.ins());
Expand Down Expand Up @@ -187,6 +211,16 @@ public void callService(MvcContext context, MvcRequest request, MvcResponse resp
response.writeAndFlush(context, new Gson().toJson(mr));
}

public List<Class> mapMethodParametersToClasses(Method method, Map<String, Class> map) {
return Arrays.stream(method.getParameters()).map(it -> {
String name = it.getParameterizedType().getTypeName();
if ((it.getType() instanceof Object) && map.containsKey(name)) {
return map.get(name);
}
return it.getType();
}).collect(Collectors.toList());
}

public void callMethod(MvcContext context, MvcRequest request, MvcResponse response, MvcResult<Object> result, HttpRequestMethod method) {
Safe.run(() -> {
Object[] params = new Object[]{null};
Expand All @@ -199,7 +233,10 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo
params[0] = context;
} else {
try {
params = methodInvoker.getMethodParams(method.getMethod(), args);
//可能方法中有泛型,这里给fix调,用实际的Class
List<Class> list = mapMethodParametersToClasses(method.getMethod(), method.getGenericSuperclassTypeArguments());
Class[] types = list.toArray(new Class[]{});
params = methodInvoker.getMethodParams(args, types);
} catch (Exception e) {
log.error("getMethodParams error,path:{},params:{},method:{}", context.getPath(),
GsonUtils.gson.toJson(context.getParams()), request.getMethod().toLowerCase(Locale.ROOT), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import lombok.Data;

import java.lang.reflect.Method;
import java.util.Map;

/**
* @author goodjava@qq.com
Expand All @@ -36,5 +37,7 @@ public class HttpRequestMethod {

private long timeout;

private Map<String, Class> genericSuperclassTypeArguments;


}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ public Object[] getMethodParams(Object obj, String methodName, JsonElement param

public Object[] getMethodParams(Method method, JsonElement params) {
Class<?>[] types = method.getParameterTypes();
return getMethodParams(params, types);
}

public Object[] getMethodParams(JsonElement params, Class<?>[] types) {
if (types.length == 0) {
return new Object[]{};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ public MongoCollection<Document> getCollection(String collectionName) {
return db.getCollection(collectionName);
}

public <T> MongoCollection<T> getCollection(String collectionName, Class<T> clazz) {
return db.getCollection(collectionName, clazz);
}

/**
* Inserts a document under the specified collection
*
* @param collectionName
* @param doc
*/
Expand All @@ -106,6 +111,7 @@ public void insert(String collectionName, Document doc) {

/**
* Inserts a list of documents under the specified collection
*
* @param collectionName
* @param docList
*/
Expand All @@ -129,6 +135,7 @@ public void insertMany(String collectionName, List<Document> docList) {

/**
* Finds the first document under the specified collection
*
* @param collectionName
* @return
*/
Expand All @@ -153,6 +160,7 @@ public Document findFirst(String collectionName) {

/**
* Finds all documents under the specified collection according to the criteria
*
* @param collectionName
* @param doc
* @return
Expand Down Expand Up @@ -184,6 +192,7 @@ public List<Document> findAll(String collectionName, Document doc) {

/**
* Deletes a document under a specified collection under specified conditions
*
* @param collectionName
* @param doc
*/
Expand All @@ -207,6 +216,7 @@ public void delete(String collectionName, Document doc) {

/**
* Returns the number of documents under the specified collection
*
* @param collectionName
* @return
*/
Expand Down
Loading