Navigation Menu

Skip to content

Commit

Permalink
Change naming rule of generated tabels
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Aug 3, 2012
1 parent 1f12540 commit 1544212
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 105 deletions.
35 changes: 17 additions & 18 deletions lib/database/domain.js
@@ -1,3 +1,10 @@
/**
* Naming rule of generated tables:
* main table: <domain name>_<domain id>
* index tables: <domain name>_<domain id>_index_<column name>
* meta tables: <domain name>_<domain id>_synonyms
*/

var nativeNroonga = require('nroonga');
var nroonga = require('../wrapped-nroonga');
var IndexField = require('./index-field').IndexField;
Expand All @@ -11,13 +18,9 @@ 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';
var INDEX_SUFFIX =
exports.INDEX_SUFFIX =
Domain.INDEX_SUFFIX = 'index';

function assertValidDomainName(domain) {
if (typeof domain != 'string')
Expand Down Expand Up @@ -108,7 +111,7 @@ Domain.prototype = {
if (this.context) {
var tableName = Domain.toTableNamePart(this.name);
var tables = this.context.tableListSync();
var tableIdMatcher = new RegExp('^' + DOMAIN_TABLE_PREFIX + '_' + tableName + '_([^_]+)$');
var tableIdMatcher = new RegExp('^' + tableName + '_([^_]+)$');
var id;
if (tables.some(function(table) {
var match = table.name.match(tableIdMatcher);
Expand Down Expand Up @@ -136,25 +139,21 @@ Domain.prototype = {
get tableName() {
if (!this._tableName) {
assertValidDomainName(this.name);
this._tableName = DOMAIN_TABLE_PREFIX + '_' +
Domain.toTableNamePart(this.name) + '_' + this.id;
this._tableName = Domain.toTableNamePart(this.name) + '_' + this.id;
}
return this._tableName;
},

get referenceTableBaseName() {
if (!this._referenceTableBaseName)
this._referenceTableBaseName = REFERENCE_TABLE_PREFIX + '_' +
Domain.toTableNamePart(this.name) + '_' + this.id;
return this._referenceTableBaseName;
get indexTableBaseName() {
return this.tableName + '_' + INDEX_SUFFIX + '_';
},

get termsTableName() {
return this.referenceTableBaseName + '_BigramTerms';
return this.indexTableBaseName + '_BigramTerms';
},

get synonymTableName() {
return this.referenceTableBaseName + '_synonyms';
return this.tableName + '_synonyms';
},

getIndexField: function(field) {
Expand Down Expand Up @@ -330,7 +329,7 @@ Domain.getAll = function(context) {
context = context && new nroonga.Context(context);

var tables = context.tableListSync();
var tableMatcher = new RegExp('^' + DOMAIN_TABLE_PREFIX + '_([^_]+)_([^_]+)$');
var tableMatcher = new RegExp('^([^_]+)_([^_]+)$');
var domains = [];
tables.forEach(function(table) {
var match = table.name.match(tableMatcher);
Expand Down
51 changes: 27 additions & 24 deletions lib/database/index-field.js
Expand Up @@ -56,21 +56,17 @@ IndexField.prototype = {
get columnName() {
if (!this._columnName) {
assertValidFieldName(this.name);
this._columnName = this.name;
this._columnName = IndexField.toColumnNamePart(this.name);
}
return this._columnName;
},
get indexColumnName() {
if (!this._indexColumnName)
this._indexColumnName = this.domain.referenceTableBaseName + '_' + this.columnName;
return this._indexColumnName;
this.domain.indexTableBaseName + '_' + this.columnName;
},
get referenceTableName() {
if (!this._referenceTableName)
this._referenceTableName = this.domain.referenceTableBaseName + '_' + this.columnName;
return this._referenceTableName;
get indexTableName() {
this.domain.indexTableBaseName + '_' + this.columnName;
},
get referenceTableKeyType() {
get indexTableKeyType() {
var type = this.type;
switch (type) {
case 'uint':
Expand All @@ -79,7 +75,7 @@ IndexField.prototype = {
return nroonga.ShortText;
default:
throw new Error('Unsupported index field type ' + type +
' for reference table');
' for index table');
}
},
fieldTypeToColumnType: function(fieldType) {
Expand All @@ -89,7 +85,7 @@ IndexField.prototype = {
case 'uint':
return nroonga.UInt32;
case 'literal':
return this.referenceTableName;
return this.indexTableName;
default:
throw new Error('Unsupported index field type ' + fieldType);
}
Expand All @@ -100,7 +96,7 @@ IndexField.prototype = {
return 'text';
case nroonga.UInt32:
return 'uint';
case this.referenceTableName:
case this.indexTableName:
return 'literal';
default:
throw new Error('Unsupported column type ' + columnType);
Expand Down Expand Up @@ -130,7 +126,7 @@ IndexField.prototype = {
} else if (column.type == 'fix') {
if (column.range == nroonga.UInt32)
return this._type = 'uint';
else if (column.range == this.referenceTableName)
else if (column.range == this.indexTableName)
return this._type = 'literal';
}

Expand Down Expand Up @@ -161,18 +157,18 @@ IndexField.prototype = {
},

createSync: function() {
var referenceTableName = this.domain.termsTableName;
var indexTableName = this.domain.termsTableName;

var type = this.type;
var columnType = this.fieldTypeToColumnType(type);

if (type == 'uint' || type == 'literal') {
this.context.commandSync('table_create', {
name: this.referenceTableName,
name: this.indexTableName,
flags: nroonga.TABLE_HASH_KEY,
key_type: this.referenceTableKeyType
key_type: this.indexTableKeyType
});
referenceTableName = this.referenceTableName;
indexTableName = this.indexTableName;
}

this.context.commandSync('column_create', {
Expand All @@ -182,7 +178,7 @@ IndexField.prototype = {
type: columnType
});
this.context.commandSync('column_create', {
table: referenceTableName,
table: indexTableName,
name: this.indexColumnName,
flags: nroonga.INDEX_COLUMN_DEFAULT_FLAGS,
type: this.domain.tableName,
Expand All @@ -193,7 +189,7 @@ IndexField.prototype = {
var type = this.type;
if (type == 'uint' || type == 'literal') {
this.context.commandSync('table_remove', {
name: this.referenceTableName
name: this.indexTableName
});
}
this.context.commandSync('column_remove', {
Expand All @@ -206,19 +202,19 @@ IndexField.prototype = {
var type = this.type;
if (type == 'uint' || type == 'literal') {
this.context.commandSync('column_remove', {
table: this.referenceTableName,
table: this.indexTableName,
name: this.indexColumnName
});
this.context.commandSync('table_remove', {
name: this.referenceTableName
name: this.indexTableName
});
this.context.commandSync('table_create', {
name: this.referenceTableName,
name: this.indexTableName,
flags: nroonga.TABLE_HASH_KEY,
key_type: this.referenceTableKeyType
key_type: this.indexTableKeyType
});
this.context.commandSync('column_create', {
table: this.referenceTableName,
table: this.indexTableName,
name: this.indexColumnName,
flags: nroonga.INDEX_COLUMN_DEFAULT_FLAGS,
type: this.domain.tableName,
Expand All @@ -245,3 +241,10 @@ IndexField.prototype = {
};

exports.IndexField = IndexField;

IndexField.toColumnNamePart = function(fieldName) {
return fieldName;
};
IndexField.toFieldName = function(columnNamePart) {
return columnNamePart;
};
6 changes: 2 additions & 4 deletions test/api-batch.test.js
Expand Up @@ -49,8 +49,7 @@ suite('documents/batch API', function() {
assert.deepEqual(response, expected);

var dump = context.commandSync('dump', {
tables: Domain.DOMAIN_TABLE_PREFIX +
'_companies_00000000000000000000000000'
tables: 'companies_00000000000000000000000000'
});
assert.equal(dump, schemeDump + '\n' + loadDump);

Expand Down Expand Up @@ -86,8 +85,7 @@ suite('documents/batch API', function() {
assert.deepEqual(response, expected);

var dump = context.commandSync('dump', {
tables: Domain.DOMAIN_TABLE_PREFIX +
'_companies_00000000000000000000000000'
tables: 'companies_00000000000000000000000000'
});
assert.equal(dump, schemeDump + '\n' + deletedLoadDump);

Expand Down
6 changes: 2 additions & 4 deletions test/batch-processor.test.js
Expand Up @@ -53,8 +53,7 @@ suite('batch/processor/Processor (instance methods)', function() {
};
assert.deepEqual(result, expected);
var dump = context.commandSync('dump', {
tables: Domain.DOMAIN_TABLE_PREFIX +
'_companies_00000000000000000000000000'
tables: 'companies_00000000000000000000000000'
});
assert.equal(dump, schemeDump + '\n' + loadDump);
done();
Expand All @@ -81,8 +80,7 @@ suite('batch/processor/Processor (instance methods)', function() {
};
assert.deepEqual(result, expected);
var dump = context.commandSync('dump', {
tables: Domain.DOMAIN_TABLE_PREFIX +
'_companies_00000000000000000000000000'
tables: 'companies_00000000000000000000000000'
});
assert.equal(dump, schemeDump + '\n' + deletedLoadDump);
done();
Expand Down
6 changes: 2 additions & 4 deletions test/database-domain.test.js
Expand Up @@ -25,16 +25,14 @@ suite('database', function() {
var domain = new Domain('valid');
domain.id = Domain.DEFAULT_ID;
assert.equal(domain.tableName,
Domain.DOMAIN_TABLE_PREFIX + '_valid_' +
Domain.DEFAULT_ID);
'valid_' + Domain.DEFAULT_ID);
});

test('lower case and number', function() {
var domain = new Domain('valid123');
domain.id = Domain.DEFAULT_ID;
assert.equal(domain.tableName,
Domain.DOMAIN_TABLE_PREFIX + '_valid123_' +
Domain.DEFAULT_ID);
'valid123_' + Domain.DEFAULT_ID);
});

test('too short', function() {
Expand Down
26 changes: 13 additions & 13 deletions test/database-index-field.test.js
Expand Up @@ -64,15 +64,15 @@ suite('database', function() {
test('indexColumnName', function() {
var field = new IndexField('valid_123', domain);
assert.equal(field.indexColumnName,
Domain.REFERENCE_TABLE_PREFIX + '_testdomain_' +
Domain.DEFAULT_ID + '_valid_123');
'testdomain_' + Domain.DEFAULT_ID + '_' +
Domain.INDEX_SUFFIX + '_valid_123');
});

test('referenceTableName', function() {
test('indexTableName', function() {
var field = new IndexField('valid_123', domain);
assert.equal(field.referenceTableName,
Domain.REFERENCE_TABLE_PREFIX + '_testdomain_' +
Domain.DEFAULT_ID + '_valid_123');
assert.equal(field.indexTableName,
'testdomain_' + Domain.DEFAULT_ID + '_' +
Domain.INDEX_SUFFIX + '_valid_123');
});

test('fieldTypeToColumnType (text)', function() {
Expand All @@ -90,8 +90,8 @@ suite('database', function() {
test('fieldTypeToColumnType (literal)', function() {
var field = new IndexField('valid_123', domain);
assert.equal(field.fieldTypeToColumnType('literal'),
Domain.REFERENCE_TABLE_PREFIX + '_testdomain_' +
Domain.DEFAULT_ID + '_valid_123');
'testdomain_' + Domain.DEFAULT_ID + '_' +
Domain.INDEX_SUFFIX + '_valid_123');
});

test('initial status (text)', function() {
Expand Down Expand Up @@ -274,9 +274,9 @@ suite('database', function() {
'table_create ' + domain.termsTableName + ' ' +
'TABLE_PAT_KEY|KEY_NORMALIZE ShortText ' +
'--default_tokenizer TokenBigram\n' +
'table_create ' + field.referenceTableName + ' ' +
'table_create ' + field.indexTableName + ' ' +
'TABLE_HASH_KEY UInt32\n' +
'column_create ' + field.referenceTableName + ' ' +
'column_create ' + field.indexTableName + ' ' +
field.indexColumnName + ' ' +
'COLUMN_INDEX|WITH_POSITION ' + domain.tableName +
' ' + field.columnName;
Expand Down Expand Up @@ -317,15 +317,15 @@ suite('database', function() {
'table_create ' + domain.termsTableName + ' ' +
'TABLE_PAT_KEY|KEY_NORMALIZE ShortText ' +
'--default_tokenizer TokenBigram\n' +
'table_create ' + field.referenceTableName + ' ' +
'table_create ' + field.indexTableName + ' ' +
'TABLE_HASH_KEY ShortText\n' +
'column_create ' + field.referenceTableName + ' ' +
'column_create ' + field.indexTableName + ' ' +
field.indexColumnName + ' ' +
'COLUMN_INDEX|WITH_POSITION ' + domain.tableName +
' ' + field.columnName + '\n' +
'column_create ' + domain.tableName + ' ' +
field.columnName + ' COLUMN_SCALAR ' +
field.referenceTableName;
field.indexTableName;
assert.equal(dump, expected);
});

Expand Down
2 changes: 1 addition & 1 deletion test/fixture/companies/data-deleted.grn
@@ -1,4 +1,4 @@
load --table domain_companies_00000000000000000000000000
load --table 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 domain_companies_00000000000000000000000000
load --table 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 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
table_create companies_id0123_index_product TABLE_HASH_KEY ShortText
table_create companies_id0123_index_age TABLE_HASH_KEY UInt32
table_create companies_id0123_index_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_index_product
column_create companies_id0123_index_BigramTerms companies_name COLUMN_INDEX|WITH_POSITION companies_id0123 name
column_create companies_id0123_index_BigramTerms companies_email_address COLUMN_INDEX|WITH_POSITION companies_id0123 email_address
column_create companies_id0123_index_BigramTerms companies_description COLUMN_INDEX|WITH_POSITION companies_id0123 description
column_create companies_id0123_index_BigramTerms companies_address COLUMN_INDEX|WITH_POSITION companies_id0123 address
column_create companies_id0123_index_age companies_age COLUMN_INDEX|WITH_POSITION companies_id0123 age
column_create companies_id0123_index_product companies_product COLUMN_INDEX|WITH_POSITION companies_id0123 product

0 comments on commit 1544212

Please sign in to comment.