[Groonga-commit] groonga/gcs [master] Make gcs-post-sdf based on HTTP APIs

Back to archive index

YUKI Hiroshi null+****@clear*****
Wed Oct 17 18:35:57 JST 2012


YUKI Hiroshi	2012-10-17 18:35:57 +0900 (Wed, 17 Oct 2012)

  New Revision: 556bb692cf8cc3b830c99126d28ea109a20060ba
  https://github.com/groonga/gcs/commit/556bb692cf8cc3b830c99126d28ea109a20060ba

  Log:
    Make gcs-post-sdf based on HTTP APIs

  Modified files:
    bin/gcs-post-sdf
    lib/client.js
    test/gcs-commands.test.js

  Modified: bin/gcs-post-sdf (+50 -48)
===================================================================
--- bin/gcs-post-sdf    2012-10-17 18:05:45 +0900 (c7e5c59)
+++ bin/gcs-post-sdf    2012-10-17 18:35:57 +0900 (23aec9c)
@@ -1,11 +1,11 @@
 #!/usr/bin/env node
 
 var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface;
-var commandLine = new CLI();
+var Client = require(__dirname + '/../lib/client').Client;
 var fs = require('fs');
 var path = require('path');
-var BatchProcessor = require(__dirname + '/../lib/batch').Processor;
 
+var commandLine = new CLI();
 commandLine
   .usage('--source <path to SDF file> [options]')
   .option('-s, --source <path to SDF file>',
@@ -16,59 +16,61 @@ commandLine
           String)
   .parse();
 
-commandLine.assertHaveDomainName();
-commandLine.assertDomainExists();
-
-if (!commandLine.options.source) {
-  console.log('You must specify the source SDF.');
-  return process.exit(1);
-}
+var client = new Client(commandLine);
 
-var sourceFile = CLI.resolve(commandLine.options.source);
-console.log('Processing: %s', sourceFile);
+client.assertHaveDomainName();
+client.assertDomainExists(function() {
+  if (!commandLine.options.source)
+    client.raiseFatalError('You must specify the source SDF.');
 
-if (!path.existsSync(sourceFile)) {
-  console.log('No such file');
-  return process.exit(1);
-}
+  var sourceFile = CLI.resolve(commandLine.options.source);
+  console.log('Processing: %s', sourceFile);
 
-var format = sourceFile.match(/\.(xml|json)$/i);
-if (!format) {
-  console.log('Unknown format');
-  return process.exit(1);
-}
+  if (!path.existsSync(sourceFile))
+    client.raiseFatalError('No such file');
 
-format = format[1].toLowerCase();
-console.log('Detected source format for %s as %s', path.basename(sourceFile), format)
+  var format = sourceFile.match(/\.(xml|json)$/i);
+  if (!format) {
+    client.raiseFatalError('Unknown format');
 
-if (format != 'json') {
-  console.log('Unsupported format: %s');
-  return process.exit(1);
-}
+  format = format[1].toLowerCase();
+  console.log('Detected source format for %s as %s', path.basename(sourceFile), format)
 
-try {
-  var batches = fs.readFileSync(sourceFile, 'UTF-8');
-  batches = JSON.parse(batches);
-  var processor = new BatchProcessor({
-        context: commandLine.context,
-        domain: commandLine.domain
-      });
+  if (format != 'json')
+    client.raiseFatalError('Unsupported format: %s');
 
   try {
-    processor.validate(batches);
-  } catch (error) {
-    console.log('Validation failed.');
-    console.log(error.errors.join('\n'));
-    return process.exit(1);
-  }
+    var batches = fs.readFileSync(sourceFile, 'UTF-8');
+    batches = JSON.parse(batches);
 
-  var result = processor.loadSync(batches);
-  console.log('Status: %s', result.status);
-  console.log('Added: %s', result.adds);
-  console.log('Deleted: %s', result.deletes);
+    client.documentsAPI.DocumentsBatch(
+      {
+        args: {
+          Docs: batches
+        }
+      },
+      function(error, response) {
+        if (error)
+          client.raiseFatalError(error);
 
-} catch(error) {
-  console.log('Fatal error!');
-  console.log(error.message + '\n' + error.stack);
-  return process.exit(1);
-}
+        try {
+          var errors = response.Body.errors.map(function(error) { return error.message; });
+          if (errors.length) {
+            console.log('Validation failed.');
+            console.log(errors.join('\n'));
+            return process.exit(1);
+          }
+          console.log('Status: %s', response.Body.status);
+          console.log('Added: %s', response.Body.adds);
+          console.log('Deleted: %s', response.Body.deletes);
+          process.exit(0);
+        } catch(error) {
+          client.raiseFatalError(error);
+        }
+      }
+    );
+  } catch(error) {
+    client.raiseFatalError('Fatal error!\n' +
+                           console.log(error.message + '\n' + error.stack);
+  }
+});

  Modified: lib/client.js (+35 -1)
===================================================================
--- lib/client.js    2012-10-17 18:05:45 +0900 (bc845d6)
+++ lib/client.js    2012-10-17 18:35:57 +0900 (5150312)
@@ -1,6 +1,7 @@
 var awssum = require('awssum');
 var amazon = awssum.load('amazon/amazon');
 var CloudSearch = awssum.load('amazon/cloudsearch').CloudSearch;
+var DocumentService = awssum.load('amazon/cloudsearch').DocumentService;
 
 function Client(options) {
   this.domainName = options.domainName;
@@ -9,7 +10,7 @@ function Client(options) {
 }
 Client.prototype = {
   get configurationAPI() {
-    if (!this._couldSearch) {
+    if (!this._configurationAPI) {
       this._configurationAPI = new CloudSearch({
         accessKeyId: 'dummy-access-key-id',
         secretAccessKey: 'dummy-access-key',
@@ -26,6 +27,39 @@ Client.prototype = {
     return this._configurationAPI;
   },
 
+  setupDocumentsAPI: function(callback) {
+    var self = this;
+    this.getDomainStatus(function(error, domain) {
+      if (error)
+        self.raiseFatalError(error);
+
+      try {
+        var endpoint = domain.DocService.Endpoint.split(':');
+        var host = endpoint[0];
+        var port = endpoint[1] || 80;
+
+        self._documentsAPI = new DocumentService({
+          accessKeyId: 'dummy-access-key-id',
+          secretAccessKey: 'dummy-access-key',
+        });
+        var self = this;
+        self._documentsAPI.host = function() {
+          return host;
+        };
+        self._documentsAPI.addExtras = function(options, args) {
+          options.protocol = self.protocol || 'http';
+          options.port =  port;
+        };
+      } catch(error) {
+        if (error)
+          self.raiseFatalError(error);
+      }
+    });
+  },
+  get documentsAPI() {
+    return this._documentsAPI;
+  },
+
   raiseFatalError: function(error) {
     if (typeof error != 'string')
       error = 'Unexpected error: ' + JSON.stringify(error);

  Modified: test/gcs-commands.test.js (+18 -9)
===================================================================
--- test/gcs-commands.test.js    2012-10-17 18:05:45 +0900 (af1a48d)
+++ test/gcs-commands.test.js    2012-10-17 18:35:57 +0900 (d9af803)
@@ -958,7 +958,8 @@ suite('gcs-post-sdf', function() {
       .run('gcs-post-sdf',
            '--domain-name', 'companies',
            '--source', batchFile,
-           '--database-path', temporaryDatabase.path)
+           '--port', utils.testPort,
+           '--base-host', 'localhost:' + utils.testPort)
       .next(function(result) {
         assert.deepEqual({ code:    result.code,
                            message: result.output.stdout },
@@ -985,11 +986,13 @@ suite('gcs-post-sdf', function() {
       .run('gcs-post-sdf',
            '--domain-name', 'companies',
            '--source', path.join(fixturesDirectory, 'add.sdf.json'),
-           '--database-path', temporaryDatabase.path)
+           '--port', utils.testPort,
+           '--base-host', 'localhost:' + utils.testPort)
       .run('gcs-post-sdf',
            '--domain-name', 'companies',
            '--source', batchFile,
-           '--database-path', temporaryDatabase.path)
+           '--port', utils.testPort,
+           '--base-host', 'localhost:' + utils.testPort)
       .next(function(result) {
         assert.deepEqual({ code:    result.code,
                            message: result.output.stdout },
@@ -1016,7 +1019,8 @@ suite('gcs-post-sdf', function() {
       .run('gcs-post-sdf',
            '--domain-name', 'companies',
            '--source', batchFile,
-           '--database-path', temporaryDatabase.path)
+           '--port', utils.testPort,
+           '--base-host', 'localhost:' + utils.testPort)
       .next(function(result) {
         assert.deepEqual({ code:    result.code,
                            message: result.output.stdout },
@@ -1049,7 +1053,8 @@ suite('gcs-post-sdf', function() {
     utils
       .run('gcs-post-sdf',
            '--domain-name', 'companies',
-           '--database-path', temporaryDatabase.path)
+           '--port', utils.testPort,
+           '--base-host', 'localhost:' + utils.testPort)
       .next(function(result) {
         assert.deepEqual({ code:    result.code,
                            message: result.output.stdout },
@@ -1070,7 +1075,8 @@ suite('gcs-post-sdf', function() {
       .run('gcs-post-sdf',
            '--domain-name', 'companies',
            '--source', batchFile,
-           '--database-path', temporaryDatabase.path)
+           '--port', utils.testPort,
+           '--base-host', 'localhost:' + utils.testPort)
       .next(function(result) {
         assert.deepEqual({ code:    result.code,
                            message: result.output.stdout },
@@ -1092,7 +1098,8 @@ suite('gcs-post-sdf', function() {
       .run('gcs-post-sdf',
            '--domain-name', 'companies',
            '--source', batchFile,
-           '--database-path', temporaryDatabase.path)
+           '--port', utils.testPort,
+           '--base-host', 'localhost:' + utils.testPort)
       .next(function(result) {
         assert.deepEqual({ code:    result.code,
                            message: result.output.stdout },
@@ -1111,7 +1118,8 @@ suite('gcs-post-sdf', function() {
     utils
       .run('gcs-post-sdf',
            '--domain-name', 'test',
-           '--database-path', temporaryDatabase.path)
+           '--port', utils.testPort,
+           '--base-host', 'localhost:' + utils.testPort)
       .next(function(result) {
         assertDomainNotExist(result, 'test');
         done();
@@ -1124,7 +1132,8 @@ suite('gcs-post-sdf', function() {
   test('post without domain', function(done) {
     utils
       .run('gcs-post-sdf',
-           '--database-path', temporaryDatabase.path)
+           '--port', utils.testPort,
+           '--base-host', 'localhost:' + utils.testPort)
       .next(function(result) {
         assertDomainNotSpecified(result);
         done();
-------------- next part --------------
HTML����������������������������...
Download 



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