forked from fanruan/demo-tabledata-redis
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
5.6 KiB
157 lines
5.6 KiB
package com.fr.plugin.db.redis.core; |
|
|
|
import com.fanruan.api.conf.HolderKit; |
|
import com.fanruan.api.conf.xml.XmlHolderKit; |
|
import com.fanruan.api.data.ConnectionKit; |
|
import com.fanruan.api.data.open.BaseTableData; |
|
import com.fanruan.api.log.LogKit; |
|
import com.fanruan.api.util.StringKit; |
|
import com.fanruan.api.xml.XmlKit; |
|
import com.fr.base.TableData; |
|
import com.fr.config.holder.Conf; |
|
import com.fr.data.impl.Connection; |
|
import com.fr.general.data.DataModel; |
|
import com.fr.intelli.record.Focus; |
|
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.script.EngineType; |
|
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.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 BaseTableData { |
|
|
|
private static final long serialVersionUID = 1525853354993816818L; |
|
|
|
private Conf<Connection> database = HolderKit.obj(null, Connection.class); |
|
private Conf<OrderValue> dbIndex = XmlHolderKit.obj(new NumberOrderValue(0), OrderValue.class, OrderValue.XML_TAG); |
|
private Conf<String> script = HolderKit.simple(StringKit.EMPTY); |
|
private Conf<Integer> engineType = HolderKit.simple(EngineType.JAVA.getType()); |
|
|
|
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 EngineType getEngineType() { |
|
return EngineType.parse(engineType.get()); |
|
} |
|
|
|
public void setEngineType(EngineType engineType) { |
|
this.engineType.set(engineType.getType()); |
|
} |
|
|
|
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(); |
|
ParameterProvider[] ps = getParameters(calculator); |
|
Connection connection = database.get(); |
|
String name = RedisUtils.getName(connection); |
|
if (StringKit.isNotEmpty(name)) { |
|
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); |
|
LogKit.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) XmlKit.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 = XmlKit.readXMLConnection(reader); |
|
this.setDatabase(con); |
|
} |
|
} else if ("Script".equals(tmpName)) { |
|
tmpVal = reader.getElementValue(); |
|
if (isNotNullValue(tmpVal)) { |
|
this.setScript(tmpVal); |
|
} |
|
} else if ("ScriptAttr".equals(tmpName)) { |
|
setEngineType(EngineType.parse(reader.getAttrAsInt("engineType", 0))); |
|
} |
|
} |
|
} |
|
|
|
@Override |
|
public void writeXML(XMLPrintWriter writer) { |
|
super.writeXML(writer); |
|
XmlKit.writeXMLable(writer, dbIndex.get(), OrderValue.XML_TAG); |
|
if (this.database.get() != null) { |
|
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) { |
|
return value != null && !"null".equals(value); |
|
} |
|
|
|
@Override |
|
public Object clone() throws CloneNotSupportedException { |
|
RedisScriptTableData cloned = (RedisScriptTableData) super.clone(); |
|
cloned.database = (Conf<Connection>) database.clone(); |
|
cloned.script = (Conf<String>) script.clone(); |
|
cloned.dbIndex = (Conf<OrderValue>) dbIndex.clone(); |
|
cloned.engineType = (Conf<Integer>) engineType.clone(); |
|
return cloned; |
|
} |
|
}
|
|
|