|
|
|
package com.fr.plugin.db.redis.core;
|
|
|
|
|
|
|
|
import com.eclipsesource.v8.V8;
|
|
|
|
import com.eclipsesource.v8.V8Array;
|
|
|
|
import com.eclipsesource.v8.V8Object;
|
|
|
|
import com.fr.base.TableData;
|
|
|
|
import com.fr.data.AbstractDataModel;
|
|
|
|
import com.fr.plugin.db.redis.help.Console;
|
|
|
|
import com.fr.plugin.db.redis.help.Files;
|
|
|
|
import com.fr.plugin.db.redis.help.RedisClient;
|
|
|
|
import com.fr.plugin.db.redis.util.RedisUtils;
|
|
|
|
import com.fr.third.redis.clients.jedis.Jedis;
|
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author richie
|
|
|
|
* @version 10.0
|
|
|
|
* Created by richie on 2019-03-18
|
|
|
|
*/
|
|
|
|
public class RedisScriptTableDataModel extends AbstractDataModel {
|
|
|
|
|
|
|
|
private RedisDatabaseConnection mc;
|
|
|
|
private String script;
|
|
|
|
private int dbIndex;
|
|
|
|
private int rowCount;
|
|
|
|
private List<List<Object>> data = new ArrayList<List<Object>>();
|
|
|
|
private String[] columnNames = null;
|
|
|
|
|
|
|
|
public RedisScriptTableDataModel(RedisDatabaseConnection mc, int dbIndex, String script, int rowCount) {
|
|
|
|
this.mc = mc;
|
|
|
|
this.script = script;
|
|
|
|
this.dbIndex = dbIndex;
|
|
|
|
this.rowCount = rowCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void buildData() {
|
|
|
|
if (columnNames != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Jedis jedis = mc.createRedisClient();
|
|
|
|
jedis.select(dbIndex);
|
|
|
|
V8 v8 = V8.createV8Runtime();
|
|
|
|
RedisClient client = new RedisClient(v8, jedis);
|
|
|
|
V8Object v8Console = initConsole(v8);
|
|
|
|
V8Object v8Require = initRequire(v8);
|
|
|
|
V8Object v8Client = initRedis(v8, client);
|
|
|
|
|
|
|
|
v8.executeVoidScript(
|
|
|
|
"function unique(array) {return Array.from(new Set(array));};" +
|
|
|
|
"function merge(table, column) {return {content:table, column:column}};");
|
|
|
|
|
|
|
|
V8Object returnObj = v8.executeObjectScript(String.format("(function(){%s})();", script));
|
|
|
|
|
|
|
|
V8Array v8Column = returnObj.getArray("column");
|
|
|
|
V8Array v8Table = returnObj.getArray("content");
|
|
|
|
int columnCount = v8Column.length();
|
|
|
|
columnNames = new String[columnCount];
|
|
|
|
for (int i = 0; i < columnCount; i++) {
|
|
|
|
columnNames[i] = String.valueOf(v8Column.get(i));
|
|
|
|
}
|
|
|
|
int returnRowCount = v8Table.length();
|
|
|
|
int realCount = rowCount == TableData.RESULT_ALL ? returnRowCount : Math.min(returnRowCount, this.rowCount);
|
|
|
|
for (int i = 0; i < realCount; i++) {
|
|
|
|
V8Array v8Row = v8Table.getArray(i);
|
|
|
|
List<Object> row = new ArrayList<Object>();
|
|
|
|
for (int j = 0, col = v8Row.length(); j < col; j++) {
|
|
|
|
row.add(v8Row.get(j));
|
|
|
|
}
|
|
|
|
v8Row.release();
|
|
|
|
data.add(row);
|
|
|
|
}
|
|
|
|
jedis.close();
|
|
|
|
v8Table.release();
|
|
|
|
v8Column.release();
|
|
|
|
returnObj.release();
|
|
|
|
v8Client.release();
|
|
|
|
v8Require.release();
|
|
|
|
v8Console.release();
|
|
|
|
|
|
|
|
v8.release(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private V8Object initConsole(V8 v8) {
|
|
|
|
V8Object v8Console = new V8Object(v8);
|
|
|
|
v8.add("console", v8Console);
|
|
|
|
Console console = new Console(v8);
|
|
|
|
RedisUtils.registerJavaMethods(v8Console, console);
|
|
|
|
return v8Console;
|
|
|
|
}
|
|
|
|
|
|
|
|
private V8Object initRequire(V8 v8) {
|
|
|
|
V8Object v8Files = new V8Object(v8);
|
|
|
|
v8.add("Files", v8Files);
|
|
|
|
Files console = new Files(v8);
|
|
|
|
RedisUtils.registerJavaMethods(v8Files, console);
|
|
|
|
return v8Files;
|
|
|
|
}
|
|
|
|
|
|
|
|
private V8Object initRedis(final V8 v8, final RedisClient client) {
|
|
|
|
V8Object v8Client = new V8Object(v8);
|
|
|
|
v8.add("redis", v8Client);
|
|
|
|
RedisUtils.registerJavaMethods(v8Client, client);
|
|
|
|
return v8Client;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getColumnCount() {
|
|
|
|
buildData();
|
|
|
|
return columnNames == null ? 0 : columnNames.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getColumnName(int i) {
|
|
|
|
buildData();
|
|
|
|
return columnNames == null ? null : columnNames[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getRowCount() {
|
|
|
|
buildData();
|
|
|
|
return data.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object getValueAt(int rowIndex, int columnIndex) {
|
|
|
|
buildData();
|
|
|
|
if (data != null && data.size() > rowIndex) {
|
|
|
|
List<Object> rowData = data.get(rowIndex);
|
|
|
|
if (rowData != null && rowData.size() > columnIndex) {
|
|
|
|
return rowData.get(columnIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void release() throws Exception {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|