[Groonga-commit] groonga/gcs [master] Define index fields with IndexField#setOptions

Back to archive index

YUKI Hiroshi null+****@clear*****
Thu Nov 22 18:10:44 JST 2012


YUKI Hiroshi	2012-11-22 18:10:44 +0900 (Thu, 22 Nov 2012)

  New Revision: 1ba5ce3f171ed5e54dfe10ea635982c36fdc5d49
  https://github.com/groonga/gcs/commit/1ba5ce3f171ed5e54dfe10ea635982c36fdc5d49

  Log:
    Define index fields with IndexField#setOptions

  Modified files:
    lib/api/2011-02-01/configuration.js

  Modified: lib/api/2011-02-01/configuration.js (+18 -77)
===================================================================
--- lib/api/2011-02-01/configuration.js    2012-11-22 17:57:42 +0900 (78adb8c)
+++ lib/api/2011-02-01/configuration.js    2012-11-22 18:10:44 +0900 (13cf32d)
@@ -232,53 +232,37 @@ handlers.DescribeDomains = function(context, request, response, config) {
 };
 
 function createIndexFieldOptionStatus(field, givenOptions) {
-  var hasOption = givenOptions && givenOptions.length;
+  var hasOption = Object.keys(field.getAllOptions()).length;
   switch (field.type) {
     case 'text':
       var textOptions = xmlbuilder.create();
       var textOptionsFragment = textOptions.begin('TextOptions', { version: '1.0' });
-      if (field.hasDefaultValue) {
-        hasOption = true;
+      if (field.hasDefaultValue)
         textOptionsFragment.element('DefaultValue').text(field.defaultValue);
-      }
-      if (field.hasFacetEnabled) {
-        hasOption = true;
+      if (field.hasFacetEnabled)
         textOptionsFragment.element('FacetEnabled').text(field.facetEnabled);
-      }
-      if (field.hasResultEnabled) {
-        hasOption = true;
+      if (field.hasResultEnabled)
         textOptionsFragment.element('ResultEnabled').text(field.resultEnabled);
-      }
       return hasOption ? textOptions : null;
 
     case 'uint':
       var uintOptions = xmlbuilder.create();
       var uintOptionsFragment = uintOptions.begin('UIntOptions', { version: '1.0' });
-      if (field.hasDefaultValue) {
-        hasOption = true;
+      if (field.hasDefaultValue)
         uintOptionsFragment.element('DefaultValue').text(field.defaultValue);
-      }
       return hasOption ? uintOptions : null;
 
     case 'literal':
       var literalOptions = xmlbuilder.create();
       var literalOptionsFragment = literalOptions.begin('LiteralOptions', { version: '1.0' });
-      if (field.hasDefaultValue) {
-        hasOption = true;
+      if (field.hasDefaultValue)
         literalOptionsFragment.element('DefaultValue').text(field.defaultValue);
-      }
-      if (field.hasFacetEnabled) {
-        hasOption = true;
+      if (field.hasFacetEnabled)
         literalOptionsFragment.element('FacetEnabled').text(field.facetEnabled);
-      }
-      if (field.hasResultEnabled) {
-        hasOption = true;
+      if (field.hasResultEnabled)
         literalOptionsFragment.element('ResultEnabled').text(field.resultEnabled);
-      }
-      if (field.hasSearchEnabled) {
-        hasOption = true;
+      if (field.hasSearchEnabled)
         literalOptionsFragment.element('SearchEnabled').text(field.searchEnabled);
-      }
       return hasOption ? literalOptions : null;
   }
 }
@@ -314,7 +298,7 @@ function createIndexFieldStatus(options) {
         .element('Options')
           .element('IndexFieldName').text(options.field.name).up()
           .element('IndexFieldType').text(options.field.type).up();
-  var optionsStatus = createIndexFieldOptionStatus(options.field, options.givenOptions);
+  var optionsStatus = createIndexFieldOptionStatus(options.field);
   if (optionsStatus) optionsFragment.importXMLBuilder(optionsStatus);
 
   statusFragment
@@ -328,39 +312,6 @@ function createIndexFieldStatus(options) {
   return indexFieldStatus;
 }
 
-var TEXT_FIELD_OPTIONS = IndexField.TEXT_FIELD_OPTIONS.map(function(option) {
-      return 'IndexField.TextOptions.' + option;
-    });
-var LITERAL_FIELD_OPTIONS = IndexField.LITERAL_FIELD_OPTIONS.map(function(option) {
-      return 'IndexField.LiteralOptions.' + option;
-    });
-var UINT_FIELD_OPTIONS = IndexField.UINT_FIELD_OPTIONS.map(function(option) {
-      return 'IndexField.UIntOptions.' + option;
-    });
-
-function getFieldOption(option, request, type) {
-  var optionName;
-  switch (type) {
-    case 'text':
-      optionName = 'IndexField.TextOptions.' + option;
-      break;
-    case 'literal':
-      optionName = 'IndexField.LiteralOptions.' + option;
-      break;
-    case 'uint':
-      optionName = 'IndexField.UIntOptions.' + option;
-      break;
-  }
-
-  var validOptions = type == 'text' ? TEXT_FIELD_OPTIONS :
-                     type == 'literal' ? LITERAL_FIELD_OPTIONS :
-                     UINT_FIELD_OPTIONS;
-  if (validOptions.indexOf(optionName) < 0)
-    return undefined;
-
-  return request.query[optionName];
-}
-
 handlers.DefineIndexField = function(context, request, response, config) {
   var domainName = request.query.DomainName || '';
   var domain = handleDomanValidationError(function() {
@@ -375,7 +326,6 @@ handlers.DefineIndexField = function(context, request, response, config) {
         return domain.getIndexField(fieldName).setType(fieldType).validate();
       });
 
-
   var textOptions = Object.keys(request.query).filter(function(name) {
         return name.indexOf('IndexField.TextOptions.') == 0;
       });
@@ -393,29 +343,20 @@ handlers.DefineIndexField = function(context, request, response, config) {
   if (fieldType == 'uint' && (textOptions.length || literalOptions.length))
     throw new errors.FieldOptionConflictError('A uint IndexField may only specify uintOptions');
 
-  var givenOptions = fieldType == 'text' ? textOptions :
+  var optionNames = fieldType == 'text' ? textOptions :
                      fieldType == 'literal' ? literalOptions :
                      uintOptions;
+  var optionValues = {};
+  optionNames.forEach(function(name) {
+    optionValues[name.replace(/^IndexField\.[^\.]+\./, '')] = request.query[name];
+  });
 
-
-  var facetEnabled = getFieldOption('FacetEnabled', request, fieldType);
-  if (facetEnabled !== undefined)
-    field.facetEnabled = facetEnabled.toLowerCase() == 'true';
-
-  var resultEnabled = getFieldOption('ResultEnabled', request, fieldType);
-  if (resultEnabled !== undefined)
-    field.resultEnabled = resultEnabled.toLowerCase() == 'true';
-
-  var searchEnabled = getFieldOption('SearchEnabled', request, fieldType);
-  if (searchEnabled !== undefined)
-    field.searchEnabled = searchEnabled.toLowerCase() == 'true';
-
+  field.setOptions(optionValues);
   field.saveSync();
 
   var result = createIndexFieldStatus({
-        field:        field,
-        givenOptions: givenOptions,
-        state:        'RequiresIndexDocuments'
+        field: field,
+        state: 'RequiresIndexDocuments'
       });
   response.contentType('application/xml');
   response.send(createGenericResponse('DefineIndexField', result, request.id));
-------------- next part --------------
HTML����������������������������...
Descargar 



More information about the Groonga-commit mailing list
Back to archive index