YUKI Hiroshi
null+****@clear*****
Thu Nov 22 17:30:26 JST 2012
YUKI Hiroshi 2012-11-22 17:30:26 +0900 (Thu, 22 Nov 2012) New Revision: e6f0fdd738f59fb45101d0a742a73f1772b1bcbe https://github.com/groonga/gcs/commit/e6f0fdd738f59fb45101d0a742a73f1772b1bcbe Log: Accept any option to index fields Modified files: lib/database/index-field.js Modified: lib/database/index-field.js (+104 -49) =================================================================== --- lib/database/index-field.js 2012-11-22 16:54:17 +0900 (0825752) +++ lib/database/index-field.js 2012-11-22 17:30:26 +0900 (2a8b514) @@ -121,6 +121,24 @@ function assertNotReservedName(name) { throw new FieldOptionConflictError(name + ' is a reserved name'); } +function toSnakeCase(input) { + return input.replace(/^[A-Z]/, function(aMatched) { + return aMatched.toLowerCase(); + }).replace(/[a-z0-9][A-Z]/g, function(aMatched) { + aMatched = aMatched.toLowerCase(); + return aMatched.charAt(0) + '_' + aMatched.charAt(1); + }); +} + +function toCamelCase(input) { + return input.replace(/^[a-z]/, function(aMatched) { + return aMatched.toUpperCase(); + }).replace(/_[a-z0-9]/g, function(aMatched) { + aMatched = aMatched.toUpperCase(); + return aMatched.charAt(1); + }); +} + function IndexField(name, domain) { this.domain = domain; this.context = domain.context; @@ -256,16 +274,86 @@ IndexField.prototype = { return this; }, + getOption: function(option) { + var key = this.getOptionKey(option); + if (this._pendingOptions) + return this._pendingOptions[key]; + return this.domain.getConfiguration(key); + }, + setOption: function(option, value) { + this._pendingOptions = this._pendingOptions || {}; + this._pendingOptions[this.getOptionKey(option)] = value; + return value; + }, + hasOption: function(option) { + return this.getOption(option) !== undefined; + }, + getOptionKey: function(option) { + return 'column_' + this.name + '_option_' + toSnakeCase(option); + }, + setOptions: function(options) { + if (!options) return; + Object.keys(options).forEach(function(option) { + this.setOption(option, options[option]); + }, this); + return this; + }, + getAllOptions: function() { + var rawOptionValues = this.getAllRawOptions(); + var optionValues = {}; + var base = this.getOptionKey(''); + Object.keys(rawOptionValues).forEach(function(key) { + var name = toCamelCase(key.replace(base, '')); + optionValues[name] = rawOptionValues[key]; + }, this); + return optionValues; + }, + getAllRawOptions: function() { + var optionValues = {}; + var existingKeys = this.domain.getConfiguration(this.getOptionKey('optionKeys')); + if (existingKeys) { + existingKeys.split(',').forEach(function(key) { + optionValues[key] = this.domain.getConfiguration(key); + }, this); + } + if (this._pendingOptions) { + Object.keys(this._pendingOptions).forEach(function(key) { + optionValues[key] = this._pendingOptions[key]; + }, this); + } + return optionValues; + }, + saveOptions: function() { + if (!this._pendingOptions) return; + var keys = Object.keys(this._pendingOptions); + keys.forEach(function(key) { + this.domain.setConfiguration(key, this._pendingOptions[key]); + }, this); + var existingKeys = this.domain.getConfiguration(this.getOptionKey('optionKeys')); + if (existingKeys) { + existingKeys.split(',').forEach(function(key) { + if (keys.indexOf(key) < 0) keys.push(key); + }); + } + this.domain.setConfiguration(this.getOptionKey('optionKeys'), keys.join(',')); + this._pendingOptions = {}; + return this; + }, + hasAnyOption: function() { + return !!this.domain.getConfiguration(this.getOptionKey('optionKeys')); + }, + + get defaultValue() { if (this.type == 'uint') return 0; return null; }, get hasDefaultValue() { - return this.domain.getConfiguration(this.defaultValueConfigurationKey) !== undefined; + return this.hasOption('defaultValue'); }, get defaultValueConfigurationKey() { - return 'column_' + this.name + '_option_default_value'; + return this.getOptionKey('defaultValue'); }, get facetEnabled() { @@ -273,88 +361,70 @@ IndexField.prototype = { if (this.type == 'uint') return false; - if (!this.exists() || this._facetEnabled !== undefined) - return !!this._facetEnabled; - - var value = this.domain.getConfiguration(this.facetEnabledConfigurationKey); - return !!(this._facetEnabled = value); + return !!this.getOption('facetEnabled'); }, set facetEnabled(value) { var booleanValue = !!value; if (booleanValue != this.facetEnabled && this.type == 'uint') throw new Error('facet option cannot be configured for the type ' + this.type + '.'); - - this._facetEnabled = booleanValue; - return value; + return this.setOption('facetEnabled', booleanValue); }, setFacetEnabled: function(value) { this.facetEnabled = value; return this; }, get hasFacetEnabled() { - return this.domain.getConfiguration(this.facetEnabledConfigurationKey) !== undefined; + return this.hasOption('facetEnabled'); }, get facetEnabledConfigurationKey() { - return 'column_' + this.name + '_option_facet_enabled'; + return this.getOptionKey('facetEnabled'); }, get resultEnabled() { if (this.type == 'uint') return true; - if (!this.exists() || this._resultEnabled !== undefined) - return !!this._resultEnabled; - - var value = this.domain.getConfiguration(this.resultEnabledConfigurationKey); - return !!(this._resultEnabled = value); + return !!this.getOption('resultEnabled'); }, set resultEnabled(value) { var booleanValue = !!value; if (booleanValue != this.resultEnabled && this.type == 'uint') throw new Error('returnable option cannot be configured for the type ' + this.type + '.'); - - this._resultEnabled = !!value; - return value; + return this.setOption('resultEnabled', booleanValue); }, setResultEnabled: function(value) { this.resultEnabled = value; return this; }, get hasResultEnabled() { - return this.domain.getConfiguration(this.resultEnabledConfigurationKey) !== undefined; + return this.hasOption('resultEnabled'); }, get resultEnabledConfigurationKey() { - return 'column_' + this.name + '_option_result_enabled'; + return this.getOptionKey('resultEnabled'); }, get searchEnabled() { if (this.type == 'text' || this.type == 'uint') return true; - if (!this.exists() || this._searchEnabled !== undefined) - return !!this._searchEnabled; - - var value = this.domain.getConfiguration(this.searchEnabledConfigurationKey); - return !!(this._searchEnabled = value); + return !!this.getOption('searchEnabled'); }, set searchEnabled(value) { var booleanValue = !!value; if (booleanValue != this.searchEnabled && (this.type == 'text' || this.type == 'uint')) throw new Error('searchable option cannot be configured for the type ' + this.type + '.'); - - this._searchEnabled = booleanValue; - return value; + return this.setOption('searchEnabled', booleanValue); }, setSearchEnabled: function(value) { this.searchEnabled = value; return this; }, get hasSearchEnabled() { - return this.domain.getConfiguration(this.searchEnabledConfigurationKey) !== undefined; + return this.hasOption('searchEnabled'); }, get searchEnabledConfigurationKey() { - return 'column_' + this.name + '_option_search_enabled'; + return this.getOptionKey('searchEnabled'); }, get defaultSearchField() { @@ -498,20 +568,7 @@ IndexField.prototype = { if (this.type != this.actualType) this.changeTypeToSync(this.type); - this.facetEnabled; - if (this._facetEnabled !== undefined) - this.domain.setConfiguration(this.facetEnabledConfigurationKey, - this._facetEnabled); - - this.resultEnabled; - if (this._resultEnabled !== undefined) - this.domain.setConfiguration(this.resultEnabledConfigurationKey, - this._resultEnabled); - - this.searchEnabled; - if (this._searchEnabled !== undefined) - this.domain.setConfiguration(this.searchEnabledConfigurationKey, - this._searchEnabled); + this.saveOptions(); if (this._defaultSearchField !== undefined) { this.domain.defaultSearchField = this._defaultSearchField ? this : null ; @@ -527,9 +584,7 @@ IndexField.prototype = { deleteSync: function() { // backup information for re-creation this._type = this.type; - this._facetEnabled = this.hasFacetEnabled ? this.facetEnabled : undefined; - this._resultEnabled = this.hasResultEnabled ? this.resultEnabled : undefined; - this._searchEnabled = this.hasSearchEnabled ? this.searchEnabled : undefined; + this._pendingOptions = this.getAllRawOptions(); this._defaultSearchField = this.defaultSearchField; this._createdAt = this.createdAt; this._updatedAt = this.updatedAt; -------------- next part -------------- HTML����������������������������...ダウンロード