Elijah
5 years ago
8 changed files with 320 additions and 1 deletions
@ -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() |
||||
}; |
||||
} |
||||
} |
@ -0,0 +1,75 @@
|
||||
package com.fr.plugin.db.redis; |
||||
|
||||
import com.fanruan.api.cal.FormulaKit; |
||||
import com.fanruan.api.data.TableDataKit; |
||||
import com.fanruan.api.util.StringKit; |
||||
import com.fr.data.impl.Connection; |
||||
import com.fr.data.impl.NameDatabaseConnection; |
||||
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.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 NameDatabaseConnection ? ((NameDatabaseConnection) 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(new NameDatabaseConnection(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; |
||||
} |
||||
} |
@ -0,0 +1,74 @@
|
||||
package com.fr.plugin.db.redis; |
||||
|
||||
import com.fanruan.api.cal.FormulaKit; |
||||
import com.fanruan.api.data.TableDataKit; |
||||
import com.fanruan.api.util.StringKit; |
||||
import com.fr.data.impl.Connection; |
||||
import com.fr.data.impl.NameDatabaseConnection; |
||||
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.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 NameDatabaseConnection ? ((NameDatabaseConnection) 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(new NameDatabaseConnection(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; |
||||
} |
||||
} |
@ -0,0 +1,51 @@
|
||||
package com.fr.plugin.db.redis.action; |
||||
|
||||
import com.fanruan.api.data.ConnectionKit; |
||||
import com.fr.base.ServerConfig; |
||||
import com.fr.data.impl.Connection; |
||||
import com.fr.decision.fun.impl.BaseHttpHandler; |
||||
import com.fr.json.JSONArray; |
||||
import com.fr.plugin.db.redis.core.RedisDatabaseConnection; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.third.springframework.web.bind.annotation.RequestMethod; |
||||
import com.fr.web.utils.WebUtils; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
/** |
||||
* @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 (StringUtils.isNotEmpty(pattern) && StringUtils.isNotEmpty(connectionName)) { |
||||
Connection connection = ConnectionKit.getConnection(connectionName); |
||||
if (connection instanceof RedisDatabaseConnection) { |
||||
keys = connection.summary(pattern); |
||||
} |
||||
} |
||||
WebUtils.printAsJSON(response, new JSONArray(keys)); |
||||
response.setContentType("application/json;charset=" + ServerConfig.getInstance().getServerCharset()); |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
} |
Loading…
Reference in new issue