null+****@clear*****
null+****@clear*****
2012年 8月 3日 (金) 14:32:05 JST
SHIMODA Hiroshi 2012-08-03 14:32:05 +0900 (Fri, 03 Aug 2012) New Revision: 28f94056f68f077e1539784867b9f9d9aae688e3 https://github.com/groonga/gcs/commit/28f94056f68f077e1539784867b9f9d9aae688e3 Log: Add method to retrieve all domains in the database Modified files: lib/database/domain.js test/batch-translator.test.js test/fixture/companies/data-deleted.grn test/fixture/companies/data.grn test/fixture/companies/ddl-custom-id.grn test/fixture/companies/ddl.grn test/fixture/companies/delete.grn test/fixture/companies/synonyms.grn Modified: lib/database/domain.js (+35 -11) =================================================================== --- lib/database/domain.js 2012-08-03 14:20:25 +0900 (7c7794f) +++ lib/database/domain.js 2012-08-03 14:32:05 +0900 (4706c08) @@ -11,6 +11,14 @@ var DEFAULT_ID = exports.DEFAULT_ID = Domain.DEFAULT_ID = '00000000000000000000000000'; +var DOMAIN_TABLE_PREFIX = + exports.DOMAIN_TABLE_PREFIX = + Domain.DOMAIN_TABLE_PREFIX = 'domain'; + +var REFERENCE_TABLE_PREFIX = + exports.REFERENCE_TABLE_PREFIX = + Domain.REFERENCE_TABLE_PREFIX = 'reference'; + function assertValidDomainName(domain) { if (typeof domain != 'string') throw new Error('domain name must be a string'); @@ -100,7 +108,7 @@ Domain.prototype = { if (this.context) { var tableName = this.toTableNamePart(this.name); var tables = this.context.tableListSync(); - var tableIdMatcher = new RegExp('^' + tableName + '_([^_]+)$'); + var tableIdMatcher = new RegExp('^' + DOMAIN_TABLE_PREFIX + '_' + tableName + '_([^_]+)$'); var id; if (tables.some(function(table) { var match = table.name.match(tableIdMatcher); @@ -128,7 +136,8 @@ Domain.prototype = { get tableName() { if (!this._tableName) { assertValidDomainName(this.name); - this._tableName = this.toTableNamePart(this.name) + '_' + this.id; + this._tableName = DOMAIN_TABLE_PREFIX + '_' + + this.toTableNamePart(this.name) + '_' + this.id; } return this._tableName; }, @@ -138,10 +147,18 @@ Domain.prototype = { get termsTableName() { if (!this._termsTableName) - this._termsTableName = this.tableName + '_BigramTerms'; + this._termsTableName = REFERENCE_TABLE_PREFIX + '_' + + this.toTableNamePart(this.name) + '_BigramTerms'; return this._termsTableName; }, + get synonymTableName() { + if (!this._synonymTableName) + this._synonymTableName = REFERENCE_TABLE_PREFIX + '_' + + this.toTableNamePart(this.name) + '_synonyms'; + return this._synonymTableName; + }, + getIndexField: function(field) { return this.cachedIndexFields[field] || (this.cachedIndexFields[field] = new IndexField(field, this)); @@ -161,12 +178,6 @@ Domain.prototype = { return fields; }, - get synonymTableName() { - if (!this._synonymTableName) - this._synonymTableName = this.tableName + '_synonyms'; - return this._synonymTableName; - }, - get id() { return this._id === undefined ? DEFAULT_ID : this._id ; }, @@ -278,8 +289,7 @@ Domain.prototype = { }, isSynonymTableAvailableSync: function() { - var results = this.context.commandSync('table_list'); - var tables = nroonga.formatResults(results); + var tables = this.context.tableListSync(); return tables.some(function(table) { return table.name === this.synonymTableName; }, this); @@ -310,3 +320,17 @@ Domain.getNameAndIdFromPath = function(path) { return { name: '', id: '' }; }; + +Domain.getAll = function(context) { + var tables = context.tableListSync(); + var tableMatcher = new RegExp('^' + DOMAIN_TABLE_PREFIX + '_([^_]+)_([^_]+)$'); + var domains = []; + tables.forEach(function(table) { + var match = table.name.match(tableMatcher); + if (match) { + var name = match[1]; // XXX this must be "unescape"d in the future. + domains.push(new Domain(name, context)); + } + }); + return domains; +}; Modified: test/batch-translator.test.js (+10 -10) =================================================================== --- test/batch-translator.test.js 2012-08-03 14:20:25 +0900 (e5f8550) +++ test/batch-translator.test.js 2012-08-03 14:32:05 +0900 (81dfda0) @@ -138,7 +138,7 @@ suite('batch/translator/Translator (class methods)', function() { var command = { command: 'load', options: { - table: 'test_00000000000000000000000000', + table: 'domain_test_00000000000000000000000000', values: JSON.stringify([{ '_key': batch['id'], 'name': batch['fields']['name'], @@ -147,7 +147,7 @@ suite('batch/translator/Translator (class methods)', function() { }]) } }; - var expected = 'load --table test_00000000000000000000000000 --values ' + command.options.values; + var expected = 'load --table domain_test_00000000000000000000000000 --values ' + command.options.values; var stringified = Translator.commandToString(command); assert.equal(stringified, expected); }); @@ -158,11 +158,11 @@ suite('batch/translator/Translator (class methods)', function() { var command = { command: 'delete', options: { - table: 'test_00000000000000000000000000', + table: 'domain_test_00000000000000000000000000', key: batch['id'] } }; - var expected = 'delete --table test_00000000000000000000000000 --key ' + command.options.key; + var expected = 'delete --table domain_test_00000000000000000000000000 --key ' + command.options.key; var stringified = Translator.commandToString(command); assert.equal(stringified, expected); }); @@ -175,7 +175,7 @@ suite('batch/translator/Translator (class methods)', function() { { command: 'load', options: { - table: 'test_00000000000000000000000000', + table: 'domain_test_00000000000000000000000000', values: JSON.stringify([{ '_key': batches[0]['id'], 'name': batches[0]['fields']['name'], @@ -187,7 +187,7 @@ suite('batch/translator/Translator (class methods)', function() { { command: 'load', options: { - table: 'test_00000000000000000000000000', + table: 'domain_test_00000000000000000000000000', values: JSON.stringify([{ '_key': batches[1]['id'], 'name': batches[1]['fields']['name'], @@ -199,15 +199,15 @@ suite('batch/translator/Translator (class methods)', function() { { command: 'delete', options: { - table: 'test_00000000000000000000000000', + table: 'domain_test_00000000000000000000000000', key: batches[2]['id'] } } ]; var expected = [ - 'load --table test_00000000000000000000000000 --values ' + commands[0].options.values, - 'load --table test_00000000000000000000000000 --values ' + commands[1].options.values, - 'delete --table test_00000000000000000000000000 --key ' + commands[2].options.key + 'load --table domain_test_00000000000000000000000000 --values ' + commands[0].options.values, + 'load --table domain_test_00000000000000000000000000 --values ' + commands[1].options.values, + 'delete --table domain_test_00000000000000000000000000 --key ' + commands[2].options.key ].join('\n'); var stringified = Translator.commandsToString(commands); assert.equal(stringified, expected); Modified: test/fixture/companies/data-deleted.grn (+1 -1) =================================================================== --- test/fixture/companies/data-deleted.grn 2012-08-03 14:20:25 +0900 (f322e7c) +++ test/fixture/companies/data-deleted.grn 2012-08-03 14:32:05 +0900 (bc87a0c) @@ -1,4 +1,4 @@ -load --table companies_00000000000000000000000000 +load --table domain_companies_00000000000000000000000000 [ ["_key","address","age","description","email_address","name","product"], ["id2","Sapporo, Hokkaido, Japan",2,"","info****@enish*****","Enishi Tech Inc.","groonga"], Modified: test/fixture/companies/data.grn (+1 -1) =================================================================== --- test/fixture/companies/data.grn 2012-08-03 14:20:25 +0900 (db92a18) +++ test/fixture/companies/data.grn 2012-08-03 14:32:05 +0900 (c990cb1) @@ -1,4 +1,4 @@ -load --table companies_00000000000000000000000000 +load --table domain_companies_00000000000000000000000000 [ ["_key","address","age","description","email_address","name","product"], ["id1","Shibuya, Tokyo, Japan",1,"","info****@razil*****","Brazil","groonga"], Modified: test/fixture/companies/ddl-custom-id.grn (+16 -16) =================================================================== --- test/fixture/companies/ddl-custom-id.grn 2012-08-03 14:20:25 +0900 (607c122) +++ test/fixture/companies/ddl-custom-id.grn 2012-08-03 14:32:05 +0900 (aaefe38) @@ -1,16 +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 +table_create reference_companies_id0123_product TABLE_HASH_KEY ShortText +table_create reference_companies_id0123_age TABLE_HASH_KEY UInt32 +table_create reference_companies_id0123_BigramTerms TABLE_PAT_KEY|KEY_NORMALIZE ShortText --default_tokenizer TokenBigram +table_create domain_companies_id0123 TABLE_HASH_KEY ShortText +column_create domain_companies_id0123 address COLUMN_SCALAR ShortText +column_create domain_companies_id0123 age COLUMN_SCALAR UInt32 +column_create domain_companies_id0123 description COLUMN_SCALAR ShortText +column_create domain_companies_id0123 email_address COLUMN_SCALAR ShortText +column_create domain_companies_id0123 name COLUMN_SCALAR ShortText +column_create domain_companies_id0123 product COLUMN_SCALAR reference_companies_id0123_product +column_create reference_companies_id0123_BigramTerms companies_name COLUMN_INDEX|WITH_POSITION domain_companies_id0123 name +column_create reference_companies_id0123_BigramTerms companies_email_address COLUMN_INDEX|WITH_POSITION domain_companies_id0123 email_address +column_create reference_companies_id0123_BigramTerms companies_description COLUMN_INDEX|WITH_POSITION domain_companies_id0123 description +column_create reference_companies_id0123_BigramTerms companies_address COLUMN_INDEX|WITH_POSITION domain_companies_id0123 address +column_create reference_companies_id0123_age companies_age COLUMN_INDEX|WITH_POSITION domain_companies_id0123 age +column_create reference_companies_id0123_product companies_product COLUMN_INDEX|WITH_POSITION domain_companies_id0123 product Modified: test/fixture/companies/ddl.grn (+16 -16) =================================================================== --- test/fixture/companies/ddl.grn 2012-08-03 14:20:25 +0900 (0e6e518) +++ test/fixture/companies/ddl.grn 2012-08-03 14:32:05 +0900 (ba3edb8) @@ -1,16 +1,16 @@ -table_create companies_00000000000000000000000000_product TABLE_HASH_KEY ShortText -table_create companies_00000000000000000000000000_age TABLE_HASH_KEY UInt32 -table_create companies_00000000000000000000000000_BigramTerms TABLE_PAT_KEY|KEY_NORMALIZE ShortText --default_tokenizer TokenBigram -table_create companies_00000000000000000000000000 TABLE_HASH_KEY ShortText -column_create companies_00000000000000000000000000 address COLUMN_SCALAR ShortText -column_create companies_00000000000000000000000000 age COLUMN_SCALAR UInt32 -column_create companies_00000000000000000000000000 description COLUMN_SCALAR ShortText -column_create companies_00000000000000000000000000 email_address COLUMN_SCALAR ShortText -column_create companies_00000000000000000000000000 name COLUMN_SCALAR ShortText -column_create companies_00000000000000000000000000 product COLUMN_SCALAR companies_00000000000000000000000000_product -column_create companies_00000000000000000000000000_BigramTerms companies_name COLUMN_INDEX|WITH_POSITION companies_00000000000000000000000000 name -column_create companies_00000000000000000000000000_BigramTerms companies_email_address COLUMN_INDEX|WITH_POSITION companies_00000000000000000000000000 email_address -column_create companies_00000000000000000000000000_BigramTerms companies_description COLUMN_INDEX|WITH_POSITION companies_00000000000000000000000000 description -column_create companies_00000000000000000000000000_BigramTerms companies_address COLUMN_INDEX|WITH_POSITION companies_00000000000000000000000000 address -column_create companies_00000000000000000000000000_age companies_age COLUMN_INDEX|WITH_POSITION companies_00000000000000000000000000 age -column_create companies_00000000000000000000000000_product companies_product COLUMN_INDEX|WITH_POSITION companies_00000000000000000000000000 product +table_create reference_companies_00000000000000000000000000_product TABLE_HASH_KEY ShortText +table_create reference_companies_00000000000000000000000000_age TABLE_HASH_KEY UInt32 +table_create reference_companies_00000000000000000000000000_BigramTerms TABLE_PAT_KEY|KEY_NORMALIZE ShortText --default_tokenizer TokenBigram +table_create domain_companies_00000000000000000000000000 TABLE_HASH_KEY ShortText +column_create domain_companies_00000000000000000000000000 address COLUMN_SCALAR ShortText +column_create domain_companies_00000000000000000000000000 age COLUMN_SCALAR UInt32 +column_create domain_companies_00000000000000000000000000 description COLUMN_SCALAR ShortText +column_create domain_companies_00000000000000000000000000 email_address COLUMN_SCALAR ShortText +column_create domain_companies_00000000000000000000000000 name COLUMN_SCALAR ShortText +column_create domain_companies_00000000000000000000000000 product COLUMN_SCALAR reference_companies_00000000000000000000000000_product +column_create reference_companies_00000000000000000000000000_BigramTerms companies_name COLUMN_INDEX|WITH_POSITION domain_companies_00000000000000000000000000 name +column_create reference_companies_00000000000000000000000000_BigramTerms companies_email_address COLUMN_INDEX|WITH_POSITION domain_companies_00000000000000000000000000 email_address +column_create reference_companies_00000000000000000000000000_BigramTerms companies_description COLUMN_INDEX|WITH_POSITION domain_companies_00000000000000000000000000 description +column_create reference_companies_00000000000000000000000000_BigramTerms companies_address COLUMN_INDEX|WITH_POSITION domain_companies_00000000000000000000000000 address +column_create reference_companies_00000000000000000000000000_age companies_age COLUMN_INDEX|WITH_POSITION domain_companies_00000000000000000000000000 age +column_create reference_companies_00000000000000000000000000_product companies_product COLUMN_INDEX|WITH_POSITION domain_companies_00000000000000000000000000 product Modified: test/fixture/companies/delete.grn (+1 -1) =================================================================== --- test/fixture/companies/delete.grn 2012-08-03 14:20:25 +0900 (231c8d8) +++ test/fixture/companies/delete.grn 2012-08-03 14:32:05 +0900 (42494b4) @@ -1 +1 @@ -delete --table companies_00000000000000000000000000 --key id1 +delete --table domain_companies_00000000000000000000000000 --key id1 Modified: test/fixture/companies/synonyms.grn (+3 -3) =================================================================== --- test/fixture/companies/synonyms.grn 2012-08-03 14:20:25 +0900 (64220d0) +++ test/fixture/companies/synonyms.grn 2012-08-03 14:32:05 +0900 (1aaa468) @@ -1,6 +1,6 @@ -table_create companies_00000000000000000000000000_synonyms TABLE_HASH_KEY|KEY_NORMALIZE ShortText -column_create companies_00000000000000000000000000_synonyms synonyms COLUMN_VECTOR ShortText -load --table companies_00000000000000000000000000_synonyms +table_create reference_companies_00000000000000000000000000_synonyms TABLE_HASH_KEY|KEY_NORMALIZE ShortText +column_create reference_companies_00000000000000000000000000_synonyms synonyms COLUMN_VECTOR ShortText +load --table reference_companies_00000000000000000000000000_synonyms [ ["_key","synonyms"], ["tokio",["tokyo"]], -------------- next part -------------- HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B... Download