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

[notebook] Cache the HDFS autocomplete data

Johan Ahlen пре 10 година
родитељ
комит
3a8bcf5

+ 2 - 1
apps/beeswax/src/beeswax/templates/execute.mako

@@ -1109,7 +1109,8 @@ var notebook = editorViewModel.newNotebook();
 var snippet = notebook.newSnippet(snippetType);
 var assistHelper = snippet.getAssistHelper();
 var autocompleter = new Autocompleter({
-  snippet: snippet
+  snippet: snippet,
+  user: HIVE_AUTOCOMPLETE_USER
 });
 
 var escapeOutput = function (str) {

+ 5 - 1
desktop/core/src/desktop/static/desktop/js/autocompleter.js

@@ -28,6 +28,8 @@
   /**
    * @param options {object}
    * @param options.snippet
+   * @param options.user
+   *
    * @constructor
    */
   function Autocompleter(options) {
@@ -40,7 +42,9 @@
           snippet: options.snippet
         })
       } else {
-        self.autocompleter = new HdfsAutocompleter();
+        self.autocompleter = new HdfsAutocompleter({
+          user: options.user
+        });
       }
     };
     self.snippet.type.subscribe(function () {

+ 44 - 20
desktop/core/src/desktop/static/desktop/js/hdfsAutocompleter.js

@@ -22,46 +22,70 @@
   }
 }(this, function () {
 
+  var TIME_TO_LIVE_IN_MILLIS = 60000; // 1 minute
   var BASE_PATH = "/filebrowser/view=";
   var PARAMETERS = "?pagesize=100&format=json";
 
   /**
    * @param options {object}
+   * @param options.user {string}
    *
    * @constructor
    */
   function HdfsAutocompleter(options) {
     var self = this;
+    self.user = options.user;
   }
 
+  HdfsAutocompleter.prototype.getTotalStorageUserPrefix = function () {
+    var self = this;
+    return self.user;
+  };
+
+  HdfsAutocompleter.prototype.hasExpired = function (timestamp) {
+    return (new Date()).getTime() - timestamp > TIME_TO_LIVE_IN_MILLIS;
+  };
+
   HdfsAutocompleter.prototype.fetchAlternatives = function (pathParts, success, error, editor) {
     var self = this;
-    if (editor) {
-      editor.showSpinner();
-    }
 
-    $.ajax({
-      dataType: "json",
-      url: BASE_PATH + "/" + pathParts.join("/") + PARAMETERS,
-      success: function (data) {
+    var url = BASE_PATH + "/" + pathParts.join("/") + PARAMETERS;
+    var cachedData = $.totalStorage("hue.assist." + self.getTotalStorageUserPrefix()) || {};
+
+    if (typeof cachedData[url] == "undefined" || self.hasExpired(cachedData[url].timestamp)) {
+      if (editor) {
+        editor.showSpinner();
+      }
+      $.ajax({
+        dataType: "json",
+        url: url,
+        success: function (data) {
+          if (editor) {
+            editor.hideSpinner();
+          }
+          if (!data.error) {
+            cachedData[url] = {
+              timestamp: (new Date()).getTime(),
+              data: data
+            };
+            $.totalStorage("hue.assist." + self.getTotalStorageUserPrefix(), cachedData);
+            success(self.extractFields(data));
+          } else {
+            error();
+          }
+        }
+      }).fail(function () {
         if (editor) {
           editor.hideSpinner();
         }
-        if (! data.error) {
-          success(self.extractFields(data, pathParts.length > 0));
-        } else {
-          error();
-        }
-      }
-    }).fail(function () {
-      if (editor) {
-        editor.hideSpinner();
-      }
-      error();
-    });
+        error();
+      });
+    } else {
+      success(self.extractFields(cachedData[url].data));
+    }
   };
 
-  HdfsAutocompleter.prototype.extractFields = function (data, includeParent) {
+  HdfsAutocompleter.prototype.extractFields = function (data) {
     var files = $.map(data.files, function (file) {
       return {
         name: file.name,

+ 3 - 1
desktop/core/src/desktop/static/desktop/spec/hdfsAutocompleterSpec.js

@@ -56,7 +56,9 @@ define([
     });
 
     beforeEach(function() {
-      subject = new HdfsAutocompleter();
+      subject = new HdfsAutocompleter({
+        user: "user"
+      });
       ajaxHelper.responseForUrls = {};
     });
 

+ 1 - 1
desktop/core/src/desktop/static/desktop/spec/sqlAutocompleterSpec.js

@@ -50,7 +50,7 @@ define([
       assistHelper.load(snippet, $.noop);
       window.setTimeout(function() {
         callback();
-      }, 10);
+      }, 0);
 
     };
 

+ 2 - 1
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -625,7 +625,8 @@
     };
 
     self.autocompleter = new Autocompleter({
-      snippet: self
+      snippet: self,
+      user: vm.user
     });
 
     self.init = function () {