[Groonga-commit] groonga/gcs [master] Add DescribeIndexFields action

Back to archive index

null+****@clear***** null+****@clear*****
2012年 8月 3日 (金) 15:53:57 JST


SHIMODA Hiroshi	2012-08-03 15:53:57 +0900 (Fri, 03 Aug 2012)

  New Revision: 12a2ae63294e4b909010edcfcc12baab04fd82e4
  https://github.com/groonga/gcs/commit/12a2ae63294e4b909010edcfcc12baab04fd82e4

  Log:
    Add DescribeIndexFields action

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

  Modified: lib/api/2011-02-01/configuration.js (+51 -0)
===================================================================
--- lib/api/2011-02-01/configuration.js    2012-08-03 15:41:26 +0900 (5fab23b)
+++ lib/api/2011-02-01/configuration.js    2012-08-03 15:53:57 +0900 (fff22bd)
@@ -279,6 +279,57 @@ handlers.DeleteIndexField = function(context, request, response) {
   }
 };
 
+function createIndexFields(options) {
+  var doc = xmlbuilder.create();
+  var indexFields = doc.begin('IndexFields', {version: '1.0'});
+  options.fields.forEach(function(field) {
+    indexFields.importXMLBuilder(createIndexFieldStatus({
+                  field:   field,
+                  element: 'member'
+                }));
+  });
+  return doc;
+}
+
+function createDescribeIndexFieldsResponse(options) {
+  var doc = xmlbuilder.create();
+  doc.begin('DescribeIndexFieldsResponse', { version: '1.0' })
+    .attribute('xmlns', XMLNS)
+    .element('DescribeIndexFieldsResult')
+      .importXMLBuilder(createIndexFields(options))
+    .up()
+    .element('ResponseMetadata')
+      .element('RequestId').text(options.requestId || '').up()
+    .up();
+  return doc.toString();
+}
+
+handlers.DescribeIndexFields = function(context, request, response) {
+  var domain = new Domain(request.query.DomainName, context);
+
+  try {
+    var keys = Object.keys(request.query).filter(function(key) {
+          return /^FieldNames\.member\.\d+$/.test(key);
+        });
+    var fieldNames = keys.sort().map(function(key) {
+          return request.query[key];
+        });
+    var fields = fieldNames.length ?
+                    fieldNames.map(function(name) {
+                      return domain.getIndexField(name);
+                    }) :
+                    domain.indexFields ;
+    response.contentType('application/xml');
+    response.send(createDescribeIndexFieldsResponse({
+      fields: fields
+    }));
+  } catch (error) {
+    var body = createCommonErrorResponse('InternalFailure', error.message);
+    response.contentType('application/xml');
+    response.send(body, 400);
+  }
+};
+
 function createIndexDocumentsResponse(options) {
   var doc = xmlbuilder.create();
   var root = doc.begin('IndexDocumentsResponse', {version: '1.0'})

  Modified: test/api-configuration.test.js (+116 -7)
===================================================================
--- test/api-configuration.test.js    2012-08-03 15:41:26 +0900 (2dcd5d8)
+++ test/api-configuration.test.js    2012-08-03 15:53:57 +0900 (7ac3b0c)
@@ -147,6 +147,24 @@ var PATTERN_DeleteIndexFieldResponse = {
       }
     };
 
+function PATTERN_DescribeIndexFieldsResponse(members) {
+  return {
+    IndexFieldsResponse: {
+      '@': { xmlns: '' },
+      IndexFieldsResult: {
+        IndexFields: (function() {
+          var pattern = {};
+          members.forEach(function(member, index) {
+            pattern[index] = member;
+          });
+          return { member: pattern };
+        })()
+      },
+      ResponseMetadata: PATTERN_ResponseMetadata
+    }
+  };
+}
+
 function PATTERN_IndexDocumentsResponse(members) {
   return {
     IndexDocumentsResponse: {
@@ -331,14 +349,14 @@ suite('Configuration API', function() {
   });
 
   function getActualDescribedDomains(response) {
-    var actualDomains = response.body.DescribeDomainsResponse
-                                     .DescribeDomainsResult
-                                     .DomainStatusList
-                                     .member;
+    var members = response.body.DescribeDomainsResponse
+                               .DescribeDomainsResult
+                               .DomainStatusList
+                               .member;
     var domains = [];
-    for (var i in actualDomains) {
-      if (actualDomains.hasOwnProperty(i))
-        domains.push(actualDomains[i].DomainName);
+    for (var i in members) {
+      if (members.hasOwnProperty(i))
+        domains.push(members[i].DomainName);
     }
     return domains;
   }
@@ -615,6 +633,97 @@ suite('Configuration API', function() {
       });
   });
 
+  function getActualDescribedIndexFields(response) {
+    var members = response.body.DescribeIndexFieldsResponse
+                               .DescribeIndexFieldsResult
+                               .IndexFields
+                               .member;
+    var domains = [];
+    for (var i in members) {
+      if (members.hasOwnProperty(i))
+        domains.push(members[i].FieldName);
+    }
+    return domains;
+  }
+
+  test('Get, Action=DescribeIndexFields (all fields)', function(done) {
+    var domain;
+    utils
+      .get('/?DomainName=companies&Action=CreateDomain&Version=2011-02-01', {
+        'Host': 'cloudsearch.localhost'
+      })
+      .get('/?DomainName=companies&IndexField.IndexFieldName=name&' +
+           'IndexField.IndexFieldType=text&' +
+           'Action=DefineIndexField&Version=2011-02-01')
+      .get('/?DomainName=companies&IndexField.IndexFieldName=age&' +
+           'IndexField.IndexFieldType=uint&' +
+           'Action=DefineIndexField&Version=2011-02-01')
+      .get('/?DomainName=companies&IndexField.IndexFieldName=product&' +
+           'IndexField.IndexFieldType=literal&' +
+           'Action=DefineIndexField&Version=2011-02-01')
+      .get('/?Action=DescribeIndexFields&Version=2011-02-01', {
+        'Host': 'cloudsearch.localhost'
+      })
+      .next(function(response) {
+        response = toParsedResponse(response);
+        assert.deepEqual(response.pattern,
+                         { statusCode: 200,
+                           body: PATTERN_DescribeIndexFieldsResponse([
+                             PATTERN_IndexFieldStatus_UInt,
+                             PATTERN_IndexFieldStatus_Literal,
+                             PATTERN_IndexFieldStatus_Text
+                           ]) });
+
+        var expectedFields = ['age', 'name', 'product'];
+        var actualFields = getActualDescribedIndexFields(response);
+        assert.deepEqual(actualFields, expectedFields);
+
+        done();
+      })
+      .error(function(error) {
+        done(error);
+      });
+  });
+
+  test('Get, Action=DescribeIndexFields (specified fields)', function(done) {
+    utils
+      .get('/?DomainName=companies&Action=CreateDomain&Version=2011-02-01', {
+        'Host': 'cloudsearch.localhost'
+      })
+      .get('/?DomainName=companies&IndexField.IndexFieldName=name&' +
+           'IndexField.IndexFieldType=text&' +
+           'Action=DefineIndexField&Version=2011-02-01')
+      .get('/?DomainName=companies&IndexField.IndexFieldName=age&' +
+           'IndexField.IndexFieldType=uint&' +
+           'Action=DefineIndexField&Version=2011-02-01')
+      .get('/?DomainName=companies&IndexField.IndexFieldName=product&' +
+           'IndexField.IndexFieldType=literal&' +
+           'Action=DefineIndexField&Version=2011-02-01')
+      .get('/?Action=DescribeIndexFields&Version=2011-02-01' +
+             '&FieldNames.member.1=name' +
+             '&FieldNames.member.2=age', {
+        'Host': 'cloudsearch.localhost'
+      })
+      .next(function(response) {
+        response = toParsedResponse(response);
+        assert.deepEqual(response.pattern,
+                         { statusCode: 200,
+                           body: PATTERN_DescribeIndexFieldsResponse([
+                             PATTERN_IndexFieldStatus_Text,
+                             PATTERN_IndexFieldStatus_UInt
+                           ]) });
+
+        var expectedFields = ['name', 'age'];
+        var actualFields = getActualDescribedIndexFields(response);
+        assert.deepEqual(actualFields, expectedFields);
+
+        done();
+      })
+      .error(function(error) {
+        done(error);
+      });
+  });
+
   test('Get, Action=IndexDocuments', function(done) {
     utils
       .get('/?DomainName=companies&Action=CreateDomain&Version=2011-02-01', {
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
다운로드 



Groonga-commit メーリングリストの案内
Back to archive index