This is an automated email from the ASF dual-hosted git repository.
jorgebg pushed a commit to branch 3.5-dev
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git
The following commit(s) were added to refs/heads/3.5-dev by this push:
new 93d7109 gremlin-javascript: Tune Writer/Reader abstraction border
new ec78520 Merge pull request #1549 from
ihoro/gremlin-javascript-tune-writer-reader-abstraction-border
93d7109 is described below
commit 93d7109153d5f44cd67e437a8db2277335ef3973
Author: Igor Ostapenko <[email protected]>
AuthorDate: Tue Jan 11 23:55:40 2022 +0200
gremlin-javascript: Tune Writer/Reader abstraction border
---
.../gremlin-javascript/lib/driver/connection.js | 66 ++++++----------------
.../lib/structure/io/graph-serializer.js | 46 +++++++++++++++
2 files changed, 62 insertions(+), 50 deletions(-)
diff --git
a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/connection.js
b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/connection.js
index cf46475..a726b21 100644
---
a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/connection.js
+++
b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/connection.js
@@ -29,7 +29,6 @@ const utils = require('../utils');
const serializer = require('../structure/io/graph-serializer');
const ResultSet = require('./result-set');
const ResponseError = require('./response-error');
-const Bytecode = require('../process/bytecode');
const responseStatusCode = {
success: 200,
@@ -92,7 +91,8 @@ class Connection extends EventEmitter {
this._pingInterval = null;
this._pongTimeout = null;
- this._header = String.fromCharCode(this.mimeType.length) + this.mimeType;
+ this._header = String.fromCharCode(this.mimeType.length) + this.mimeType;
// TODO: what if mimeType.length > 255
+ this._header_buf = Buffer.from(this._header);
this.isOpen = false;
this.traversalSource = options.traversalSource || 'g';
this._authenticator = options.authenticator;
@@ -169,7 +169,19 @@ class Connection extends EventEmitter {
};
}
- const message = Buffer.from(this._header +
JSON.stringify(this._getRequest(rid, op, args, processor)));
+ const request = {
+ 'requestId': rid,
+ 'op': op || 'bytecode',
+ // if using op eval need to ensure processor stays unset if caller
didn't set it.
+ 'processor': (!processor && op !== 'eval') ? 'traversal' : processor,
+ 'args': args || {},
+ };
+
+ const request_buf = this._writer.writeRequest(request);
+ const message = Buffer.concat(
+ [this._header_buf, request_buf],
+ this._header_buf.length + request_buf.length
+ );
this._ws.send(message);
}));
}
@@ -186,26 +198,6 @@ class Connection extends EventEmitter {
: new serializer.GraphSONWriter();
}
- _getRequest(id, op, args, processor) {
- if (args) {
- args = this._adaptArgs(args, true);
- } else {
- args = {};
- }
-
- if (args['gremlin'] instanceof Bytecode) {
- args['gremlin'] = this._writer.adaptObject(args['gremlin']);
- }
-
- return ({
- 'requestId': { '@type': 'g:UUID', '@value': id },
- 'op': op || 'bytecode',
- // if using op eval need to ensure processor stays unset if caller
didn't set it.
- 'processor': (!processor && op !== 'eval') ? 'traversal' : processor,
- 'args': args
- });
- }
-
_pingHeartbeat() {
if (this._pingInterval) {
@@ -247,7 +239,7 @@ class Connection extends EventEmitter {
}
_handleMessage(data) {
- const response = this._reader.read(JSON.parse(data.toString()));
+ const response = this._reader.readResponse(data);
if (response.requestId === null || response.requestId === undefined) {
// There was a serialization issue on the server that prevented the
parsing of the request id
// We invoke any of the pending handlers with an error
@@ -337,32 +329,6 @@ class Connection extends EventEmitter {
}
/**
- * Takes the given args map and ensures all arguments are passed through to
_write.adaptObject
- * @param {Object} args Map of arguments to process.
- * @param {Boolean} protocolLevel Determines whether it's a protocol level
binding.
- * @returns {Object}
- * @private
- */
- _adaptArgs(args, protocolLevel) {
- if (args instanceof Object) {
- let newObj = {};
- Object.keys(args).forEach((key) => {
- // bindings key (at the protocol-level needs special handling. without
this, it wraps the generated Map
- // in another map for types like EnumValue. Could be a nicer way to do
this but for now it's solving the
- // problem with script submission of non JSON native types
- if (protocolLevel && key === 'bindings')
- newObj[key] = this._adaptArgs(args[key], false);
- else
- newObj[key] = this._writer.adaptObject(args[key]);
- });
-
- return newObj;
- }
-
- return args;
- }
-
- /**
* Closes the Connection.
* @return {Promise}
*/
diff --git
a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/graph-serializer.js
b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/graph-serializer.js
index 122f315..ed0704f 100644
---
a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/graph-serializer.js
+++
b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/graph-serializer.js
@@ -23,6 +23,7 @@
'use strict';
const typeSerializers = require('./type-serializers');
+const Bytecode = require('../../process/bytecode');
/**
* GraphSON2 writer.
@@ -98,6 +99,47 @@ class GraphSON2Writer {
write(obj) {
return JSON.stringify(this.adaptObject(obj));
}
+
+ writeRequest({ requestId, op, processor, args }) {
+ const req = {
+ requestId: { '@type': 'g:UUID', '@value': requestId },
+ op,
+ processor,
+ args: this._adaptArgs(args, true),
+ };
+
+ if (req.args['gremlin'] instanceof Bytecode) {
+ req.args['gremlin'] = this.adaptObject(req.args['gremlin']);
+ }
+
+ return Buffer.from( JSON.stringify(req) );
+ }
+
+ /**
+ * Takes the given args map and ensures all arguments are passed through to
adaptObject
+ * @param {Object} args Map of arguments to process.
+ * @param {Boolean} protocolLevel Determines whether it's a protocol level
binding.
+ * @returns {Object}
+ * @private
+ */
+ _adaptArgs(args, protocolLevel) {
+ if (args instanceof Object) {
+ let newObj = {};
+ Object.keys(args).forEach((key) => {
+ // bindings key (at the protocol-level needs special handling. without
this, it wraps the generated Map
+ // in another map for types like EnumValue. Could be a nicer way to do
this but for now it's solving the
+ // problem with script submission of non JSON native types
+ if (protocolLevel && key === 'bindings')
+ newObj[key] = this._adaptArgs(args[key], false);
+ else
+ newObj[key] = this.adaptObject(args[key]);
+ });
+
+ return newObj;
+ }
+
+ return args;
+ }
}
/**
@@ -179,6 +221,10 @@ class GraphSON2Reader {
return obj;
}
+ readResponse(buffer) {
+ return this.read( JSON.parse(buffer.toString()) );
+ }
+
_deserializeObject(obj) {
const keys = Object.keys(obj);
const result = {};