forked from fanruan/demo-tabledata-redis
richie
6 years ago
12 changed files with 309 additions and 264 deletions
@ -1,155 +1,172 @@ |
|||||||
package com.fr.plugin.db.redis.core; |
package com.fr.plugin.db.redis.core; |
||||||
|
|
||||||
import com.fr.base.Parameter; |
import com.fr.base.Parameter; |
||||||
import com.fr.base.TableData; |
import com.fr.base.TableData; |
||||||
import com.fr.base.TemplateUtils; |
import com.fr.base.TemplateUtils; |
||||||
import com.fr.config.holder.Conf; |
import com.fr.config.holder.Conf; |
||||||
import com.fr.config.holder.factory.Holders; |
import com.fr.config.holder.factory.Holders; |
||||||
import com.fr.config.holder.factory.XmlHolders; |
import com.fr.config.holder.factory.XmlHolders; |
||||||
import com.fr.data.AbstractParameterTableData; |
import com.fr.data.AbstractParameterTableData; |
||||||
import com.fr.data.core.DataCoreXmlUtils; |
import com.fr.data.core.DataCoreXmlUtils; |
||||||
import com.fr.data.impl.Connection; |
import com.fr.data.impl.Connection; |
||||||
import com.fr.data.impl.NameDatabaseConnection; |
import com.fr.data.impl.NameDatabaseConnection; |
||||||
import com.fr.file.DatasourceManager; |
import com.fr.file.DatasourceManager; |
||||||
import com.fr.general.data.DataModel; |
import com.fr.general.data.DataModel; |
||||||
import com.fr.general.xml.GeneralXMLTools; |
import com.fr.general.xml.GeneralXMLTools; |
||||||
import com.fr.intelli.record.Focus; |
import com.fr.intelli.record.Focus; |
||||||
import com.fr.intelli.record.Original; |
import com.fr.intelli.record.Original; |
||||||
import com.fr.plugin.db.redis.core.order.OrderValue; |
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.core.order.impl.NumberOrderValue; |
||||||
import com.fr.record.analyzer.EnableMetrics; |
import com.fr.record.analyzer.EnableMetrics; |
||||||
import com.fr.script.Calculator; |
import com.fr.script.Calculator; |
||||||
import com.fr.stable.ArrayUtils; |
import com.fr.stable.ArrayUtils; |
||||||
import com.fr.stable.ParameterProvider; |
import com.fr.stable.ParameterProvider; |
||||||
import com.fr.stable.StringUtils; |
import com.fr.stable.StringUtils; |
||||||
import com.fr.stable.xml.XMLPrintWriter; |
import com.fr.stable.xml.XMLPrintWriter; |
||||||
import com.fr.stable.xml.XMLableReader; |
import com.fr.stable.xml.XMLableReader; |
||||||
|
|
||||||
import java.util.HashMap; |
import java.util.HashMap; |
||||||
import java.util.Map; |
import java.util.Map; |
||||||
|
|
||||||
|
|
||||||
@EnableMetrics |
@EnableMetrics |
||||||
public class RedisTableData extends AbstractParameterTableData { |
public class RedisTableData extends AbstractParameterTableData { |
||||||
|
|
||||||
private static final long serialVersionUID = 7017455818551800001L; |
private static final long serialVersionUID = 7017455818551800001L; |
||||||
private Conf<Connection> database = Holders.obj(null, Connection.class); |
private Conf<Connection> database = Holders.obj(null, Connection.class); |
||||||
private Conf<OrderValue> dbIndex = XmlHolders.obj(new NumberOrderValue(0), OrderValue.class, OrderValue.XML_TAG); |
private Conf<OrderValue> dbIndex = XmlHolders.obj(new NumberOrderValue(0), OrderValue.class, OrderValue.XML_TAG); |
||||||
private Conf<String> query = Holders.simple(StringUtils.EMPTY); |
private Conf<String> query = Holders.simple(StringUtils.EMPTY); |
||||||
|
private Conf<String> script = Holders.simple(StringUtils.EMPTY); |
||||||
public void setDatabase(Connection c) { |
|
||||||
this.database.set(c); |
public void setDatabase(Connection c) { |
||||||
} |
this.database.set(c); |
||||||
|
} |
||||||
public Connection getDatabase() { |
|
||||||
return database.get(); |
public Connection getDatabase() { |
||||||
} |
return database.get(); |
||||||
|
} |
||||||
public OrderValue getOrderValue() { |
|
||||||
return dbIndex.get(); |
public OrderValue getOrderValue() { |
||||||
} |
return dbIndex.get(); |
||||||
|
} |
||||||
public void setOrderValue(OrderValue dbIndex) { |
|
||||||
this.dbIndex.set(dbIndex); |
public void setOrderValue(OrderValue dbIndex) { |
||||||
} |
this.dbIndex.set(dbIndex); |
||||||
|
} |
||||||
public String getQuery() { |
|
||||||
return query.get(); |
public String getQuery() { |
||||||
} |
return query.get(); |
||||||
|
} |
||||||
public void setQuery(String query) { |
|
||||||
this.query.set(query); |
public void setQuery(String query) { |
||||||
} |
this.query.set(query); |
||||||
|
} |
||||||
public void setParameters(ParameterProvider[] providers) { |
|
||||||
super.setDefaultParameters(providers); |
public String getScript() { |
||||||
} |
return script.get(); |
||||||
|
} |
||||||
@Override |
|
||||||
public DataModel createDataModel(Calculator calculator) { |
public void setScript(String script) { |
||||||
return createDataModel(calculator, TableData.RESULT_ALL); |
this.script.set(script); |
||||||
} |
} |
||||||
|
|
||||||
@Override |
public void setParameters(ParameterProvider[] providers) { |
||||||
@Focus(id = RedisConstants.PLUGIN_ID, text = "Plugin-Redis_DB", source = Original.PLUGIN) |
super.setDefaultParameters(providers); |
||||||
public DataModel createDataModel(Calculator calculator, int rowCount) { |
} |
||||||
Parameter[] ps = Parameter.providers2Parameter(getParameters(calculator)); |
|
||||||
Connection connection = database.get(); |
@Override |
||||||
if (connection instanceof NameDatabaseConnection) { |
public DataModel createDataModel(Calculator calculator) { |
||||||
String name = ((NameDatabaseConnection) connection).getName(); |
return createDataModel(calculator, TableData.RESULT_ALL); |
||||||
RedisDatabaseConnection rc = DatasourceManager.getProviderInstance().getConnection(name, RedisDatabaseConnection.class); |
} |
||||||
if (rc != null) { |
|
||||||
OrderValue orderValue = dbIndex.get(); |
@Override |
||||||
return new RedisTableDataModel(calculator, ps, rc, |
@Focus(id = RedisConstants.PLUGIN_ID, text = "Plugin-Redis_DB", source = Original.PLUGIN) |
||||||
orderValue == null ? 0 : orderValue.toIndex(calculator, ps), |
public DataModel createDataModel(Calculator calculator, int rowCount) { |
||||||
calculateQuery(query.get(), ps), |
Parameter[] ps = Parameter.providers2Parameter(getParameters(calculator)); |
||||||
rowCount); |
Connection connection = database.get(); |
||||||
} |
if (connection instanceof NameDatabaseConnection) { |
||||||
} |
String name = ((NameDatabaseConnection) connection).getName(); |
||||||
return null; |
RedisDatabaseConnection rc = DatasourceManager.getProviderInstance().getConnection(name, RedisDatabaseConnection.class); |
||||||
} |
if (rc != null) { |
||||||
|
OrderValue orderValue = dbIndex.get(); |
||||||
private String calculateQuery(String query, Parameter[] ps) { |
return new RedisTableDataModel(calculator, ps, rc, |
||||||
if (ArrayUtils.isEmpty(ps)) { |
orderValue == null ? 0 : orderValue.toIndex(calculator, ps), |
||||||
return query; |
calculateQuery(query.get(), ps), |
||||||
} |
calculateQuery(script.get(), ps), |
||||||
Map<String, Object> map = new HashMap<String, Object>(); |
rowCount); |
||||||
for (Parameter p : ps) { |
} |
||||||
map.put(p.getName(), p.getValue()); |
} |
||||||
} |
return null; |
||||||
try { |
} |
||||||
return TemplateUtils.renderParameter4Tpl(query, map); |
|
||||||
} catch (Exception e) { |
private String calculateQuery(String query, Parameter[] ps) { |
||||||
return query; |
if (ArrayUtils.isEmpty(ps)) { |
||||||
} |
return query; |
||||||
} |
} |
||||||
|
Map<String, Object> map = new HashMap<String, Object>(); |
||||||
public void readXML(XMLableReader reader) { |
for (Parameter p : ps) { |
||||||
super.readXML(reader); |
map.put(p.getName(), p.getValue()); |
||||||
|
} |
||||||
if (reader.isChildNode()) { |
try { |
||||||
String tmpName = reader.getTagName(); |
return TemplateUtils.renderParameter4Tpl(query, map); |
||||||
String tmpVal; |
} catch (Exception e) { |
||||||
|
return query; |
||||||
if (OrderValue.XML_TAG.equals(tmpName)) { |
} |
||||||
OrderValue orderValue = (OrderValue) GeneralXMLTools.readXMLable(reader); |
} |
||||||
if (orderValue != null) { |
|
||||||
setOrderValue(orderValue); |
public void readXML(XMLableReader reader) { |
||||||
} |
super.readXML(reader); |
||||||
} else if (com.fr.data.impl.Connection.XML_TAG.equals(tmpName)) { |
|
||||||
if (reader.getAttrAsString("class", null) != null) { |
if (reader.isChildNode()) { |
||||||
com.fr.data.impl.Connection con = DataCoreXmlUtils.readXMLConnection(reader); |
String tmpName = reader.getTagName(); |
||||||
this.setDatabase(con); |
String tmpVal; |
||||||
} |
|
||||||
} else if ("Query".equals(tmpName)) { |
if (OrderValue.XML_TAG.equals(tmpName)) { |
||||||
tmpVal = reader.getElementValue(); |
OrderValue orderValue = (OrderValue) GeneralXMLTools.readXMLable(reader); |
||||||
if (isNotNullValue(tmpVal)) { |
if (orderValue != null) { |
||||||
this.setQuery(tmpVal); |
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); |
||||||
@Override |
} |
||||||
public void writeXML(XMLPrintWriter writer) { |
} else if ("Query".equals(tmpName)) { |
||||||
super.writeXML(writer); |
tmpVal = reader.getElementValue(); |
||||||
GeneralXMLTools.writeXMLable(writer, dbIndex.get(), OrderValue.XML_TAG); |
if (isNotNullValue(tmpVal)) { |
||||||
if (this.database.get() != null) { |
this.setQuery(tmpVal); |
||||||
DataCoreXmlUtils.writeXMLConnection(writer, this.database.get()); |
} |
||||||
} |
} else if ("Script".equals(tmpName)) { |
||||||
writer.startTAG("Query").textNode(getQuery()).end(); |
tmpVal = reader.getElementValue(); |
||||||
} |
if (isNotNullValue(tmpVal)) { |
||||||
|
this.setScript(tmpVal); |
||||||
private boolean isNotNullValue(String value) { |
} |
||||||
return value != null && !"null".equals(value); |
} |
||||||
} |
} |
||||||
|
} |
||||||
@Override |
|
||||||
public Object clone() throws CloneNotSupportedException { |
@Override |
||||||
RedisTableData cloned = (RedisTableData) super.clone(); |
public void writeXML(XMLPrintWriter writer) { |
||||||
cloned.database = (Conf<Connection>) database.clone(); |
super.writeXML(writer); |
||||||
cloned.query = (Conf<String>) query.clone(); |
GeneralXMLTools.writeXMLable(writer, dbIndex.get(), OrderValue.XML_TAG); |
||||||
cloned.dbIndex = (Conf<OrderValue>) dbIndex.clone(); |
if (this.database.get() != null) { |
||||||
return cloned; |
DataCoreXmlUtils.writeXMLConnection(writer, this.database.get()); |
||||||
} |
} |
||||||
|
writer.startTAG("Query").textNode(getQuery()).end(); |
||||||
|
writer.startTAG("Script").textNode(getScript()).end(); |
||||||
|
} |
||||||
|
|
||||||
|
private boolean isNotNullValue(String value) { |
||||||
|
return value != null && !"null".equals(value); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object clone() throws CloneNotSupportedException { |
||||||
|
RedisTableData cloned = (RedisTableData) super.clone(); |
||||||
|
cloned.database = (Conf<Connection>) database.clone(); |
||||||
|
cloned.query = (Conf<String>) query.clone(); |
||||||
|
cloned.script = (Conf<String>) script.clone(); |
||||||
|
cloned.dbIndex = (Conf<OrderValue>) dbIndex.clone(); |
||||||
|
return cloned; |
||||||
|
} |
||||||
} |
} |
@ -1,91 +0,0 @@ |
|||||||
package com.fr.plugin.db.redis.core.visit.impl; |
|
||||||
|
|
||||||
import com.fr.base.Parameter; |
|
||||||
import com.fr.general.FRLogger; |
|
||||||
import com.fr.plugin.db.redis.conf.ShellConfigManager; |
|
||||||
import com.fr.plugin.db.redis.core.DataWrapper; |
|
||||||
import com.fr.plugin.db.redis.core.visit.AbstractVisitor; |
|
||||||
import com.fr.script.Calculator; |
|
||||||
import com.fr.stable.ArrayUtils; |
|
||||||
import com.fr.stable.StringUtils; |
|
||||||
import redis.clients.jedis.Jedis; |
|
||||||
|
|
||||||
import java.io.BufferedInputStream; |
|
||||||
import java.io.ByteArrayOutputStream; |
|
||||||
import java.io.IOException; |
|
||||||
import java.io.InputStream; |
|
||||||
import java.util.List; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by richie on 2017/6/5. |
|
||||||
*/ |
|
||||||
public class SingleArrayVisitor extends AbstractVisitor<String> { |
|
||||||
@Override |
|
||||||
public List<List<String>> getContent(Calculator calculator, Parameter[] ps, Jedis client, String query, int rowCount) throws Exception { |
|
||||||
String[] arr = query.trim().split(TOKEN_SPACE); |
|
||||||
if (ArrayUtils.getLength(arr) < 1) { |
|
||||||
throw new IllegalArgumentException("Illegal query:" + query); |
|
||||||
} |
|
||||||
String shell = ShellConfigManager.getProviderInstance().getShellText(); |
|
||||||
if (StringUtils.isNotEmpty(shell)) { |
|
||||||
for (Parameter parameter : ps) { |
|
||||||
shell += " " + parameter.getName() + " " + parameter.getValue(); |
|
||||||
} |
|
||||||
Process process = Runtime.getRuntime().exec(shell); |
|
||||||
String statusText = getShellResult(process); |
|
||||||
if (isSuccess(statusText)) { |
|
||||||
return calculateData(client, arr[1]); |
|
||||||
} |
|
||||||
} |
|
||||||
return null; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public String keyWord() { |
|
||||||
return "singlemaparray"; |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
private boolean isSuccess(String statusText) { |
|
||||||
return "success".equals(statusText); |
|
||||||
} |
|
||||||
|
|
||||||
// TODO:richie 只要实现这个方法就可以了,还要处理列名的时候,需要修改下面 buildData方法
|
|
||||||
private List<List<String>> calculateData(Jedis client, String key) { |
|
||||||
String result = client.get(key); |
|
||||||
return null; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public DataWrapper<String> buildData(Calculator calculator, Parameter[] ps, Jedis client, String query, int rowCount) throws Exception { |
|
||||||
return super.buildData(calculator, ps, client, query, rowCount); |
|
||||||
} |
|
||||||
|
|
||||||
private String getShellResult(Process process) { |
|
||||||
String result = null; |
|
||||||
try { |
|
||||||
ByteArrayOutputStream resultOutStream = new ByteArrayOutputStream(); |
|
||||||
InputStream errorInStream = new BufferedInputStream(process.getErrorStream()); |
|
||||||
InputStream processInStream = new BufferedInputStream(process.getInputStream()); |
|
||||||
int num = 0; |
|
||||||
byte[] bs = new byte[1024]; |
|
||||||
while ((num = errorInStream.read(bs)) != -1) { |
|
||||||
resultOutStream.write(bs, 0, num); |
|
||||||
} |
|
||||||
while ((num = processInStream.read(bs)) != -1) { |
|
||||||
resultOutStream.write(bs, 0, num); |
|
||||||
} |
|
||||||
result = new String(resultOutStream.toByteArray()); |
|
||||||
errorInStream.close(); |
|
||||||
processInStream.close(); |
|
||||||
resultOutStream.close(); |
|
||||||
} catch (IOException e) { |
|
||||||
FRLogger.getLogger().error(e.getMessage(), e); |
|
||||||
} finally { |
|
||||||
if (process != null) { |
|
||||||
process.destroy(); |
|
||||||
} |
|
||||||
} |
|
||||||
return result; |
|
||||||
} |
|
||||||
} |
|
Loading…
Reference in new issue