MCPcopy Create free account
hub / github.com/apache/cassandra-nodejs-driver / write

Method write

lib/requests.js:427–500  ·  view source on GitHub ↗

* Writes a batch request

(encoder, streamId)

Source from the content-addressed store, hash-verified

425 * Writes a batch request
426 */
427 write(encoder, streamId) {
428 //v2: <type><n><query_1>...<query_n><consistency>
429 //v3: <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>]
430 //dseV1+: similar to v3/v4, flags is an int instead of a byte
431 if (!this.queries || !(this.queries.length > 0)) {
432 throw new TypeError(util.format('Invalid queries provided %s', this.queries));
433 }
434 const frameWriter = new FrameWriter(types.opcodes.batch);
435 let headerFlags = this.options.isQueryTracing() ? types.frameFlags.tracing : 0;
436 if (this.options.getCustomPayload()) {
437 //The body may contain the custom payload
438 headerFlags |= types.frameFlags.customPayload;
439 frameWriter.writeCustomPayload(this.options.getCustomPayload());
440 }
441 frameWriter.writeByte(this.type);
442 frameWriter.writeShort(this.queries.length);
443 const self = this;
444 this.queries.forEach(function eachQuery(item, i) {
445 const hints = self.hints[i];
446 const params = item.params || utils.emptyArray;
447 let getParamType;
448 if (item.queryId) {
449 // Contains prepared queries
450 frameWriter.writeByte(1);
451 frameWriter.writeShortBytes(item.queryId);
452 getParamType = i => item.meta.columns[i].type;
453 }
454 else {
455 // Contains string queries
456 frameWriter.writeByte(0);
457 frameWriter.writeLString(item.query);
458 getParamType = hints ? (i => hints[i]) : (() => null);
459 }
460
461 frameWriter.writeShort(params.length);
462 params.forEach((param, index) => frameWriter.writeBytes(encoder.encode(param, getParamType(index))));
463 }, this);
464
465 frameWriter.writeShort(this.options.getConsistency());
466
467 if (types.protocolVersion.supportsTimestamp(encoder.protocolVersion)) {
468 // Batch flags
469 let flags = this.options.getSerialConsistency() ? batchFlag.withSerialConsistency : 0;
470 const timestamp = this.timestamp;
471 flags |= timestamp !== null && timestamp !== undefined ? batchFlag.withDefaultTimestamp : 0;
472
473 flags |= this.options.getKeyspace() && types.protocolVersion.supportsKeyspaceInRequest(encoder.protocolVersion)
474 ? batchFlag.withKeyspace : 0;
475
476 if (types.protocolVersion.uses4BytesQueryFlags(encoder.protocolVersion)) {
477 frameWriter.writeInt(flags);
478 }
479 else {
480 frameWriter.writeByte(flags);
481 }
482
483 if (flags & batchFlag.withSerialConsistency) {
484 frameWriter.writeShort(this.options.getSerialConsistency());

Callers

nothing calls this directly

Calls 15

writeCustomPayloadMethod · 0.95
writeByteMethod · 0.95
writeShortMethod · 0.95
writeShortBytesMethod · 0.95
writeLStringMethod · 0.95
writeBytesMethod · 0.95
writeIntMethod · 0.95
writeLongMethod · 0.95
writeStringMethod · 0.95
writeMethod · 0.95
isQueryTracingMethod · 0.65
getCustomPayloadMethod · 0.65

Tested by

no test coverage detected