|
|
|
package com.fr.plugin.db.redis.core;
|
|
|
|
|
|
|
|
import com.fanruan.api.data.open.BaseDataModel;
|
|
|
|
import com.fanruan.api.log.LogKit;
|
|
|
|
import com.fanruan.api.util.StringKit;
|
|
|
|
import com.fr.plugin.context.PluginContexts;
|
|
|
|
import com.fr.plugin.db.redis.core.emb.Redis;
|
|
|
|
import com.fr.plugin.db.redis.core.visit.VisitorFactory;
|
|
|
|
import com.fr.stable.ParameterProvider;
|
|
|
|
import com.fr.stable.script.CalculatorProvider;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class RedisTableDataModel extends BaseDataModel {
|
|
|
|
private String[] columnNames;
|
|
|
|
private List<List<Object>> data;
|
|
|
|
|
|
|
|
public RedisTableDataModel(CalculatorProvider calculator, ParameterProvider[] ps, RedisDatabaseConnection mc, int dbIndex, String query, String script, int rowCount) {
|
|
|
|
if (PluginContexts.currentContext().isAvailable()) {
|
|
|
|
initRedisData(calculator, ps, mc, dbIndex, query, script, rowCount);
|
|
|
|
} else {
|
|
|
|
throw new RuntimeException("Redis Plugin License Expired!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initRedisData(CalculatorProvider calculator, ParameterProvider[] ps, RedisDatabaseConnection mc, int dbIndex, String query, String script, int rowCount) {
|
|
|
|
if (StringKit.isEmpty(query)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Redis redis = mc.createRedisClient();
|
|
|
|
try {
|
|
|
|
redis.getClient().select(dbIndex);
|
|
|
|
LogKit.info("Connect to redis and select database:" + dbIndex);
|
|
|
|
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);
|
|
|
|
data = wrapper.getData();
|
|
|
|
columnNames = wrapper.getColumnNames();
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e.getCause());
|
|
|
|
} finally {
|
|
|
|
redis.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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 {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|