diff --git a/plugin.xml b/plugin.xml
index 8d09dbf..b67b0be 100755
--- a/plugin.xml
+++ b/plugin.xml
@@ -5,7 +5,7 @@
yes
7.0
10.0
- 2019-07-29
+ 2019-02-29
richie
database = HolderKit.obj(null, Connection.class);
private Conf dbIndex = XmlHolderKit.obj(new NumberOrderValue(0), OrderValue.class, OrderValue.XML_TAG);
private Conf script = HolderKit.simple(StringKit.EMPTY);
+ private Conf engineType = HolderKit.simple(EngineType.JAVA.getType());
public void setDatabase(Connection c) {
this.database.set(c);
@@ -52,6 +54,14 @@ public class RedisScriptTableData extends BaseTableData {
this.dbIndex.set(dbIndex);
}
+ public EngineType getEngineType() {
+ return EngineType.parse(engineType.get());
+ }
+
+ public void setEngineType(EngineType engineType) {
+ this.engineType.set(engineType.getType());
+ }
+
public String getScript() {
return script.get();
}
@@ -80,7 +90,9 @@ public class RedisScriptTableData extends BaseTableData {
RedisDatabaseConnection rc = ConnectionKit.getConnection(name, RedisDatabaseConnection.class);
if (rc != null) {
OrderValue orderValue = dbIndex.get();
+ EngineType engineType = getEngineType();
DataModel model = new RedisScriptTableDataModel(rc,
+ engineType,
orderValue == null ? 0 : orderValue.toIndex(calculator, ps),
RedisUtils.calculateQuery(script.get(), ps),
rowCount);
@@ -113,6 +125,8 @@ public class RedisScriptTableData extends BaseTableData {
if (isNotNullValue(tmpVal)) {
this.setScript(tmpVal);
}
+ } else if ("ScriptAttr".equals(tmpName)) {
+ setEngineType(EngineType.parse(reader.getAttrAsInt("engineType", 0)));
}
}
}
@@ -125,6 +139,7 @@ public class RedisScriptTableData extends BaseTableData {
XmlKit.writeXMLConnection(writer, this.database.get());
}
writer.startTAG("Script").textNode(getScript()).end();
+ writer.startTAG("ScriptAttr").attr("engineType", engineType.get()).end();
}
private boolean isNotNullValue(String value) {
@@ -137,6 +152,7 @@ public class RedisScriptTableData extends BaseTableData {
cloned.database = (Conf) database.clone();
cloned.script = (Conf) script.clone();
cloned.dbIndex = (Conf) dbIndex.clone();
+ cloned.engineType = (Conf) engineType.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
index 863f3e1..3ff402d 100644
--- a/src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableDataModel.java
+++ b/src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableDataModel.java
@@ -2,6 +2,7 @@ package com.fr.plugin.db.redis.core;
import com.fanruan.api.data.open.BaseDataModel;
import com.fr.plugin.db.redis.core.emb.Redis;
+import com.fr.plugin.db.redis.core.script.EngineType;
import com.fr.plugin.db.redis.help.RedisClient;
import com.fr.plugin.db.redis.help.RedisClientSelector;
import com.fr.plugin.db.redis.help.client.SimpleDataModel;
@@ -17,14 +18,16 @@ import java.util.List;
public class RedisScriptTableDataModel extends BaseDataModel {
private RedisDatabaseConnection mc;
+ private EngineType engineType;
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) {
+ public RedisScriptTableDataModel(RedisDatabaseConnection mc, EngineType engineType, int dbIndex, String script, int rowCount) {
this.mc = mc;
+ this.engineType = engineType;
this.script = script;
this.dbIndex = dbIndex;
this.rowCount = rowCount;
@@ -36,7 +39,7 @@ public class RedisScriptTableDataModel extends BaseDataModel {
}
Redis redis = mc.createRedisClient();
redis.getClient().select(dbIndex);
- RedisClient redisClient = RedisClientSelector.select(redis);
+ RedisClient redisClient = RedisClientSelector.select(redis, engineType);
SimpleDataModel simple = redisClient.build(redis, script, rowCount);
if (simple != null) {
this.columnNames = simple.getColumnNames();
diff --git a/src/main/java/com/fr/plugin/db/redis/core/script/EngineType.java b/src/main/java/com/fr/plugin/db/redis/core/script/EngineType.java
new file mode 100644
index 0000000..61942cb
--- /dev/null
+++ b/src/main/java/com/fr/plugin/db/redis/core/script/EngineType.java
@@ -0,0 +1,30 @@
+package com.fr.plugin.db.redis.core.script;
+
+/**
+ * @author richie
+ * @version 10.0
+ * Created by richie on 2019-09-18
+ */
+public enum EngineType {
+
+ V8(0), JAVA(1);
+
+ private int index;
+
+ EngineType(int index) {
+ this.index = index;
+ }
+
+ public int getType() {
+ return index;
+ }
+
+ public static EngineType parse(int type) {
+ for (EngineType engineType : values()) {
+ if (engineType.index == type) {
+ return engineType;
+ }
+ }
+ return V8;
+ }
+}
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
index 6361311..5791f6d 100644
--- a/src/main/java/com/fr/plugin/db/redis/help/RedisClient.java
+++ b/src/main/java/com/fr/plugin/db/redis/help/RedisClient.java
@@ -8,7 +8,7 @@ import com.fr.plugin.db.redis.help.client.SimpleDataModel;
* @version 10.0
* Created by richie on 2019-08-26
*/
-public interface RedisClient {
+public interface RedisClient {
SimpleDataModel build(Redis redis, String query, int limit);
@@ -19,23 +19,23 @@ public interface RedisClient {
String hget(String key, String field);
@ScriptBridge
- A hkeys(String key);
+ String hkeys(String key);
@ScriptBridge
- A hmget(String key, String... fields);
+ String hmget(String key, String... fields);
@ScriptBridge
- O hgetAll(String key);
+ String hgetAll(String key);
@ScriptBridge
- A keys(String pattern);
+ String keys(String pattern);
@ScriptBridge
- A lrange(String key, long start, long end);
+ String lrange(String key, long start, long end);
@ScriptBridge
- A mget(A keys);
+ String mget(A keys);
@ScriptBridge
- A smembers(String key);
+ String smembers(String key);
}
diff --git a/src/main/java/com/fr/plugin/db/redis/help/RedisClientSelector.java b/src/main/java/com/fr/plugin/db/redis/help/RedisClientSelector.java
index c3aa5b6..7e05b60 100644
--- a/src/main/java/com/fr/plugin/db/redis/help/RedisClientSelector.java
+++ b/src/main/java/com/fr/plugin/db/redis/help/RedisClientSelector.java
@@ -2,6 +2,7 @@ package com.fr.plugin.db.redis.help;
import com.eclipsesource.v8.V8;
import com.fr.plugin.db.redis.core.emb.Redis;
+import com.fr.plugin.db.redis.core.script.EngineType;
import com.fr.plugin.db.redis.help.client.RedisNashornClient;
import com.fr.plugin.db.redis.help.client.RedisV8Client;
@@ -25,7 +26,10 @@ public class RedisClientSelector {
return true;
}
- public static RedisClient select(Redis redis) {
+ public static RedisClient select(Redis redis, EngineType engineType) {
+ if (engineType == EngineType.JAVA) {
+ return new RedisNashornClient(redis.getClient());
+ }
if (SUPPORT_J2V8) {
return new RedisV8Client(redis.getClient());
} else {
diff --git a/src/main/java/com/fr/plugin/db/redis/help/client/BaseRedisClient.java b/src/main/java/com/fr/plugin/db/redis/help/client/BaseRedisClient.java
index 6815115..9d809b8 100644
--- a/src/main/java/com/fr/plugin/db/redis/help/client/BaseRedisClient.java
+++ b/src/main/java/com/fr/plugin/db/redis/help/client/BaseRedisClient.java
@@ -1,18 +1,24 @@
package com.fr.plugin.db.redis.help.client;
import com.fanruan.api.log.LogKit;
-
+import com.fr.json.JSON;
+import com.fr.json.JSONFactory;
import com.fr.plugin.db.redis.core.accessor.EmbedRedis;
-import com.fr.plugin.db.redis.help.ScriptBridge;
import com.fr.plugin.db.redis.help.RedisClient;
-import redis.clients.jedis.Jedis;
+import com.fr.plugin.db.redis.help.ScriptBridge;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
/**
* @author richie
* @version 10.0
* Created by richie on 2019-08-26
*/
-public abstract class BaseRedisClient implements RedisClient {
+public abstract class BaseRedisClient implements RedisClient {
EmbedRedis jedis;
@@ -40,4 +46,79 @@ public abstract class BaseRedisClient implements RedisClient {
}
}
+ @Override
+ @ScriptBridge
+ public String hkeys(String key) {
+ long start = System.currentTimeMillis();
+ Set set = jedis.keys(key);
+ LogKit.info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start);
+ return object2JSONString(set);
+ }
+
+ @Override
+ @ScriptBridge
+ public String hmget(String key, String... fields) {
+ long start = System.currentTimeMillis();
+ List list = jedis.hmget(key, fields);
+ LogKit.info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start);
+ return object2JSONString(list);
+ }
+
+ @Override
+ @ScriptBridge
+ public String hgetAll(String key) {
+ long start = System.currentTimeMillis();
+ Map map = jedis.hgetAll(key);
+ LogKit.info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start);
+ return object2JSONString(map);
+ }
+
+ @Override
+ @ScriptBridge
+ public String keys(String pattern) {
+ long start = System.currentTimeMillis();
+ Set set = jedis.keys(pattern);
+ LogKit.info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start);
+ return object2JSONString(set);
+ }
+
+ @Override
+ @ScriptBridge
+ public String lrange(String key, long start, long end) {
+ long start0 = System.currentTimeMillis();
+ List list = jedis.lrange(key, start, end);
+ LogKit.info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start0);
+ return object2JSONString(list);
+ }
+
+ @Override
+ @ScriptBridge
+ public String smembers(String key) {
+ long start = System.currentTimeMillis();
+ Set set = jedis.smembers(key);
+ LogKit.info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start);
+ return object2JSONString(set);
+ }
+
+ /**
+ * 将集合输出为JSON格式的字符串,便于JS和Java互传数据
+ *
+ * @param collection 集合
+ * @return 字符串
+ */
+ String object2JSONString(Collection collection) {
+ return JSONFactory.createJSON(collection).toString();
+ }
+
+ /**
+ * 将集合输出为JSON格式的字符串,便于JS和Java互传数据
+ *
+ * @param map 集合
+ * @return 字符串
+ */
+ String object2JSONString(Map map) {
+ Map newMap = new TreeMap();
+ newMap.putAll(map);
+ return JSONFactory.createJSON(JSON.OBJECT, newMap).toString();
+ }
}
diff --git a/src/main/java/com/fr/plugin/db/redis/help/client/RedisNashornClient.java b/src/main/java/com/fr/plugin/db/redis/help/client/RedisNashornClient.java
index e79d6bc..1b720d1 100644
--- a/src/main/java/com/fr/plugin/db/redis/help/client/RedisNashornClient.java
+++ b/src/main/java/com/fr/plugin/db/redis/help/client/RedisNashornClient.java
@@ -23,7 +23,7 @@ import java.util.TreeMap;
* @version 10.0
* Created by richie on 2019-08-26
*/
-public class RedisNashornClient extends BaseRedisClient