[Groonga-commit] groonga/gcs [master] Simplify "getIndexFieldsSync()" to "indexFields" (getter)

Back to archive index

null+****@clear***** null+****@clear*****
2012年 7月 12日 (木) 17:23:45 JST


SHIMODA Hiroshi	2012-07-12 17:23:45 +0900 (Thu, 12 Jul 2012)

  New Revision: 5b0035716d14ace1ed45c63d9b7d974ffa313250
  https://github.com/groonga/gcs/commit/5b0035716d14ace1ed45c63d9b7d974ffa313250

  Log:
    Simplify "getIndexFieldsSync()" to "indexFields" (getter)

  Modified files:
    lib/api/2011-02-01/search.js
    lib/batch/processor.js
    lib/database/domain.js
    test/batch-processor.test.js
    test/database-domain.test.js

  Modified: lib/api/2011-02-01/search.js (+1 -1)
===================================================================
--- lib/api/2011-02-01/search.js    2012-07-12 17:14:56 +0900 (2274dfb)
+++ lib/api/2011-02-01/search.js    2012-07-12 17:23:45 +0900 (b575d71)
@@ -45,7 +45,7 @@ exports.createHandler = function(context) {
     var startedAt = new Date();
     var domain = new Domain(request, context);
     var query = request.query.q || '';
-    var matchIndexFields = domain.getIndexFieldsSync(context);
+    var matchIndexFields = domain.indexFields;
     matchIndexFields = matchIndexFields.filter(function(field) {
       return field.type == 'text';
     });

  Modified: lib/batch/processor.js (+1 -10)
===================================================================
--- lib/batch/processor.js    2012-07-12 17:14:56 +0900 (da733c8)
+++ lib/batch/processor.js    2012-07-12 17:23:45 +0900 (861c6b6)
@@ -19,17 +19,8 @@ Processor.prototype = {
     this.context = new nroonga.Context(this.context || this.databasePath);
     this.translator = new translator.Translator(this.domain);
   },
-  getIndexFields: function() {
-    var self = this;
-    if (this._indexFields) {
-      return self._indexFields;
-    } else {
-      var fields = this.domain.getIndexFieldsSync(this.context);
-      return this._indexFields = fields;
-    }
-  },
   validate: function(batches) {
-    var fields = this.getIndexFields();
+    var fields = this.domain.indexFields;
     var errors = [];
     batches.forEach(function(batch) {
       if (batch.type == 'add') {

  Modified: lib/database/domain.js (+4 -2)
===================================================================
--- lib/database/domain.js    2012-07-12 17:14:56 +0900 (a369f60)
+++ lib/database/domain.js    2012-07-12 17:23:45 +0900 (839d7d5)
@@ -97,8 +97,10 @@ Domain.prototype = {
     return this.indexFields[field] ||
            (this.indexFields[field] = new IndexField(field, this));
   },
-  getIndexFieldsSync: function(context) {
-    var columns = context.ordinalColumnsSync(this.tableName);
+  get indexFields() {
+    if (!this.context)
+      throw new Error('no context');
+    var columns = this.context.ordinalColumnsSync(this.tableName);
     var fields = columns.map(this.columnToIndexField, this);
     return fields;
   },

  Modified: test/batch-processor.test.js (+0 -28)
===================================================================
--- test/batch-processor.test.js    2012-07-12 17:14:56 +0900 (5ab84e9)
+++ test/batch-processor.test.js    2012-07-12 17:23:45 +0900 (fadcf29)
@@ -46,34 +46,6 @@ suite('batch/processor/Processor (instance methods)', function() {
     assert.equal(processor.databasePath, temporaryDatabase.path);
   });
 
-  test('getIndexFields', function() {
-    var fields = processor.getIndexFields();
-    fields = fields.map(function(field) {
-      return {
-        name: field.name,
-        type: field.type
-      };
-    });
-    var expected = [
-          { name: 'address',
-            type: 'text'},
-          { name: 'age',
-            type: 'uint'},
-          { name: 'description',
-            type: 'text'},
-          { name: 'email_address',
-            type: 'text'},
-          { name: 'name',
-            type: 'text'},
-          { name: 'product',
-            type: 'literal'}
-        ];
-    function sortFields(a, b) {
-      return a.name - b.name;
-    }
-    assert.deepEqual(fields.sort(sortFields), expected.sort(sortFields));
-  });
-
   test('load add-batches', function(done) {
     var batches = fs.readFileSync(__dirname + '/fixture/companies/add.sdf.json', 'UTF-8');
     batches = JSON.parse(batches);

  Modified: test/database-domain.test.js (+45 -1)
===================================================================
--- test/database-domain.test.js    2012-07-12 17:14:56 +0900 (e13e01e)
+++ test/database-domain.test.js    2012-07-12 17:23:45 +0900 (84cacfb)
@@ -4,7 +4,7 @@ var assert = require('chai').assert;
 
 var Domain = require('../lib/database/domain').Domain;
 
-suite('domain', function() {
+suite('database', function() {
   suite('Domain', function() {
     test('lower case', function() {
       var domain = new Domain('valid');
@@ -175,5 +175,49 @@ suite('domain', function() {
         assert.equal(domain.name, 'test890');
       });
     });
+
+    suite('getting data from database', function() {
+      var temporaryDatabase;
+      var context;
+
+      setup(function() {
+        temporaryDatabase = utils.createTemporaryDatabase();
+        context = temporaryDatabase.get();
+        utils.loadDumpFile(context, __dirname + '/fixture/companies/ddl.grn');
+      });
+
+      teardown(function() {
+        temporaryDatabase.teardown();
+        temporaryDatabase = undefined;
+      });
+
+      test('indexFields', function() {
+        var fields = domain.indexFields;
+        fields = fields.map(function(field) {
+          return {
+            name: field.name,
+            type: field.type
+          };
+        });
+        var expected = [
+              { name: 'address',
+                type: 'text'},
+              { name: 'age',
+                type: 'uint'},
+              { name: 'description',
+                type: 'text'},
+              { name: 'email_address',
+                type: 'text'},
+              { name: 'name',
+                type: 'text'},
+              { name: 'product',
+                type: 'literal'}
+            ];
+        function sortFields(a, b) {
+          return a.name - b.name;
+        }
+        assert.deepEqual(fields.sort(sortFields), expected.sort(sortFields));
+      });
+    });
   });
 });
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
Download 



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