Skip to content

Commit b05702d

Browse files
author
Jorge Bay Gondra
committed
copy buffer as a client option
1 parent 6288b92 commit b05702d

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

lib/client.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ var clientOptions = require('./client-options');
1414
/**
1515
* Client options
1616
* @typedef {Object} ClientOptions
17-
* @property {Array.<string>} contactPoints Array of addresses or host names of the nodes to add as contact points.
18-
* Contact points are addresses of Cassandra nodes that the driver uses to discover the cluster topology.
19-
* Only one contact point is required (the driver will retrieve the address of the other nodes automatically),
17+
* @property {Array.<string>} contactPoints
18+
* Array of addresses or host names of the nodes to add as contact points.
19+
* <p>
20+
* Contact points are addresses of Cassandra nodes that the driver uses to discover the cluster topology.
21+
* </p>
22+
* <p>
23+
* Only one contact point is required (the driver will retrieve the address of the other nodes automatically),
2024
* but it is usually a good idea to provide more than one contact point, because if that single contact point is unavailable, the driver will not be able to initialize correctly.
25+
* </p>
2126
* @property {Object} policies
2227
* @property {LoadBalancingPolicy} policies.loadBalancing The load balancing policy instance to be used to determine the coordinator per query.
2328
* @property {RetryPolicy} policies.retry The retry policy.
@@ -41,10 +46,19 @@ var clientOptions = require('./client-options');
4146
* @property {Object} sslOptions Client-to-node ssl options, when set the driver will use the secure layer.
4247
* You can specify cert, ca, ... options named after the Node.js tls.connect options.
4348
* @property {Object} encoding
44-
* @property {Function} encoding.map Map constructor to use for Cassandra map<k,v> types encoding and decoding.
49+
* @property {Function} encoding.map Map constructor to use for Cassandra map<k,v> type encoding and decoding.
4550
* If not set, it will default to Javascript Object with map keys as property names.
46-
* @property {Function} encoding.set Set constructor to use for Cassandra set<k> types encoding and decoding.
47-
* If not set, it will default to Javascript Array
51+
* @property {Function} encoding.set Set constructor to use for Cassandra set<k> type encoding and decoding.
52+
* If not set, it will default to Javascript Array.
53+
* @property {Boolean} encoding.copyBuffer Determines if the network buffer should be copied.
54+
* <p>
55+
* Setting it to true will cause that the network buffer is copied for each row value, causing an additional allocations but freeing the network buffer to be reused.
56+
* Setting it to true is a good choice for cases where the Row and ResultSet returned by the queries are long-lived objects.
57+
* </p>
58+
* <p>
59+
* Setting it to false will cause less overhead and the reference of the network buffer to be maintained until the row / result set are de-referenced.
60+
* Default: true.
61+
* </p>
4862
*/
4963

5064
/**

lib/encoder.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ function Encoder(protocolVersion, options) {
6161
this.encodingOptions = options.encoding || utils.emptyObject;
6262
this.setProtocolVersion(protocolVersion);
6363
this.setEncoders();
64+
if (this.encodingOptions.copyBuffer) {
65+
this.handleBuffer = this.handleBufferCopy;
66+
}
67+
else {
68+
this.handleBuffer = this.handleBufferRef;
69+
}
6470
}
6571

6672
/**
@@ -319,7 +325,7 @@ Encoder.prototype.setProtocolVersion = function (value) {
319325
};
320326

321327
Encoder.prototype.decodeBlob = function (bytes) {
322-
return utils.copyBuffer(bytes);
328+
return bytes;
323329
};
324330

325331
Encoder.prototype.decodeUtf8String = function (bytes) {
@@ -448,15 +454,15 @@ Encoder.prototype.decodeCollectionLengthV2 = function (bytes, offset) {
448454
};
449455

450456
Encoder.prototype.decodeUuid = function (bytes) {
451-
return new types.Uuid(utils.copyBuffer(bytes));
457+
return new types.Uuid(bytes);
452458
};
453459

454460
Encoder.prototype.decodeTimeUuid = function (bytes) {
455-
return new types.TimeUuid(utils.copyBuffer(bytes));
461+
return new types.TimeUuid(bytes);
456462
};
457463

458464
Encoder.prototype.decodeInet = function (bytes) {
459-
return new types.InetAddress(utils.copyBuffer(bytes));
465+
return new types.InetAddress(bytes);
460466
};
461467

462468
/**
@@ -843,6 +849,14 @@ Encoder.prototype.getLengthBufferV3 = function (value) {
843849
return lengthBuffer;
844850
};
845851

852+
Encoder.prototype.handleBufferCopy = function (buffer) {
853+
return utils.copyBuffer(buffer);
854+
};
855+
856+
Encoder.prototype.handleBufferRef = function (buffer) {
857+
return buffer;
858+
};
859+
846860
/**
847861
* Parses a given Cassandra type name to get the data type information
848862
* @param {String} typeName

lib/streams.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ Parser.prototype.parseResult = function (frameInfo, reader) {
274274
}
275275
};
276276

277+
/**
278+
* @param frameInfo
279+
* @param {FrameReader} reader
280+
*/
277281
Parser.prototype.parseRows = function (frameInfo, reader) {
278282
if (typeof frameInfo.rowLength === 'undefined') {
279283
try {
@@ -299,7 +303,7 @@ Parser.prototype.parseRows = function (frameInfo, reader) {
299303
var rowValues = new Array(meta.columns.length);
300304
for(var j = 0; j < meta.columns.length; j++ ) {
301305
try {
302-
rowValues[j] = reader.readBytes();
306+
rowValues[j] = this.encoder.handleBuffer(reader.readBytes());
303307
}
304308
catch (e) {
305309
if (e instanceof RangeError) {

0 commit comments

Comments
 (0)