[Groonga-commit] groonga/gcs [master] Support uint type index field

Back to archive index

null+****@clear***** null+****@clear*****
2012年 7月 6日 (金) 18:42:06 JST


SHIMODA Hiroshi	2012-07-06 18:42:06 +0900 (Fri, 06 Jul 2012)

  New Revision: c72eb75fa9ebf5a308f7d2d77344f65b60ba580e
  https://github.com/groonga/gcs/commit/c72eb75fa9ebf5a308f7d2d77344f65b60ba580e

  Log:
    Support uint type index field

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

  Modified: lib/api/2011-02-01/configuration.js (+74 -40)
===================================================================
--- lib/api/2011-02-01/configuration.js    2012-07-06 18:20:17 +0900 (806b626)
+++ lib/api/2011-02-01/configuration.js    2012-07-06 18:42:06 +0900 (725f3cd)
@@ -147,18 +147,29 @@ handlers.DeleteDomain = function(database, request, response) {
   }
 };
 
-function createIndexFieldStatus(options) {
-  return '<IndexField>' +
-           '<Options>' +
-             '<IndexFieldName>' + options.fieldName + '</IndexFieldName>' +
-             '<IndexFieldType>' + options.fieldType + '</IndexFieldType>' +
-             '<TextOptions>' +
+function createIndexFieldOptionStatus(options) {
+  switch (options.fieldType) {
+    case 'text':
+      return '<TextOptions>' +
                '<DefaultValue/>' +
                '<FacetEnabled>' + (options.facetEnabled || false) +
                  '</FacetEnabled>' +
                '<ResultEnabled>' + (options.resultEnabled || false) +
                  '</ResultEnabled>' +
-             '</TextOptions>' +
+             '</TextOptions>';
+    case 'uint':
+      return '<UIntOptions>' +
+               '<DefaultValue/>' +
+             '</UIntOptions>';
+  }
+}
+
+function createIndexFieldStatus(options) {
+  return '<IndexField>' +
+           '<Options>' +
+             '<IndexFieldName>' + options.fieldName + '</IndexFieldName>' +
+             '<IndexFieldType>' + options.fieldType + '</IndexFieldType>' +
+             createIndexFieldOptionStatus(options) +
            '</Options>' +
            '<Status>' +
              '<CreationDate>' + dateFormat(options.createdAt,
@@ -189,43 +200,58 @@ handlers.DefineIndexField = function(database, request, response) {
   var domain = new Domain(request);
 
   var fieldName = request.query['IndexField.IndexFieldName'] || '';
+  var fieldType = request.query['IndexField.IndexFieldType'] || 'text';
   var field = domain.getIndexField(fieldName);
 
   var createdAt = new Date();
-  var options = {
-        table: domain.tableName,
-        name: field.columnName,
-        flags: Database.COLUMN_SCALAR,
-        type: Database.ShortText
-      };
-  database.commandDeferred('column_create', options)
-    .next(function(data) {
-      var termsOptions = {
-            table: domain.termsTableName,
-            name: field.indexColumnName,
-            flags: Database.INDEX_COLUMN_DEFAULT_FLAGS,
-            type: domain.tableName,
-            source: field.columnName
-          };
-      return database.commandDeferred('column_create', termsOptions);
-    })
-    .next(function() {
-      response.contentType('application/xml');
-      response.send(createDefineIndexFieldResponse({
-        fieldName: fieldName,
-        fieldType: 'text',
-        facetEnabled: false,
-        resultEnabled: true,
-        state: 'RequiresIndexDocuments',
-        createdAt: createdAt,
-        updatedAt: createdAt
-      }));
-    })
-    .error(function(error) {
-      var body = createCommonErrorResponse('InternalFailure', error.message);
-      response.contentType('application/xml');
-      return response.send(body, 400);
+  var columnType = Translator.translateIndexFieldType(fieldType);
+  try {
+    database.commandSync('column_create', {
+      table: domain.tableName,
+      name: field.columnName,
+      flags: Database.COLUMN_SCALAR,
+      type: columnType
     });
+
+    if (fieldType == 'text') {
+      database.commandSync('column_create', {
+        table: domain.termsTableName,
+        name: field.indexColumnName,
+        flags: Database.INDEX_COLUMN_DEFAULT_FLAGS,
+        type: domain.tableName,
+        source: field.columnName
+      });
+    } else if (fieldType == 'uint') {
+      var alterTableName = domain.getAlterTableName(field.columnName);
+      database.commandSync('table_create', {
+        name: alterTableName,
+        flags: Database.TABLE_HASH_KEY,
+        key_type: columnType
+      });
+      database.commandSync('column_create', {
+        table: alterTableName,
+        name: field.indexColumnName,
+        flags: Database.INDEX_COLUMN_DEFAULT_FLAGS,
+        type: domain.tableName,
+        source: field.columnName
+      });
+    }
+
+    response.contentType('application/xml');
+    response.send(createDefineIndexFieldResponse({
+      fieldName: fieldName,
+      fieldType: fieldType,
+      facetEnabled: false,
+      resultEnabled: true,
+      state: 'RequiresIndexDocuments',
+      createdAt: createdAt,
+      updatedAt: createdAt
+    }));
+  } catch(error) {
+    var body = createCommonErrorResponse('InternalFailure', error.message);
+    response.contentType('application/xml');
+    response.send(body, 400);
+  }
 };
 
 function createDeleteIndexFieldResponse(options) {
@@ -249,6 +275,14 @@ handlers.DeleteIndexField = function(database, request, response) {
       table: domain.tableName,
       name: field.columnName
     });
+
+    if (fieldType == 'uint') {
+      var alterTableName = domain.getAlterTableName(field.columnName);
+      database.commandSync('table_remove', {
+        name: alterTableName
+      });
+    }
+
     response.contentType('application/xml');
     response.send(createDeleteIndexFieldResponse({}));
   } catch(error) {

  Modified: lib/domain.js (+3 -0)
===================================================================
--- lib/domain.js    2012-07-06 18:20:17 +0900 (1974bfb)
+++ lib/domain.js    2012-07-06 18:42:06 +0900 (e1a2f65)
@@ -69,6 +69,9 @@ Domain.prototype = {
       this._termsTableName = this.tableName + '_BigramTerms';
     return this._termsTableName;
   },
+  getAlterTableName: function(field) {
+    return this.tableName + '_' + field;
+  },
   getIndexField: function(field) {
     return this.indexFields[field] ||
            (this.indexFields[field] = new IndexField(field, this));

  Modified: test/api-configuration.test.js (+67 -1)
===================================================================
--- test/api-configuration.test.js    2012-07-06 18:20:17 +0900 (2dd710b)
+++ test/api-configuration.test.js    2012-07-06 18:42:06 +0900 (b973f82)
@@ -144,13 +144,14 @@ suite('Configuration API', function() {
       });
   });
 
-  test('Get, Action=DefineIndexField', function(done) {
+  test('Get, Action=DefineIndexField (text)', function(done) {
     var path = '/?DomainName=companies&Action=CreateDomain&Version=2011-02-01';
     utils.get(path, {
                 'Host': 'cloudsearch.localhost'
               })
       .next(function(response) {
         var path = '/?DomainName=companies&IndexField.IndexFieldName=name&' +
+                   'IndexField.IndexFieldType=text&' +
                    'Action=DefineIndexField&Version=2011-02-01';
         return utils.get(path);
       })
@@ -210,6 +211,71 @@ suite('Configuration API', function() {
       });
   });
 
+  test('Get, Action=DefineIndexField (uint)', function(done) {
+    var path = '/?DomainName=companies&Action=CreateDomain&Version=2011-02-01';
+    utils.get(path, {
+                'Host': 'cloudsearch.localhost'
+              })
+      .next(function(response) {
+        var path = '/?DomainName=companies&IndexField.IndexFieldName=age&' +
+                   'IndexField.IndexFieldType=text&' +
+                   'Action=DefineIndexField&Version=2011-02-01';
+        return utils.get(path);
+      })
+      .next(function(response) {
+        var expected = {
+              statusCode: 200,
+              body: '<?xml version="1.0"?>\n' +
+                    '<DefineIndexFieldResponse xmlns="' + XMLNS + '">' +
+                      '<DefineIndexFieldResult>' +
+                        '<IndexField>' +
+                          '<Options>' +
+                            '<IndexFieldName>age</IndexFieldName>' +
+                            '<IndexFieldType>uint</IndexFieldType>' +
+                            '<UIntOptions>' +
+                              '<DefaultValue/>' +
+                            '</UIntOptions>' +
+                          '</Options>' +
+                          '<Status>' +
+                            '<CreationDate>1970-01-01T00:00:00Z</CreationDate>' +
+                            '<State>RequiresIndexDocuments</State>' +
+                            '<UpdateDate>1970-01-01T00:00:00Z</UpdateDate>' +
+                            '<UpdateVersion>0</UpdateVersion>' +
+                          '</Status>' +
+                        '</IndexField>' +
+                      '</DefineIndexFieldResult>' +
+                      '<ResponseMetadata>' +
+                        '<RequestId></RequestId>' +
+                      '</ResponseMetadata>' +
+                    '</DefineIndexFieldResponse>'
+            };
+        var actual = {
+              statusCode: response.statusCode,
+              body: response.body
+                      .replace(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/g,
+                               '1970-01-01T00:00:00Z')
+            };
+        assert.deepEqual(actual, expected);
+
+        var dump = database.commandSync('dump', {
+              tables: 'companies'
+            });
+        var expected = 'table_create companies TABLE_HASH_KEY ShortText\n' +
+                       'column_create companies age COLUMN_SCALAR UInt32\n' +
+                       'table_create companies_BigramTerms ' +
+                         'TABLE_PAT_KEY|KEY_NORMALIZE ShortText ' +
+                         '--default_tokenizer TokenBigram\n' +
+                       'column_create companies_BigramTerms companies_age ' +
+                         'COLUMN_INDEX|WITH_POSITION companies age';
+        assert.equal(dump, expected);
+
+        done();
+      })
+      .error(function(error) {
+        done(error);
+      });
+  });
+
   test('Get, Action=DeleteIndexField', function(done) {
     var path = '/?DomainName=companies&Action=CreateDomain&Version=2011-02-01';
     utils.get(path, {
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
Download 



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