[Groonga-commit] groonga/gcs [master] Detect domain id given as a part of host name or path

Back to archive index

null+****@clear***** null+****@clear*****
2012年 8月 1日 (水) 15:40:28 JST


piro	2012-08-01 15:40:28 +0900 (Wed, 01 Aug 2012)

  New Revision: 00b0600b7e70b5edad07011e9215f547a8f416fe
  https://github.com/groonga/gcs/commit/00b0600b7e70b5edad07011e9215f547a8f416fe

  Log:
    Detect domain id given as a part of host name or path

  Modified files:
    lib/database/domain.js
    test/database-domain.test.js

  Modified: lib/database/domain.js (+44 -21)
===================================================================
--- lib/database/domain.js    2012-08-01 15:08:41 +0900 (5d1b438)
+++ lib/database/domain.js    2012-08-01 15:40:28 +0900 (bbba79d)
@@ -56,47 +56,69 @@ function Domain() {
 }
 Domain.prototype = {
   initialize: function(source, context) {
-    this.context = context && new nroonga.Context(context);
-    this.name = this.getName(source);
     this.cachedIndexFields = {};
+
+    this.context = context && new nroonga.Context(context);
+    this.initializeNameAndId(source);
+
     // for validation
     this.tableName;
     this.termsTableName;
   },
-  getName: function(source) {
-    if (typeof source == 'string')
-      return source;
+  initializeNameAndId: function(source) {
+    if (typeof source == 'string') {
+      this.name = source;
+      return;
+    }
 
-    if (source.query && source.query.DomainName)
-      return source.query.DomainName;
+    if (source.query && source.query.DomainName) {
+      this.name = source.query.DomainName;
+      this.id = this.getIdFromTable(this.toTableNamePart(this.name));
+      return;
+    }
 
     if (source.headers && source.headers.host) {
       var host = source.headers.host;
-      var domainNameFromHost = Domain.getNameFromHost(host);
-      if (domainNameFromHost)
-        return domainNameFromHost;
+      var nameAndIdFromHost = Domain.getNameAndIdFromHost(host);
+      if (nameAndIdFromHost.name) {
+        this.name = nameAndIdFromHost.name;
+        this.id = nameAndIdFromHost.id;
+        return;
+      }
     }
 
     if (source.url) {
-      var domainNameFromPath = Domain.getNameFromPath(source.url);
-      if (domainNameFromPath)
-        return domainNameFromPath;
+      var nameAndIdFromPath = Domain.getNameAndIdFromPath(source.url);
+      if (nameAndIdFromPath.name) {
+        this.name = nameAndIdFromPath.name;
+        this.id = nameAndIdFromPath.id;
+        return;
+      }
     }
 
     throw new Error('no domain name');
   },
+  getIdFromTable: function(tableName) {
+    return this.id;
+  },
+
   get tableName() {
     if (!this._tableName) {
       assertValidDomainName(this.name);
-      this._tableName = this.name + '_' + this.id;
+      this._tableName = this.toTableNamePart(this.name) + '_' + this.id;
     }
     return this._tableName;
   },
+  toTableNamePart: function(string) {
+    return string;
+  },
+
   get termsTableName() {
     if (!this._termsTableName)
       this._termsTableName = this.tableName + '_BigramTerms';
     return this._termsTableName;
   },
+
   getIndexField: function(field) {
     return this.cachedIndexFields[field] ||
            (this.cachedIndexFields[field] = new IndexField(field, this));
@@ -115,6 +137,7 @@ Domain.prototype = {
                  }, this);
     return fields;
   },
+
   get synonymTableName() {
     if (!this._synonymTableName)
       this._synonymTableName = this.tableName + '_synonyms';
@@ -230,21 +253,21 @@ Domain.prototype = {
 
 exports.Domain = Domain;
 
-Domain.getNameFromHost = function(host) {
+Domain.getNameAndIdFromHost = function(host) {
   var domainMatcher = /^(?:doc|search)-([^\.]+)-([^\.\-]+)\./;
   var match = host.match(domainMatcher);
   if (match)
-    return match[1];
+    return{ name: match[1], id: match[2] };
 
-  return '';
+  return { name: '', id: '' };
 };
 
-Domain.getNameFromPath = function(path) {
-  var domainMatcher = /^\/gcs\/([^\/]+)/;
+Domain.getNameAndIdFromPath = function(path) {
+  var domainMatcher = /^\/gcs\/([^\/]+)-([^\/\-]+)/;
 
   var match = path.match(domainMatcher);
   if (match)
-    return match[1];
+    return{ name: match[1], id: match[2] };
 
-  return '';
+  return { name: '', id: '' };
 };

  Modified: test/database-domain.test.js (+32 -29)
===================================================================
--- test/database-domain.test.js    2012-08-01 15:08:41 +0900 (fce8c2e)
+++ test/database-domain.test.js    2012-08-01 15:40:28 +0900 (2613b6c)
@@ -82,61 +82,61 @@ suite('database', function() {
       });
     });
 
-    suite('getNameFromHost', function() {
+    suite('getNameAndIdFromHost', function() {
       test('valid, doc, lower case and number', function() {
         var host = 'doc-test0123-id0123.example.com';
-        var name = Domain.getNameFromHost(host);
-        assert.equal(name, 'test0123');
+        var nameAndId = Domain.getNameAndIdFromHost(host);
+        assert.deepEqual(nameAndId, { name: 'test0123', id: 'id0123' });
       });
 
       test('valid, search, lower case and number', function() {
         var host = 'search-test0123-id0123.example.com';
-        var name = Domain.getNameFromHost(host);
-        assert.equal(name, 'test0123');
+        var nameAndId = Domain.getNameAndIdFromHost(host);
+        assert.deepEqual(nameAndId, { name: 'test0123', id: 'id0123' });
       });
 
       test('valid, doc, lower case, hyphen and number', function() {
         var host = 'doc-test-0123-id0123.example.com';
-        var name = Domain.getNameFromHost(host);
-        assert.equal(name, 'test-0123');
+        var nameAndId = Domain.getNameAndIdFromHost(host);
+        assert.deepEqual(nameAndId, { name: 'test-0123', id: 'id0123' });
       });
 
       test('valid, search, lower case, hyphen and number', function() {
         var host = 'search-test-0123-id0123.example.com';
-        var name = Domain.getNameFromHost(host);
-        assert.equal(name, 'test-0123');
+        var nameAndId = Domain.getNameAndIdFromHost(host);
+        assert.deepEqual(nameAndId, { name: 'test-0123', id: 'id0123' });
       });
 
       test('valid, search, lower case, hyphen and number, deep subdomain including region identifier', function() {
         var host = 'search-test-0123-id0123.us-east-1.example.com';
-        var name = Domain.getNameFromHost(host);
-        assert.equal(name, 'test-0123');
+        var nameAndId = Domain.getNameAndIdFromHost(host);
+        assert.deepEqual(nameAndId, { name: 'test-0123', id: 'id0123' });
       });
 
       test('invalid', function() {
         var host = 'cloudsearch.example.com';
-        var name = Domain.getNameFromHost(host);
-        assert.equal(name, '');
+        var nameAndId = Domain.getNameAndIdFromHost(host);
+        assert.deepEqual(nameAndId, { name: '', id: '' });
       });
     });
 
-    suite('getNameFromPath', function() {
+    suite('getNameAndIdFromPath', function() {
       test('valid, lower case and number', function() {
-        var path = '/gcs/test0123/';
-        var name = Domain.getNameFromPath(path);
-        assert.equal(name, 'test0123');
+        var path = '/gcs/test0123-id0123/';
+        var nameAndId = Domain.getNameAndIdFromPath(path);
+        assert.deepEqual(nameAndId, { name: 'test0123', id: 'id0123' });
       });
 
       test('valid, lower case, hyphen and number', function() {
-        var path = '/gcs/test-0123/';
-        var name = Domain.getNameFromPath(path);
-        assert.equal(name, 'test-0123');
+        var path = '/gcs/test-0123-id0123/';
+        var nameAndId = Domain.getNameAndIdFromPath(path);
+        assert.deepEqual(nameAndId, { name: 'test-0123', id: 'id0123' });
       });
 
       test('invalid', function() {
         var path = '/gcs';
-        var name = Domain.getNameFromPath(path);
-        assert.equal(name, '');
+        var nameAndId = Domain.getNameAndIdFromPath(path);
+        assert.deepEqual(nameAndId, { name: '', id: '' });
       });
     });
 
@@ -145,7 +145,8 @@ suite('database', function() {
         var host = 'doc-test0123-id0123.example.com';
         var request = { headers: { host: host } };
         var domain = new Domain(request);
-        assert.equal(domain.name, 'test0123');
+        assert.deepEqual({ name: domain.name, id: domain.id },
+                         { name: 'test0123', id: 'id0123' });
       });
 
       test('from host, invalid', function() {
@@ -159,16 +160,17 @@ suite('database', function() {
       test('from path, valid', function() {
         var host = 'example.com';
         var request = { headers: { host: host },
-                        url: '/gcs/test0123' };
+                        url: '/gcs/test0123-id0123' };
         var domain = new Domain(request);
-        assert.equal(domain.name, 'test0123');
+        assert.deepEqual({ name: domain.name, id: domain.id },
+                         { name: 'test0123', id: 'id0123' });
       });
 
       test('from path, invalid', function() {
         assert.throw(function() {
           var host = 'example.com';
         var request = { headers: { host: host },
-                        url: '/gcs/test_01234' };
+                        url: '/gcs/test_0123-id0123' };
           var domain = new Domain(request);
         }, /cannot appear in a domain name/);
       });
@@ -176,15 +178,16 @@ suite('database', function() {
       test('host vs path', function() {
         var host = 'doc-test0123-id0123.example.com';
         var request = { headers: { host: host },
-                        url: '/gcs/test4567' };
+                        url: '/gcs/test4567-id4567' };
         var domain = new Domain(request);
-        assert.equal(domain.name, 'test0123');
+        assert.deepEqual({ name: domain.name, id: domain.id },
+                         { name: 'test0123', id: 'id0123' });
       });
 
       test('option vs host vs path', function() {
         var host = 'doc-test0123-id0123.example.com';
         var request = { headers: { host: host },
-                        url: '/gcs/test4567',
+                        url: '/gcs/test4567-id4567',
                         query: { DomainName: 'test890' } };
         var domain = new Domain(request);
         assert.equal(domain.name, 'test890');
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
Download 



Groonga-commit メーリングリストの案内
Back to archive index