1、包结构修改

2、dict的bug修改
3、arangodb查询find()默认1000条修改为库里所有数据
This commit is contained in:
2019-07-02 17:37:55 +08:00
parent bbf9b8b9c4
commit 972fb387c7
44 changed files with 397 additions and 248 deletions

View File

@@ -1,14 +1,19 @@
package net.tccn.base.arango;
import com.arangodb.*;
import com.arangodb.ArangoCollection;
import com.arangodb.ArangoDB;
import com.arangodb.ArangoDatabase;
import com.arangodb.Function;
import com.arangodb.entity.DocumentCreateEntity;
import com.arangodb.entity.DocumentDeleteEntity;
import com.arangodb.entity.MultiDocumentEntity;
import net.tccn.base.PropKit;
import net.tccn.base.X;
import javax.persistence.Table;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -16,6 +21,7 @@ import static java.util.Arrays.asList;
/**
* 管理 数据源连接对象
*
* @author: liangxianyou at 2018/12/15 11:35.
*/
public class ArangoSource {
@@ -24,38 +30,46 @@ public class ArangoSource {
private ArangoDB arangoDb;
private static Map<String, ArangoSource> sources = new HashMap();
static {
init();
}
public static void init() {
sources.put("main", new ArangoSource(new ArangoDB.Builder().host("120.24.230.60", 8529).user("root").password("abc123").build()));
sources.put("abc", new ArangoSource(new ArangoDB.Builder().host("192.168.199.135", 8529).user("root").password("root").build()));
}
public ArangoSource(ArangoDB arangoDb) {
private ArangoSource(ArangoDB arangoDb) {
this.arangoDb = arangoDb;
}
public static ArangoSource use() {
return use("main");
}
public static ArangoSource use(String unit) {
if (unit == null || unit.isEmpty()) {
unit = "main";
if (unit == null || unit.isEmpty() || "main".equals(unit)) {
unit = "";
} else {
unit = "." + unit;
}
return sources.get(unit);
ArangoSource source = sources.get(unit);
if (source == null) {
String host = PropKit.getProperty("arango.host" + unit);
int port = Integer.parseInt(PropKit.getProperty("arango.port" + unit, "8529"));
String user = PropKit.getProperty("arango.user" + unit);
String password = PropKit.getProperty("arango.passwd" + unit);
source = new ArangoSource(new ArangoDB.Builder().host(host, port).user(user).password(password).build());
}
return source;
}
public ArangoDB arangoDB() {
return arangoDb;
}
public ArangoDatabase db(String db) {
return arangoDb.db(db);
}
public <T extends Doc> ArangoCollection collection(Doc<T> doc) {
return collection(doc.getClass());
}
public <T extends Doc> ArangoCollection collection(Class<T> type) {
Table table = type.getAnnotation(Table.class);
//createDb(table.catalog());
@@ -70,6 +84,7 @@ public class ArangoSource {
logger.log(Level.INFO, "arango database exists");
return true;
}
public <T extends Doc> ArangoCollection createDocument(Doc<T> doc) {
Class<? extends Doc> type = doc.getClass();
Table table = type.getAnnotation(Table.class);
@@ -92,92 +107,9 @@ public class ArangoSource {
return s.substring(0, 1).toUpperCase() + s.substring(1);
};
private static Map<Class, Class[]> clazzMap = new HashMap<>();
static {
clazzMap.put(ArrayList.class, new Class[]{List.class, ArrayList.class, String.class});
clazzMap.put(HashMap.class, new Class[]{Map.class, HashMap.class, String.class});
clazzMap.put(Long.class, new Class[]{Long.class, Integer.class, long.class, int.class, short.class, String.class});
clazzMap.put(String.class, new Class[]{String.class});
}
/**
* 还原 Doc对象
* @param map
* @param type
* @param <T>
* @return
*/
public <T extends Doc> T toDoc1(Map<String, Object> map, Class<T> type) {
try {
Doc doc = type.newInstance();
map.forEach((k, v) -> {
String methodName = "set" + upFirst.apply(k);
Method method = null;
Class[] clazzs = clazzMap.get(v == null ? null : v.getClass());
if (clazzs == null) {
doc.set(k, v);
} else {
for (Class clazz : clazzs) {
try {
method = type.getDeclaredMethod(methodName, clazz);
} catch (NoSuchMethodException e) {
}
if (method != null) {
try {
if (v.getClass() == Long.class && clazz != Long.class) {//多种数值类型的处理
Object _v;
switch (clazz.getSimpleName()) {
case "int":
case "Integer": _v = (int)((long) v); break;
case "short":
case "Short": _v = (short)((long) v); break;
default: _v = v;
}
System.out.println(clazz.getSimpleName());
method.invoke(doc, _v);
} else {
method.invoke(doc, v);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
doc.set(k, v);
} catch (InvocationTargetException e) {
e.printStackTrace();
doc.set(k, v);
}
break;
}
}
}
if (method == null) {
doc.set(k, v);
}
});
return (T) doc;
} catch (Exception e) {
}
return null;
}
private Predicate isEmpty = (x) -> {
if (x == null)
return true;
if (x instanceof List)
return ((List) x).isEmpty();
if (x instanceof String)
return ((String) x).isEmpty();
if (x instanceof Map)
return ((Map) x).isEmpty();
if (x instanceof Collection)
return ((Collection) x).isEmpty();
return false;
};
/**
* Doc 转为查询对象
*
* @param t
* @param <T>
* @return
@@ -205,7 +137,7 @@ public class ArangoSource {
private Function<Doc, StringBuilder> orderBuilder = (t) -> {
StringBuilder buf = new StringBuilder();
Map<String, Integer> order = t.getOrder();
if (isEmpty.test(order)) {
if (X.isEmpty(order)) {
return buf.append(" sort d._key desc");
}
buf.append(" sort ");
@@ -219,7 +151,7 @@ public class ArangoSource {
private Function<Doc, StringBuilder> returnBuilder = (t) -> {
StringBuilder buf = new StringBuilder();
if (isEmpty.test(t.get_Shows())) {
if (X.isEmpty(t.get_Shows())) {
return buf.append(" return d");
}
@@ -239,6 +171,7 @@ public class ArangoSource {
//logger.log(Level.INFO, buf.toString());
return buf.toString();
}
public <T extends Doc> String parseAql(T t, int offset, int ps) {
if (offset < 0) offset = 0;
if (ps <= 0) ps = 1000;
@@ -251,6 +184,7 @@ public class ArangoSource {
//logger.log(Level.INFO, buf.toString());
return buf.toString();
}
//----------------------------------------
//ok
public <T extends Doc> T save(T doc) {
@@ -267,7 +201,7 @@ public class ArangoSource {
//ok
public <T extends Doc> T getDoc(Object key, Class<T> type) {
return collection(type).getDocument(String.valueOf(key), type);
return collection(type).getDocument(String.valueOf(key), type);
}
//ok
@@ -276,7 +210,7 @@ public class ArangoSource {
}
//ok
public <T extends Doc> MultiDocumentEntity<DocumentDeleteEntity<Void>> deleteAll(Doc<T> ... docs) {
public <T extends Doc> MultiDocumentEntity<DocumentDeleteEntity<Void>> deleteAll(Doc<T>... docs) {
return collection(docs[0]).deleteDocuments(asList(docs));
}