[Groonga-commit] groonga/gcs [master] Commonize codes to setup documents API and search API

Back to archive index

YUKI Hiroshi null+****@clear*****
Mon Dec 17 17:24:06 JST 2012


YUKI Hiroshi	2012-12-17 17:24:06 +0900 (Mon, 17 Dec 2012)

  New Revision: 50e986aac7f393c1d5ad0206f5e006954d19c38f
  https://github.com/groonga/gcs/commit/50e986aac7f393c1d5ad0206f5e006954d19c38f

  Log:
    Commonize codes to setup documents API and search API

  Modified files:
    bin/gcs-post-sdf
    lib/client.js

  Modified: bin/gcs-post-sdf (+1 -1)
===================================================================
--- bin/gcs-post-sdf    2012-12-13 16:57:01 +0900 (75af52b)
+++ bin/gcs-post-sdf    2012-12-17 17:24:06 +0900 (e9fa4eb)
@@ -50,7 +50,7 @@ client.assertDomainExists(function() {
     else
       batches = JSON.parse(batches);
 
-    client.setupDocumentsAPI(function(documentsAPI) {
+    client.setupAPI('doc', function(documentsAPI) {
       documentsAPI.DocumentsBatch(
         {
           Docs: batches

  Modified: lib/client.js (+33 -16)
===================================================================
--- lib/client.js    2012-12-13 16:57:01 +0900 (8aa2fff)
+++ lib/client.js    2012-12-17 17:24:06 +0900 (4f7f9a6)
@@ -14,6 +14,7 @@ function Client(options) {
   this.host = options.host;
   this.port = options.port;
   this.docEndpoint = options.docEndpoint;
+  this.searchEndpoint = options.searchEndpoint;
   this.isACS = options.isACS;
 
   this.accessKeyId     = options.accessKeyId;
@@ -40,20 +41,38 @@ Client.prototype = {
     return this._configurationAPI;
   },
 
-  setupDocumentsAPI: function(callback) {
-    if (this.documentsAPI)
-      return callback(this.documentsAPI);
+  get APIDefinitions() {
+    return {
+      'doc': {
+        endPoint:    this.docEndPoint,
+        elementName: 'DocService',
+        klass:       DocumentService
+      },
+      'search': {
+        endPoint:    this.searchEndPoint,
+        elementName: 'SearchService',
+        klass:       SearchService
+      }
+    };
+  },
+  setupAPI: function(type, callback) {
+    this.cachedAPI = this.cachedAPI || {};
+    if (type in this.cachedAPI)
+      return this.cacnedAPI[type];
 
     var self = this;
-    if (this.docEndpoint) {
-      var parsedEndpoint = this.docEndpoint.match(/^(doc-([^-]+)-([^\.]+)[^:]+)(?::(\d+))?$/);
-      this._documentsAPI = this.createDocumentService({
+    var definition = this.APIDefinitions[type];
+    if (definition.endPoint) {
+      var parsedEndpoint = definition.endPoint.match(/^([^-]+-([^-]+)-([^\.]+)[^:]+)(?::(\d+))?$/);
+      var api = this.createService({
         domainName: parsedEndpoint[2],
         domainId: parsedEndpoint[3],
         host: parsedEndpoint[1],
-        port: parsedEndpoint[4] || 80
+        port: parsedEndpoint[4] || 80,
+        klass: definition.klass
       });
-      return callback(this.documentsAPI);
+      this.cacnedAPI[type] = api;
+      return callback(api);
     }
 
     this.getDomainStatus(function(error, domain) {
@@ -61,21 +80,22 @@ Client.prototype = {
         self.raiseFatalError(error);
 
       try {
-        var endpoint = self.arnToEndpoint(domain.DocService.Arn).split(':');
-        self._documentsAPI = self.createDocumentService({
+        var endpoint = self.arnToEndpoint(domain[definition.elementName].Arn).split(':');
+        var api = self.createService({
           domainName: domain.DomainName,
           domainId: domain.DomainId.replace(new RegExp('/' + domain.DomainName + '$'), ''),
           host: endpoint[0],
           port: endpoint[1] || 80
         });
-        callback(self.documentsAPI);
+        self.cacnedAPI[type] = api;
+        callback(api);
       } catch(error) {
         self.raiseFatalError(error);
       }
     });
   },
-  createDocumentService: function(options) {
-    var service = new DocumentService({
+  createService: function(options) {
+    var service = new options.klass({
       accessKeyId: this.accessKeyId,
       secretAccessKey: this.secretAccessKey,
       domainName: options.domainName,
@@ -110,9 +130,6 @@ Client.prototype = {
     var notEscapedParts = string.replace(escapedPartsMatcher, '');
     return notEscapedParts.length + (escapedParts ? escapedParts.length : 0);
   },
-  get documentsAPI() {
-    return this._documentsAPI;
-  },
 
   raiseFatalError: function(error) {
     if (typeof error != 'string') {
-------------- next part --------------
HTML����������������������������...
Télécharger 



More information about the Groonga-commit mailing list
Back to archive index