Skip to content

Commit 699fe26

Browse files
committed
cache encoded error-message length and always emit the error
length field for non-OK responses, including empty messages.
1 parent 6a6698d commit 699fe26

1 file changed

Lines changed: 27 additions & 10 deletions

File tree

core/src/main/java/io/questdb/client/cutlass/qwp/client/WebSocketResponse.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ public class WebSocketResponse {
6767
public static final byte STATUS_SECURITY_ERROR = 0x08;
6868
public static final byte STATUS_WRITE_ERROR = 0x09;
6969
private String errorMessage;
70+
private int errorMessageUtf8Length;
7071
private long sequence;
7172
private byte status;
7273

7374
public WebSocketResponse() {
7475
this.status = STATUS_OK;
7576
this.sequence = 0;
7677
this.errorMessage = null;
78+
this.errorMessageUtf8Length = -1;
7779
}
7880

7981
/**
@@ -84,6 +86,7 @@ public static WebSocketResponse error(long sequence, byte status, String errorMe
8486
response.status = status;
8587
response.sequence = sequence;
8688
response.errorMessage = errorMessage;
89+
response.errorMessageUtf8Length = -1;
8790
return response;
8891
}
8992

@@ -125,6 +128,7 @@ public static WebSocketResponse success(long sequence) {
125128
WebSocketResponse response = new WebSocketResponse();
126129
response.status = STATUS_OK;
127130
response.sequence = sequence;
131+
response.errorMessageUtf8Length = -1;
128132
return response;
129133
}
130134

@@ -204,11 +208,14 @@ public boolean readFrom(long ptr, int length) {
204208
msgBytes[i] = Unsafe.getUnsafe().getByte(ptr + offset + i);
205209
}
206210
errorMessage = new String(msgBytes, StandardCharsets.UTF_8);
211+
errorMessageUtf8Length = -1;
207212
} else {
208213
errorMessage = null;
214+
errorMessageUtf8Length = 0;
209215
}
210216
} else {
211217
errorMessage = null;
218+
errorMessageUtf8Length = -1;
212219
}
213220

214221
return true;
@@ -218,12 +225,10 @@ public boolean readFrom(long ptr, int length) {
218225
* Calculates the serialized size of this response.
219226
*/
220227
public int serializedSize() {
221-
int size = MIN_RESPONSE_SIZE;
222-
if (errorMessage != null && !errorMessage.isEmpty()) {
223-
int msgLen = Utf8s.utf8Bytes(errorMessage, MAX_ERROR_MESSAGE_LENGTH);
224-
size += 2 + msgLen; // 2 bytes for length prefix
228+
if (status == STATUS_OK) {
229+
return MIN_RESPONSE_SIZE;
225230
}
226-
return size;
231+
return MIN_ERROR_RESPONSE_SIZE + getErrorMessageUtf8Length();
227232
}
228233

229234
@Override
@@ -253,18 +258,30 @@ public int writeTo(long ptr) {
253258
Unsafe.getUnsafe().putLong(ptr + offset, sequence);
254259
offset += 8;
255260

256-
// Error message (if any)
257-
if (status != STATUS_OK && errorMessage != null && !errorMessage.isEmpty()) {
258-
int msgLen = Utf8s.utf8Bytes(errorMessage, MAX_ERROR_MESSAGE_LENGTH);
259-
261+
// Error message length and bytes (if any)
262+
if (status != STATUS_OK) {
263+
int msgLen = getErrorMessageUtf8Length();
260264
// Length prefix (2 bytes, little-endian)
261265
Unsafe.getUnsafe().putShort(ptr + offset, (short) msgLen);
262266
offset += 2;
263267

264268
// Message bytes
265-
offset += Utf8s.strCpyUtf8(errorMessage, ptr + offset, msgLen);
269+
if (msgLen > 0) {
270+
offset += Utf8s.strCpyUtf8(errorMessage, ptr + offset, msgLen);
271+
}
266272
}
267273

268274
return offset;
269275
}
276+
277+
private int getErrorMessageUtf8Length() {
278+
if (status == STATUS_OK || errorMessage == null || errorMessage.isEmpty()) {
279+
errorMessageUtf8Length = 0;
280+
return 0;
281+
}
282+
if (errorMessageUtf8Length < 0) {
283+
errorMessageUtf8Length = Utf8s.utf8Bytes(errorMessage, MAX_ERROR_MESSAGE_LENGTH);
284+
}
285+
return errorMessageUtf8Length;
286+
}
270287
}

0 commit comments

Comments
 (0)