null+****@clear*****
null+****@clear*****
2012年 7月 12日 (木) 16:05:16 JST
SHIMODA Hiroshi 2012-07-12 16:05:16 +0900 (Thu, 12 Jul 2012) New Revision: 536d80e1b2322ccb3ce074cdb1a08c2b2fd4803d https://github.com/groonga/gcs/commit/536d80e1b2322ccb3ce074cdb1a08c2b2fd4803d Log: Rename to "database.js" and split implementations of modeled Domain/IndexField Added files: lib/database.js lib/database/domain.js lib/database/index-field.js Removed files: lib/domain.js Modified files: lib/api/2011-02-01/batch.js lib/api/2011-02-01/configuration.js lib/api/2011-02-01/search.js lib/batch/processor.js lib/batch/translator.js test/domain.test.js Modified: lib/api/2011-02-01/batch.js (+1 -1) =================================================================== --- lib/api/2011-02-01/batch.js 2012-07-12 16:10:11 +0900 (cdfceaa) +++ lib/api/2011-02-01/batch.js 2012-07-12 16:05:16 +0900 (23a375d) @@ -1,5 +1,5 @@ var BatchProcessor = require('../../batch/processor').Processor; -var Domain = require('../../domain').Domain; +var Domain = require('../../database').Domain; function createErrorBody(errors) { errors = errors.map(function(error) { Modified: lib/api/2011-02-01/configuration.js (+1 -1) =================================================================== --- lib/api/2011-02-01/configuration.js 2012-07-12 16:10:11 +0900 (b0f8833) +++ lib/api/2011-02-01/configuration.js 2012-07-12 16:05:16 +0900 (7a5e809) @@ -1,6 +1,6 @@ var path = require('path'); var Database = require('../../wrapped-nroonga').Database; -var Domain = require('../../domain').Domain; +var Domain = require('../../database').Domain; var Translator = require('../../batch/translator').Translator; var Deferred = require('jsdeferred').Deferred; var dateFormat = require('dateformat'); Modified: lib/api/2011-02-01/search.js (+1 -1) =================================================================== --- lib/api/2011-02-01/search.js 2012-07-12 16:10:11 +0900 (453f70f) +++ lib/api/2011-02-01/search.js 2012-07-12 16:05:16 +0900 (51d8761) @@ -1,4 +1,4 @@ -var Domain = require('../../domain').Domain; +var Domain = require('../../database').Domain; var Database = require('../../wrapped-nroonga').Database; function formatSelectResults(data) { Modified: lib/batch/processor.js (+1 -1) =================================================================== --- lib/batch/processor.js 2012-07-12 16:10:11 +0900 (2916997) +++ lib/batch/processor.js 2012-07-12 16:05:16 +0900 (51c3ab2) @@ -1,6 +1,6 @@ var Database = require('../wrapped-nroonga').Database; var translator = require('./translator'); -var Domain = require('../domain').Domain; +var Domain = require('../database').Domain; var Deferred = require('jsdeferred').Deferred; exports.INVALID_BATCH = Processor.INVALID_BATCH = 'Invalid Batch'; Modified: lib/batch/translator.js (+1 -1) =================================================================== --- lib/batch/translator.js 2012-07-12 16:10:11 +0900 (816d53c) +++ lib/batch/translator.js 2012-07-12 16:05:16 +0900 (045006f) @@ -1,4 +1,4 @@ -var Domain = require('../domain').Domain; +var Domain = require('../database').Domain; var Database = require('../wrapped-nroonga').Database; function Translator(domain) { Added: lib/database.js (+5 -0) 100644 =================================================================== --- /dev/null +++ lib/database.js 2012-07-12 16:05:16 +0900 (7cc7183) @@ -0,0 +1,5 @@ +var Domain = require('./database/domain').Domain; +exports.Domain = Domain; + +var IndexField = require('./database/index-field').IndexField; +exports.IndexField = IndexField; Added: lib/database/domain.js (+136 -0) 100644 =================================================================== --- /dev/null +++ lib/database/domain.js 2012-07-12 16:05:16 +0900 (6efafd4) @@ -0,0 +1,136 @@ +var Database = require('../wrapped-nroonga').Database; +var IndexField = require('./index-field').IndexField; + +exports.MINIMUM_DOMAIN_NAME_LENGTH = 3; +exports.MAXIMUM_DOMAIN_NAME_LENGTH = 28; +exports.INVALID_DOMAIN_NAME_CHARACTER_PATTERN = /[^\-a-z0-9]/g; +exports.INVALID_TABLE_NAME_CHARACTER_PATTERN = /[^_a-z0-9]/g; + +function assertValidDomainName(domain) { + if (typeof domain != 'string') + throw new Error('domain name must be a string'); + + if (domain.length < exports.MINIMUM_DOMAIN_NAME_LENGTH) + throw new Error('too short domain name (minimum length = ' + + exports.MINIMUM_DOMAIN_NAME_LENGTH + ')'); + + if (domain.length > exports.MAXIMUM_DOMAIN_NAME_LENGTH) + throw new Error('too long domain name (max length = ' + + exports.MAXIMUM_DOMAIN_NAME_LENGTH + ')'); + + var invalidCharacter = domain.match(exports.INVALID_DOMAIN_NAME_CHARACTER_PATTERN) || + domain.match(exports.INVALID_TABLE_NAME_CHARACTER_PATTERN); + if (invalidCharacter) { + var characters = Array.prototype.map.call(invalidCharacter, function(aCharacter) { + return '"' + aCharacter + '"'; + }); + throw new Error(characters.join(', ') + ' cannot appear in a domain name'); + } +} + +function Domain(source) { + if (source instanceof Domain) + return source; + + this.initialize(source); +} +Domain.prototype = { + initialize: function(source) { + this.name = this.getName(source); + this.indexFields = {}; + // for validation + this.tableName; + this.termsTableName; + }, + getName: function(source) { + if (typeof source == 'string') + return source; + + if (source.query && source.query.DomainName) + return source.query.DomainName; + + if (source.headers && source.headers.host) { + var host = source.headers.host; + var domainNameFromHost = Domain.getNameFromHost(host); + if (domainNameFromHost) + return domainNameFromHost; + } + + var domainNameFromPath = Domain.getNameFromPath(source.url); + if (domainNameFromPath) + return domainNameFromPath; + + throw new Error('no domain name'); + }, + get tableName() { + if (!this._tableName) { + assertValidDomainName(this.name); + this._tableName = this.name; + } + return this._tableName; + }, + get termsTableName() { + if (!this._termsTableName) + this._termsTableName = this.tableName + '_BigramTerms'; + return this._termsTableName; + }, + getIndexField: function(field) { + return this.indexFields[field] || + (this.indexFields[field] = new IndexField(field, this)); + }, + getIndexFieldsSync: function(database) { + var columns = database.ordinalColumnsSync(this.tableName); + var fields = columns.map(this.columnToIndexField, this); + return fields; + }, + columnToIndexField: function(column) { + // XXX The "name" must be the field name given by the user, + // not normalized. Because there is no such information in the + // database and currently the column name is luckly equals to the + // given field name, we can use the column name. + var name = column.name; + var field = this.getIndexField(name); + + var type; + if (column.type == 'var') { + if (column.range == Database.ShortText) + type = 'text'; + } else if (column.type == 'fix') { + if (column.range == Database.UInt32) + type = 'uint'; + else if (column.range == field.alterTableName) + type = 'literal'; + } + if (!type) + throw new Error('unknown unfixed column '+column.name); + + field.type = type; + return field; + }, + get synonymTableName() { + if (!this._synonymTableName) + this._synonymTableName = this.tableName + '_synonyms'; + return this._synonymTableName; + } +}; + +exports.Domain = Domain; + +Domain.getNameFromHost = function(host) { + var domainMatcher = /^(?:doc|search)-([^\.]+)-([^\.\-]+)\./; + var match = host.match(domainMatcher); + if (match) + return match[1]; + + return ''; +}; + +Domain.getNameFromPath = function(path) { + var domainMatcher = /^\/gcs\/([^\/]+)/; + + var match = path.match(domainMatcher); + if (match) + return match[1]; + + return ''; +}; Added: lib/database/index-field.js (+109 -0) 100644 =================================================================== --- /dev/null +++ lib/database/index-field.js 2012-07-12 16:05:16 +0900 (8606064) @@ -0,0 +1,109 @@ +var Database = require('../wrapped-nroonga').Database; + +exports.MINIMUM_FIELD_NAME_LENGTH = 3; +exports.MAXIMUM_FIELD_NAME_LENGTH = 64; +exports.INVALID_FIELD_NAME_CHARACTER_PATTERN = /[^_a-z0-9]/g; +exports.INVALID_COLUMN_NAME_CHARACTER_PATTERN = /[^_a-z0-9]/g; +exports.RESERVED_FIELD_NAMES = [ + 'body', + 'docid', + 'text_relevance' +]; +exports.RESERVED_COLUMN_NAMES = [ + '_key' +]; + +function assertValidFieldName(field) { + if (typeof field != 'string') + throw new Error('field name must be a string'); + + if (field.length < exports.MINIMUM_FIELD_NAME_LENGTH) + throw new Error('too short field name (minimum length = ' + + exports.MINIMUM_FIELD_NAME_LENGTH + ')'); + + if (field.length > exports.MAXIMUM_FIELD_NAME_LENGTH) + throw new Error('too long field name (max length = ' + + exports.MAXIMUM_FIELD_NAME_LENGTH + ')'); + + var invalidCharacter = field.match(exports.INVALID_FIELD_NAME_CHARACTER_PATTERN) || + field.match(exports.INVALID_COLUMN_NAME_CHARACTER_PATTERN); + if (invalidCharacter) { + var characters = Array.prototype.map.call(invalidCharacter, function(aCharacter) { + return '"' + aCharacter + '"'; + }); + throw new Error(characters.join(', ') + ' cannot appear in a field name'); + } + + var index = exports.RESERVED_FIELD_NAMES.indexOf(field); + if (index > -1) index = exports.RESERVED_COLUMN_NAMES.indexOf(field); + if (index > -1) + throw new Error(field + ' is a reserved field name'); +} + +function IndexField(name, domain) { + this.domain = domain; + this.name = name; + this.type = null; + this.initialize(); +}; +IndexField.prototype = { + initialize: function() { + // for validation + this.columnName; + this.indexColumnName; + }, + get columnName() { + if (!this._columnName) { + assertValidFieldName(this.name); + this._columnName = this.name; + } + return this._columnName; + }, + get indexColumnName() { + if (!this._indexColumnName) + this._indexColumnName = this.domain.tableName + '_' + this.columnName; + return this._indexColumnName; + }, + get alterTableName() { + if (!this._alterTableName) + this._alterTableName = this.domain.tableName + '_' + this.columnName; + return this._alterTableName; + }, + fieldTypeToColumnType: function(fieldType) { + switch (fieldType) { + case 'text': + return Database.ShortText; + case 'uint': + return Database.UInt32; + case 'literal': + return this.alterTableName; + default: + throw new Error('Unsupported index field type '+fieldType); + } + }, + columnTypeToFieldType: function(columnType) { + switch (columnType) { + case Database.ShortText: + return 'text'; + case Database.UInt32: + return 'uint'; + case this.alterTableName: + return 'literal'; + default: + throw new Error('Unsupported column type '+columnType); + } + }, + fieldTypeToAlterTableKeyType: function(fieldType) { + switch (fieldType) { + case 'uint': + return Database.UInt32; + case 'literal': + return Database.ShortText; + default: + throw new Error('Unsupported index field type '+fieldType+ + ' for alter table'); + } + } +}; + +exports.IndexField = IndexField; Deleted: lib/domain.js (+0 -245) 100644 =================================================================== --- lib/domain.js 2012-07-12 16:10:11 +0900 (fc1631e) +++ /dev/null @@ -1,245 +0,0 @@ -var http = require('http'); -var Database = require('./wrapped-nroonga').Database; - -exports.MINIMUM_DOMAIN_NAME_LENGTH = 3; -exports.MAXIMUM_DOMAIN_NAME_LENGTH = 28; -exports.INVALID_DOMAIN_NAME_CHARACTER_PATTERN = /[^\-a-z0-9]/g; -exports.INVALID_TABLE_NAME_CHARACTER_PATTERN = /[^_a-z0-9]/g; - -function assertValidDomainName(domain) { - if (typeof domain != 'string') - throw new Error('domain name must be a string'); - - if (domain.length < exports.MINIMUM_DOMAIN_NAME_LENGTH) - throw new Error('too short domain name (minimum length = ' + - exports.MINIMUM_DOMAIN_NAME_LENGTH + ')'); - - if (domain.length > exports.MAXIMUM_DOMAIN_NAME_LENGTH) - throw new Error('too long domain name (max length = ' + - exports.MAXIMUM_DOMAIN_NAME_LENGTH + ')'); - - var invalidCharacter = domain.match(exports.INVALID_DOMAIN_NAME_CHARACTER_PATTERN) || - domain.match(exports.INVALID_TABLE_NAME_CHARACTER_PATTERN); - if (invalidCharacter) { - var characters = Array.prototype.map.call(invalidCharacter, function(aCharacter) { - return '"' + aCharacter + '"'; - }); - throw new Error(characters.join(', ') + ' cannot appear in a domain name'); - } -} - -function Domain(source) { - if (source instanceof Domain) - return source; - - this.initialize(source); -} -Domain.prototype = { - initialize: function(source) { - this.name = this.getName(source); - this.indexFields = {}; - // for validation - this.tableName; - this.termsTableName; - }, - getName: function(source) { - if (typeof source == 'string') - return source; - - if (source.query && source.query.DomainName) - return source.query.DomainName; - - if (source.headers && source.headers.host) { - var host = source.headers.host; - var domainNameFromHost = Domain.getNameFromHost(host); - if (domainNameFromHost) - return domainNameFromHost; - } - - var domainNameFromPath = Domain.getNameFromPath(source.url); - if (domainNameFromPath) - return domainNameFromPath; - - throw new Error('no domain name'); - }, - get tableName() { - if (!this._tableName) { - assertValidDomainName(this.name); - this._tableName = this.name; - } - return this._tableName; - }, - get termsTableName() { - if (!this._termsTableName) - this._termsTableName = this.tableName + '_BigramTerms'; - return this._termsTableName; - }, - getIndexField: function(field) { - return this.indexFields[field] || - (this.indexFields[field] = new IndexField(field, this)); - }, - getIndexFieldsSync: function(database) { - var columns = database.ordinalColumnsSync(this.tableName); - var fields = columns.map(this.columnToIndexField, this); - return fields; - }, - columnToIndexField: function(column) { - // XXX The "name" must be the field name given by the user, - // not normalized. Because there is no such information in the - // database and currently the column name is luckly equals to the - // given field name, we can use the column name. - var name = column.name; - var field = this.getIndexField(name); - - var type; - if (column.type == 'var') { - if (column.range == Database.ShortText) - type = 'text'; - } else if (column.type == 'fix') { - if (column.range == Database.UInt32) - type = 'uint'; - else if (column.range == field.alterTableName) - type = 'literal'; - } - if (!type) - throw new Error('unknown unfixed column '+column.name); - - field.type = type; - return field; - }, - get synonymTableName() { - if (!this._synonymTableName) - this._synonymTableName = this.tableName + '_synonyms'; - return this._synonymTableName; - } -}; - -exports.Domain = Domain; - -Domain.getNameFromHost = function(host) { - var domainMatcher = /^(?:doc|search)-([^\.]+)-([^\.\-]+)\./; - var match = host.match(domainMatcher); - if (match) - return match[1]; - - return ''; -}; - -Domain.getNameFromPath = function(path) { - var domainMatcher = /^\/gcs\/([^\/]+)/; - - var match = path.match(domainMatcher); - if (match) - return match[1]; - - return ''; -}; - - -exports.MINIMUM_FIELD_NAME_LENGTH = 3; -exports.MAXIMUM_FIELD_NAME_LENGTH = 64; -exports.INVALID_FIELD_NAME_CHARACTER_PATTERN = /[^_a-z0-9]/g; -exports.INVALID_COLUMN_NAME_CHARACTER_PATTERN = /[^_a-z0-9]/g; -exports.RESERVED_FIELD_NAMES = [ - 'body', - 'docid', - 'text_relevance' -]; -exports.RESERVED_COLUMN_NAMES = [ - '_key' -]; - -function assertValidFieldName(field) { - if (typeof field != 'string') - throw new Error('field name must be a string'); - - if (field.length < exports.MINIMUM_FIELD_NAME_LENGTH) - throw new Error('too short field name (minimum length = ' + - exports.MINIMUM_FIELD_NAME_LENGTH + ')'); - - if (field.length > exports.MAXIMUM_FIELD_NAME_LENGTH) - throw new Error('too long field name (max length = ' + - exports.MAXIMUM_FIELD_NAME_LENGTH + ')'); - - var invalidCharacter = field.match(exports.INVALID_FIELD_NAME_CHARACTER_PATTERN) || - field.match(exports.INVALID_COLUMN_NAME_CHARACTER_PATTERN); - if (invalidCharacter) { - var characters = Array.prototype.map.call(invalidCharacter, function(aCharacter) { - return '"' + aCharacter + '"'; - }); - throw new Error(characters.join(', ') + ' cannot appear in a field name'); - } - - var index = exports.RESERVED_FIELD_NAMES.indexOf(field); - if (index > -1) index = exports.RESERVED_COLUMN_NAMES.indexOf(field); - if (index > -1) - throw new Error(field + ' is a reserved field name'); -} - -function IndexField(name, domain) { - this.domain = domain; - this.name = name; - this.type = null; - this.initialize(); -}; -IndexField.prototype = { - initialize: function() { - // for validation - this.columnName; - this.indexColumnName; - }, - get columnName() { - if (!this._columnName) { - assertValidFieldName(this.name); - this._columnName = this.name; - } - return this._columnName; - }, - get indexColumnName() { - if (!this._indexColumnName) - this._indexColumnName = this.domain.tableName + '_' + this.columnName; - return this._indexColumnName; - }, - get alterTableName() { - if (!this._alterTableName) - this._alterTableName = this.domain.tableName + '_' + this.columnName; - return this._alterTableName; - }, - fieldTypeToColumnType: function(fieldType) { - switch (fieldType) { - case 'text': - return Database.ShortText; - case 'uint': - return Database.UInt32; - case 'literal': - return this.alterTableName; - default: - throw new Error('Unsupported index field type '+fieldType); - } - }, - columnTypeToFieldType: function(columnType) { - switch (columnType) { - case Database.ShortText: - return 'text'; - case Database.UInt32: - return 'uint'; - case this.alterTableName: - return 'literal'; - default: - throw new Error('Unsupported column type '+columnType); - } - }, - fieldTypeToAlterTableKeyType: function(fieldType) { - switch (fieldType) { - case 'uint': - return Database.UInt32; - case 'literal': - return Database.ShortText; - default: - throw new Error('Unsupported index field type '+fieldType+ - ' for alter table'); - } - } -}; - -exports.IndexField = IndexField; Modified: test/domain.test.js (+1 -1) =================================================================== --- test/domain.test.js 2012-07-12 16:10:11 +0900 (b0cd8d3) +++ test/domain.test.js 2012-07-12 16:05:16 +0900 (0ee2a98) @@ -2,7 +2,7 @@ var utils = require('./test-utils'); var assert = require('chai').assert; -var domain = require('../lib/domain'); +var domain = require('../lib/database/domain'); var Domain = domain.Domain; var IndexField = domain.IndexField; -------------- next part -------------- HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B... Download