[Groonga-commit] groonga/gcs [master] Detect domain id from the related table automatically

Back to archive index

null+****@clear***** null+****@clear*****
2012年 8月 1日 (水) 16:27:11 JST


YUKI "Piro" Hiroshi	2012-08-01 16:27:11 +0900 (Wed, 01 Aug 2012)

  New Revision: 03bf24a439ba7520b7424618ec04bcbc329d6ace
  https://github.com/groonga/gcs/commit/03bf24a439ba7520b7424618ec04bcbc329d6ace

  Log:
    Detect domain id from the related table automatically

  Added files:
    test/fixture/companies/ddl-custom-id.grn
  Modified files:
    lib/database/domain.js
    lib/wrapped-nroonga.js
    test/database-domain.test.js

  Modified: lib/database/domain.js (+15 -0)
===================================================================
--- lib/database/domain.js    2012-08-01 15:59:58 +0900 (8ce373f)
+++ lib/database/domain.js    2012-08-01 16:27:11 +0900 (ea8f50b)
@@ -68,6 +68,7 @@ Domain.prototype = {
   initializeNameAndId: function(source) {
     if (typeof source == 'string') {
       this.name = source;
+      this.id = this.getIdFromTable(this.toTableNamePart(this.name));
       return;
     }
 
@@ -99,6 +100,20 @@ Domain.prototype = {
     throw new Error('no domain name');
   },
   getIdFromTable: function(tableName) {
+    if (this.context) {
+      var tables = this.context.tableListSync();
+      var tableIdMatcher = new RegExp('^' + tableName + '_([^_]+)$');
+      var id;
+      if (tables.some(function(table) {
+            var match = table.name.match(tableIdMatcher);
+            if (match) {
+              id = match[1];
+              return true;
+            }
+            return false;
+          }, this))
+        return id;
+    }
     return this.id;
   },
 

  Modified: lib/wrapped-nroonga.js (+5 -0)
===================================================================
--- lib/wrapped-nroonga.js    2012-08-01 15:59:58 +0900 (38d3af2)
+++ lib/wrapped-nroonga.js    2012-08-01 16:27:11 +0900 (cd3ea4a)
@@ -80,6 +80,11 @@ Context.prototype = {
     return deferred;
   },
 
+  tableListSync: function(tableName) {
+    var result =  this.commandSync('table_list');
+    return formatResults(result);
+  },
+
   columnListSync: function(tableName) {
     var result =  this.commandSync('column_list', { table: tableName });
     return formatResults(result);

  Modified: test/database-domain.test.js (+10 -1)
===================================================================
--- test/database-domain.test.js    2012-08-01 15:59:58 +0900 (2613b6c)
+++ test/database-domain.test.js    2012-08-01 16:27:11 +0900 (2b15fee)
@@ -202,7 +202,7 @@ suite('database', function() {
       setup(function() {
         temporaryDatabase = utils.createTemporaryDatabase();
         context = temporaryDatabase.get();
-        utils.loadDumpFile(context, __dirname + '/fixture/companies/ddl.grn');
+        utils.loadDumpFile(context, __dirname + '/fixture/companies/ddl-custom-id.grn');
         domain = new Domain('companies', context);
       });
 
@@ -212,6 +212,15 @@ suite('database', function() {
         temporaryDatabase = undefined;
       });
 
+      test('id from database (known table)', function() {
+        assert.equal(domain.id, 'id0123');
+      });
+
+      test('id from database (unknown table)', function() {
+        domain = new Domain('unknown', context);
+        assert.equal(domain.id, Domain.DEFAULT_DOMAIN_ID);
+      });
+
       test('indexFields', function() {
         var fields = domain.indexFields;
         fields = fields.map(function(field) {

  Added: test/fixture/companies/ddl-custom-id.grn (+16 -0) 100644
===================================================================
--- /dev/null
+++ test/fixture/companies/ddl-custom-id.grn    2012-08-01 16:27:11 +0900 (607c122)
@@ -0,0 +1,16 @@
+table_create companies_id0123_product TABLE_HASH_KEY ShortText
+table_create companies_id0123_age TABLE_HASH_KEY UInt32
+table_create companies_id0123_BigramTerms TABLE_PAT_KEY|KEY_NORMALIZE ShortText --default_tokenizer TokenBigram
+table_create companies_id0123 TABLE_HASH_KEY ShortText
+column_create companies_id0123 address COLUMN_SCALAR ShortText
+column_create companies_id0123 age COLUMN_SCALAR UInt32
+column_create companies_id0123 description COLUMN_SCALAR ShortText
+column_create companies_id0123 email_address COLUMN_SCALAR ShortText
+column_create companies_id0123 name COLUMN_SCALAR ShortText
+column_create companies_id0123 product COLUMN_SCALAR companies_id0123_product
+column_create companies_id0123_BigramTerms companies_name COLUMN_INDEX|WITH_POSITION companies_id0123 name
+column_create companies_id0123_BigramTerms companies_email_address COLUMN_INDEX|WITH_POSITION companies_id0123 email_address
+column_create companies_id0123_BigramTerms companies_description COLUMN_INDEX|WITH_POSITION companies_id0123 description
+column_create companies_id0123_BigramTerms companies_address COLUMN_INDEX|WITH_POSITION companies_id0123 address
+column_create companies_id0123_age companies_age COLUMN_INDEX|WITH_POSITION companies_id0123 age
+column_create companies_id0123_product companies_product COLUMN_INDEX|WITH_POSITION companies_id0123 product
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
Download 



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