null+****@clear*****
null+****@clear*****
2012年 7月 10日 (火) 17:27:13 JST
Yoji SHIDARA 2012-07-10 17:27:13 +0900 (Tue, 10 Jul 2012) New Revision: eca87aef89407cc7345875ed9356b119a2ada515 https://github.com/groonga/gcs/commit/eca87aef89407cc7345875ed9356b119a2ada515 Log: Partially implement UpdateSynonymOptions Refs. #35 The response is not correct so far. Modified files: lib/api/2011-02-01/configuration.js lib/database.js lib/domain.js test/api-configuration.test.js Modified: lib/api/2011-02-01/configuration.js (+42 -0) =================================================================== --- lib/api/2011-02-01/configuration.js 2012-07-10 12:47:06 +0900 (dd061ff) +++ lib/api/2011-02-01/configuration.js 2012-07-10 17:27:13 +0900 (8129ab0) @@ -385,6 +385,48 @@ handlers.IndexDocuments = function(database, request, response) { } }; +handlers.UpdateSynonymOptions = function(database, request, response) { + var domain = new Domain(request); + try { + var synonymsJson = request.query.Synonyms; + var synonyms = JSON.parse(synonymsJson).synonyms; + + try { + database.commandSync('table_remove', { + table: domain.synonymTableName + }); + } catch(error) { + // The synonym table should be inexistent. Do nothing. + } + + database.commandSync('table_create', { + name: domain.synonymTableName, + flags: Database.TABLE_HASH_KEY, + key_type: Database.ShortText + }); + database.commandSync('column_create', { + table: domain.synonymTableName, + name: 'synonyms', + type: Database.ShortText, + flags: Database.COLUMN_VECTOR + }); + + var load = Object.keys(synonyms).map(function(key) { + return {_key: key, synonyms: synonyms[key]}; + }); + database.commandSync('load', { + table: domain.synonymTableName, + values: JSON.stringify(load) + }); + + response.send("OK", 200); // FIXME the response + } catch(error) { + var body = createCommonErrorResponse('InternalFailure', error.message); + response.contentType('application/xml'); + response.send(body, 400); + } +}; + exports.createHandler = function(database) { return function(request, response, next) { var message, body; Modified: lib/database.js (+3 -0) =================================================================== --- lib/database.js 2012-07-10 12:47:06 +0900 (3b589cf) +++ lib/database.js 2012-07-10 17:27:13 +0900 (2ab3fe4) @@ -26,6 +26,9 @@ var COLUMN_SCALAR = var COLUMN_INDEX = exports.COLUMN_INDEX = Database.COLUMN_INDEX = 'COLUMN_INDEX'; +var COLUMN_VECTOR = + exports.COLUMN_VECTOR = + Database.COLUMN_VECTOR = 'COLUMN_VECTOR'; var WITH_POSITION = exports.WITH_POSITION = Database.WITH_POSITION = 'WITH_POSITION'; Modified: lib/domain.js (+5 -0) =================================================================== --- lib/domain.js 2012-07-10 12:47:06 +0900 (adc3a64) +++ lib/domain.js 2012-07-10 17:27:13 +0900 (c778ed6) @@ -73,6 +73,11 @@ Domain.prototype = { getIndexField: function(field) { return this.indexFields[field] || (this.indexFields[field] = new IndexField(field, this)); + }, + get synonymTableName() { + if (!this._synonymTableName) + this._synonymTableName = this.tableName + '_synonyms'; + return this._synonymTableName; } }; Modified: test/api-configuration.test.js (+38 -18) =================================================================== --- test/api-configuration.test.js 2012-07-10 12:47:06 +0900 (6b1204d) +++ test/api-configuration.test.js 2012-07-10 17:27:13 +0900 (d397d99) @@ -551,19 +551,38 @@ suite('Configuration API', function() { }); }); - test('Get, no version', function(done) { - var path = '/?Action=unknown'; + test('Get, Action=UpdateSynonymOptions', function(done) { + var synonymsObject = { + synonyms: { + tokio: ["tokyo"], + dekkaido: ["hokkaido"] + } + }; + var json = JSON.stringify(synonymsObject); + var synonyms = encodeURIComponent(json); + var path = '/?Version=2011-02-01&Action=UpdateSynonymOptions&DomainName=companies&Synonyms='+synonyms; utils.get(path, { 'Host': 'cloudsearch.localhost' }) .next(function(response) { - var message = 'An input parameter "Version" that is mandatory for ' + - 'processing the request is not supplied.';; - var expected = { - statusCode: 400, - body: createCommonErrorResponse('MissingParameter', message) - }; - assert.deepEqual(response, expected); + assert.equal(response.statusCode, 200); + var responseExpected = "OK"; // FIXME; + + assert.equal(response.body, responseExpected); + var dumpExpected = + 'table_create companies_synonyms TABLE_HASH_KEY ShortText\n' + + 'column_create companies_synonyms synonyms COLUMN_VECTOR ShortText\n' + + 'load --table companies_synonyms\n' + + '[\n' + + '["_key","synonyms"],\n' + + '["tokio",["tokyo"]],\n' + + '["dekkaido",["hokkaido"]]\n' + + ']'; + var dumpActual = database.commandSync('dump', { + tables: 'companies_synonyms' + }); + assert.equal(dumpExpected, dumpActual); + done(); }) .error(function(error) { @@ -571,17 +590,17 @@ suite('Configuration API', function() { }); }); - test('Get, invalid version', function(done) { - var path = '/?Version=2011-02-02&Action=unknown'; + test('Get, no version', function(done) { + var path = '/?Action=unknown'; utils.get(path, { 'Host': 'cloudsearch.localhost' }) .next(function(response) { - var message = 'A bad or out-of-range value "2011-02-02" was supplied ' + - 'for the "Version" input parameter.'; + var message = 'An input parameter "Version" that is mandatory for ' + + 'processing the request is not supplied.';; var expected = { statusCode: 400, - body: createCommonErrorResponse('InvalidParameterValue', message) + body: createCommonErrorResponse('MissingParameter', message) }; assert.deepEqual(response, expected); done(); @@ -591,16 +610,17 @@ suite('Configuration API', function() { }); }); - test('Get, invalid action', function(done) { - var path = '/?Version=2011-02-01&Action=unknown'; + test('Get, invalid version', function(done) { + var path = '/?Version=2011-02-02&Action=unknown'; utils.get(path, { 'Host': 'cloudsearch.localhost' }) .next(function(response) { - var message = 'The action unknown is not valid for this web service.'; + var message = 'A bad or out-of-range value "2011-02-02" was supplied ' + + 'for the "Version" input parameter.'; var expected = { statusCode: 400, - body: createCommonErrorResponse('InvalidAction', message) + body: createCommonErrorResponse('InvalidParameterValue', message) }; assert.deepEqual(response, expected); done(); -------------- next part -------------- HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B... 下载