Browse Source

Merge branch 'refactor' of Elijah/demo-tabledata-redis into refactor

paid
richie 5 years ago committed by Gogs
parent
commit
d520da205b
  1. BIN
      lib/finekit-10.0.jar
  2. 6
      plugin.xml
  3. 19
      src/main/java/com/fr/plugin/db/redis/RedisHttpHandlerProvider.java
  4. 76
      src/main/java/com/fr/plugin/db/redis/RedisScriptUniversalTableDataImpl.java
  5. 75
      src/main/java/com/fr/plugin/db/redis/RedisUniversalTableDataImpl.java
  6. 52
      src/main/java/com/fr/plugin/db/redis/action/SearchKeysAction.java
  7. 72
      src/main/java/com/fr/plugin/db/redis/bean/ParameterBean.java
  8. 7
      src/main/java/com/fr/plugin/db/redis/core/RedisConstants.java
  9. 17
      src/main/java/com/fr/plugin/db/redis/util/RedisUtils.java

BIN
lib/finekit-10.0.jar

Binary file not shown.

6
plugin.xml

@ -3,12 +3,13 @@
<id>com.fr.solution.plugin.db.redis.v10</id> <id>com.fr.solution.plugin.db.redis.v10</id>
<name><![CDATA[Redis数据集]]></name> <name><![CDATA[Redis数据集]]></name>
<active>yes</active> <active>yes</active>
<version>7.1</version> <version>7.2</version>
<env-version>10.0</env-version> <env-version>10.0</env-version>
<jartime>2019-03-29</jartime> <jartime>2019-03-29</jartime>
<vendor>richie</vendor> <vendor>richie</vendor>
<description><![CDATA[可以连接Redis数据库,支持哈希表、列表、集合以及有序集合]]></description> <description><![CDATA[可以连接Redis数据库,支持哈希表、列表、集合以及有序集合]]></description>
<change-notes><![CDATA[ <change-notes><![CDATA[
[2019-10-30]适配新服务器数据集接口,在决策平台可以直接定义<br/>
[2019-09-29]修复无法兼容老版本的问题。<br/> [2019-09-29]修复无法兼容老版本的问题。<br/>
[2019-09-18]支持手动选择不同JavaScript脚本引擎。<br/> [2019-09-18]支持手动选择不同JavaScript脚本引擎。<br/>
[2019-09-18]支持连接redis集群。<br/> [2019-09-18]支持连接redis集群。<br/>
@ -39,6 +40,9 @@
</extra-designer> </extra-designer>
<extra-decision> <extra-decision>
<UniversalConnectionProvider class="com.fr.plugin.db.redis.RedisUniversalConnectionImpl"/> <UniversalConnectionProvider class="com.fr.plugin.db.redis.RedisUniversalConnectionImpl"/>
<UniversalServerTableDataProvider class="com.fr.plugin.db.redis.RedisUniversalTableDataImpl" />
<UniversalServerTableDataProvider class="com.fr.plugin.db.redis.RedisScriptUniversalTableDataImpl"/>
<HttpHandlerProvider class="com.fr.plugin.db.redis.RedisHttpHandlerProvider"/>
</extra-decision> </extra-decision>
<function-recorder class="com.fr.plugin.db.redis.core.RedisTableData"/> <function-recorder class="com.fr.plugin.db.redis.core.RedisTableData"/>
<function-recorder class="com.fr.plugin.db.redis.core.RedisScriptTableData"/> <function-recorder class="com.fr.plugin.db.redis.core.RedisScriptTableData"/>

19
src/main/java/com/fr/plugin/db/redis/RedisHttpHandlerProvider.java

@ -0,0 +1,19 @@
package com.fr.plugin.db.redis;
import com.fr.decision.fun.HttpHandler;
import com.fr.decision.fun.impl.AbstractHttpHandlerProvider;
import com.fr.plugin.db.redis.action.SearchKeysAction;
/**
* @author Elijah
* @version 10.0.4
* Created by Elijah on 2019/10/30
*/
public class RedisHttpHandlerProvider extends AbstractHttpHandlerProvider {
@Override
public HttpHandler[] registerHandlers() {
return new HttpHandler[] {
new SearchKeysAction()
};
}
}

76
src/main/java/com/fr/plugin/db/redis/RedisScriptUniversalTableDataImpl.java

@ -0,0 +1,76 @@
package com.fr.plugin.db.redis;
import com.fanruan.api.cal.FormulaKit;
import com.fanruan.api.data.ConnectionKit;
import com.fanruan.api.data.TableDataKit;
import com.fanruan.api.util.StringKit;
import com.fr.data.impl.Connection;
import com.fr.decision.fun.impl.AbstractUniversalServerTableDataProvider;
import com.fr.json.JSONObject;
import com.fr.plugin.db.redis.bean.ParameterBean;
import com.fr.plugin.db.redis.core.RedisConstants;
import com.fr.plugin.db.redis.core.RedisScriptTableData;
import com.fr.plugin.db.redis.core.order.impl.FormulaOrderValue;
import com.fr.plugin.db.redis.core.order.impl.NumberOrderValue;
import com.fr.plugin.db.redis.core.script.EngineType;
import com.fr.plugin.db.redis.util.RedisUtils;
import com.fr.script.Calculator;
import com.fr.stable.NameReference;
import com.fr.web.struct.Atom;
/**
* @author Elijah
* @version 10.0.4
* Created by Elijah on 2019/10/30
*/
public class RedisScriptUniversalTableDataImpl extends AbstractUniversalServerTableDataProvider<RedisScriptTableData> {
@Override
public Class<RedisScriptTableData> classForTableData() {
return RedisScriptTableData.class;
}
@Override
public String nameForTableData() {
return "RedisScript";
}
@Override
public JSONObject serialize(RedisScriptTableData redisScriptTableData) {
JSONObject data = new JSONObject();
Connection database = redisScriptTableData.getDatabase();
data.put(RedisConstants.DATABASE, database instanceof NameReference ? ((NameReference) database).getName() : StringKit.EMPTY);
data.put(RedisConstants.ORDER, redisScriptTableData.getOrderValue().toString());
data.put(RedisConstants.SCRIPT, redisScriptTableData.getScript());
data.put(RedisConstants.ENGINE, redisScriptTableData.getEngineType().getType());
data.put(RedisConstants.PARAMETERS, ParameterBean.createParameterBeans(redisScriptTableData.getParameters(Calculator.createCalculator())));
return data;
}
@Override
public RedisScriptTableData deserialize(RedisScriptTableData oldTableData, JSONObject data) {
RedisScriptTableData newTableData = new RedisScriptTableData();
String databaseName = data.getString(RedisConstants.DATABASE);
String script = data.getString(RedisConstants.SCRIPT);
if (!StringKit.isEmpty(databaseName) && TableDataKit.findTableData(databaseName) != null) {
newTableData.setDatabase(ConnectionKit.createNameConnection(databaseName));
}
newTableData.setEngineType(EngineType.parse(data.getInt(RedisConstants.ENGINE)));
newTableData.setScript(script);
Object order = data.get(RedisConstants.ORDER);
if (order instanceof Integer) {
newTableData.setOrderValue(new NumberOrderValue((Integer) order));
} else {
newTableData.setOrderValue(new FormulaOrderValue(FormulaKit.newFormula(order)));
}
newTableData.setParameters(RedisUtils.analyzeParameter(oldTableData.getParameters(Calculator.createCalculator()), newTableData.getOrderValue(), script));
return newTableData;
}
@Override
public Atom client() {
return RedisConnectionComponent.KEY;
}
}

75
src/main/java/com/fr/plugin/db/redis/RedisUniversalTableDataImpl.java

@ -0,0 +1,75 @@
package com.fr.plugin.db.redis;
import com.fanruan.api.cal.FormulaKit;
import com.fanruan.api.data.ConnectionKit;
import com.fanruan.api.data.TableDataKit;
import com.fanruan.api.util.StringKit;
import com.fr.data.impl.Connection;
import com.fr.decision.fun.impl.AbstractUniversalServerTableDataProvider;
import com.fr.json.JSONObject;
import com.fr.plugin.db.redis.bean.ParameterBean;
import com.fr.plugin.db.redis.core.RedisConstants;
import com.fr.plugin.db.redis.core.RedisTableData;
import com.fr.plugin.db.redis.core.order.impl.FormulaOrderValue;
import com.fr.plugin.db.redis.core.order.impl.NumberOrderValue;
import com.fr.plugin.db.redis.util.RedisUtils;
import com.fr.script.Calculator;
import com.fr.stable.NameReference;
import com.fr.web.struct.Atom;
/**
* @author Elijah
* @version 10.0.4
* Created by Elijah on 2019/10/30
*/
public class RedisUniversalTableDataImpl extends AbstractUniversalServerTableDataProvider<RedisTableData> {
@Override
public Class<RedisTableData> classForTableData() {
return RedisTableData.class;
}
@Override
public String nameForTableData() {
return "Redis";
}
@Override
public JSONObject serialize(RedisTableData redisTableData) {
JSONObject data = new JSONObject();
Connection database = redisTableData.getDatabase();
data.put(RedisConstants.DATABASE, database instanceof NameReference ? ((NameReference) database).getName() : StringKit.EMPTY);
data.put(RedisConstants.ORDER, redisTableData.getOrderValue().toString());
data.put(RedisConstants.SCRIPT, redisTableData.getScript());
data.put(RedisConstants.QUERY, redisTableData.getQuery());
data.put(RedisConstants.PARAMETERS, ParameterBean.createParameterBeans(redisTableData.getParameters(Calculator.createCalculator())));
return data;
}
@Override
public RedisTableData deserialize(RedisTableData oldTableData, JSONObject data) {
RedisTableData newTableData = oldTableData;
String databaseName = data.getString(RedisConstants.DATABASE);
String script = data.getString(RedisConstants.SCRIPT);
String query = data.getString(RedisConstants.QUERY);
if (!StringKit.isEmpty(databaseName) && TableDataKit.findTableData(databaseName) != null) {
newTableData.setDatabase(ConnectionKit.createNameConnection(databaseName));
}
newTableData.setScript(script);
newTableData.setQuery(query);
//与前台约定,数字说明是序号
Object order = data.get(RedisConstants.ORDER);
if (order instanceof Integer) {
newTableData.setOrderValue(new NumberOrderValue((Integer) order));
} else {
newTableData.setOrderValue(new FormulaOrderValue(FormulaKit.newFormula(order)));
}
newTableData.setParameters(RedisUtils.analyzeParameter(oldTableData.getParameters(Calculator.createCalculator()), newTableData.getOrderValue(), script, query));
return newTableData;
}
@Override
public Atom client() {
return RedisConnectionComponent.KEY;
}
}

52
src/main/java/com/fr/plugin/db/redis/action/SearchKeysAction.java

@ -0,0 +1,52 @@
package com.fr.plugin.db.redis.action;
import com.fanruan.api.data.ConnectionKit;
import com.fanruan.api.util.StringKit;
import com.fanruan.api.web.FlushKit;
import com.fr.base.ServerConfig;
import com.fr.data.impl.Connection;
import com.fr.decision.fun.impl.BaseHttpHandler;
import com.fr.json.JSONFactory;
import com.fr.plugin.db.redis.core.RedisDatabaseConnection;
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
/**
* @author Elijah
* @version 10.0.4
* Created by Elijah on 2019/10/30
*/
public class SearchKeysAction extends BaseHttpHandler {
@Override
public RequestMethod getMethod() {
return RequestMethod.GET;
}
@Override
public String getPath() {
return "/redis/keys";
}
@Override
public boolean isPublic() {
return false;
}
@Override
public void handle(HttpServletRequest request, HttpServletResponse response) throws Exception {
String[] keys = new String[0];
String pattern = request.getParameter("pattern");
String connectionName = request.getParameter("database");
if (StringKit.isNotEmpty(pattern) && StringKit.isNotEmpty(connectionName)) {
Connection connection = ConnectionKit.getConnection(connectionName);
if (connection instanceof RedisDatabaseConnection) {
keys = connection.summary(pattern);
}
}
FlushKit.printAsJSON(response, JSONFactory.createJSON(Arrays.asList(keys)));
response.setContentType("application/json;charset=" + ServerConfig.getInstance().getServerCharset());
}
}

72
src/main/java/com/fr/plugin/db/redis/bean/ParameterBean.java

@ -0,0 +1,72 @@
package com.fr.plugin.db.redis.bean;
import com.fr.base.ParameterTypeHandler;
import com.fr.stable.ParameterProvider;
import java.util.ArrayList;
import java.util.List;
/**
* @author Elijah
* @version 10.0.4
* Created by Elijah on 2019/10/30
*/
public class ParameterBean{
public static final String TYPE = "type";
public static final String NAME = "name";
public static final String VALUE = "value";
private String type = ParameterTypeHandler.String.name();
private String name;
private String value;
public ParameterBean() {
}
public ParameterBean(String name, String value) {
this.name = name;
this.value = value;
}
public ParameterBean(String type, String name, String value) {
this.type = type;
this.name = name;
this.value = value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public static ParameterBean createBean(ParameterProvider parameterProvider) {
return new ParameterBean(parameterProvider.getClass().getSimpleName(),parameterProvider.getName(), parameterProvider.valueToString());
}
public static List<ParameterBean> createParameterBeans(ParameterProvider[] parameterProviders) {
List<ParameterBean> parameterBeans = new ArrayList<ParameterBean>();
for (ParameterProvider parameterProvider: parameterProviders) {
parameterBeans.add(ParameterBean.createBean(parameterProvider));
}
return parameterBeans;
}
}

7
src/main/java/com/fr/plugin/db/redis/core/RedisConstants.java

@ -7,4 +7,11 @@ public class RedisConstants {
public static final int DEFAULT_DB_INDEX = 0; public static final int DEFAULT_DB_INDEX = 0;
public static final String[] DEFAULT_COLUMN_NAMES = new String[]{"text", "value"}; public static final String[] DEFAULT_COLUMN_NAMES = new String[]{"text", "value"};
public static final String DATABASE= "database";
public static final String QUERY = "query";
public static final String SCRIPT = "script";
public static final String ORDER = "orderValue";
public static final String ENGINE = "engineType";
public static final String PARAMETERS = "parameters";
} }

17
src/main/java/com/fr/plugin/db/redis/util/RedisUtils.java

@ -1,10 +1,12 @@
package com.fr.plugin.db.redis.util; package com.fr.plugin.db.redis.util;
import com.eclipsesource.v8.V8Object; import com.eclipsesource.v8.V8Object;
import com.fanruan.api.cal.ParameterKit;
import com.fanruan.api.util.ArrayKit; import com.fanruan.api.util.ArrayKit;
import com.fanruan.api.util.GeneralKit; import com.fanruan.api.util.GeneralKit;
import com.fanruan.api.util.RenderKit; import com.fanruan.api.util.RenderKit;
import com.fanruan.api.util.StringKit; import com.fanruan.api.util.StringKit;
import com.fr.plugin.db.redis.core.order.OrderValue;
import com.fr.plugin.db.redis.help.ScriptBridge; import com.fr.plugin.db.redis.help.ScriptBridge;
import com.fr.stable.ParameterProvider; import com.fr.stable.ParameterProvider;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
@ -69,4 +71,19 @@ public class RedisUtils {
return StringKit.EMPTY; return StringKit.EMPTY;
} }
} }
public static ParameterProvider[] analyzeParameter(ParameterProvider[] oldParameters, OrderValue orderValue, String ...parameterTexts) {
ParameterProvider[] newParameters = ParameterKit.analyzeAndUnionSameParameters(parameterTexts, oldParameters);
ParameterProvider[] newOrderParameters = orderValue.analyze4Parameters();
Map<String, ParameterProvider> parameterProviderMap = new HashMap<String, ParameterProvider>();
for (ParameterProvider parameterProvider: oldParameters) {
parameterProviderMap.put(parameterProvider.getName(), parameterProvider);
}
for (ParameterProvider parameterProvider: newOrderParameters) {
if (parameterProviderMap.containsKey(parameterProvider.getName())) {
parameterProvider.setValue(parameterProviderMap.get(parameterProvider.getName()).getValue());
}
}
return ArrayKit.addAll(newParameters, newOrderParameters);
}
} }

Loading…
Cancel
Save