Skip to content

Commit 165d827

Browse files
committed
Merge branch 'binary_protocol_arrays' into arrays_support
2 parents ab6f968 + d873996 commit 165d827

File tree

14 files changed

+279
-148
lines changed

14 files changed

+279
-148
lines changed

src/buffer/base.ts

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ import {
1717
TimestampUnit,
1818
} from "../utils";
1919

20+
// Default maximum length for table and column names.
2021
const DEFAULT_MAX_NAME_LENGTH = 127;
2122

22-
/** @classdesc
23-
* The QuestDB client's API provides methods to connect to the database, ingest data, and close the connection.
24-
* If no custom agent is configured, the Sender will use its own agent which overrides some default values
25-
* of <i>undici.Agent</i>. The Sender's own agent uses persistent connections with 1 minute idle timeout, pipelines requests default to 1.
26-
* </p>
23+
/**
24+
* Abstract base class for SenderBuffer implementations. <br>
25+
* Provides common functionality for writing data into the buffer.
2726
*/
2827
abstract class SenderBufferBase implements SenderBuffer {
2928
private bufferSize: number;
@@ -41,7 +40,7 @@ abstract class SenderBufferBase implements SenderBuffer {
4140
protected readonly log: Logger;
4241

4342
/**
44-
* Creates an instance of Sender.
43+
* Creates an instance of SenderBufferBase.
4544
*
4645
* @param {SenderOptions} options - Sender configuration object. <br>
4746
* See SenderOptions documentation for detailed description of configuration options. <br>
@@ -69,7 +68,7 @@ abstract class SenderBufferBase implements SenderBuffer {
6968
}
7069

7170
/**
72-
* Extends the size of the sender's buffer. <br>
71+
* Extends the size of the buffer. <br>
7372
* Can be used to increase the size of buffer if overflown.
7473
* The buffer's content is copied into the new buffer.
7574
*
@@ -113,22 +112,20 @@ abstract class SenderBufferBase implements SenderBuffer {
113112
}
114113

115114
/**
116-
* @ignore
117-
* @return {Buffer} Returns a cropped buffer, or null if there is nothing to send.
118-
* The returned buffer is backed by the sender's buffer.
119-
* Used only in tests.
115+
* @return {Buffer} Returns a cropped buffer, or null if there is nothing to send. <br>
116+
* The returned buffer is backed by this buffer instance, meaning the view can change as the buffer is mutated.
117+
* Used only in tests to assert the buffer's content.
120118
*/
121119
toBufferView(pos = this.endOfLastRow): Buffer {
122120
return pos > 0 ? this.buffer.subarray(0, pos) : null;
123121
}
124122

125123
/**
126-
* @ignore
127-
* @return {Buffer|null} Returns a cropped buffer ready to send to the server, or null if there is nothing to send.
128-
* The returned buffer is a copy of the sender's buffer.
129-
* It also compacts the Sender's buffer.
124+
* @return {Buffer} Returns a cropped buffer ready to send to the server, or null if there is nothing to send. <br>
125+
* The returned buffer is a copy of this buffer.
126+
* It also compacts the buffer.
130127
*/
131-
toBufferNew(pos = this.endOfLastRow): Buffer | null {
128+
toBufferNew(pos = this.endOfLastRow): Buffer {
132129
if (pos > 0) {
133130
const data = Buffer.allocUnsafe(pos);
134131
this.buffer.copy(data, 0, 0, pos);
@@ -139,7 +136,7 @@ abstract class SenderBufferBase implements SenderBuffer {
139136
}
140137

141138
/**
142-
* Write the table name into the buffer of the sender.
139+
* Write the table name into the buffer.
143140
*
144141
* @param {string} table - Table name.
145142
* @return {Sender} Returns with a reference to this sender.
@@ -159,7 +156,7 @@ abstract class SenderBufferBase implements SenderBuffer {
159156
}
160157

161158
/**
162-
* Write a symbol name and value into the buffer of the sender.
159+
* Write a symbol name and value into the buffer.
163160
*
164161
* @param {string} name - Symbol name.
165162
* @param {unknown} value - Symbol value, toString() is called to extract the actual symbol value from the parameter.
@@ -186,7 +183,7 @@ abstract class SenderBufferBase implements SenderBuffer {
186183
}
187184

188185
/**
189-
* Write a string column with its value into the buffer of the sender.
186+
* Write a string column with its value into the buffer.
190187
*
191188
* @param {string} name - Column name.
192189
* @param {string} value - Column value, accepts only string values.
@@ -208,7 +205,7 @@ abstract class SenderBufferBase implements SenderBuffer {
208205
}
209206

210207
/**
211-
* Write a boolean column with its value into the buffer of the sender.
208+
* Write a boolean column with its value into the buffer.
212209
*
213210
* @param {string} name - Column name.
214211
* @param {boolean} value - Column value, accepts only boolean values.
@@ -228,7 +225,7 @@ abstract class SenderBufferBase implements SenderBuffer {
228225
}
229226

230227
/**
231-
* Write a float column with its value into the buffer of the sender.
228+
* Write a float column with its value into the buffer.
232229
*
233230
* @param {string} name - Column name.
234231
* @param {number} value - Column value, accepts only number values.
@@ -239,11 +236,12 @@ abstract class SenderBufferBase implements SenderBuffer {
239236
abstract arrayColumn(name: string, value: unknown[]): SenderBuffer;
240237

241238
/**
242-
* Write an integer column with its value into the buffer of the sender.
239+
* Write an integer column with its value into the buffer.
243240
*
244241
* @param {string} name - Column name.
245242
* @param {number} value - Column value, accepts only number values.
246243
* @return {Sender} Returns with a reference to this sender.
244+
* @throws Error if the value is not an integer
247245
*/
248246
intColumn(name: string, value: number): SenderBuffer {
249247
if (!Number.isInteger(value)) {
@@ -259,7 +257,7 @@ abstract class SenderBufferBase implements SenderBuffer {
259257
}
260258

261259
/**
262-
* Write a timestamp column with its value into the buffer of the sender.
260+
* Write a timestamp column with its value into the buffer.
263261
*
264262
* @param {string} name - Column name.
265263
* @param {number | bigint} value - Epoch timestamp, accepts numbers or BigInts.
@@ -285,7 +283,7 @@ abstract class SenderBufferBase implements SenderBuffer {
285283
}
286284

287285
/**
288-
* Closing the row after writing the designated timestamp into the buffer of the sender.
286+
* Closing the row after writing the designated timestamp into the buffer.
289287
*
290288
* @param {number | bigint} timestamp - Designated epoch timestamp, accepts numbers or BigInts.
291289
* @param {string} [unit=us] - Timestamp unit. Supported values: 'ns' - nanoseconds, 'us' - microseconds, 'ms' - milliseconds. Defaults to 'us'.
@@ -311,7 +309,7 @@ abstract class SenderBufferBase implements SenderBuffer {
311309
}
312310

313311
/**
314-
* Closing the row without writing designated timestamp into the buffer of the sender. <br>
312+
* Closing the row without writing designated timestamp into the buffer. <br>
315313
* Designated timestamp will be populated by the server on this record.
316314
*/
317315
atNow() {
@@ -325,6 +323,14 @@ abstract class SenderBufferBase implements SenderBuffer {
325323
this.startNewRow();
326324
}
327325

326+
/**
327+
* Returns the current position of the buffer. <br>
328+
* New data will be written into the buffer starting from this position.
329+
*/
330+
currentPosition(): number {
331+
return this.position;
332+
}
333+
328334
protected checkCapacity(data: string[], base = 0) {
329335
let length = base;
330336
for (const str of data) {

src/buffer/bufferv1.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,22 @@ import { SenderOptions } from "../options";
33
import { SenderBuffer } from "./index";
44
import { SenderBufferBase } from "./base";
55

6+
/**
7+
* Buffer implementation for protocol version 1.
8+
* Sends floating point numbers in their text form.
9+
*/
610
class SenderBufferV1 extends SenderBufferBase {
711
constructor(options: SenderOptions) {
812
super(options);
913
}
1014

15+
/**
16+
* Write a float column with its value into the buffer using v1 serialization (text format).
17+
*
18+
* @param {string} name - Column name.
19+
* @param {number} value - Column value, accepts only number values.
20+
* @return {Sender} Returns with a reference to this sender.
21+
*/
1122
floatColumn(name: string, value: number): SenderBuffer {
1223
this.writeColumn(
1324
name,

src/buffer/bufferv2.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,22 @@ const ENTITY_TYPE_DOUBLE: number = 16;
1212

1313
const EQUALS_SIGN: number = "=".charCodeAt(0);
1414

15+
/**
16+
* Buffer implementation for protocol version 2.
17+
* Sends floating point numbers in binary form.
18+
*/
1519
class SenderBufferV2 extends SenderBufferBase {
1620
constructor(options: SenderOptions) {
1721
super(options);
1822
}
1923

24+
/**
25+
* Write a float column with its value into the buffer using v2 serialization (binary format).
26+
*
27+
* @param {string} name - Column name.
28+
* @param {number} value - Column value, accepts only number values.
29+
* @return {Sender} Returns with a reference to this sender.
30+
*/
2031
floatColumn(name: string, value: number): SenderBuffer {
2132
this.writeColumn(
2233
name,

src/buffer/index.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@ function createBuffer(options: SenderOptions): SenderBuffer {
3434
}
3535
}
3636

37-
/** @classdesc
38-
* The QuestDB client's API provides methods to connect to the database, ingest data, and close the connection.
39-
* If no custom agent is configured, the Sender will use its own agent which overrides some default values
40-
* of <i>undici.Agent</i>. The Sender's own agent uses persistent connections with 1 minute idle timeout, pipelines requests default to 1.
41-
* </p>
37+
/**
38+
* Buffer used by the Sender.
4239
*/
4340
interface SenderBuffer {
4441
/**
@@ -50,31 +47,29 @@ interface SenderBuffer {
5047
reset(): SenderBuffer;
5148

5249
/**
53-
* @ignore
54-
* @return {Buffer} Returns a cropped buffer, or null if there is nothing to send.
55-
* The returned buffer is backed by the sender's buffer.
56-
* Used only in tests.
50+
* @return {Buffer} Returns a cropped buffer, or null if there is nothing to send. <br>
51+
* The returned buffer is backed by this buffer instance, meaning the view can change as the buffer is mutated.
52+
* Used only in tests to assert the buffer's content.
5753
*/
5854
toBufferView(pos?: number): Buffer;
5955

6056
/**
61-
* @ignore
62-
* @return {Buffer|null} Returns a cropped buffer ready to send to the server, or null if there is nothing to send.
63-
* The returned buffer is a copy of the sender's buffer.
64-
* It also compacts the Sender's buffer.
57+
* @return {Buffer} Returns a cropped buffer ready to send to the server, or null if there is nothing to send. <br>
58+
* The returned buffer is a copy of this buffer.
59+
* It also compacts the buffer.
6560
*/
6661
toBufferNew(pos?: number): Buffer | null;
6762

6863
/**
69-
* Write the table name into the buffer of the sender.
64+
* Write the table name into the buffer.
7065
*
7166
* @param {string} table - Table name.
7267
* @return {Sender} Returns with a reference to this sender.
7368
*/
7469
table(table: string): SenderBuffer;
7570

7671
/**
77-
* Write a symbol name and value into the buffer of the sender.
72+
* Write a symbol name and value into the buffer.
7873
*
7974
* @param {string} name - Symbol name.
8075
* @param {unknown} value - Symbol value, toString() is called to extract the actual symbol value from the parameter.
@@ -83,7 +78,7 @@ interface SenderBuffer {
8378
symbol(name: string, value: unknown): SenderBuffer;
8479

8580
/**
86-
* Write a string column with its value into the buffer of the sender.
81+
* Write a string column with its value into the buffer.
8782
*
8883
* @param {string} name - Column name.
8984
* @param {string} value - Column value, accepts only string values.
@@ -92,7 +87,7 @@ interface SenderBuffer {
9287
stringColumn(name: string, value: string): SenderBuffer;
9388

9489
/**
95-
* Write a boolean column with its value into the buffer of the sender.
90+
* Write a boolean column with its value into the buffer.
9691
*
9792
* @param {string} name - Column name.
9893
* @param {boolean} value - Column value, accepts only boolean values.
@@ -101,7 +96,7 @@ interface SenderBuffer {
10196
booleanColumn(name: string, value: boolean): SenderBuffer;
10297

10398
/**
104-
* Write a float column with its value into the buffer of the sender.
99+
* Write a float column with its value into the buffer.
105100
*
106101
* @param {string} name - Column name.
107102
* @param {number} value - Column value, accepts only number values.
@@ -112,16 +107,17 @@ interface SenderBuffer {
112107
arrayColumn(name: string, value: unknown[]): SenderBuffer;
113108

114109
/**
115-
* Write an integer column with its value into the buffer of the sender.
110+
* Write an integer column with its value into the buffer.
116111
*
117112
* @param {string} name - Column name.
118113
* @param {number} value - Column value, accepts only number values.
119114
* @return {Sender} Returns with a reference to this sender.
115+
* @throws Error if the value is not an integer
120116
*/
121117
intColumn(name: string, value: number): SenderBuffer;
122118

123119
/**
124-
* Write a timestamp column with its value into the buffer of the sender.
120+
* Write a timestamp column with its value into the buffer.
125121
*
126122
* @param {string} name - Column name.
127123
* @param {number | bigint} value - Epoch timestamp, accepts numbers or BigInts.
@@ -135,18 +131,24 @@ interface SenderBuffer {
135131
): SenderBuffer;
136132

137133
/**
138-
* Closing the row after writing the designated timestamp into the buffer of the sender.
134+
* Closing the row after writing the designated timestamp into the buffer.
139135
*
140136
* @param {number | bigint} timestamp - Designated epoch timestamp, accepts numbers or BigInts.
141137
* @param {string} [unit=us] - Timestamp unit. Supported values: 'ns' - nanoseconds, 'us' - microseconds, 'ms' - milliseconds. Defaults to 'us'.
142138
*/
143139
at(timestamp: number | bigint, unit: TimestampUnit): void;
144140

145141
/**
146-
* Closing the row without writing designated timestamp into the buffer of the sender. <br>
142+
* Closing the row without writing designated timestamp into the buffer. <br>
147143
* Designated timestamp will be populated by the server on this record.
148144
*/
149145
atNow(): void;
146+
147+
/**
148+
* Returns the current position of the buffer. <br>
149+
* New data will be written into the buffer starting from this position.
150+
*/
151+
currentPosition(): number;
150152
}
151153

152154
export {

0 commit comments

Comments
 (0)