From 28813e12315cbb80b1421b5b65f8f9221bb0040c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BB=9D=E5=B0=98?= <237809796@qq.com> Date: Mon, 1 Apr 2024 16:06:07 +0800 Subject: [PATCH] . --- root/qtask/list.html | 4 +- .../net/tccn/base/arango/ArangoSource.java | 216 -------------- src/main/java/net/tccn/base/arango/Doc.java | 271 ------------------ .../java/net/tccn/base/arango/Source.java | 15 - 4 files changed, 2 insertions(+), 504 deletions(-) delete mode 100644 src/main/java/net/tccn/base/arango/ArangoSource.java delete mode 100644 src/main/java/net/tccn/base/arango/Doc.java delete mode 100644 src/main/java/net/tccn/base/arango/Source.java diff --git a/root/qtask/list.html b/root/qtask/list.html index 268e776..25e9908 100644 --- a/root/qtask/list.html +++ b/root/qtask/list.html @@ -129,7 +129,7 @@ 数据平台 CataLog @@ -418,7 +418,7 @@ catalogs: function () { let dbPlats = this.dbPlats; for (i in dbPlats) { - if (dbPlats[i].key === this.row.dbplatid) { + if (dbPlats[i].dbid === this.row.dbid) { return dbPlats[i]["catalogs"] } } diff --git a/src/main/java/net/tccn/base/arango/ArangoSource.java b/src/main/java/net/tccn/base/arango/ArangoSource.java deleted file mode 100644 index 9b858b3..0000000 --- a/src/main/java/net/tccn/base/arango/ArangoSource.java +++ /dev/null @@ -1,216 +0,0 @@ -package net.tccn.base.arango; - -import com.arangodb.ArangoCollection; -import com.arangodb.ArangoDB; -import com.arangodb.ArangoDatabase; -import com.arangodb.entity.DocumentCreateEntity; -import com.arangodb.entity.DocumentDeleteEntity; -import com.arangodb.entity.MultiDocumentEntity; -import net.tccn.base.MetaListenter; -import net.tccn.base.Utils; - -import javax.annotation.Resource; -import javax.persistence.Table; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Function; -import java.util.logging.Level; -import java.util.logging.Logger; - -import static java.util.Arrays.asList; - -/** - * 管理 数据源连接对象 - * - * @author: liangxianyou at 2018/12/15 11:35. - */ -public class ArangoSource { - - @Resource(name = "property.arango.host") - private String host = "127.0.0.1"; - @Resource(name = "property.arango.user") - private String user = "root"; - @Resource(name = "property.arango.password") - private String password = "123456"; - @Resource(name = "property.arango.port") - private int port = 8529; - - public Logger logger = Logger.getLogger(this.getClass().getSimpleName()); - private ArangoDB arangoDb; - - private static Map sourceMap = new HashMap(); - - private ArangoSource() { - MetaListenter.resourceFactory.inject(this); - - arangoDb = new ArangoDB.Builder().host(host, port).user(user).password(password).timeout(1000*15).build(); - } - - private ArangoSource(ArangoDB arangoDb) { - MetaListenter.resourceFactory.inject(this); - - this.arangoDb = arangoDb; - } - - public static ArangoSource use(String unit) { - ArangoSource arangoSource = new ArangoSource(); - return arangoSource; - } - - public ArangoDatabase db(String db) { - return arangoDb.db(db); - } - - public ArangoCollection collection(Doc doc) { - return collection(doc.getClass()); - } - - public ArangoCollection collection(Class type) { - Table table = type.getAnnotation(Table.class); - //createDb(table.catalog()); - return arangoDb.db(table.catalog()).collection(table.name()); - } - - public boolean createDb(String db) { - ArangoDatabase database = arangoDb.db(db); - if (!database.exists()) { - return database.create(); - } - logger.log(Level.INFO, "arango database exists"); - return true; - } - - public ArangoCollection createDocument(Doc doc) { - Class type = doc.getClass(); - Table table = type.getAnnotation(Table.class); - - ArangoDatabase database = arangoDb.db(table.catalog()); - if (!database.exists()) { - database.create(); - } - - ArangoCollection collection = database.collection(table.name()); - if (!collection.exists()) { - collection.create(); - } - - return collection; - } - - // 首字母大写 - private static Function upFirst = (s) -> { - return s.substring(0, 1).toUpperCase() + s.substring(1); - }; - - /** - * Doc 转为查询对象 - * - * @param t - * @param - * @return - */ - private Function filterBuilder = (t) -> { - Table table = t.getClass().getAnnotation(Table.class); - StringBuilder buf = new StringBuilder(); - - buf.append("for d in ").append(table.name()); - buf.append(" filter 1==1"); - t.toDoc().forEach((k, v) -> { - if (v != null && (v.getClass() == String.class || v instanceof Number)) { - buf.append(" and d.").append(k).append("=="); - } - - if (v.getClass() == String.class) { - buf.append("'").append(v).append("'"); - } else if (v instanceof Number) { - buf.append(v); - } - }); - return buf; - }; - - private Function orderBuilder = (t) -> { - StringBuilder buf = new StringBuilder(); - Map order = t.getOrder(); - if (Utils.isEmpty(order)) { - return buf.append(" sort d._key desc"); - } - buf.append(" sort "); - order.forEach((k, v) -> { - buf.append("d.").append(k).append(v == 1 ? " desc," : " asc,"); - }); - buf.deleteCharAt(buf.length() - 1); - return buf; - }; - - private Function returnBuilder = (t) -> { - StringBuilder buf = new StringBuilder(); - - if (Utils.isEmpty(t.get_Shows())) { - return buf.append(" return d"); - } - - buf.append(" return {"); - t.get_Shows().forEach(x -> { - buf.append(x).append(":d.").append(x).append(","); - }); - buf.deleteCharAt(buf.length() - 1).append("}"); - return buf; - }; - - public String parseAqlCount(T t) { - StringBuilder buf = new StringBuilder(); - buf.append(filterBuilder.apply(t)); - buf.append(" COLLECT WITH COUNT INTO total"); - buf.append(" return total"); - //logger.log(Level.INFO, buf.toString()); - return buf.toString(); - } - - public String parseAql(T t, int offset, int ps) { - if (offset < 0) offset = 0; - if (ps <= 0) ps = 1000; - StringBuilder buf = new StringBuilder(); - - buf.append(filterBuilder.apply(t)); - buf.append(orderBuilder.apply(t)); - buf.append(" limit ").append(offset).append(",").append(ps); - buf.append(returnBuilder.apply(t)); - //logger.log(Level.INFO, buf.toString()); - return buf.toString(); - } - - //---------------------------------------- - //ok - public T save(T doc) { - DocumentCreateEntity tmap = collection(doc).insertDocument(doc.toDoc()); - doc.setKey(tmap.getKey()); - doc.setId(tmap.getId()); - return doc; - } - - //ok - public void update(T doc) { - collection(doc).updateDocument(doc.getKey(), doc.toDoc()); - } - - //ok - public T getDoc(Object key, Class type) { - return collection(type).getDocument(String.valueOf(key), type); - } - - //ok - public DocumentDeleteEntity delete(String key, Class type) { - return collection(type).deleteDocument(key); - } - - //ok - public MultiDocumentEntity> deleteAll(Doc... docs) { - return collection(docs[0]).deleteDocuments(asList(docs)); - } - - public MultiDocumentEntity find(Collection keys, Class type) { - return collection(type).getDocuments(keys, type); - } -} diff --git a/src/main/java/net/tccn/base/arango/Doc.java b/src/main/java/net/tccn/base/arango/Doc.java deleted file mode 100644 index de8fbb0..0000000 --- a/src/main/java/net/tccn/base/arango/Doc.java +++ /dev/null @@ -1,271 +0,0 @@ -package net.tccn.base.arango; - -import com.arangodb.ArangoCollection; -import com.arangodb.ArangoDBException; -import com.arangodb.ArangoDatabase; -import com.arangodb.entity.DocumentCreateEntity; -import net.tccn.base.PageBean; -import org.redkale.source.Flipper; - -import javax.persistence.Table; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Function; - -/** - * @author: liangxianyou at 2018/12/1 22:59. - */ -public abstract class Doc { - private HashMap attr = new HashMap(); - - private String _id; - private String _key; - - private Set _shows; - private Map _order; - private List _filters;//[{col, value, expr}] - - public String getId() { - return _id; - } - - public void setId(String id) { - this._id = id; - } - - public String getKey() { - return _key; - } - - public void setKey(String key) { - this._key = key; - } - - protected Doc set(String k, Object v) { - attr.put(k, v); - return this; - } - - protected V get(String k) { - return (V)attr.get(k); - } - - /*public T setShows(String... show) { - if (_shows == null) { - _shows = new HashSet<>(); - } - for (String s : show) { - _shows.add(s); - } - return (T) this; - }*/ - - public Set get_Shows() { - return _shows; - } - - /*public void set_Shows(Set shows) { - this._shows = shows; - }*/ - - public T setOrder(String col, int desc) { - if (_order == null) { - _order = new LinkedHashMap(); - } - - _order.put(col, desc); - return (T) this; - } - - public Map getOrder() { - return _order; - } - - /*public void setOrder(Map order) { - this._order = order; - }*/ - /*@Override - public String toString() { - //convert. - - String doc = convert.convertTo(this); - if (attr.size() == 0) { - return doc; - } - - StringBuilder builder = new StringBuilder(); - if (attr.size() != 0) { - String attrStr = convert.convertTo(attr); - builder.append("{"); - builder.append(attrStr, 1, attrStr.length() - 1); - builder.append(","); - builder.append(doc, 1, doc.length()); //builder.append(doc.substring(1)); - } - - return builder.toString(); - }*/ - - private Function fieldName = (s) -> { - return s.replace("get", "").substring(0, 1).toLowerCase() + s.substring(4); - }; - - /** - * 提取Doc 属性到 Map,用于存贮到Arango中 - * 提取规则: - * 1、将doc中自带非空( !=null )属性提取都map中, - * 2、将attr中属性覆盖到map中(如果attr中存在同名属性,attr为主) - * @return - */ - public Map toDoc() { - HashMap clone = (HashMap) attr.clone(); - - Class type = this.getClass(); - Method[] methods = type.getDeclaredMethods(); - - for (Method method : methods) { - String name = method.getName(); - if (name.startsWith("get") && method.getParameterCount() == 0) { - //Type mt = method.getAnnotatedReturnType().getType(); - try { - //System.out.println(method.getName()); - Object v = method.invoke(this); - if (v != null) { - clone.put(fieldName.apply(name), v); - } - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - } - } - return clone; - } - - //--------------------------------------------------------------------------------------------------- - - private ArangoSource arangoSource; - private ArangoDatabase db; - private ArangoCollection collection; - - private static ConcurrentHashMap daos = new ConcurrentHashMap(); - public Doc() { - Table table = this.getClass().getAnnotation(Table.class); - String sourceName = null; - Source source = this.getClass().getAnnotation(Source.class); - if (source != null) { - sourceName = source.name(); - } - - try { - arangoSource = ArangoSource.use(sourceName); - this.db = arangoSource.db(table.catalog()); - this.collection = arangoSource.collection(this); - } catch (Exception e) { - e.printStackTrace(); - } - } - protected final static T dao(Class type) { - - if (daos.get(type) == null) { - try { - daos.put(type, type.newInstance()); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } - - return (T) daos.get(type); - } - - //ok - public T save() { - DocumentCreateEntity tmap = collection.insertDocument(this.toDoc()); - this.setId(tmap.getId()); - this.setKey(tmap.getKey()); - return (T) this; - } - - //ok - public void update() { - collection.updateDocument(this.getKey(), this.toDoc()); - } - - public PageBean findPage(T t, Flipper flipper) { - if (flipper == null) { - flipper = new Flipper(); - } - if (t == null) { - t = (T) this; - } - - List list = find(t, flipper.getOffset(), flipper.getLimit()); - long count = count(t); - return PageBean.by(list, count); - } - - public T findFirst(T t) { - return findFirst(arangoSource.parseAql(t, 0, 1), (Class) t.getClass()); - } - public List find() { - int count = count(this); - return find((T) this, 0, count); - } - public List find(T t) { - return find(t, 0, 1000); - } - public List find(T t, int offset, int ps) { - if (t == null) { - t = (T) this; - } - return find(arangoSource.parseAql(t, offset, ps), (Class)this.getClass()); - } - - public List find(String aql, Class clazz) { - try { - return db.query(aql, clazz).asListRemaining(); - } catch (ArangoDBException e) { - System.out.println(aql); - e.printStackTrace(); - } - return db.query(aql, clazz).asListRemaining(); - } - public T findFirst(String aql, Class clazz) { - try { - return db.query(aql, clazz).first(); - } catch (ArangoDBException e) { - System.out.println(aql); - e.printStackTrace(); - } - return db.query(aql, clazz).first(); - } - - public int count() { - return count(this); - } - public int count(T t) { - if (t == null) { - t = (T) this; - } - // arangoSource.createDocument(this); - return db.query(arangoSource.parseAqlCount(t), int.class).first(); - } - //ok - public T findByKey(Object key) { - return (T) collection.getDocument(String.valueOf(key), this.getClass()); - } - - // ok todo: 将数据放入回收库 - public void delete() { - collection.deleteDocument(getKey()); - } - - public void delete(Collection key) { - collection.deleteDocuments(key); - } - -} diff --git a/src/main/java/net/tccn/base/arango/Source.java b/src/main/java/net/tccn/base/arango/Source.java deleted file mode 100644 index 08fd2dc..0000000 --- a/src/main/java/net/tccn/base/arango/Source.java +++ /dev/null @@ -1,15 +0,0 @@ -package net.tccn.base.arango; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * @author: liangxianyou at 2018/12/22 0:32. - */ -@Target({ElementType.TYPE}) -@Retention(RetentionPolicy.RUNTIME) -public @interface Source { - String name() default ""; -}