|
|
|
package com.fr.plugin.db.redis.core;
|
|
|
|
|
|
|
|
import com.fr.stable.ParameterProvider;
|
|
|
|
import com.fr.data.AbstractDataModel;
|
|
|
|
import com.fanruan.api.log.LogKit;
|
|
|
|
import com.fr.plugin.PluginLicense;
|
|
|
|
import com.fr.plugin.PluginLicenseManager;
|
|
|
|
import com.fr.plugin.db.redis.core.emb.Redis;
|
|
|
|
import com.fr.plugin.db.redis.core.visit.VisitorFactory;
|
|
|
|
import com.fr.plugin.db.redis.util.RedisUtils;
|
|
|
|
import com.fr.script.Calculator;
|
|
|
|
import com.fanruan.api.util.StringKit;
|
|
|
|
import com.fr.third.redis.clients.jedis.Jedis;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class RedisTableDataModel extends AbstractDataModel {
|
|
|
|
private String[] columnNames;
|
|
|
|
private List<List<Object>> data;
|
|
|
|
|
|
|
|
public RedisTableDataModel(Calculator calculator, Parameter[] ps, RedisDatabaseConnection mc, int dbIndex, String query, String script, int rowCount) {
|
|
|
|
PluginLicense pluginLicense = PluginLicenseManager.getInstance().getPluginLicenseByID(RedisConstants.PLUGIN_ID);
|
|
|
|
if (pluginLicense.isAvailable()) {
|
|
|
|
initRedisData(calculator, ps, mc, dbIndex, query, script, rowCount);
|
|
|
|
} else {
|
|
|
|
throw new RuntimeException("Redis Plugin License Expired!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private synchronized void initRedisData(Calculator calculator, Parameter[] ps, RedisDatabaseConnection mc, int dbIndex, String query, String script, int rowCount) {
|
|
|
|
if (StringKit.isEmpty(query)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Redis redis = mc.createRedisClient();
|
|
|
|
redis.getClient().select(dbIndex);
|
|
|
|
LogKit.info("Connect to redis and select database:" + dbIndex);
|
|
|
|
try {
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
DataWrapper<Object> wrapper = VisitorFactory.getKeyValueResult(calculator, ps, redis.getClient(), query, rowCount);
|
|
|
|
LogKit.info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start);
|
|
|
|
wrapper.transform(script);
|
|
|
|
data = wrapper.getData();
|
|
|
|
columnNames = wrapper.getColumnNames();
|
|
|
|
redis.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e.getCause());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getColumnCount() {
|
|
|
|
return columnNames.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getColumnName(int columnIndex) {
|
|
|
|
return columnNames[columnIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasRow(int rowIndex) {
|
|
|
|
return data != null && data.get(0).size() > rowIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getRowCount() {
|
|
|
|
return data == null ? 0 : data.get(0).size();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object getValueAt(int rowIndex, int columnIndex) {
|
|
|
|
if (data != null && data.size() > columnIndex) {
|
|
|
|
List<Object> columnData = data.get(columnIndex);
|
|
|
|
if (columnData != null && columnData.size() > rowIndex) {
|
|
|
|
return columnData.get(rowIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void release() throws Exception {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|