[Groonga-commit] groonga/gcs [master] Load batchs via Domain#addRecordSync and Domain#deleteRecordSync

Back to archive index

SHIMODA Hiroshi null+****@clear*****
Mon Aug 13 14:35:05 JST 2012


SHIMODA Hiroshi	2012-08-13 14:35:05 +0900 (Mon, 13 Aug 2012)

  New Revision: 3baf26246c4c4e16ed0909f49d91bfa9fde69115
  https://github.com/groonga/gcs/commit/3baf26246c4c4e16ed0909f49d91bfa9fde69115

  Log:
    Load batchs via Domain#addRecordSync and Domain#deleteRecordSync

  Modified files:
    lib/batch/processor.js

  Modified: lib/batch/processor.js (+33 -43)
===================================================================
--- lib/batch/processor.js    2012-08-13 13:55:51 +0900 (50c6d53)
+++ lib/batch/processor.js    2012-08-13 14:35:05 +0900 (0fdefb7)
@@ -1,5 +1,4 @@
 var nroonga = require('../wrapped-nroonga');
-var translator = require('./translator');
 var Domain = require('../database').Domain;
 var Deferred = require('jsdeferred').Deferred;
 
@@ -17,7 +16,6 @@ Processor.prototype = {
     if (!this.context && !this.databasePath)
       throw new Error('no database path');
     this.context = new nroonga.Context(this.context || this.databasePath);
-    this.translator = new translator.Translator(this.domain, this.context);
   },
   validate: function(batches) {
     var fields = this.domain.indexFields;
@@ -72,61 +70,53 @@ Processor.prototype = {
     return errors;
   },
   load: function(batches) {
-    var commandSets = this.translator.translate(batches);
-    var result = {
-          adds: 0,
-          deletes: 0,
-          errors: [],
-          warnings: []
-        };
+    var result = this.createNewResult();
     var self = this;
-    return Deferred.loop(commandSets.length, function(i) {
-             var commandSet = commandSets[i];
-             return self.context.commandDeferred(commandSet.command,
-                                                  commandSet.options)
-                        .next(function(commandResult) {
-                          switch (commandSet.command) {
-                            case 'load':
-                              result.adds++;
-                              break;
-                            case 'delete':
-                              result.deletes++;
-                              break;
-                          }
-                        })
-                        .error(function(error) {
-                          result.errors.push(error);
-                        });
+    return Deferred.loop(batches.length, function(i) {
+             self.loadOneBatchSync(batches[i], result);
+           })
+           .error(function(error) {
+             result.errors.push(error);
            })
            .next(function() {
              return self.formatResult(result);
            });
   },
   loadSync: function(batches) {
-    var commandSets = this.translator.translate(batches);
-    var result = {
-          adds: 0,
-          deletes: 0,
-          errors: [],
-          warnings: []
-        };
-    commandSets.forEach(function(commandSet) {
+    var result = this.createNewResult();
+    batches.forEach(function(batch) {
       try {
-        this.context.commandSync(commandSet.command, commandSet.options);
-        switch (commandSet.command) {
-          case 'load':
-            result.adds++;
-            break;
-          case 'delete':
-            result.deletes++;
-            break;
-        }
+        this.loadOneBatchSync(batch, result);
       } catch(error) {
         result.errors.push(error);
       }
     }, this);
     return this.formatResult(result);
   },
+  loadOneBatchSync: function(batch, results) {
+    switch (batch.type) {
+      case 'add':
+        var record = { id: batch.id };
+        Object.keys(batch.fields).forEach(function(key) {
+          record[key] = batch.fields[key];
+        });
+        this.domain.addRecordSync(record);
+        results.adds++;
+        break;
+      case 'delete':
+        this.domain.deleteRecordSync(batch.id);
+        results.deletes++;
+        break;
+    }
+  },
+  createNewResult: function() {
+    return {
+      adds: 0,
+      deletes: 0,
+      errors: [],
+      warnings: []
+    };
+  },
   formatResult: function(result) {
     var formattedResult = {
           status: 'success',
-------------- next part --------------
HTML����������������������������...
다운로드 



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