From d07f2acb4bbb3879d7235be56cf8f553c36bf8b9 Mon Sep 17 00:00:00 2001 From: richie Date: Tue, 19 Mar 2019 16:58:58 +0800 Subject: [PATCH] =?UTF-8?q?Redis=E7=A8=8B=E5=BA=8F=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=9B=86=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin.xml | 6 +- .../db/redis/RedisScriptTableDataDefine.java | 52 ++++++ .../redis/core/RedisDatabaseConnection.java | 20 ++- .../fr/plugin/db/redis/core/RedisPool.java | 75 ++++++++ .../db/redis/core/RedisScriptTableData.java | 144 +++++++++++++++ .../redis/core/RedisScriptTableDataModel.java | 141 +++++++++++++++ .../plugin/db/redis/core/RedisTableData.java | 26 +-- .../db/redis/core/RedisTableDataModel.java | 4 +- .../db/redis/core/visit/AbstractVisitor.java | 2 +- .../plugin/db/redis/core/visit/Visitor.java | 3 +- .../db/redis/core/visit/VisitorFactory.java | 2 +- .../db/redis/core/visit/impl/GetVisitor.java | 2 +- .../redis/core/visit/impl/HashGetVisitor.java | 2 +- .../core/visit/impl/HashKeysVisitor.java | 2 +- .../core/visit/impl/HashMGetVisitor.java | 2 +- .../db/redis/core/visit/impl/HashVisitor.java | 2 +- .../db/redis/core/visit/impl/KeysVisitor.java | 2 +- .../db/redis/core/visit/impl/ListVisitor.java | 2 +- .../db/redis/core/visit/impl/MGetVisitor.java | 2 +- .../db/redis/core/visit/impl/SetVisitor.java | 2 +- .../redis/core/visit/impl/ZRangeVisitor.java | 4 +- .../com/fr/plugin/db/redis/help/Console.java | 36 ++++ .../com/fr/plugin/db/redis/help/Files.java | 34 ++++ .../fr/plugin/db/redis/help/RedisClient.java | 102 +++++++++++ .../db/redis/ui/RedisBaseTableDataPane.java | 164 ++++++++++++++++++ .../db/redis/ui/RedisConnectionPane.java | 14 +- .../fr/plugin/db/redis/ui/RedisQueryPane.java | 26 +-- .../plugin/db/redis/ui/RedisScriptPane.java | 71 ++++++++ .../db/redis/ui/RedisScriptTableDataPane.java | 76 ++++++++ .../db/redis/ui/RedisTableDataPane.java | 135 ++------------ .../db/redis/util/RedisDesignUtils.java | 24 +++ .../fr/plugin/db/redis/util/RedisUtils.java | 40 +++++ .../plugin/db/redis/locale/redis.properties | 3 + .../db/redis/locale/redis_en_US.properties | 3 + .../db/redis/locale/redis_zh_CN.properties | 5 +- 35 files changed, 1044 insertions(+), 186 deletions(-) create mode 100644 src/main/java/com/fr/plugin/db/redis/RedisScriptTableDataDefine.java create mode 100644 src/main/java/com/fr/plugin/db/redis/core/RedisPool.java create mode 100644 src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableData.java create mode 100644 src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableDataModel.java create mode 100644 src/main/java/com/fr/plugin/db/redis/help/Console.java create mode 100644 src/main/java/com/fr/plugin/db/redis/help/Files.java create mode 100644 src/main/java/com/fr/plugin/db/redis/help/RedisClient.java create mode 100644 src/main/java/com/fr/plugin/db/redis/ui/RedisBaseTableDataPane.java create mode 100644 src/main/java/com/fr/plugin/db/redis/ui/RedisScriptPane.java create mode 100644 src/main/java/com/fr/plugin/db/redis/ui/RedisScriptTableDataPane.java create mode 100644 src/main/java/com/fr/plugin/db/redis/util/RedisDesignUtils.java create mode 100644 src/main/java/com/fr/plugin/db/redis/util/RedisUtils.java diff --git a/plugin.xml b/plugin.xml index b1c65ad..6dc3add 100755 --- a/plugin.xml +++ b/plugin.xml @@ -3,12 +3,13 @@ com.fr.solution.plugin.db.redis.v10 yes - 4.2 + 5.0 10.0 2018-11-29 richie [2019-02-15]结果集可以通过JavaScript脚本进行转换。
[2018-11-30]解决远程设计时无法查询key列表的问题。
[2018-11-26]解决10.0版本中服务器数据集无法保存的问题。
@@ -25,6 +26,9 @@ + + + \ No newline at end of file diff --git a/src/main/java/com/fr/plugin/db/redis/RedisScriptTableDataDefine.java b/src/main/java/com/fr/plugin/db/redis/RedisScriptTableDataDefine.java new file mode 100644 index 0000000..52f73fc --- /dev/null +++ b/src/main/java/com/fr/plugin/db/redis/RedisScriptTableDataDefine.java @@ -0,0 +1,52 @@ +package com.fr.plugin.db.redis; + +import com.fr.base.TableData; +import com.fr.design.data.tabledata.tabledatapane.AbstractTableDataPane; +import com.fr.design.fun.ServerTableDataDefineProvider; +import com.fr.design.fun.impl.AbstractTableDataDefineProvider; +import com.fr.locale.InterProviderFactory; +import com.fr.plugin.db.redis.core.RedisScriptTableData; +import com.fr.plugin.db.redis.ui.RedisScriptTableDataPane; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-03-18 + */ +public class RedisScriptTableDataDefine extends AbstractTableDataDefineProvider implements ServerTableDataDefineProvider { + + @Override + public int currentAPILevel() { + return CURRENT_LEVEL; + } + + @Override + public Class classForTableData() { + return RedisScriptTableData.class; + } + + @Override + public Class classForInitTableData() { + return RedisScriptTableData.class; + } + + @Override + public Class appearanceForTableData() { + return RedisScriptTableDataPane.class; + } + + @Override + public String nameForTableData() { + return InterProviderFactory.getProvider().getLocText("Plugin-Redis_Script_Table_Data"); + } + + @Override + public String prefixForTableData() { + return "redis"; + } + + @Override + public String iconPathForTableData() { + return "/com/fr/plugin/db/redis/images/redis.png"; + } +} diff --git a/src/main/java/com/fr/plugin/db/redis/core/RedisDatabaseConnection.java b/src/main/java/com/fr/plugin/db/redis/core/RedisDatabaseConnection.java index 299c739..6c931f7 100755 --- a/src/main/java/com/fr/plugin/db/redis/core/RedisDatabaseConnection.java +++ b/src/main/java/com/fr/plugin/db/redis/core/RedisDatabaseConnection.java @@ -4,7 +4,6 @@ import com.fr.config.holder.Conf; import com.fr.config.holder.factory.Holders; import com.fr.data.impl.AbstractDatabaseConnection; import com.fr.data.impl.Connection; -import com.fr.general.Inter; import com.fr.locale.InterProviderFactory; import com.fr.security.SecurityToolbox; import com.fr.stable.ArrayUtils; @@ -12,7 +11,7 @@ import com.fr.stable.StableUtils; import com.fr.stable.StringUtils; import com.fr.stable.xml.XMLPrintWriter; import com.fr.stable.xml.XMLableReader; -import redis.clients.jedis.Jedis; +import com.fr.third.redis.clients.jedis.Jedis; import java.util.List; import java.util.Set; @@ -20,10 +19,12 @@ import java.util.Set; public class RedisDatabaseConnection extends AbstractDatabaseConnection { private static final int DEFAULT_REDIS_PORT = 6379; + private static final int MAX_TOTAL = 10; private Conf host = Holders.simple(StringUtils.EMPTY); private Conf port = Holders.simple(DEFAULT_REDIS_PORT); private Conf password = Holders.simple(StringUtils.EMPTY); + private Conf maxTotal = Holders.simple(MAX_TOTAL); public RedisDatabaseConnection() { @@ -53,7 +54,13 @@ public class RedisDatabaseConnection extends AbstractDatabaseConnection { this.password.set(password); } + public int getMaxTotal() { + return maxTotal.get(); + } + public void setMaxTotal(Integer maxTotal) { + this.maxTotal.set(maxTotal); + } @Override public void testConnection() throws Exception { @@ -70,11 +77,7 @@ public class RedisDatabaseConnection extends AbstractDatabaseConnection { } public Jedis createRedisClient() { - Jedis client = new Jedis(getHost(), getPort()); - if (StringUtils.isNotEmpty(getPassword())) { - client.auth(getPassword()); - } - return client; + return RedisPool.getPool().getResource(getHost(), getPort(), getPassword(), getMaxTotal()); } @Override @@ -148,6 +151,7 @@ public class RedisDatabaseConnection extends AbstractDatabaseConnection { if (StringUtils.isNotEmpty(pwd)) { setPassword(SecurityToolbox.encrypt(pwd)); } + setMaxTotal(reader.getAttrAsInt("maxTotal", MAX_TOTAL)); } } } @@ -161,6 +165,7 @@ public class RedisDatabaseConnection extends AbstractDatabaseConnection { if (StringUtils.isNotEmpty(getPassword())) { writer.attr("password", SecurityToolbox.decrypt(getPassword())); } + writer.attr("maxTotal", getMaxTotal()); writer.end(); } @@ -170,6 +175,7 @@ public class RedisDatabaseConnection extends AbstractDatabaseConnection { cloned.host = (Conf) host.clone(); cloned.port = (Conf) port.clone(); cloned.password = (Conf) password.clone(); + cloned.maxTotal = (Conf) maxTotal.clone(); return cloned; } } \ No newline at end of file diff --git a/src/main/java/com/fr/plugin/db/redis/core/RedisPool.java b/src/main/java/com/fr/plugin/db/redis/core/RedisPool.java new file mode 100644 index 0000000..a12daa9 --- /dev/null +++ b/src/main/java/com/fr/plugin/db/redis/core/RedisPool.java @@ -0,0 +1,75 @@ +package com.fr.plugin.db.redis.core; + +import com.fr.stable.StringUtils; +import com.fr.third.redis.clients.jedis.Jedis; +import com.fr.third.redis.clients.jedis.JedisPool; +import com.fr.third.redis.clients.jedis.JedisPoolConfig; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-03-18 + */ +public class RedisPool { + + private static final int TIME_OUT = 100 * 1000; + + private static RedisPool pool = new RedisPool(); + + public static RedisPool getPool() { + return pool; + } + + private Map jedisPoolMap = new ConcurrentHashMap(); + + public Jedis getResource(String host, int port, String password, int maxTotal) { + JedisPool jedisPool = jedisPoolMap.get(host); + if (jedisPool == null) { + jedisPool = createJedisPool(host, port, password, maxTotal); + jedisPoolMap.put(host, jedisPool); + } + return jedisPool.getResource(); + } + + private JedisPool createJedisPool(String host, int port, String password, int maxTotal) { + JedisPoolConfig config = new JedisPoolConfig(); + config.setMaxTotal(maxTotal); + //连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true + config.setBlockWhenExhausted(true); + //设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数) + config.setEvictionPolicyClassName("com.fr.third.org.apache.commons.pool2.impl.DefaultEvictionPolicy"); + //是否启用pool的jmx管理功能, 默认true + config.setJmxEnabled(true); + //MBean ObjectName = new ObjectName("org.apache.commons.pool2:type=GenericObjectPool,name=" + "pool" + i); 默认为"pool", JMX不熟,具体不知道是干啥的...默认就好. + config.setJmxNamePrefix("pool"); + //是否启用后进先出, 默认true + config.setLifo(true); + //最大空闲连接数 + config.setMaxIdle(10); + //最大连接数,可配置 + //获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1 + config.setMaxWaitMillis(-1); + //逐出连接的最小空闲时间 默认1800000毫秒(30分钟) + config.setMinEvictableIdleTimeMillis(1800000); + //最小空闲连接数, 默认0 + config.setMinIdle(0); + //每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3 + config.setNumTestsPerEvictionRun(3); + //对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断 (默认逐出策略) + config.setSoftMinEvictableIdleTimeMillis(1800000); + //在获取连接的时候检查有效性, 默认false + config.setTestOnBorrow(false); + //在空闲时检查有效性, 默认false + config.setTestWhileIdle(false); + //逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1 + config.setTimeBetweenEvictionRunsMillis(-1); + if (StringUtils.isNotBlank(password)) { + return new JedisPool(config, host, port, TIME_OUT, password); + } else { + return new JedisPool(config, host, port, TIME_OUT); + } + } +} diff --git a/src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableData.java b/src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableData.java new file mode 100644 index 0000000..25befc0 --- /dev/null +++ b/src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableData.java @@ -0,0 +1,144 @@ +package com.fr.plugin.db.redis.core; + +import com.fr.base.Parameter; +import com.fr.base.TableData; +import com.fr.config.holder.Conf; +import com.fr.config.holder.factory.Holders; +import com.fr.config.holder.factory.XmlHolders; +import com.fr.data.AbstractParameterTableData; +import com.fr.data.core.DataCoreXmlUtils; +import com.fr.data.impl.Connection; +import com.fr.data.impl.NameDatabaseConnection; +import com.fr.file.DatasourceManager; +import com.fr.general.data.DataModel; +import com.fr.general.xml.GeneralXMLTools; +import com.fr.intelli.record.Focus; +import com.fr.log.FineLoggerFactory; +import com.fr.plugin.db.redis.core.order.OrderValue; +import com.fr.plugin.db.redis.core.order.impl.NumberOrderValue; +import com.fr.plugin.db.redis.util.RedisUtils; +import com.fr.record.analyzer.EnableMetrics; +import com.fr.script.Calculator; +import com.fr.stable.ParameterProvider; +import com.fr.stable.StringUtils; +import com.fr.stable.xml.XMLPrintWriter; +import com.fr.stable.xml.XMLableReader; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-03-18 + */ +@EnableMetrics +public class RedisScriptTableData extends AbstractParameterTableData { + + private static final long serialVersionUID = 1525853354993816818L; + + private Conf database = Holders.obj(null, Connection.class); + private Conf dbIndex = XmlHolders.obj(new NumberOrderValue(0), OrderValue.class, OrderValue.XML_TAG); + private Conf script = Holders.simple(StringUtils.EMPTY); + + public void setDatabase(Connection c) { + this.database.set(c); + } + + public Connection getDatabase() { + return database.get(); + } + + public OrderValue getOrderValue() { + return dbIndex.get(); + } + + public void setOrderValue(OrderValue dbIndex) { + this.dbIndex.set(dbIndex); + } + + public String getScript() { + return script.get(); + } + + public void setScript(String script) { + this.script.set(script); + } + + public void setParameters(ParameterProvider[] providers) { + super.setDefaultParameters(providers); + } + + @Override + public DataModel createDataModel(Calculator calculator) { + return createDataModel(calculator, TableData.RESULT_ALL); + } + + @Override + @Focus(id = RedisConstants.PLUGIN_ID, text = "Plugin-Redis_Script_Table_Data") + public DataModel createDataModel(Calculator calculator, int rowCount) { + long start = System.currentTimeMillis(); + Parameter[] ps = Parameter.providers2Parameter(getParameters(calculator)); + Connection connection = database.get(); + if (connection instanceof NameDatabaseConnection) { + String name = ((NameDatabaseConnection) connection).getName(); + RedisDatabaseConnection rc = DatasourceManager.getProviderInstance().getConnection(name, RedisDatabaseConnection.class); + if (rc != null) { + OrderValue orderValue = dbIndex.get(); + DataModel model = new RedisScriptTableDataModel(rc, + orderValue == null ? 0 : orderValue.toIndex(calculator, ps), + RedisUtils.calculateQuery(script.get(), ps), + rowCount); + FineLoggerFactory.getLogger().info("Build data model spend time {} ms.", System.currentTimeMillis() - start); + return model; + } + } + return null; + } + + public void readXML(XMLableReader reader) { + super.readXML(reader); + + if (reader.isChildNode()) { + String tmpName = reader.getTagName(); + String tmpVal; + + if (OrderValue.XML_TAG.equals(tmpName)) { + OrderValue orderValue = (OrderValue) GeneralXMLTools.readXMLable(reader); + if (orderValue != null) { + setOrderValue(orderValue); + } + } else if (com.fr.data.impl.Connection.XML_TAG.equals(tmpName)) { + if (reader.getAttrAsString("class", null) != null) { + com.fr.data.impl.Connection con = DataCoreXmlUtils.readXMLConnection(reader); + this.setDatabase(con); + } + } else if ("Script".equals(tmpName)) { + tmpVal = reader.getElementValue(); + if (isNotNullValue(tmpVal)) { + this.setScript(tmpVal); + } + } + } + } + + @Override + public void writeXML(XMLPrintWriter writer) { + super.writeXML(writer); + GeneralXMLTools.writeXMLable(writer, dbIndex.get(), OrderValue.XML_TAG); + if (this.database.get() != null) { + DataCoreXmlUtils.writeXMLConnection(writer, this.database.get()); + } + writer.startTAG("Script").textNode(getScript()).end(); + } + + private boolean isNotNullValue(String value) { + return value != null && !"null".equals(value); + } + + @Override + public Object clone() throws CloneNotSupportedException { + RedisScriptTableData cloned = (RedisScriptTableData) super.clone(); + cloned.database = (Conf) database.clone(); + cloned.script = (Conf) script.clone(); + cloned.dbIndex = (Conf) dbIndex.clone(); + return cloned; + } +} diff --git a/src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableDataModel.java b/src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableDataModel.java new file mode 100644 index 0000000..f8f3c09 --- /dev/null +++ b/src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableDataModel.java @@ -0,0 +1,141 @@ +package com.fr.plugin.db.redis.core; + +import com.eclipsesource.v8.V8; +import com.eclipsesource.v8.V8Array; +import com.eclipsesource.v8.V8Object; +import com.fr.data.AbstractDataModel; +import com.fr.plugin.db.redis.help.Console; +import com.fr.plugin.db.redis.help.Files; +import com.fr.plugin.db.redis.help.RedisClient; +import com.fr.plugin.db.redis.util.RedisUtils; +import com.fr.third.redis.clients.jedis.Jedis; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-03-18 + */ +public class RedisScriptTableDataModel extends AbstractDataModel { + + private RedisDatabaseConnection mc; + private String script; + private int dbIndex; + private int rowCount; + private List> data = new ArrayList>(); + private String[] columnNames = null; + + public RedisScriptTableDataModel(RedisDatabaseConnection mc, int dbIndex, String script, int rowCount) { + this.mc = mc; + this.script = script; + this.dbIndex = dbIndex; + this.rowCount = rowCount; + } + + private void buildData() { + if (columnNames != null) { + return; + } + Jedis jedis = mc.createRedisClient(); + jedis.select(dbIndex); + V8 v8 = V8.createV8Runtime(); + RedisClient client = new RedisClient(v8, jedis); + V8Object v8Console = initConsole(v8); + V8Object v8Require = initRequire(v8); + V8Object v8Client = initRedis(v8, client); + + v8.executeVoidScript( + "function unique(array) {return Array.from(new Set(array));};" + + "function merge(table, column) {return {content:table, column:column}};"); + + V8Object returnObj = v8.executeObjectScript(String.format("(function(){%s})();", script)); + + V8Array v8Column = returnObj.getArray("column"); + V8Array v8Table = returnObj.getArray("content"); + int columnCount = v8Column.length(); + columnNames = new String[columnCount]; + for (int i = 0; i < columnCount; i++) { + columnNames[i] = String.valueOf(v8Column.get(i)); + } + int returnRowCount = v8Table.length(); + for (int i = 0, realCount = Math.min(returnRowCount, this.rowCount); i < realCount; i++) { + V8Array v8Row = v8Table.getArray(i); + List row = new ArrayList(); + for (int j = 0, col = v8Row.length(); j < col; j++) { + row.add(v8Row.get(j)); + } + v8Row.release(); + data.add(row); + } + jedis.close(); + v8Table.release(); + v8Column.release(); + returnObj.release(); + v8Client.release(); + v8Require.release(); + v8Console.release(); + + v8.release(true); + } + + private V8Object initConsole(V8 v8) { + V8Object v8Console = new V8Object(v8); + v8.add("console", v8Console); + Console console = new Console(v8); + RedisUtils.registerJavaMethods(v8Console, console); + return v8Console; + } + + private V8Object initRequire(V8 v8) { + V8Object v8Files = new V8Object(v8); + v8.add("Files", v8Files); + Files console = new Files(v8); + RedisUtils.registerJavaMethods(v8Files, console); + return v8Files; + } + + private V8Object initRedis(final V8 v8, final RedisClient client) { + V8Object v8Client = new V8Object(v8); + v8.add("redis", v8Client); + RedisUtils.registerJavaMethods(v8Client, client); + return v8Client; + } + + @Override + public int getColumnCount() { + buildData(); + return columnNames == null ? 0 : columnNames.length; + } + + @Override + public String getColumnName(int i) { + buildData(); + return columnNames == null ? null : columnNames[i]; + } + + @Override + public int getRowCount() { + buildData(); + return data.size(); + } + + @Override + public Object getValueAt(int rowIndex, int columnIndex) { + buildData(); + if (data != null && data.size() > rowIndex) { + List rowData = data.get(rowIndex); + if (rowData != null && rowData.size() > columnIndex) { + return rowData.get(columnIndex); + } + } + return null; + } + + @Override + public void release() throws Exception { + + } +} diff --git a/src/main/java/com/fr/plugin/db/redis/core/RedisTableData.java b/src/main/java/com/fr/plugin/db/redis/core/RedisTableData.java index 4fd6a10..529b143 100644 --- a/src/main/java/com/fr/plugin/db/redis/core/RedisTableData.java +++ b/src/main/java/com/fr/plugin/db/redis/core/RedisTableData.java @@ -15,8 +15,10 @@ import com.fr.general.data.DataModel; import com.fr.general.xml.GeneralXMLTools; import com.fr.intelli.record.Focus; import com.fr.intelli.record.Original; +import com.fr.log.FineLoggerFactory; import com.fr.plugin.db.redis.core.order.OrderValue; import com.fr.plugin.db.redis.core.order.impl.NumberOrderValue; +import com.fr.plugin.db.redis.util.RedisUtils; import com.fr.record.analyzer.EnableMetrics; import com.fr.script.Calculator; import com.fr.stable.ArrayUtils; @@ -82,6 +84,7 @@ public class RedisTableData extends AbstractParameterTableData { @Override @Focus(id = RedisConstants.PLUGIN_ID, text = "Plugin-Redis_DB", source = Original.PLUGIN) public DataModel createDataModel(Calculator calculator, int rowCount) { + long start = System.currentTimeMillis(); Parameter[] ps = Parameter.providers2Parameter(getParameters(calculator)); Connection connection = database.get(); if (connection instanceof NameDatabaseConnection) { @@ -89,30 +92,19 @@ public class RedisTableData extends AbstractParameterTableData { RedisDatabaseConnection rc = DatasourceManager.getProviderInstance().getConnection(name, RedisDatabaseConnection.class); if (rc != null) { OrderValue orderValue = dbIndex.get(); - return new RedisTableDataModel(calculator, ps, rc, + DataModel model = new RedisTableDataModel(calculator, ps, rc, orderValue == null ? 0 : orderValue.toIndex(calculator, ps), - calculateQuery(query.get(), ps), - calculateQuery(script.get(), ps), + RedisUtils.calculateQuery(query.get(), ps), + RedisUtils.calculateQuery(script.get(), ps), rowCount); + FineLoggerFactory.getLogger().info("Build data model spend time {} ms.", System.currentTimeMillis() - start); + return model; } } return null; } - private String calculateQuery(String query, Parameter[] ps) { - if (ArrayUtils.isEmpty(ps)) { - return query; - } - Map map = new HashMap(); - for (Parameter p : ps) { - map.put(p.getName(), p.getValue()); - } - try { - return TemplateUtils.renderParameter4Tpl(query, map); - } catch (Exception e) { - return query; - } - } + public void readXML(XMLableReader reader) { super.readXML(reader); diff --git a/src/main/java/com/fr/plugin/db/redis/core/RedisTableDataModel.java b/src/main/java/com/fr/plugin/db/redis/core/RedisTableDataModel.java index b49b49d..91e1acd 100755 --- a/src/main/java/com/fr/plugin/db/redis/core/RedisTableDataModel.java +++ b/src/main/java/com/fr/plugin/db/redis/core/RedisTableDataModel.java @@ -9,7 +9,7 @@ import com.fr.plugin.PluginLicenseManager; import com.fr.plugin.db.redis.core.visit.VisitorFactory; import com.fr.script.Calculator; import com.fr.stable.StringUtils; -import redis.clients.jedis.Jedis; +import com.fr.third.redis.clients.jedis.Jedis; import java.util.List; @@ -39,7 +39,9 @@ public class RedisTableDataModel extends AbstractDataModel { FineLoggerFactory.getLogger().info("Connect to redis and select database:" + dbIndex); } try { + long start = System.currentTimeMillis(); DataWrapper wrapper = VisitorFactory.getKeyValueResult(calculator, ps, redisClient, query, rowCount); + FineLoggerFactory.getLogger().info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start); wrapper.transform(script); data = wrapper.getData(); columnNames = wrapper.getColumnNames(); diff --git a/src/main/java/com/fr/plugin/db/redis/core/visit/AbstractVisitor.java b/src/main/java/com/fr/plugin/db/redis/core/visit/AbstractVisitor.java index 2d6bf36..4d1ab44 100644 --- a/src/main/java/com/fr/plugin/db/redis/core/visit/AbstractVisitor.java +++ b/src/main/java/com/fr/plugin/db/redis/core/visit/AbstractVisitor.java @@ -5,7 +5,7 @@ import com.fr.plugin.db.redis.core.DataWrapper; import com.fr.plugin.db.redis.core.RedisConstants; import com.fr.script.Calculator; import com.fr.stable.StringUtils; -import redis.clients.jedis.Jedis; +import com.fr.third.redis.clients.jedis.Jedis; /** * Created by richie on 2017/5/2. diff --git a/src/main/java/com/fr/plugin/db/redis/core/visit/Visitor.java b/src/main/java/com/fr/plugin/db/redis/core/visit/Visitor.java index 53f1898..7a25dc2 100644 --- a/src/main/java/com/fr/plugin/db/redis/core/visit/Visitor.java +++ b/src/main/java/com/fr/plugin/db/redis/core/visit/Visitor.java @@ -3,8 +3,7 @@ package com.fr.plugin.db.redis.core.visit; import com.fr.base.Parameter; import com.fr.plugin.db.redis.core.DataWrapper; import com.fr.script.Calculator; -import com.fr.stable.StringUtils; -import redis.clients.jedis.Jedis; +import com.fr.third.redis.clients.jedis.Jedis; import java.util.List; diff --git a/src/main/java/com/fr/plugin/db/redis/core/visit/VisitorFactory.java b/src/main/java/com/fr/plugin/db/redis/core/visit/VisitorFactory.java index 911b9fd..6141296 100644 --- a/src/main/java/com/fr/plugin/db/redis/core/visit/VisitorFactory.java +++ b/src/main/java/com/fr/plugin/db/redis/core/visit/VisitorFactory.java @@ -4,7 +4,7 @@ import com.fr.base.Parameter; import com.fr.plugin.db.redis.core.DataWrapper; import com.fr.plugin.db.redis.core.visit.impl.*; import com.fr.script.Calculator; -import redis.clients.jedis.Jedis; +import com.fr.third.redis.clients.jedis.Jedis; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/GetVisitor.java b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/GetVisitor.java index 281104e..947f69c 100644 --- a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/GetVisitor.java +++ b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/GetVisitor.java @@ -3,7 +3,7 @@ package com.fr.plugin.db.redis.core.visit.impl; import com.fr.base.Parameter; import com.fr.plugin.db.redis.core.visit.AbstractVisitor; import com.fr.script.Calculator; -import redis.clients.jedis.Jedis; +import com.fr.third.redis.clients.jedis.Jedis; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/HashGetVisitor.java b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/HashGetVisitor.java index e6fb36c..b66df87 100644 --- a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/HashGetVisitor.java +++ b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/HashGetVisitor.java @@ -3,7 +3,7 @@ package com.fr.plugin.db.redis.core.visit.impl; import com.fr.base.Parameter; import com.fr.plugin.db.redis.core.visit.AbstractVisitor; import com.fr.script.Calculator; -import redis.clients.jedis.Jedis; +import com.fr.third.redis.clients.jedis.Jedis; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/HashKeysVisitor.java b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/HashKeysVisitor.java index bf6b21a..ee9cba6 100644 --- a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/HashKeysVisitor.java +++ b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/HashKeysVisitor.java @@ -3,7 +3,7 @@ package com.fr.plugin.db.redis.core.visit.impl; import com.fr.base.Parameter; import com.fr.plugin.db.redis.core.visit.AbstractVisitor; import com.fr.script.Calculator; -import redis.clients.jedis.Jedis; +import com.fr.third.redis.clients.jedis.Jedis; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/HashMGetVisitor.java b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/HashMGetVisitor.java index 73f5549..ccae5e5 100644 --- a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/HashMGetVisitor.java +++ b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/HashMGetVisitor.java @@ -3,7 +3,7 @@ package com.fr.plugin.db.redis.core.visit.impl; import com.fr.base.Parameter; import com.fr.plugin.db.redis.core.visit.AbstractVisitor; import com.fr.script.Calculator; -import redis.clients.jedis.Jedis; +import com.fr.third.redis.clients.jedis.Jedis; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/HashVisitor.java b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/HashVisitor.java index 0ed4e07..96822f9 100644 --- a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/HashVisitor.java +++ b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/HashVisitor.java @@ -3,7 +3,7 @@ package com.fr.plugin.db.redis.core.visit.impl; import com.fr.base.Parameter; import com.fr.plugin.db.redis.core.visit.AbstractVisitor; import com.fr.script.Calculator; -import redis.clients.jedis.Jedis; +import com.fr.third.redis.clients.jedis.Jedis; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/KeysVisitor.java b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/KeysVisitor.java index aff98d7..8177bb6 100644 --- a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/KeysVisitor.java +++ b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/KeysVisitor.java @@ -3,7 +3,7 @@ package com.fr.plugin.db.redis.core.visit.impl; import com.fr.base.Parameter; import com.fr.plugin.db.redis.core.visit.AbstractVisitor; import com.fr.script.Calculator; -import redis.clients.jedis.Jedis; +import com.fr.third.redis.clients.jedis.Jedis; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/ListVisitor.java b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/ListVisitor.java index 8010619..a254833 100644 --- a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/ListVisitor.java +++ b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/ListVisitor.java @@ -4,7 +4,7 @@ import com.fr.base.Parameter; import com.fr.base.TableData; import com.fr.plugin.db.redis.core.visit.AbstractVisitor; import com.fr.script.Calculator; -import redis.clients.jedis.Jedis; +import com.fr.third.redis.clients.jedis.Jedis; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/MGetVisitor.java b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/MGetVisitor.java index 5a21fc7..fc25e2b 100644 --- a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/MGetVisitor.java +++ b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/MGetVisitor.java @@ -4,7 +4,7 @@ import com.fr.base.Parameter; import com.fr.plugin.db.redis.core.visit.AbstractVisitor; import com.fr.script.Calculator; import com.fr.stable.ArrayUtils; -import redis.clients.jedis.Jedis; +import com.fr.third.redis.clients.jedis.Jedis; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/SetVisitor.java b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/SetVisitor.java index 8d03314..e51ef06 100644 --- a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/SetVisitor.java +++ b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/SetVisitor.java @@ -3,7 +3,7 @@ package com.fr.plugin.db.redis.core.visit.impl; import com.fr.base.Parameter; import com.fr.plugin.db.redis.core.visit.AbstractVisitor; import com.fr.script.Calculator; -import redis.clients.jedis.Jedis; +import com.fr.third.redis.clients.jedis.Jedis; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/ZRangeVisitor.java b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/ZRangeVisitor.java index b58725e..53c9976 100644 --- a/src/main/java/com/fr/plugin/db/redis/core/visit/impl/ZRangeVisitor.java +++ b/src/main/java/com/fr/plugin/db/redis/core/visit/impl/ZRangeVisitor.java @@ -4,8 +4,8 @@ import com.fr.base.Parameter; import com.fr.base.TableData; import com.fr.plugin.db.redis.core.visit.AbstractVisitor; import com.fr.script.Calculator; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.Tuple; +import com.fr.third.redis.clients.jedis.Jedis; +import com.fr.third.redis.clients.jedis.Tuple; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/fr/plugin/db/redis/help/Console.java b/src/main/java/com/fr/plugin/db/redis/help/Console.java new file mode 100644 index 0000000..c7a3669 --- /dev/null +++ b/src/main/java/com/fr/plugin/db/redis/help/Console.java @@ -0,0 +1,36 @@ +package com.fr.plugin.db.redis.help; + +import com.eclipsesource.v8.V8; +import com.fr.general.IOUtils; +import com.fr.io.utils.ResourceIOUtils; +import com.fr.log.FineLoggerFactory; +import com.fr.stable.EncodeConstants; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-03-18 + */ +public class Console { + + private V8 v8; + + public Console(V8 v8) { + this.v8 = v8; + } + + public void log(Object message) { + FineLoggerFactory.getLogger().info(message == null ? null : message.toString()); + } + + public void error(Object message) { + FineLoggerFactory.getLogger().error(message == null ? null : message.toString()); + } + +} diff --git a/src/main/java/com/fr/plugin/db/redis/help/Files.java b/src/main/java/com/fr/plugin/db/redis/help/Files.java new file mode 100644 index 0000000..8e34ab6 --- /dev/null +++ b/src/main/java/com/fr/plugin/db/redis/help/Files.java @@ -0,0 +1,34 @@ +package com.fr.plugin.db.redis.help; + +import com.eclipsesource.v8.V8; +import com.fr.general.IOUtils; +import com.fr.io.utils.ResourceIOUtils; +import com.fr.stable.EncodeConstants; + +import java.io.InputStream; +import java.io.UnsupportedEncodingException; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-03-19 + */ +public class Files { + + private V8 v8; + + public Files(V8 v8) { + this.v8 = v8; + } + + public void require(String filePath) { + InputStream in = ResourceIOUtils.read(filePath); + if (in != null) { + try { + v8.executeVoidScript(IOUtils.inputStream2String(in, EncodeConstants.ENCODING_UTF_8)); + } catch (UnsupportedEncodingException ignore) { + + } + } + } +} diff --git a/src/main/java/com/fr/plugin/db/redis/help/RedisClient.java b/src/main/java/com/fr/plugin/db/redis/help/RedisClient.java new file mode 100644 index 0000000..6dc6bd0 --- /dev/null +++ b/src/main/java/com/fr/plugin/db/redis/help/RedisClient.java @@ -0,0 +1,102 @@ +package com.fr.plugin.db.redis.help; + +import com.eclipsesource.v8.V8; +import com.eclipsesource.v8.V8Array; +import com.eclipsesource.v8.V8Object; +import com.eclipsesource.v8.utils.V8ObjectUtils; +import com.fr.log.FineLoggerFactory; +import com.fr.third.redis.clients.jedis.Jedis; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-03-18 + */ +public class RedisClient { + + private Jedis jedis; + private V8 v8; + + public RedisClient(V8 v8, Jedis jedis) { + this.jedis = jedis; + this.v8 = v8; + } + + public String get(String key) { + long start = System.currentTimeMillis(); + try { + return jedis.get(key); + } finally { + FineLoggerFactory.getLogger().info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start); + } + } + + public String hget(String key, String field) { + long start = System.currentTimeMillis(); + try { + return jedis.hget(key, field); + } finally { + FineLoggerFactory.getLogger().info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start); + } + } + + public V8Array hkeys(String key) { + long start = System.currentTimeMillis(); + Set set = jedis.keys(key); + FineLoggerFactory.getLogger().info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start); + return V8ObjectUtils.toV8Array(v8, new ArrayList(set)); + } + + public V8Array hmget(String key, String... fields) { + long start = System.currentTimeMillis(); + List list = jedis.hmget(key, fields); + FineLoggerFactory.getLogger().info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start); + return V8ObjectUtils.toV8Array(v8, list); + } + + public V8Object hgetAll(String key) { + long start = System.currentTimeMillis(); + Map map = jedis.hgetAll(key); + FineLoggerFactory.getLogger().info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start); + return V8ObjectUtils.toV8Object(v8, map); + } + + public V8Array keys(String pattern) { + long start = System.currentTimeMillis(); + Set set = jedis.keys(pattern); + FineLoggerFactory.getLogger().info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start); + return V8ObjectUtils.toV8Array(v8, new ArrayList(set)); + } + + public V8Array lrange(String key, long start, long end) { + long start0 = System.currentTimeMillis(); + List list = jedis.lrange(key, start, end); + FineLoggerFactory.getLogger().info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start0); + return V8ObjectUtils.toV8Array(v8, list); + } + + public V8Array mget(V8Array keys) { + long start = System.currentTimeMillis(); + int len = keys.length(); + String[] parameters = new String[len]; + for (int i = 0; i < len; i ++) { + parameters[i] = keys.getString(i); + } + List list = jedis.mget(parameters); + FineLoggerFactory.getLogger().info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start); + keys.release(); + return V8ObjectUtils.toV8Array(v8, list); + } + + public V8Array smembers(String key) { + long start = System.currentTimeMillis(); + Set set = jedis.smembers(key); + FineLoggerFactory.getLogger().info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start); + return V8ObjectUtils.toV8Array(v8, new ArrayList(set)); + } +} diff --git a/src/main/java/com/fr/plugin/db/redis/ui/RedisBaseTableDataPane.java b/src/main/java/com/fr/plugin/db/redis/ui/RedisBaseTableDataPane.java new file mode 100644 index 0000000..1de93ad --- /dev/null +++ b/src/main/java/com/fr/plugin/db/redis/ui/RedisBaseTableDataPane.java @@ -0,0 +1,164 @@ +package com.fr.plugin.db.redis.ui; + +import com.fr.base.BaseUtils; +import com.fr.base.ParameterHelper; +import com.fr.base.TableData; +import com.fr.design.actions.UpdateAction; +import com.fr.design.data.datapane.preview.PreviewTablePane; +import com.fr.design.data.tabledata.tabledatapane.AbstractTableDataPane; +import com.fr.design.gui.itableeditorpane.ParameterTableModel; +import com.fr.design.gui.itableeditorpane.UITableEditAction; +import com.fr.design.gui.itableeditorpane.UITableEditorPane; +import com.fr.design.gui.itoolbar.UIToolbar; +import com.fr.design.i18n.Toolkit; +import com.fr.design.menu.ToolBarDef; +import com.fr.plugin.db.redis.core.order.OrderValue; +import com.fr.stable.ArrayUtils; +import com.fr.stable.ParameterProvider; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.util.ArrayList; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-03-18 + */ +public abstract class RedisBaseTableDataPane extends AbstractTableDataPane { + + private static final String PREVIEW_BUTTON = Toolkit.i18nText("Plugin-Redis_Preview"); + private static final String REFRESH_BUTTON = Toolkit.i18nText("Plugin-Redis_Refresh"); + + protected RedisDBConnectionChosePane chosePane; + + protected UITableEditorPane editorPane; + + + public RedisBaseTableDataPane() { + this.setLayout(new BorderLayout(4, 4)); + + Box box = new Box(BoxLayout.Y_AXIS); + + JPanel northPane = new JPanel(new BorderLayout(4, 4)); + JToolBar editToolBar = createToolBar(); + northPane.add(editToolBar, BorderLayout.CENTER); + northPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 6, 0)); + + + ParameterTableModel model = new ParameterTableModel() { + @Override + public UITableEditAction[] createAction() { + return ArrayUtils.add(super.createDBTableAction(), new RedisBaseTableDataPane.RefreshAction()); + } + }; + editorPane = new UITableEditorPane(model); + + box.add(northPane); + + box.add(createQueryPane()); + + box.add(editorPane); + + JPanel sqlSplitPane = new JPanel(new BorderLayout(4, 4)); + sqlSplitPane.add(box, BorderLayout.CENTER); + + chosePane = new RedisDBConnectionChosePane(); + chosePane.setPreferredSize(new Dimension(200, 200)); + sqlSplitPane.add(chosePane, BorderLayout.WEST); + + this.add(sqlSplitPane, BorderLayout.CENTER); + } + + private JToolBar createToolBar() { + ToolBarDef toolBarDef = new ToolBarDef(); + toolBarDef.addShortCut(new RedisBaseTableDataPane.PreviewAction()); + UIToolbar editToolBar = ToolBarDef.createJToolBar(); + toolBarDef.updateToolBar(editToolBar); + return editToolBar; + } + + @Override + protected String title4PopupWindow() { + return Toolkit.i18nText("Plugin-Redis_Query"); + } + + protected abstract JComponent createQueryPane(); + + protected abstract String[] paramTexts(); + + public abstract OrderValue getOrderValue(); + + + private void refresh() { + String[] paramTexts = paramTexts(); + + ParameterProvider[] parameters = ParameterHelper.analyze4Parameters(paramTexts, false); + + ParameterProvider[] providers = getOrderValue().analyze4Parameters(); + + editorPane.populate(ArrayUtils.addAll(parameters, providers)); + } + + + private void checkParameter() { + String[] paramTexts = paramTexts(); + + ParameterProvider[] parameters = ParameterHelper.analyze4Parameters(paramTexts, false); + parameters = ArrayUtils.addAll(parameters, getOrderValue().analyze4Parameters()); + + if (parameters.length < 1 && editorPane.update().size() < 1) { + return; + } + boolean isIn = true; + java.util.List list = editorPane.update(); + java.util.List name = new ArrayList(); + for (int i = 0; i < list.size(); i++) { + name.add(list.get(i).getName()); + } + for (int i = 0; i < parameters.length; i++) { + if (!name.contains(parameters[i].getName())) { + isIn = false; + break; + } + } + if (list.size() == parameters.length && isIn) { + return; + } + refresh(); + } + + + private class PreviewAction extends UpdateAction { + public PreviewAction() { + this.setName(PREVIEW_BUTTON); + this.setMnemonic('P'); + this.setSmallIcon(BaseUtils.readIcon("/com/fr/design/images/m_file/preview.png")); + } + + public void actionPerformed(ActionEvent evt) { + checkParameter(); + PreviewTablePane.previewTableData(RedisBaseTableDataPane.this.updateBean()); + } + } + + + protected class RefreshAction extends UITableEditAction { + public RefreshAction() { + this.setName(REFRESH_BUTTON); + this.setSmallIcon(BaseUtils.readIcon("/com/fr/design/images/control/refresh.png")); + } + + public void actionPerformed(ActionEvent e) { + refresh(); + } + + @Override + public void checkEnabled() { + } + } + + + +} diff --git a/src/main/java/com/fr/plugin/db/redis/ui/RedisConnectionPane.java b/src/main/java/com/fr/plugin/db/redis/ui/RedisConnectionPane.java index 40040c9..7609f4b 100755 --- a/src/main/java/com/fr/plugin/db/redis/ui/RedisConnectionPane.java +++ b/src/main/java/com/fr/plugin/db/redis/ui/RedisConnectionPane.java @@ -6,6 +6,7 @@ import com.fr.design.gui.ibutton.UIPasswordField; import com.fr.design.gui.ilable.UILabel; import com.fr.design.gui.itextfield.UIIntNumberField; import com.fr.design.gui.itextfield.UITextField; +import com.fr.design.i18n.Toolkit; import com.fr.design.layout.FRGUIPaneFactory; import com.fr.design.layout.TableLayout; import com.fr.design.layout.TableLayoutHelper; @@ -21,6 +22,7 @@ public class RedisConnectionPane extends DatabaseConnectionPane { + + private RedisScriptPane scriptPane; + + public RedisScriptTableDataPane() { + super(); + } + + @Override + protected JComponent createQueryPane() { + if (scriptPane == null) { + scriptPane = new RedisScriptPane(); + } + return scriptPane; + } + + @Override + protected String[] paramTexts() { + return new String[] {scriptPane.getScript()}; + } + + @Override + public OrderValue getOrderValue() { + return scriptPane.getOrderValue(); + } + + @Override + public void populateBean(RedisScriptTableData tableData) { + if (tableData == null) { + return; + } + Calculator c = Calculator.createCalculator(); + + editorPane.populate(tableData.getParameters(c)); + + chosePane.populateConnection(tableData.getDatabase()); + + scriptPane.setScript(tableData.getScript()); + + scriptPane.setOrderValue(tableData.getOrderValue()); + } + + @Override + public RedisScriptTableData updateBean() { + RedisScriptTableData tableData = new RedisScriptTableData(); + String connectionName = chosePane.getSelectRedisConnectionName(); + if (StringUtils.isNotEmpty(connectionName)) { + tableData.setDatabase(new NameDatabaseConnection(connectionName)); + } + List parameterList = editorPane.update(); + ParameterProvider[] parameters = parameterList.toArray(new ParameterProvider[parameterList.size()]); + tableData.setParameters(parameters); + + tableData.setScript(scriptPane.getScript()); + tableData.setOrderValue(scriptPane.getOrderValue()); + + return tableData; + } +} diff --git a/src/main/java/com/fr/plugin/db/redis/ui/RedisTableDataPane.java b/src/main/java/com/fr/plugin/db/redis/ui/RedisTableDataPane.java index 0efb4d8..8a76a0e 100755 --- a/src/main/java/com/fr/plugin/db/redis/ui/RedisTableDataPane.java +++ b/src/main/java/com/fr/plugin/db/redis/ui/RedisTableDataPane.java @@ -15,6 +15,7 @@ import com.fr.design.menu.ToolBarDef; import com.fr.general.ComparatorUtils; import com.fr.general.Inter; import com.fr.plugin.db.redis.core.RedisTableData; +import com.fr.plugin.db.redis.core.order.OrderValue; import com.fr.script.Calculator; import com.fr.stable.ArrayUtils; import com.fr.stable.ParameterProvider; @@ -28,113 +29,35 @@ import java.util.ArrayList; import java.util.List; -public class RedisTableDataPane extends AbstractTableDataPane { - private static final String PREVIEW_BUTTON = Toolkit.i18nText("Plugin-Redis_Preview"); - private static final String REFRESH_BUTTON = Toolkit.i18nText("Plugin-Redis_Refresh"); - - private RedisDBConnectionChosePane chosePane; - - private UITableEditorPane editorPane; +public class RedisTableDataPane extends RedisBaseTableDataPane { private RedisQueryPane queryPane; public RedisTableDataPane() { - this.setLayout(new BorderLayout(4, 4)); - - - Box box = new Box(BoxLayout.Y_AXIS); - - - queryPane = new RedisQueryPane(); - JPanel northPane = new JPanel(new BorderLayout(4, 4)); - JToolBar editToolBar = createToolBar(); - northPane.add(editToolBar, BorderLayout.CENTER); - northPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 6, 0)); - - - ParameterTableModel model = new ParameterTableModel() { - @Override - public UITableEditAction[] createAction() { - return ArrayUtils.add(super.createDBTableAction(), new RefreshAction()); - } - }; - editorPane = new UITableEditorPane(model); - - - box.add(northPane); - - box.add(queryPane); - - box.add(editorPane); - - JPanel sqlSplitPane = new JPanel(new BorderLayout(4, 4)); - sqlSplitPane.add(box, BorderLayout.CENTER); - - chosePane = new RedisDBConnectionChosePane(); - chosePane.setPreferredSize(new Dimension(200, 200)); - sqlSplitPane.add(chosePane, BorderLayout.WEST); - - this.add(sqlSplitPane, BorderLayout.CENTER); - } - - private boolean isPreviewOrRefreshButton(FocusEvent e) { - if (e.getOppositeComponent() != null) { - String name = e.getOppositeComponent().getName(); - return ComparatorUtils.equals(name, PREVIEW_BUTTON) || ComparatorUtils.equals(name, REFRESH_BUTTON); - } - return false; + super(); } - private JToolBar createToolBar() { - ToolBarDef toolBarDef = new ToolBarDef(); - toolBarDef.addShortCut(new PreviewAction()); - UIToolbar editToolBar = ToolBarDef.createJToolBar(); - toolBarDef.updateToolBar(editToolBar); - return editToolBar; - } @Override protected String title4PopupWindow() { return Toolkit.i18nText("Plugin-Redis_Query"); } - private void refresh() { - String[] paramTexts = new String[]{queryPane.getQuery(), queryPane.getScript()}; - - ParameterProvider[] parameters = ParameterHelper.analyze4Parameters(paramTexts, false); - - ParameterProvider[] providers = queryPane.getOrderValue().analyze4Parameters(); - - editorPane.populate(ArrayUtils.addAll(parameters, providers)); + protected JComponent createQueryPane() { + if (queryPane == null) { + queryPane = new RedisQueryPane(); + } + return queryPane; } + public String[] paramTexts() { + return new String[]{queryPane.getQuery(), queryPane.getScript()}; + } - private void checkParameter() { - String[] paramTexts = new String[]{queryPane.getQuery(), queryPane.getScript()}; - - ParameterProvider[] parameters = ParameterHelper.analyze4Parameters(paramTexts, false); - parameters = ArrayUtils.addAll(parameters, queryPane.getOrderValue().analyze4Parameters()); - - if (parameters.length < 1 && editorPane.update().size() < 1) { - return; - } - boolean isIn = true; - List list = editorPane.update(); - List name = new ArrayList(); - for (int i = 0; i < list.size(); i++) { - name.add(list.get(i).getName()); - } - for (int i = 0; i < parameters.length; i++) { - if (!name.contains(parameters[i].getName())) { - isIn = false; - break; - } - } - if (list.size() == parameters.length && isIn) { - return; - } - refresh(); + @Override + public OrderValue getOrderValue() { + return queryPane.getOrderValue(); } @Override @@ -143,6 +66,7 @@ public class RedisTableDataPane extends AbstractTableDataPane { return; } Calculator c = Calculator.createCalculator(); + editorPane.populate(tableData.getParameters(c)); chosePane.populateConnection(tableData.getDatabase()); @@ -177,33 +101,4 @@ public class RedisTableDataPane extends AbstractTableDataPane { return tableData; } - - private class PreviewAction extends UpdateAction { - public PreviewAction() { - this.setName(PREVIEW_BUTTON); - this.setMnemonic('P'); - this.setSmallIcon(BaseUtils.readIcon("/com/fr/design/images/m_file/preview.png")); - } - - public void actionPerformed(ActionEvent evt) { - checkParameter(); - PreviewTablePane.previewTableData(RedisTableDataPane.this.updateBean()); - } - } - - - protected class RefreshAction extends UITableEditAction { - public RefreshAction() { - this.setName(REFRESH_BUTTON); - this.setSmallIcon(BaseUtils.readIcon("/com/fr/design/images/control/refresh.png")); - } - - public void actionPerformed(ActionEvent e) { - refresh(); - } - - @Override - public void checkEnabled() { - } - } } \ No newline at end of file diff --git a/src/main/java/com/fr/plugin/db/redis/util/RedisDesignUtils.java b/src/main/java/com/fr/plugin/db/redis/util/RedisDesignUtils.java new file mode 100644 index 0000000..d6822d2 --- /dev/null +++ b/src/main/java/com/fr/plugin/db/redis/util/RedisDesignUtils.java @@ -0,0 +1,24 @@ +package com.fr.plugin.db.redis.util; + +import com.fr.design.border.UIRoundedBorder; +import com.fr.design.constants.UIConstants; +import com.fr.design.gui.syntax.ui.rsyntaxtextarea.RSyntaxTextArea; +import com.fr.design.gui.syntax.ui.rtextarea.RTextScrollPane; + +import java.awt.*; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-03-18 + */ +public class RedisDesignUtils { + public static RTextScrollPane createConditionTextPane(RSyntaxTextArea editorPane, String type, int height) { + editorPane.setSyntaxEditingStyle(type); + RTextScrollPane sqlTextScrollPane = new RTextScrollPane(editorPane); + sqlTextScrollPane.setBorder(new UIRoundedBorder(UIConstants.LINE_COLOR, 1, UIConstants.ARC)); + sqlTextScrollPane.setPreferredSize(new Dimension(680, height)); + sqlTextScrollPane.setLineNumbersEnabled(false); + return sqlTextScrollPane; + } +} diff --git a/src/main/java/com/fr/plugin/db/redis/util/RedisUtils.java b/src/main/java/com/fr/plugin/db/redis/util/RedisUtils.java new file mode 100644 index 0000000..0751e40 --- /dev/null +++ b/src/main/java/com/fr/plugin/db/redis/util/RedisUtils.java @@ -0,0 +1,40 @@ +package com.fr.plugin.db.redis.util; + +import com.eclipsesource.v8.V8Object; +import com.fr.base.Parameter; +import com.fr.base.TemplateUtils; +import com.fr.stable.ArrayUtils; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-03-18 + */ +public class RedisUtils { + + public static String calculateQuery(String query, Parameter[] ps) { + if (ArrayUtils.isEmpty(ps)) { + return query; + } + Map map = new HashMap(); + for (Parameter p : ps) { + map.put(p.getName(), p.getValue()); + } + try { + return TemplateUtils.renderParameter4Tpl(query, map); + } catch (Exception e) { + return query; + } + } + + public static void registerJavaMethods(V8Object v8Object, Object target) { + Method[] methods = target.getClass().getDeclaredMethods(); + for (Method m : methods) { + v8Object.registerJavaMethod(target, m.getName(), m.getName(), m.getParameterTypes()); + } + } +} diff --git a/src/main/resources/com/fr/plugin/db/redis/locale/redis.properties b/src/main/resources/com/fr/plugin/db/redis/locale/redis.properties index 1cb8a8f..26fb7be 100755 --- a/src/main/resources/com/fr/plugin/db/redis/locale/redis.properties +++ b/src/main/resources/com/fr/plugin/db/redis/locale/redis.properties @@ -2,6 +2,7 @@ Plugin-Redis_Host=Host Plugin-Redis_Port=Port Plugin-Redis_Password=Password Plugin-Redis_Table_Data=Redis Table Data +Plugin-Redis_Script_Table_Data=Redis Script Table Data Plugin-Redis_Query_Condition=Query Condition Plugin-Redis_DB=RedisDB Plugin-Redis_Help=Help Doc @@ -16,3 +17,5 @@ Plugin-Redis_Connection_Successfully=Connection Successfully Plugin-Redis_Connection_Failed=Connection Failed Plugin-Redis_Script_Text=Transform Script Plugin-Redis_Script_Text_Description=Script Description +Plugin-Redis_Pool_Max_Total=Max Total +Plugin-Redis_Script_Query_Text=Query Script diff --git a/src/main/resources/com/fr/plugin/db/redis/locale/redis_en_US.properties b/src/main/resources/com/fr/plugin/db/redis/locale/redis_en_US.properties index eb350d7..4ed0672 100755 --- a/src/main/resources/com/fr/plugin/db/redis/locale/redis_en_US.properties +++ b/src/main/resources/com/fr/plugin/db/redis/locale/redis_en_US.properties @@ -2,6 +2,7 @@ Plugin-Redis_Host=Host Plugin-Redis_Port=Port Plugin-Redis_Password=Password Plugin-Redis_Table_Data=Redis +Plugin-Redis_Script_Table_Data=Redis Script Table Data Plugin-Redis_Query_Condition=Query Condition Plugin-Redis_DB=RedisDB Plugin-Redis_Help=Help Doc @@ -16,3 +17,5 @@ Plugin-Redis_Connection_Successfully=Connection Successfully Plugin-Redis_Connection_Failed=Connection Failed Plugin-Redis_Script_Text=Transform Script Plugin-Redis_Script_Text_Description=Script Description +Plugin-Redis_Pool_Max_Total=Max Total +Plugin-Redis_Script_Query_Text=Query Script diff --git a/src/main/resources/com/fr/plugin/db/redis/locale/redis_zh_CN.properties b/src/main/resources/com/fr/plugin/db/redis/locale/redis_zh_CN.properties index 8f4ca08..ed3b945 100755 --- a/src/main/resources/com/fr/plugin/db/redis/locale/redis_zh_CN.properties +++ b/src/main/resources/com/fr/plugin/db/redis/locale/redis_zh_CN.properties @@ -2,6 +2,7 @@ Plugin-Redis_Host=\u6570\u636E\u5E93\u5730\u5740 Plugin-Redis_Port=\u7AEF\u53E3 Plugin-Redis_Password=\u5BC6\u7801 Plugin-Redis_Table_Data=Redis\u6570\u636E\u96C6 +Plugin-Redis_Script_Table_Data=Redis\u7A0B\u5E8F\u6570\u636E\u96C6 Plugin-Redis_Query_Condition=\u67E5\u8BE2\u6761\u4EF6 Plugin-Redis_DB=RedisDB\u6570\u636E\u96C6 Plugin-Redis_Help=\u5E2E\u52A9\u6587\u6863 @@ -16,4 +17,6 @@ Plugin-Redis_Connection_Successfully=\u8FDE\u63A5\u6210\u529F Plugin-Redis_Connection_Failed=\u8FDE\u63A5\u5931\u8D25 Plugin-Redis_Script_Text=\u8F6C\u6362\u811A\u672C Plugin-Redis_Script_Text_Description=\u53EF\u4EE5\u4F7F\u7528JavaScript\u811A\u672C\u6765\u8F6C\u6362\u67E5\u8BE2\u7ED3\u679C\u3002\n\u6CE8\u610F1\uFF1A\u5185\u7F6E\u53C2\u6570$content\u548C$column\uFF0C\u5206\u522B\u8868\u793A\u539F\u59CB\u7684\u6570\u636E\u96C6\u5185\u5BB9\uFF08\u4E8C\u7EF4\u6570\u7EC4\uFF09\u548C\u6570\u636E\u96C6\u7684\u5217\u540D\uFF08\u4E00\u7EF4\u6570\u7EC4\uFF09\u3002\u5982\u679C\u8FD4\u56DE\u4E00\u4E2A\u4E8C\u7EF4\u6570\u7EC4\uFF0C\u8868\u793A\u4EC5\u4FEE\u6539\u6570\u636E\u96C6\u5185\u5BB9\uFF0C\u4E0D\u4FEE\u6539\u6570\u636E\u96C6\u7684\u5217\u540D\uFF1B\u5982\u679C\u8FD4\u56DE\u4E00\u4E2A\u5982\u4E0B\u683C\u5F0F\u7684\u5BF9\u8C61:\ -{content:[][],column:[]}\uFF0C\u8868\u793A\u540C\u65F6\u4FEE\u6539\u4E86\u6570\u636E\u96C6\u7684\u5185\u5BB9\u548C\u5217\u540D\u3002\n\u6CE8\u610F2\uFF1A\u5982\u679C\u5E0C\u671B\u5F15\u7528\u5916\u90E8JS\uFF0C\u53EF\u4EE5\u5728\u811A\u672C\u6846\u4E2D\u5199\u5165file:///$ENV_HOME/transform.js\uFF0C\u5176\u4E2D$ENV_HOME\u8868\u793A\u5F53\u524D\u5DE5\u4F5C\u76EE\u5F55\u3002 \ No newline at end of file +{content:[][],column:[]}\uFF0C\u8868\u793A\u540C\u65F6\u4FEE\u6539\u4E86\u6570\u636E\u96C6\u7684\u5185\u5BB9\u548C\u5217\u540D\u3002\n\u6CE8\u610F2\uFF1A\u5982\u679C\u5E0C\u671B\u5F15\u7528\u5916\u90E8JS\uFF0C\u53EF\u4EE5\u5728\u811A\u672C\u6846\u4E2D\u5199\u5165file:///$ENV_HOME/transform.js\uFF0C\u5176\u4E2D$ENV_HOME\u8868\u793A\u5F53\u524D\u5DE5\u4F5C\u76EE\u5F55\u3002 +Plugin-Redis_Pool_Max_Total=\u6700\u5927\u8FDE\u63A5\u6570 +Plugin-Redis_Script_Query_Text=\u67E5\u8BE2\u811A\u672C \ No newline at end of file