Skip to content

Commit 9c56bfe

Browse files
committed
feat: 优化 sql 执行大小
1 parent 4bed76e commit 9c56bfe

File tree

4 files changed

+83
-6
lines changed

4 files changed

+83
-6
lines changed

backend/framework/src/main/java/org/jumpserver/chen/framework/console/QueryConsole.java

+63
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.ArrayList;
3535
import java.util.HashMap;
3636
import java.util.Map;
37+
import java.util.concurrent.ConcurrentHashMap;
3738
import java.util.concurrent.CountDownLatch;
3839
import java.util.concurrent.atomic.AtomicBoolean;
3940

@@ -141,6 +142,7 @@ public void handle(Packet packet) {
141142
}
142143
}
143144

145+
144146
private void onAction(QueryConsoleAction action) {
145147
switch (action.getAction()) {
146148
case QueryConsoleAction.ACTION_RUN_SQL -> {
@@ -153,6 +155,13 @@ private void onAction(QueryConsoleAction action) {
153155
this.getState().setInQuery(false);
154156
this.stateManager.commit();
155157
}
158+
case QueryConsoleAction.ACTION_RUN_SQL_CHUNK -> {
159+
this.handleSQLChunk(action);
160+
}
161+
case QueryConsoleAction.ACTION_RUN_SQL_COMPLETE -> {
162+
this.handleSQLComplete();
163+
}
164+
156165
case QueryConsoleAction.ACTION_RUN_SQL_FILE -> {
157166
this.getState().setInQuery(true);
158167
this.stateManager.commit();
@@ -177,6 +186,60 @@ private void onAction(QueryConsoleAction action) {
177186
}
178187
}
179188

189+
private final ConcurrentHashMap<Integer, String> sqlChunks = new ConcurrentHashMap<>();
190+
private CountDownLatch latch;
191+
private int expectedChunks = -1;
192+
193+
private void handleSQLChunk(QueryConsoleAction action) {
194+
var data = (Map<String, Object>) action.getData();
195+
var chunk = (String) data.get("chunk");
196+
var index = (Integer) data.get("index");
197+
var total = (Integer) data.get("total");
198+
199+
synchronized (this) {
200+
if (expectedChunks == -1) {
201+
expectedChunks = total;
202+
latch = new CountDownLatch(total);
203+
}
204+
}
205+
206+
if (sqlChunks.putIfAbsent(index, chunk) == null) {
207+
latch.countDown();
208+
}
209+
}
210+
211+
/**
212+
* 处理分段 SQL 接收完成
213+
*/
214+
private void handleSQLComplete() {
215+
try {
216+
// 等待所有分段接收完成
217+
latch.await();
218+
219+
// 按照索引顺序合并所有分段
220+
StringBuilder sqlBuilder = new StringBuilder();
221+
for (int i = 0; i < expectedChunks; i++) {
222+
sqlBuilder.append(sqlChunks.get(i));
223+
}
224+
225+
// 合并完成后清理缓存
226+
var sql = sqlBuilder.toString();
227+
sqlChunks.clear();
228+
expectedChunks = -1;
229+
230+
// 执行完整 SQL
231+
this.getState().setInQuery(true);
232+
this.stateManager.commit();
233+
234+
this.onSQL(sql);
235+
236+
} catch (InterruptedException e) {
237+
Thread.currentThread().interrupt();
238+
} finally {
239+
this.getState().setInQuery(false);
240+
this.stateManager.commit();
241+
}
242+
}
180243

181244
private void onDataViewAction(DataViewAction action) {
182245
var dataView = this.dataViews.get(action.getDataView());

backend/framework/src/main/java/org/jumpserver/chen/framework/console/action/QueryConsoleAction.java

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
@EqualsAndHashCode(callSuper = true)
88
public class QueryConsoleAction extends Action {
99
public static final String ACTION_RUN_SQL = "run_sql";
10+
public static final String ACTION_RUN_SQL_CHUNK = "run_sql_chunk";
11+
public static final String ACTION_RUN_SQL_COMPLETE = "run_sql_complete";
1012
public static final String ACTION_RUN_SQL_FILE = "run_sql_file";
1113
public static final String ACTION_CANCEL = "cancel";
1214
public static final String ACTION_CHANGE_CURRENT_CONTEXT = "change_current_context";

frontend/src/components/Main/Explore/QueryConsole/CodeEditor.vue

+18-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,24 @@ export default {
253253
},
254254
onRun() {
255255
const sql = this.selectionValue || this.statement
256-
this.$emit('action', { action: 'run_sql', data: sql })
256+
const CHUNK_SIZE = 4096
257+
258+
if (sql.length <= CHUNK_SIZE) {
259+
this.$emit('action', { action: 'run_sql', data: sql })
260+
} else {
261+
const totalChunks = Math.ceil(sql.length / CHUNK_SIZE)
262+
for (let i = 0; i < totalChunks; i++) {
263+
const chunk = sql.slice(i * CHUNK_SIZE, (i + 1) * CHUNK_SIZE)
264+
this.$emit('action', {
265+
action: 'run_sql_chunk',
266+
data: { chunk, index: i, total: totalChunks }
267+
})
268+
}
269+
this.$emit('action', {
270+
action: 'run_sql_complete',
271+
data: { total: totalChunks }
272+
})
273+
}
257274
},
258275
onStop() {
259276
this.$emit('action', { action: 'cancel' })

frontend/src/components/Main/Explore/QueryConsole/index.vue

-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
:state="state"
99
:subjects="subjects"
1010
@action="onEditorAction"
11-
@run="onRunSql"
1211
/>
1312
</template>
1413
<template slot="paneR">
@@ -153,11 +152,7 @@ export default {
153152
onDataViewAction(action) {
154153
this.ws.send(JSON.stringify({ type: 'data_view_action', data: action }))
155154
},
156-
onRunSql(sql) {
157-
this.ws.send(JSON.stringify({ type: 'sql', data: sql }))
158-
},
159155
onCloseDataView(name) {
160-
console.log(name)
161156
this.ws.send(JSON.stringify({ type: 'close_data_view', data: name }))
162157
},
163158
onLimitChange(limit) {

0 commit comments

Comments
 (0)