فهرست منبع

HUE-7738 [editor] Cache the responses from the UDF autocomplete endpoint

Johan Ahlen 5 سال پیش
والد
کامیت
0bd98e8673

+ 14 - 5
desktop/core/src/desktop/js/sql/reference/sqlReferenceRepository.js

@@ -17,6 +17,10 @@
 import ApiHelper from 'api/apiHelper';
 import { matchesType } from './typeUtils';
 import I18n from 'utils/i18n';
+import huePubSub from 'utils/huePubSub';
+import { clearUdfCache, getCachedApiUdfs, setCachedApiUdfs } from './sqlReferenceRepositoryCache';
+
+export const CLEAR_UDF_CACHE_EVENT = 'hue.clear.udf.cache';
 
 const SET_REFS = {
   impala: async () => import(/* webpackChunkName: "impala-ref" */ './impala/setReference')
@@ -69,6 +73,7 @@ const findUdfsToAdd = (apiUdfs, existingCategories) => {
   const result = {};
 
   apiUdfs.forEach(apiUdf => {
+    // TODO: Impala reports the same UDF multiple times, once per argument type.
     if (
       !result[apiUdf.name] &&
       !existingUdfNames.has(apiUdf.name.toUpperCase()) &&
@@ -82,11 +87,15 @@ const findUdfsToAdd = (apiUdfs, existingCategories) => {
 };
 
 const mergeWithApiUdfs = async (categories, connector, database) => {
-  const apiUdfs = await ApiHelper.fetchUdfs({
-    connector: connector,
-    database: database,
-    silenceErrors: true
-  });
+  let apiUdfs = await getCachedApiUdfs(connector, database);
+  if (!apiUdfs) {
+    apiUdfs = await ApiHelper.fetchUdfs({
+      connector: connector,
+      database: database,
+      silenceErrors: true
+    });
+    await setCachedApiUdfs(connector, database, apiUdfs);
+  }
 
   if (apiUdfs.length) {
     const additionalUdfs = findUdfsToAdd(apiUdfs, categories);

+ 32 - 0
desktop/core/src/desktop/js/sql/reference/sqlReferenceRepositoryCache.js

@@ -0,0 +1,32 @@
+// Licensed to Cloudera, Inc. under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  Cloudera, Inc. licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+import localForage from 'localforage';
+
+const GLOBAL_UDF_CACHE_KEY = 'HUE_GLOBAL_UDF_KEY';
+
+const getStore = connector =>
+  localForage.createInstance({
+    name: 'HueUdfCatalog_' + connector.id
+  });
+
+export const clearUdfCache = async connector => await getStore(connector).clear();
+
+export const getCachedApiUdfs = async (connector, database) =>
+  await getStore(connector).getItem(database ? database.name : GLOBAL_UDF_CACHE_KEY);
+
+export const setCachedApiUdfs = async (connector, database, apiUdfs) =>
+  await getStore(connector).setItem(database ? database.name : GLOBAL_UDF_CACHE_KEY, apiUdfs);