tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jorg...@apache.org
Subject [26/45] tinkerpop git commit: Support bulk
Date Thu, 25 Jan 2018 11:01:11 GMT
Support bulk


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/53a7f279
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/53a7f279
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/53a7f279

Branch: refs/heads/tp32
Commit: 53a7f2798209de4c2a8449d3676ca00785360858
Parents: 15c8679
Author: Jorge Bay Gondra <jorgebaygondra@gmail.com>
Authored: Mon Nov 27 12:13:28 2017 +0100
Committer: Jorge Bay Gondra <jorgebaygondra@gmail.com>
Committed: Fri Jan 19 09:30:16 2018 +0100

----------------------------------------------------------------------
 gremlin-javascript/glv/TraversalSource.template | 48 +++++++------
 .../gremlin-javascript/lib/process/traversal.js | 48 +++++++------
 .../test/unit/traversal-test.js                 | 71 ++++++++++++++++++--
 3 files changed, 119 insertions(+), 48 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/53a7f279/gremlin-javascript/glv/TraversalSource.template
----------------------------------------------------------------------
diff --git a/gremlin-javascript/glv/TraversalSource.template b/gremlin-javascript/glv/TraversalSource.template
index dbb7bc4..f75765f 100644
--- a/gremlin-javascript/glv/TraversalSource.template
+++ b/gremlin-javascript/glv/TraversalSource.template
@@ -22,15 +22,15 @@
  */
 'use strict';
 
-var utils = require('../utils');
-var parseArgs = utils.parseArgs;
-var itemDone = Object.freeze({ value: null, done: true });
-var emptyArray = Object.freeze([]);
+const utils = require('../utils');
+const parseArgs = utils.parseArgs;
+const itemDone = Object.freeze({ value: null, done: true });
 
 function Traversal(graph, traversalStrategies, bytecode) {
   this.graph = graph;
   this.traversalStrategies = traversalStrategies;
   this.bytecode = bytecode;
+  /** @type {Array<Traverser>} */
   this.traversers = null;
   this.sideEffects = null;
   this._traversalStrategiesPromise = null;
@@ -47,17 +47,13 @@ Traversal.prototype.getBytecode = function () {
  * @returns {Promise.<Array>}
  */
 Traversal.prototype.toList = function () {
-  var self = this;
-  return this._applyStrategies().then(function () {
-    if (!self.traversers || self._traversersIteratorIndex === self.traversers.length) {
-      return emptyArray;
+  return this._applyStrategies().then(() => {
+    const result = [];
+    let it;
+    while ((it = this._getNext()) && !it.done) {
+      result.push(it.value);
     }
-    var arr = new Array(self.traversers.length - self._traversersIteratorIndex);
-    for (var i = self._traversersIteratorIndex; i < self.traversers.length; i++) {
-      arr[i] = self.traversers[i].object;
-    }
-    self._traversersIteratorIndex = self.traversers.length;
-    return arr;
+    return result;
   });
 };
 
@@ -67,13 +63,23 @@ Traversal.prototype.toList = function () {
  * @returns {Promise.<{value, done}>}
  */
 Traversal.prototype.next = function () {
-  var self = this;
-  return this._applyStrategies().then(function () {
-    if (!self.traversers || self._traversersIteratorIndex === self.traversers.length) {
-      return itemDone;
+  return this._applyStrategies().then(() => this._getNext());
+};
+
+/**
+ * Synchronous iterator of traversers including
+ * @private
+ */
+Traversal.prototype._getNext = function () {
+  while (this.traversers && this._traversersIteratorIndex < this.traversers.length)
{
+    let traverser = this.traversers[this._traversersIteratorIndex];
+    if (traverser.bulk > 0) {
+      traverser.bulk--;
+      return { value: traverser.object, done: false };
     }
-    return { value: self.traversers[self._traversersIteratorIndex++].object, done: false
};
-  });
+    this._traversersIteratorIndex++;
+  }
+  return itemDone;
 };
 
 Traversal.prototype._applyStrategies = function () {
@@ -133,7 +139,7 @@ P.prototype.or = function (arg) {
 
 function Traverser(object, bulk) {
   this.object = object;
-  this.bulk = bulk == undefined ? 1 : bulk;
+  this.bulk = bulk || 1;
 }
 
 function TraversalSideEffects() {

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/53a7f279/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/traversal.js
----------------------------------------------------------------------
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/traversal.js
b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/traversal.js
index 49c6aef..ebf8755 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/traversal.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/traversal.js
@@ -22,15 +22,15 @@
  */
 'use strict';
 
-var utils = require('../utils');
-var parseArgs = utils.parseArgs;
-var itemDone = Object.freeze({ value: null, done: true });
-var emptyArray = Object.freeze([]);
+const utils = require('../utils');
+const parseArgs = utils.parseArgs;
+const itemDone = Object.freeze({ value: null, done: true });
 
 function Traversal(graph, traversalStrategies, bytecode) {
   this.graph = graph;
   this.traversalStrategies = traversalStrategies;
   this.bytecode = bytecode;
+  /** @type {Array<Traverser>} */
   this.traversers = null;
   this.sideEffects = null;
   this._traversalStrategiesPromise = null;
@@ -47,17 +47,13 @@ Traversal.prototype.getBytecode = function () {
  * @returns {Promise.<Array>}
  */
 Traversal.prototype.toList = function () {
-  var self = this;
-  return this._applyStrategies().then(function () {
-    if (!self.traversers || self._traversersIteratorIndex === self.traversers.length) {
-      return emptyArray;
+  return this._applyStrategies().then(() => {
+    const result = [];
+    let it;
+    while ((it = this._getNext()) && !it.done) {
+      result.push(it.value);
     }
-    var arr = new Array(self.traversers.length - self._traversersIteratorIndex);
-    for (var i = self._traversersIteratorIndex; i < self.traversers.length; i++) {
-      arr[i] = self.traversers[i].object;
-    }
-    self._traversersIteratorIndex = self.traversers.length;
-    return arr;
+    return result;
   });
 };
 
@@ -67,13 +63,23 @@ Traversal.prototype.toList = function () {
  * @returns {Promise.<{value, done}>}
  */
 Traversal.prototype.next = function () {
-  var self = this;
-  return this._applyStrategies().then(function () {
-    if (!self.traversers || self._traversersIteratorIndex === self.traversers.length) {
-      return itemDone;
+  return this._applyStrategies().then(() => this._getNext());
+};
+
+/**
+ * Synchronous iterator of traversers including
+ * @private
+ */
+Traversal.prototype._getNext = function () {
+  while (this.traversers && this._traversersIteratorIndex < this.traversers.length)
{
+    let traverser = this.traversers[this._traversersIteratorIndex];
+    if (traverser.bulk > 0) {
+      traverser.bulk--;
+      return { value: traverser.object, done: false };
     }
-    return { value: self.traversers[self._traversersIteratorIndex++].object, done: false
};
-  });
+    this._traversersIteratorIndex++;
+  }
+  return itemDone;
 };
 
 Traversal.prototype._applyStrategies = function () {
@@ -193,7 +199,7 @@ P.prototype.or = function (arg) {
 
 function Traverser(object, bulk) {
   this.object = object;
-  this.bulk = bulk == undefined ? 1 : bulk;
+  this.bulk = bulk || 1;
 }
 
 function TraversalSideEffects() {

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/53a7f279/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/traversal-test.js
----------------------------------------------------------------------
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/traversal-test.js
b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/traversal-test.js
index c5271c9..635c7e8 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/traversal-test.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/traversal-test.js
@@ -22,14 +22,17 @@
  */
 'use strict';
 
-var assert = require('assert');
-var graph = require('../../lib/structure/graph');
-var utils = require('../../lib/utils');
-var t = require('../../lib/process/traversal');
-var TraversalStrategies = require('../../lib/process/traversal-strategy').TraversalStrategies;
+const assert = require('assert');
+const expect = require('chai').expect;
+const graph = require('../../lib/structure/graph');
+const utils = require('../../lib/utils');
+const t = require('../../lib/process/traversal');
+const TraversalStrategies = require('../../lib/process/traversal-strategy').TraversalStrategies;
 
 describe('Traversal', function () {
+
   describe('#getByteCode()', function () {
+
     it('should add steps for with a string parameter', function () {
       var g = new graph.Graph().traversal();
       var bytecode = g.V().out('created').getBytecode();
@@ -40,6 +43,7 @@ describe('Traversal', function () {
       assert.strictEqual(bytecode.stepInstructions[1][0], 'out');
       assert.strictEqual(bytecode.stepInstructions[1][1], 'created');
     });
+
     it('should add steps with an enum value', function () {
       var g = new graph.Graph().traversal();
       var bytecode = g.V().order().by('age', t.order.decr).getBytecode();
@@ -55,7 +59,9 @@ describe('Traversal', function () {
       assert.strictEqual(bytecode.stepInstructions[2][2].elementName, 'decr');
     });
   });
+
   describe('#next()', function () {
+
     it('should apply the strategies and return a Promise with the iterator item', function
() {
       var strategyMock = {
         apply: function (traversal) {
@@ -83,8 +89,43 @@ describe('Traversal', function () {
           return traversal.next();
         });
     });
+
+    it('should support bulk', function () {
+      const strategyMock = {
+        apply: function (traversal) {
+          traversal.traversers = [ new t.Traverser(1, 2), new t.Traverser(2, 1) ];
+          return utils.resolvedPromise();
+        }
+      };
+      const strategies = new TraversalStrategies();
+      strategies.addStrategy(strategyMock);
+      const traversal = new t.Traversal(null, strategies, null);
+      return traversal.next()
+        .then(function (item) {
+          assert.strictEqual(item.value, 1);
+          assert.strictEqual(item.done, false);
+          return traversal.next();
+        })
+        .then(function (item) {
+          assert.strictEqual(item.value, 1);
+          assert.strictEqual(item.done, false);
+          return traversal.next();
+        })
+        .then(function (item) {
+          assert.strictEqual(item.value, 2);
+          assert.strictEqual(item.done, false);
+          return traversal.next();
+        })
+        .then(function (item) {
+          assert.strictEqual(item.value, null);
+          assert.strictEqual(item.done, true);
+          return traversal.next();
+        });
+    });
   });
+
   describe('#toList()', function () {
+
     it('should apply the strategies and return a Promise with an array', function () {
       var strategyMock = {
         apply: function (traversal) {
@@ -100,6 +141,7 @@ describe('Traversal', function () {
         assert.deepEqual(list, [ 'a', 'b' ]);
       });
     });
+
     it('should return an empty array when traversers is empty', function () {
       var strategyMock = {
         apply: function (traversal) {
@@ -114,6 +156,23 @@ describe('Traversal', function () {
         assert.ok(Array.isArray(list));
         assert.strictEqual(list.length, 0);
       });
-    })
+    });
+
+    it('should support bulk', function () {
+      const strategyMock = {
+        apply: function (traversal) {
+          traversal.traversers = [ new t.Traverser(1, 1), new t.Traverser(2, 3), new t.Traverser(3,
2),
+            new t.Traverser(4, 1) ];
+          return utils.resolvedPromise();
+        }
+      };
+      const strategies = new TraversalStrategies();
+      strategies.addStrategy(strategyMock);
+      const traversal = new t.Traversal(null, strategies, null);
+      return traversal.toList()
+        .then(list => {
+          expect(list).to.have.members([1, 2, 2, 2, 3, 3, 4]);
+        });
+    });
   });
 });
\ No newline at end of file


Mime
View raw message