[Groonga-commit] groonga/gcs [master] Update processor implementation for delete type batches

Back to archive index

null+****@clear***** null+****@clear*****
2012年 7月 6日 (金) 16:08:51 JST


SHIMODA Hiroshi	2012-07-06 16:08:51 +0900 (Fri, 06 Jul 2012)

  New Revision: 80d417a8d9819fbff77065a50bf07a0dda1dc488
  https://github.com/groonga/gcs/commit/80d417a8d9819fbff77065a50bf07a0dda1dc488

  Log:
    Update processor implementation for delete type batches

  Added files:
    test/fixture/companies/data-deleted.grn
    test/fixture/companies/delete.grn
  Modified files:
    lib/batch/processor.js
    test/batch-processor.test.js

  Modified: lib/batch/processor.js (+37 -31)
===================================================================
--- lib/batch/processor.js    2012-07-06 15:57:02 +0900 (30105a6)
+++ lib/batch/processor.js    2012-07-06 16:08:51 +0900 (181dec2)
@@ -32,38 +32,9 @@ Processor.prototype = {
     var columns = this.getColumns();
     var errors = [];
     batches.forEach(function(batch) {
-      if (batch.type != 'add')
-        return;
-
-      if (!batch.fields) {
-        errors.push(batch.id + ': You must specify "fields".');
-        return;
-      }
-
-      var column;
-      var fieldsCount = 0;
-      for (var field in batch.fields) {
-        if (!batch.fields.hasOwnProperty(field))
-          continue;
-
-        fieldsCount++;
-
-        try {
-          column = this.domain.getIndexField(field).columnName;
-        } catch(error) {
-          errors.push(batch.id + ': ' + error.message);
-          continue;
-        }
-
-        if (columns.indexOf(column) < 0)
-          errors.push(batch.id + ': The field "' + field + '" is unknown.');
-
-        if (batch.fields[field] === null)
-          errors.push(batch.id + ': The field "' + field + '" is null.');
+      if (batch.type == 'add') {
+        errors = errors.concat(this.validateAddBatch(batch));
       }
-
-      if (!fieldsCount)
-        errors.push(batch.id + ': You must specify one or more fields to "fields".');
     }, this);
     if (errors.length) {
       var error = new Error(exports.INVALID_BATCH);
@@ -72,6 +43,38 @@ Processor.prototype = {
       throw error;
     }
   },
+  validateAddBatch: function(batch) {
+    if (!batch.fields) {
+      return [batch.id + ': You must specify "fields".'];
+
+    var column;
+    var fieldsCount = 0;
+    var errors = [];
+    for (var field in batch.fields) {
+      if (!batch.fields.hasOwnProperty(field))
+        continue;
+
+      fieldsCount++;
+
+      try {
+        column = this.domain.getIndexField(field).columnName;
+      } catch(error) {
+         errors.push(batch.id + ': ' + error.message);
+        continue;
+      }
+
+      if (columns.indexOf(column) < 0)
+        errors.push(batch.id + ': The field "' + field + '" is unknown.');
+
+      if (batch.fields[field] === null)
+        errors.push(batch.id + ': The field "' + field + '" is null.');
+    }
+
+    if (!fieldsCount)
+      errors.push(batch.id + ': You must specify one or more fields to "fields".');
+
+    return errors;
+  },
   load: function(batches) {
     var commandSets = this.translator.translate(batches);
     var result = {
@@ -90,6 +93,9 @@ Processor.prototype = {
                             case 'load':
                               result.adds++;
                               break;
+                            case 'delete':
+                              result.deletes++;
+                              break;
                           }
                         })
                         .error(function(error) {

  Modified: test/batch-processor.test.js (+31 -1)
===================================================================
--- test/batch-processor.test.js    2012-07-06 15:57:02 +0900 (c5a42da)
+++ test/batch-processor.test.js    2012-07-06 16:08:51 +0900 (f201476)
@@ -8,6 +8,8 @@ var Processor = require('../lib/batch/processor').Processor;
 
 var schemeDump = fs.readFileSync(__dirname + '/fixture/companies/ddl.grn', 'UTF-8').replace(/\s+$/, '');
 var loadDump = fs.readFileSync(__dirname + '/fixture/companies/data.grn', 'UTF-8').replace(/\s+$/, '');
+var deletedLoadDump = fs.readFileSync(__dirname + '/fixture/companies/data-deleted.grn', 'UTF-8').replace(/\s+$/, '');
+var deleteDump = fs.readFileSync(__dirname + '/fixture/companies/delete.grn', 'UTF-8').replace(/\s+$/, '');
 
 var temporaryDatabase;
 
@@ -72,9 +74,37 @@ suite('batch/processor/Processor (instance methods)', function() {
       });
   });
 
-  test('validation, valid batches', function() {
+  test('load delete-batches', function(done) {
     var batches = fs.readFileSync(__dirname + '/fixture/companies/add.sdf.json', 'UTF-8');
     batches = JSON.parse(batches);
+    processor.load(batches)
+      .next(function(result) {
+        var batches = fs.readFileSync(__dirname + '/fixture/companies/delete.sdf.json', 'UTF-8');
+        batches = JSON.parse(batches);
+        return processor.load(batches)
+      })
+      .next(function(result) {
+        var expected = {
+              status: 'success',
+              adds: 0,
+              deletes: 1
+            };
+        assert.deepEqual(result, expected);
+        var dump = database.commandSync('dump', {
+              tables: 'companies'
+            });
+        assert.equal(dump, schemeDump + '\n' + deletedLoadDump);
+        done();
+      })
+      .error(function(error) {
+        done(error);
+      });
+  });
+
+  test('validation, valid batches', function() {
+    var addBatches = fs.readFileSync(__dirname + '/fixture/companies/add.sdf.json', 'UTF-8');
+    var deleteBatches = fs.readFileSync(__dirname + '/fixture/companies/delete.sdf.json', 'UTF-8');
+    var batches = JSON.parse(addBatches).concat(JSON.parse(deleteBatches));
     // assert.notThrow(function() {
       processor.validate(batches);
     // }, Processor.INVALID_BATCH);

  Added: test/fixture/companies/data-deleted.grn (+13 -0) 100644
===================================================================
--- /dev/null
+++ test/fixture/companies/data-deleted.grn    2012-07-06 16:08:51 +0900 (a561686)
@@ -0,0 +1,13 @@
+load --table companies
+[
+["_key","address","description","email_address","name"],
+["id2","Sapporo, Hokkaido, Japan","","info****@enish*****","Enishi Tech Inc."],
+["id3","Hongo, Tokyo, Japan","","info****@clear*****","ClearCode Inc."],
+["id4","Granada, Moon","","","Anaheim Electronics"],
+["id5","Earth","","","Shinsei Industry"],
+["id6","Detroit and Delta City, Michigan, United States","","","Omni Consumer Products"],
+["id7","West City","","","Capsule Corporation"],
+["id8","United States","","","Stark Industries"],
+["id9","Tokyo, Japan","","","Umbrella Corporation"],
+["id10","New York, United States","","","U.S. Robots and Mechanical Men"]
+]

  Added: test/fixture/companies/delete.grn (+1 -0) 100644
===================================================================
--- /dev/null
+++ test/fixture/companies/delete.grn    2012-07-06 16:08:51 +0900 (1d336f7)
@@ -0,0 +1 @@
+delete --table companies --key id1
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
Download 



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