Navigation Menu

Skip to content

Commit

Permalink
Add method to retrieve all domains in the database
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Aug 3, 2012
1 parent 5bfd526 commit 28f9405
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 59 deletions.
46 changes: 35 additions & 11 deletions lib/database/domain.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
},
Expand All @@ -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));
Expand All @@ -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 ;
},
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
};
20 changes: 10 additions & 10 deletions test/batch-translator.test.js
Expand Up @@ -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'],
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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'],
Expand All @@ -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'],
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/companies/data-deleted.grn
@@ -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@enishi-tech.com","Enishi Tech Inc.","groonga"],
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/companies/data.grn
@@ -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.jp","Brazil","groonga"],
Expand Down
32 changes: 16 additions & 16 deletions test/fixture/companies/ddl-custom-id.grn
@@ -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
32 changes: 16 additions & 16 deletions test/fixture/companies/ddl.grn
@@ -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
2 changes: 1 addition & 1 deletion test/fixture/companies/delete.grn
@@ -1 +1 @@
delete --table companies_00000000000000000000000000 --key id1
delete --table domain_companies_00000000000000000000000000 --key id1
6 changes: 3 additions & 3 deletions test/fixture/companies/synonyms.grn
@@ -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"]],
Expand Down

0 comments on commit 28f9405

Please sign in to comment.