UNPKG

2.42 kBJavaScriptView Raw
1"use strict";
2/*!
3 * Copyright 2022 Google LLC. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.RowBatch = exports.BATCH_LIMITS = void 0;
19exports.BATCH_LIMITS = {
20 maxBytes: 9 * 1024 * 1024,
21 maxRows: 50000,
22};
23/**
24 * Call used to help batch rows.
25 *
26 * @private
27 *
28 * @param {BatchInsertOptions} options The batching options.
29 */
30class RowBatch {
31 constructor(options) {
32 this.batchOptions = options;
33 this.rows = [];
34 this.callbacks = [];
35 this.created = Date.now();
36 this.bytes = 0;
37 }
38 /**
39 * Adds a row to the current batch.
40 *
41 * @param {object} row The row to insert.
42 * @param {InsertRowsCallback} callback The callback function.
43 */
44 add(row, callback) {
45 this.rows.push(row);
46 this.callbacks.push(callback);
47 this.bytes += Buffer.byteLength(JSON.stringify(row));
48 }
49 /**
50 * Indicates if a given row can fit in the batch.
51 *
52 * @param {object} row The row in question.
53 * @returns {boolean}
54 */
55 canFit(row) {
56 const { maxRows, maxBytes } = this.batchOptions;
57 return (this.rows.length < maxRows &&
58 this.bytes + Buffer.byteLength(JSON.stringify(row)) <= maxBytes);
59 }
60 /**
61 * Checks to see if this batch is at the maximum allowed payload size.
62 *
63 * @returns {boolean}
64 */
65 isAtMax() {
66 const { maxRows, maxBytes } = exports.BATCH_LIMITS;
67 return this.rows.length >= maxRows || this.bytes >= maxBytes;
68 }
69 /**
70 * Indicates if the batch is at capacity.
71 *
72 * @returns {boolean}
73 */
74 isFull() {
75 const { maxRows, maxBytes } = this.batchOptions;
76 return this.rows.length >= maxRows || this.bytes >= maxBytes;
77 }
78}
79exports.RowBatch = RowBatch;
80//# sourceMappingURL=rowBatch.js.map
\No newline at end of file