splitting query moved to web worker

This commit is contained in:
Jan Prochazka
2021-12-12 12:26:26 +01:00
parent 42d7166d2b
commit f016ece2af
5 changed files with 68 additions and 21 deletions

View File

@@ -21,13 +21,17 @@
"@tsconfig/svelte": "^1.0.0", "@tsconfig/svelte": "^1.0.0",
"ace-builds": "^1.4.8", "ace-builds": "^1.4.8",
"chart.js": "^3.6.0", "chart.js": "^3.6.0",
"chartjs-adapter-moment": "^1.0.0",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"dbgate-datalib": "^4.1.1", "dbgate-datalib": "^4.1.1",
"dbgate-sqltree": "^4.1.1",
"dbgate-query-splitter": "^4.1.1", "dbgate-query-splitter": "^4.1.1",
"dbgate-sqltree": "^4.1.1",
"dbgate-tools": "^4.1.1", "dbgate-tools": "^4.1.1",
"dbgate-types": "^4.1.1", "dbgate-types": "^4.1.1",
"diff": "^5.0.0",
"diff2html": "^3.4.13",
"file-selector": "^0.2.4", "file-selector": "^0.2.4",
"js-yaml": "^4.1.0",
"json-stable-stringify": "^1.0.1", "json-stable-stringify": "^1.0.1",
"localforage": "^1.9.0", "localforage": "^1.9.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",
@@ -39,6 +43,7 @@
"rollup-plugin-livereload": "^2.0.0", "rollup-plugin-livereload": "^2.0.0",
"rollup-plugin-svelte": "^7.0.0", "rollup-plugin-svelte": "^7.0.0",
"rollup-plugin-terser": "^7.0.0", "rollup-plugin-terser": "^7.0.0",
"rollup-plugin-web-worker-loader": "^1.6.1",
"sirv-cli": "^1.0.0", "sirv-cli": "^1.0.0",
"socket.io-client": "^2.3.0", "socket.io-client": "^2.3.0",
"sql-formatter": "^2.3.3", "sql-formatter": "^2.3.3",
@@ -49,10 +54,6 @@
"svelte-select": "^3.17.0", "svelte-select": "^3.17.0",
"tslib": "^2.3.1", "tslib": "^2.3.1",
"typescript": "^4.4.3", "typescript": "^4.4.3",
"uuid": "^3.4.0", "uuid": "^3.4.0"
"chartjs-adapter-moment": "^1.0.0",
"diff": "^5.0.0",
"diff2html": "^3.4.13",
"js-yaml": "^4.1.0"
} }
} }

View File

@@ -7,8 +7,10 @@ import { terser } from 'rollup-plugin-terser';
import sveltePreprocess from 'svelte-preprocess'; import sveltePreprocess from 'svelte-preprocess';
import typescript from '@rollup/plugin-typescript'; import typescript from '@rollup/plugin-typescript';
import replace from '@rollup/plugin-replace'; import replace from '@rollup/plugin-replace';
import webWorkerLoader from 'rollup-plugin-web-worker-loader';
import css from 'rollup-plugin-css-only'; import css from 'rollup-plugin-css-only';
const production = !process.env.ROLLUP_WATCH; const production = !process.env.ROLLUP_WATCH;
function serve() { function serve() {
@@ -99,6 +101,8 @@ export default {
// If we're building for production (npm run build // If we're building for production (npm run build
// instead of npm run dev), minify // instead of npm run dev), minify
production && terser(), production && terser(),
webWorkerLoader(),
], ],
watch: { watch: {
clearScreen: false, clearScreen: false,

View File

@@ -1,6 +1,6 @@
<script lang="ts"> <script lang="ts">
// copied from https://github.com/nateshmbhat/svelte-ace/blob/main/src/AceEditor.svelte // copied from https://github.com/nateshmbhat/svelte-ace/blob/main/src/AceEditor.svelte
import { createEventDispatcher, tick, onMount, onDestroy } from 'svelte'; import { createEventDispatcher, tick, onMount, onDestroy, getContext } from 'svelte';
import * as ace from 'ace-builds/src-noconflict/ace'; import * as ace from 'ace-builds/src-noconflict/ace';
@@ -25,7 +25,8 @@
import _ from 'lodash'; import _ from 'lodash';
import { handleCommandKeyDown } from '../commands/CommandListener.svelte'; import { handleCommandKeyDown } from '../commands/CommandListener.svelte';
import resizeObserver from '../utility/resizeObserver'; import resizeObserver from '../utility/resizeObserver';
import { splitQuery } from 'dbgate-query-splitter'; // @ts-ignore
import QueryParserWorker from 'web-worker:./QueryParserWorker';
const EDITOR_ID = `svelte-ace-editor-div:${Math.floor(Math.random() * 10000000000)}`; const EDITOR_ID = `svelte-ace-editor-div:${Math.floor(Math.random() * 10000000000)}`;
const dispatch = createEventDispatcher<{ const dispatch = createEventDispatcher<{
@@ -55,6 +56,8 @@
export let readOnly = false; export let readOnly = false;
export let splitterOptions = null; export let splitterOptions = null;
const tabVisible: any = getContext('tabVisible');
let editor: ace.Editor; let editor: ace.Editor;
let contentBackup: string = ''; let contentBackup: string = '';
@@ -65,6 +68,8 @@
let currentPart = null; let currentPart = null;
let currentPartMarker = null; let currentPartMarker = null;
let queryParserWorker;
const stdOptions = { const stdOptions = {
showPrintMargin: false, showPrintMargin: false,
}; };
@@ -122,8 +127,24 @@
} }
} }
$: { $: watchQueryParserWorker(splitterOptions && $tabVisible);
splitterOptions; function watchQueryParserWorker(enabled) {
if (enabled) {
if (!queryParserWorker) {
queryParserWorker = new QueryParserWorker();
queryParserWorker.onmessage = e => {
queryParts = e.data;
editor.setHighlightActiveLine(queryParts.length <= 1);
changedCurrentQueryPart();
};
}
} else {
if (queryParserWorker) {
queryParserWorker.terminate();
queryParserWorker = null;
}
}
changedQueryParts(); changedQueryParts();
} }
@@ -151,22 +172,28 @@
function changedQueryParts() { function changedQueryParts() {
const editor = getEditor(); const editor = getEditor();
if (splitterOptions && editor) { if (splitterOptions && editor && queryParserWorker) {
const sql = editor.getValue(); const editor = getEditor();
queryParts = splitQuery(sql, {
queryParserWorker.postMessage({
text: editor.getValue(),
options: {
...splitterOptions, ...splitterOptions,
returnRichInfo: true, returnRichInfo: true,
},
}); });
editor.setHighlightActiveLine(queryParts.length <= 1);
if (queryParts.length <= 1) {
removeCurrentPartMarker();
} }
} // if (splitterOptions && editor) {
changedCurrentQueryPart(); // const sql = editor.getValue();
// }
} }
function changedCurrentQueryPart() { function changedCurrentQueryPart() {
if (queryParts.length <= 1) return; if (queryParts.length <= 1) {
removeCurrentPartMarker();
return;
}
const selectionRange = editor.getSelectionRange(); const selectionRange = editor.getSelectionRange();
if ( if (
@@ -235,6 +262,10 @@
editor.destroy(); editor.destroy();
editor.container.remove(); editor.container.remove();
} }
if (queryParserWorker) {
queryParserWorker.terminate();
queryParserWorker = null;
}
}); });
function setEventCallBacks() { function setEventCallBacks() {

View File

@@ -0,0 +1,6 @@
import { splitQuery } from 'dbgate-query-splitter';
onmessage = e => {
const result = splitQuery(e.data.text, e.data.options);
postMessage(result);
};

View File

@@ -9138,6 +9138,11 @@ rollup-plugin-terser@^7.0.0:
serialize-javascript "^4.0.0" serialize-javascript "^4.0.0"
terser "^5.0.0" terser "^5.0.0"
rollup-plugin-web-worker-loader@^1.6.1:
version "1.6.1"
resolved "https://registry.yarnpkg.com/rollup-plugin-web-worker-loader/-/rollup-plugin-web-worker-loader-1.6.1.tgz#9d7a27575b64b0780fe4e8b3bc87470d217e485f"
integrity sha512-4QywQSz1NXFHKdyiou16mH3ijpcfLtLGOrAqvAqu1Gx+P8+zj+3gwC2BSL/VW1d+LW4nIHC8F7d7OXhs9UdR2A==
rollup-pluginutils@^2.8.2: rollup-pluginutils@^2.8.2:
version "2.8.2" version "2.8.2"
resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.8.2.tgz#72f2af0748b592364dbd3389e600e5a9444a351e" resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.8.2.tgz#72f2af0748b592364dbd3389e600e5a9444a351e"