Преглед изворни кода

HUE-8758 [connector] Switch to using connectors in the web workers

Johan Ahlen пре 5 година
родитељ
комит
e5ee775264

+ 4 - 1
desktop/core/src/desktop/js/apps/notebook/snippet.js

@@ -37,6 +37,7 @@ import {
   ASSIST_GET_SOURCE_EVENT,
   ASSIST_SET_SOURCE_EVENT
 } from 'ko/components/assist/events';
+import { POST_FROM_LOCATION_WORKER_EVENT } from 'sql/sqlWorkerHandler';
 
 const NOTEBOOK_MAPPING = {
   ignore: [
@@ -215,6 +216,8 @@ class Snippet {
       };
     });
 
+    self.dialect = ko.pureComputed(() => this.connector().dialect);
+
     self.isBatchable = ko.computed(() => {
       return (
         self.type() == 'hive' ||
@@ -863,7 +866,7 @@ class Snippet {
     });
 
     const activeSourcePromises = [];
-    huePubSub.subscribe('ace.sql.location.worker.message', e => {
+    huePubSub.subscribe(POST_FROM_LOCATION_WORKER_EVENT, e => {
       while (activeSourcePromises.length) {
         const promise = activeSourcePromises.pop();
         if (promise.cancel) {

+ 2 - 1
desktop/core/src/desktop/js/apps/notebook2/snippet.js

@@ -54,6 +54,7 @@ import {
   ASSIST_GET_SOURCE_EVENT,
   ASSIST_SET_SOURCE_EVENT
 } from 'ko/components/assist/events';
+import { POST_FROM_LOCATION_WORKER_EVENT } from 'sql/sqlWorkerHandler';
 
 // TODO: Remove for ENABLE_NOTEBOOK_2. Temporary here for debug
 window.SqlExecutable = SqlExecutable;
@@ -582,7 +583,7 @@ export default class Snippet {
     });
 
     const activeSourcePromises = [];
-    huePubSub.subscribe('ace.sql.location.worker.message', e => {
+    huePubSub.subscribe(POST_FROM_LOCATION_WORKER_EVENT, e => {
       while (activeSourcePromises.length) {
         const promise = activeSourcePromises.pop();
         if (promise.cancel) {

+ 13 - 7
desktop/core/src/desktop/js/ko/bindings/ace/aceLocationHandler.js

@@ -25,6 +25,12 @@ import sqlStatementsParser from 'parse/sqlStatementsParser';
 import sqlUtils from 'sql/sqlUtils';
 import stringDistance from 'sql/stringDistance';
 import { DIALECT } from 'apps/notebook2/snippet';
+import {
+  POST_FROM_LOCATION_WORKER_EVENT,
+  POST_FROM_SYNTAX_WORKER_EVENT,
+  POST_TO_LOCATION_WORKER_EVENT,
+  POST_TO_SYNTAX_WORKER_EVENT
+} from 'sql/sqlWorkerHandler';
 
 // TODO: depends on Ace, sqlStatementsParser
 
@@ -719,13 +725,13 @@ class AceLocationHandler {
             statementLocation.last_column
           )
         );
-      huePubSub.publish('ace.sql.syntax.worker.post', {
+      huePubSub.publish(POST_TO_SYNTAX_WORKER_EVENT, {
         id: self.snippet.id(),
         editorChangeTime: editorChangeTime,
         beforeCursor: beforeCursor,
         afterCursor: afterCursor,
         statementLocation: statementLocation,
-        type: self.dialect()
+        connector: self.snippet.connector()
       });
     }
   }
@@ -753,7 +759,7 @@ class AceLocationHandler {
       return;
     }
 
-    self.sqlSyntaxWorkerSub = huePubSub.subscribe('ace.sql.syntax.worker.message', e => {
+    self.sqlSyntaxWorkerSub = huePubSub.subscribe(POST_FROM_SYNTAX_WORKER_EVENT, e => {
       if (
         e.data.id !== self.snippet.id() ||
         e.data.editorChangeTime !== self.editor.lastChangeTime
@@ -1191,7 +1197,7 @@ class AceLocationHandler {
       getLocationsSub.remove();
     });
 
-    const locationWorkerSub = huePubSub.subscribe('ace.sql.location.worker.message', e => {
+    const locationWorkerSub = huePubSub.subscribe(POST_FROM_LOCATION_WORKER_EVENT, e => {
       if (
         e.data.id !== self.snippet.id() ||
         e.data.editorChangeTime !== self.editor.lastChangeTime ||
@@ -1202,7 +1208,7 @@ class AceLocationHandler {
 
       lastKnownLocations = {
         id: self.editorId,
-        type: self.dialect(),
+        connector: self.snippet.connector(),
         namespace: self.snippet.namespace(),
         compute: self.snippet.compute(),
         defaultDatabase: self.snippet.database(),
@@ -1337,10 +1343,10 @@ class AceLocationHandler {
             lastContextRequest.dispose();
           }
           lastContextRequest = self.snippet.whenContextSet().done(() => {
-            huePubSub.publish('ace.sql.location.worker.post', {
+            huePubSub.publish(POST_TO_LOCATION_WORKER_EVENT, {
               id: self.snippet.id(),
               statementDetails: statementDetails,
-              type: self.snippet && self.snippet.dialect ? self.snippet.dialect() : self.dialect(),
+              connector: self.snippet.connector(),
               namespace: self.snippet.namespace(),
               compute: self.snippet.compute(),
               defaultDatabase: self.snippet.database()

+ 1 - 0
desktop/core/src/desktop/js/ko/components/assist/assistDbSource.js

@@ -38,6 +38,7 @@ class AssistDbSource {
   constructor(options) {
     const self = this;
 
+    // TODO: Get rid of sourceType
     self.sourceType = options.type;
     self.connector = options.connector;
     self.name = options.name;

+ 9 - 8
desktop/core/src/desktop/js/ko/components/assist/ko.assistEditorContextPanel.js

@@ -346,12 +346,13 @@ class AssistEditorContextPanel {
       }
       updateOnVisible = false;
 
-      if (!sources[activeLocations.type]) {
-        sources[activeLocations.type] = {
+      if (!sources[activeLocations.connector.id]) {
+        sources[activeLocations.connector.id] = {
           assistDbSource: new AssistDbSource({
             i18n: i18n,
             initialNamespace: activeLocations.namespace,
-            type: activeLocations.type,
+            connector: activeLocations.connector,
+            type: activeLocations.connector.id,
             name: activeLocations.type,
             navigationSettings: navigationSettings
           }),
@@ -360,9 +361,9 @@ class AssistEditorContextPanel {
         };
       }
 
-      const assistDbSource = sources[activeLocations.type].assistDbSource;
-      const databaseIndex = sources[activeLocations.type].databaseIndex;
-      const activeTableIndex = sources[activeLocations.type].activeTableIndex;
+      const assistDbSource = sources[activeLocations.connector.id].assistDbSource;
+      const databaseIndex = sources[activeLocations.connector.id].databaseIndex;
+      const activeTableIndex = sources[activeLocations.connector.id].activeTableIndex;
 
       if (!activeLocations) {
         this.activeLocations(undefined);
@@ -414,7 +415,7 @@ class AssistEditorContextPanel {
                   .getEntry({
                     namespace: activeLocations.namespace,
                     compute: activeLocations.compute,
-                    connector: this.connector(),
+                    connector: activeLocations.connector,
                     path: [database],
                     definition: { type: 'database' }
                   })
@@ -494,7 +495,7 @@ class AssistEditorContextPanel {
                                 .getEntry({
                                   namespace: activeLocations.namespace,
                                   compute: activeLocations.compute,
-                                  connector: self.connector,
+                                  connector: activeLocations.connector,
                                   path: []
                                 })
                                 .done(sourceEntry => {

+ 9 - 9
desktop/core/src/desktop/js/parse/sql/sqlParserRepository.js

@@ -49,25 +49,25 @@ class SqlParserRepository {
     this.modulePromises = {};
   }
 
-  async getParser(sourceType, parserType) {
-    if (!this.modulePromises[sourceType + parserType]) {
+  async getParser(dialect, parserType) {
+    if (!this.modulePromises[dialect + parserType]) {
       const modules = parserType === 'Autocomplete' ? AUTOCOMPLETE_MODULES : SYNTAX_MODULES;
-      this.modulePromises[sourceType + parserType] = new Promise((resolve, reject) => {
-        const targetModule = modules[sourceType] || modules.generic;
+      this.modulePromises[dialect + parserType] = new Promise((resolve, reject) => {
+        const targetModule = modules[dialect] || modules.generic;
         targetModule()
           .then(module => resolve(module.default))
           .catch(reject);
       });
     }
-    return this.modulePromises[sourceType + parserType];
+    return this.modulePromises[dialect + parserType];
   }
 
-  async getAutocompleter(sourceType) {
-    return this.getParser(sourceType, 'Autocomplete');
+  async getAutocompleter(dialect) {
+    return this.getParser(dialect, 'Autocomplete');
   }
 
-  async getSyntaxParser(sourceType) {
-    return this.getParser(sourceType, 'Syntax');
+  async getSyntaxParser(dialect) {
+    return this.getParser(dialect, 'Syntax');
   }
 }
 

+ 2 - 4
desktop/core/src/desktop/js/sql/sqlAutocompleter.js

@@ -75,9 +75,7 @@ class SqlAutocompleter {
               }
             }) + this.fixedPostfix();
           sqlParserRepository
-            .getAutocompleter(
-              window.ENABLE_NOTEBOOK_2 ? this.snippet.dialect() : this.snippet.type()
-            )
+            .getAutocompleter(this.snippet.dialect())
             .then(autocompleteParser => {
               resolve(autocompleteParser.parseSql(beforeCursor, afterCursor));
             })
@@ -97,7 +95,7 @@ class SqlAutocompleter {
   async parseAll() {
     return new Promise((resolve, reject) => {
       sqlParserRepository
-        .getAutocompleter(window.ENABLE_NOTEBOOK_2 ? this.snippet.dialect() : this.snippet.type())
+        .getAutocompleter(this.snippet.dialect())
         .then(autocompleteParser => {
           resolve(
             autocompleteParser.parseSql(

+ 2 - 2
desktop/core/src/desktop/js/sql/sqlLocationWebWorker.js

@@ -56,7 +56,7 @@ const onMessage = msg => {
   clearTimeout(throttle);
   throttle = setTimeout(() => {
     if (msg.data.statementDetails) {
-      sqlParserRepository.getAutocompleter(msg.data.type).then(parser => {
+      sqlParserRepository.getAutocompleter(msg.data.connector.dialect).then(parser => {
         let locations = [];
         const activeStatementLocations = [];
         msg.data.statementDetails.precedingStatements.forEach(statement => {
@@ -102,7 +102,7 @@ const onMessage = msg => {
 
         postMessage({
           id: msg.data.id,
-          sourceType: msg.data.type,
+          connector: msg.data.connector,
           namespace: msg.data.namespace,
           compute: msg.data.compute,
           editorChangeTime: msg.data.statementDetails.editorChangeTime,

+ 2 - 1
desktop/core/src/desktop/js/sql/sqlSyntaxWebWorker.js

@@ -50,7 +50,7 @@ const onMessage = msg => {
   }
   clearTimeout(throttle);
   throttle = setTimeout(() => {
-    sqlParserRepository.getSyntaxParser(msg.data.type).then(parser => {
+    sqlParserRepository.getSyntaxParser(msg.data.connector.dialect).then(parser => {
       const syntaxError = parser.parseSyntax(msg.data.beforeCursor, msg.data.afterCursor);
 
       if (syntaxError) {
@@ -58,6 +58,7 @@ const onMessage = msg => {
       }
       postMessage({
         id: msg.data.id,
+        connector: msg.data.connector,
         editorChangeTime: msg.data.editorChangeTime,
         syntaxError: syntaxError,
         statementLocation: msg.data.statementLocation

+ 4 - 6
desktop/core/src/desktop/js/sql/sqlUtils.js

@@ -654,7 +654,7 @@ const identifierChainToPath = identifierChain => identifierChain.map(identifier
 /**
  *
  * @param {Object} options
- * @param {String} options.sourceType
+ * @param {String} options.connector
  * @param {ContextNamespace} options.namespace
  * @param {ContextCompute} options.compute
  * @param {boolean} [options.temporaryOnly] - Default: false
@@ -665,7 +665,7 @@ const identifierChainToPath = identifierChain => identifierChain.map(identifier
  *
  * @return {CancellablePromise}
  */
-const resolveCatalogEntry = options => {
+export const resolveCatalogEntry = options => {
   const cancellablePromises = [];
   const deferred = $.Deferred();
   const promise = new CancellablePromise(deferred, undefined, cancellablePromises);
@@ -731,10 +731,9 @@ const resolveCatalogEntry = options => {
     }
 
     cancellablePromises.push(
-      // TODO: Use connectors in sqlUtils
       dataCatalog
         .getChildren({
-          connector: { id: options.sourceType },
+          connector: options.connector,
           namespace: options.namespace,
           compute: options.compute,
           path: identifierChainToPath(nextTable.identifierChain),
@@ -771,7 +770,7 @@ const resolveCatalogEntry = options => {
       .getEntry({
         namespace: options.namespace,
         compute: options.compute,
-        connector: { id: options.sourceType }, // TODO: Use connector in sqlUtils
+        connector: options.connector,
         path: [],
         cachedOnly: options && options.cachedOnly,
         cancellable: options && options.cancellable,
@@ -825,6 +824,5 @@ export default {
     a.last_column === b.last_column,
   identifierEquals: identifierEquals,
   sortSuggestions: sortSuggestions,
-  resolveCatalogEntry: resolveCatalogEntry,
   identifierChainToPath: identifierChainToPath
 };

+ 22 - 11
desktop/core/src/desktop/js/sql/sqlWorkerHandler.js

@@ -18,9 +18,14 @@ import $ from 'jquery';
 
 import dataCatalog from 'catalog/dataCatalog';
 import huePubSub from 'utils/huePubSub';
-import sqlUtils from 'sql/sqlUtils';
+import { resolveCatalogEntry } from 'sql/sqlUtils';
 
-const attachEntryResolver = function(location, sourceType, namespace, compute) {
+export const POST_TO_LOCATION_WORKER_EVENT = 'ace.sql.location.worker.post';
+export const POST_FROM_LOCATION_WORKER_EVENT = 'ace.sql.location.worker.message';
+export const POST_TO_SYNTAX_WORKER_EVENT = 'ace.sql.syntax.worker.post';
+export const POST_FROM_SYNTAX_WORKER_EVENT = 'ace.sql.syntax.worker.message';
+
+const attachEntryResolver = function(location, connector, namespace, compute) {
   location.resolveCatalogEntry = function(options) {
     if (!options) {
       options = {};
@@ -39,8 +44,8 @@ const attachEntryResolver = function(location, sourceType, namespace, compute) {
       return location.resolvePathPromise;
     }
 
-    const promise = sqlUtils.resolveCatalogEntry({
-      sourceType: sourceType,
+    const promise = resolveCatalogEntry({
+      connector: connector,
       namespace: namespace,
       compute: compute,
       temporaryOnly: options.temporaryOnly,
@@ -78,23 +83,29 @@ export default {
 
       // For syntax checking
       const aceSqlSyntaxWorker = new Worker(
-        window.HUE_BASE_URL + '/desktop/workers/aceSqlSyntaxWorker.js?v=' + window.HUE_VERSION
+        window.HUE_BASE_URL +
+          '/desktop/workers/aceSqlSyntaxWorker.js?v=' +
+          window.HUE_VERSION +
+          '.1'
       );
       aceSqlSyntaxWorker.onmessage = function(e) {
         if (e.data.ping) {
           aceSqlSyntaxWorker.isReady = true;
         } else {
-          huePubSub.publish('ace.sql.syntax.worker.message', e);
+          huePubSub.publish(POST_FROM_SYNTAX_WORKER_EVENT, e);
         }
       };
 
-      huePubSub.subscribe('ace.sql.syntax.worker.post', message => {
+      huePubSub.subscribe(POST_TO_SYNTAX_WORKER_EVENT, message => {
         whenWorkerIsReady(aceSqlSyntaxWorker, message);
       });
 
       // For location marking
       const aceSqlLocationWorker = new Worker(
-        window.HUE_BASE_URL + '/desktop/workers/aceSqlLocationWorker.js?v=' + window.HUE_VERSION
+        window.HUE_BASE_URL +
+          '/desktop/workers/aceSqlLocationWorker.js?v=' +
+          window.HUE_VERSION +
+          '.1'
       );
       aceSqlLocationWorker.onmessage = function(e) {
         if (e.data.ping) {
@@ -102,14 +113,14 @@ export default {
         } else {
           if (e.data.locations) {
             e.data.locations.forEach(location => {
-              attachEntryResolver(location, e.data.sourceType, e.data.namespace, e.data.compute);
+              attachEntryResolver(location, e.data.connector, e.data.namespace, e.data.compute);
             });
           }
-          huePubSub.publish('ace.sql.location.worker.message', e);
+          huePubSub.publish(POST_FROM_LOCATION_WORKER_EVENT, e);
         }
       };
 
-      huePubSub.subscribe('ace.sql.location.worker.post', message => {
+      huePubSub.subscribe(POST_TO_LOCATION_WORKER_EVENT, message => {
         whenWorkerIsReady(aceSqlLocationWorker, message);
       });