YUKI Hiroshi
null+****@clear*****
Mon Dec 17 18:41:52 JST 2012
YUKI Hiroshi 2012-12-17 18:41:52 +0900 (Mon, 17 Dec 2012) New Revision: 02df137705be0f0ef28832b59723becc50624e7b https://github.com/groonga/gcs/commit/02df137705be0f0ef28832b59723becc50624e7b Log: Commonize operations to read SDF batches Modified files: bin/gcs-post-sdf lib/client.js tools/scenario-runner.js Modified: bin/gcs-post-sdf (+2 -14) =================================================================== --- bin/gcs-post-sdf 2012-12-17 18:33:07 +0900 (e9fa4eb) +++ bin/gcs-post-sdf 2012-12-17 18:41:52 +0900 (b5ffc35) @@ -2,8 +2,6 @@ var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface; var Client = require(__dirname + '/../lib/client').Client; -var xml = require(__dirname + '/../lib/batch/xml'); -var fs = require('fs'); var path = require('path'); var commandLine = new CLI(); @@ -36,24 +34,14 @@ client.assertDomainExists(function() { if (!path.existsSync(sourceFile)) client.raiseFatalError('No such file'); - var format = sourceFile.match(/\.(xml|json)$/i); - if (!format) - client.raiseFatalError('Unknown format'); - - format = format[1].toLowerCase(); + var format = client.detectFileFormat(sourceFile); console.log('Detected source format for %s as %s', path.basename(sourceFile), format) try { - var batches = fs.readFileSync(sourceFile, 'UTF-8'); - if (format == 'xml') - batches = xml.toJSON(batches); - else - batches = JSON.parse(batches); - client.setupAPI('doc', function(documentsAPI) { documentsAPI.DocumentsBatch( { - Docs: batches + Docs: client.readSDFBatch(sourceFile, format) }, function(error, response) { if (error) Modified: lib/client.js (+20 -0) =================================================================== --- lib/client.js 2012-12-17 18:33:07 +0900 (03c51bd) +++ lib/client.js 2012-12-17 18:41:52 +0900 (e697d29) @@ -3,6 +3,8 @@ var amazon = awssum.load('amazon/amazon'); var CloudSearch = awssum.load('amazon/cloudsearch').CloudSearch; var DocumentService = awssum.load('amazon/cloudsearch').DocumentService; var SearchService = awssum.load('amazon/cloudsearch').SearchService; +var xml = require('./batch/xml'); +var fs = require('fs'); var OPTIONAL_PARAM = { required : false, type : 'param' }; @@ -350,6 +352,24 @@ Client.prototype = { arnToEndpoint: function(arn) { return arnToEndpoint(arn, this.host + ':' + this.port); + }, + + detectFileFormat: function(path) { + var format = path.match(/\.([^\.]+)$/i); + if (!format) + this.raiseFatalError('Unknown format'); + + format = format[1].toLowerCase(); + return format; + }, + readSDFBatch: function(path, format) { + format = format || this.detectFileFormat(path); + var batches = fs.readFileSync(path, 'UTF-8'); + if (format == 'xml') + batches = xml.toJSON(batches); + else + batches = JSON.parse(batches); + return batches; } }; Modified: tools/scenario-runner.js (+1 -8) =================================================================== --- tools/scenario-runner.js 2012-12-17 18:33:07 +0900 (66b5317) +++ tools/scenario-runner.js 2012-12-17 18:41:52 +0900 (9efd76f) @@ -1,8 +1,5 @@ var Client = require(__dirname + '/../lib/client').Client; var EventEmitter = require('events').EventEmitter; -var xml = require(__dirname + '/../lib/batch/xml'); -var fs = require('fs'); -var path = require('path'); var xml2js = require('xml2js'); var xml2jsConfig = JSON.parse(JSON.stringify(xml2js.defaults['0.1'])); xml2jsConfig.explicitRoot = true; @@ -291,11 +288,7 @@ ScenarioRunner.prototype._process = function(scenario, callback) { if (typeof batches == 'string') { var path = request.body; path = path.replace(/%ROOT%/gim, __dirname + '/../'); - batches = fs.readFileSync(path, 'UTF-8'); - if (format == 'xml') - batches = xml.toJSON(batches); - else - batches = JSON.parse(batches); + batches = this.client.readSDFBatch(path); } return this.client.DocumentsBatch({ Docs: batches }, requestCallback); -------------- next part -------------- HTML����������������������������... Descargar