Skip to content

Commit 3d921d4

Browse files
authored
Merge pull request #841 from zhicwu/exception-handling
More changes for patch5
2 parents ea49d7f + db4e219 commit 3d921d4

31 files changed

+2058
-494
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ jobs:
3434
matrix:
3535
# most recent LTS releases as well as latest stable builds
3636
clickhouse: ["21.3", "21.8", "latest"]
37-
protocol: ["http", "grpc"]
37+
# http2 here represents http protocol + JDK HttpClient(http_connection_provider=HTTP_CLIENT)
38+
protocol: ["http", "http2", "grpc"]
3839
exclude:
3940
- clickhouse: "21.3"
4041
protocol: grpc
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package com.clickhouse.client;
2+
3+
import java.io.Serializable;
4+
import java.nio.ByteBuffer;
5+
import java.util.Arrays;
6+
7+
/**
8+
* Lite version of {@link java.nio.ByteBuffer}.
9+
*/
10+
public class ClickHouseByteBuffer implements Serializable {
11+
private static final long serialVersionUID = -8178041799873465082L;
12+
13+
/**
14+
* Empty byte array.
15+
*/
16+
public static final byte[] EMPTY_BYTES = new byte[0];
17+
18+
/**
19+
* Empty and read-only byte buffer.
20+
*/
21+
public static final ByteBuffer EMPTY_BUFFER = ByteBuffer.wrap(EMPTY_BYTES).asReadOnlyBuffer();
22+
23+
/**
24+
* Creates an empty byte buffer.
25+
*
26+
* @return empty byte buffer
27+
*/
28+
public static ClickHouseByteBuffer newInstance() {
29+
return new ClickHouseByteBuffer(EMPTY_BYTES, 0, 0);
30+
}
31+
32+
/**
33+
* Wraps given byte array as byte buffer.
34+
*
35+
* @param bytes byte array
36+
* @return byte buffer
37+
*/
38+
public static ClickHouseByteBuffer of(byte[] bytes) {
39+
return bytes == null || bytes.length == 0 ? newInstance() : new ClickHouseByteBuffer(bytes, 0, bytes.length);
40+
}
41+
42+
/**
43+
* Wraps given byte array as byte buffer.
44+
*
45+
* @param bytes byte array
46+
* @param offset offset
47+
* @param length length
48+
* @return byte buffer
49+
*/
50+
public static ClickHouseByteBuffer of(byte[] bytes, int offset, int length) {
51+
if (bytes == null || bytes.length == 0 || length == 0) {
52+
return newInstance();
53+
} else {
54+
validate(bytes, offset, length);
55+
}
56+
57+
return new ClickHouseByteBuffer(bytes, offset, length);
58+
}
59+
60+
static void validate(byte[] bytes, int offset, int length) {
61+
int len = ClickHouseChecker.nonNull(bytes, "Byte array").length;
62+
if (ClickHouseChecker.between(offset, "Offset", 0, len)
63+
+ ClickHouseChecker.between(length, "Length", 0, len) > len) {
64+
throw new IllegalArgumentException(
65+
ClickHouseUtils.format("Offset(%d) plus length(%d) should not greater than %d", offset, length,
66+
len));
67+
}
68+
}
69+
70+
protected byte[] array;
71+
protected int position;
72+
protected int length;
73+
74+
protected ClickHouseByteBuffer(byte[] bytes, int offset, int length) {
75+
this.array = bytes;
76+
this.position = offset;
77+
this.length = length;
78+
}
79+
80+
public boolean isEmpty() {
81+
return length < 1;
82+
}
83+
84+
public ClickHouseByteBuffer reset() {
85+
array = EMPTY_BYTES;
86+
position = 0;
87+
length = 0;
88+
return this;
89+
}
90+
91+
public ClickHouseByteBuffer update(byte[] bytes) {
92+
if (bytes == null || bytes.length == 0) {
93+
reset();
94+
} else {
95+
array = bytes;
96+
position = 0;
97+
length = bytes.length;
98+
}
99+
100+
return this;
101+
}
102+
103+
public ClickHouseByteBuffer update(byte[] bytes, int offset, int length) {
104+
if (bytes == null || bytes.length == 0 || length == 0) {
105+
return reset();
106+
} else {
107+
validate(bytes, offset, length);
108+
}
109+
110+
this.array = bytes;
111+
this.position = offset;
112+
this.length = length;
113+
return this;
114+
}
115+
116+
public byte[] array() {
117+
return array;
118+
}
119+
120+
public int position() {
121+
return position;
122+
}
123+
124+
public int length() {
125+
return length;
126+
}
127+
128+
public int limit() {
129+
return position + length;
130+
}
131+
132+
@Override
133+
public int hashCode() {
134+
final int prime = 31;
135+
int result = prime + Arrays.hashCode(array);
136+
result = prime * result + position;
137+
result = prime * result + length;
138+
return result;
139+
}
140+
141+
@Override
142+
public boolean equals(Object obj) {
143+
if (this == obj) {
144+
return true;
145+
} else if (obj == null || getClass() != obj.getClass()) {
146+
return false;
147+
}
148+
149+
ClickHouseByteBuffer other = (ClickHouseByteBuffer) obj;
150+
return Arrays.equals(array, other.array) && position == other.position && length == other.length;
151+
}
152+
153+
@Override
154+
public String toString() {
155+
return new StringBuilder().append(getClass().getSimpleName()).append("array=").append(array)
156+
.append(", position=").append(position).append(", length=").append(length).append(')').toString();
157+
}
158+
}

clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseConfig.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ protected static final Object mergeMetricRegistry(List<ClickHouseConfig> list) {
115115
private final int maxExecutionTime;
116116
private final int maxQueuedBuffers;
117117
private final int maxQueuedRequests;
118-
private final int maxResultRows;
118+
private final long maxResultRows;
119119
private final int maxThreads;
120120
private final boolean retry;
121121
private final boolean reuseValueWrapper;
@@ -193,7 +193,7 @@ public ClickHouseConfig(Map<ClickHouseOption, Serializable> options, ClickHouseC
193193
this.maxExecutionTime = (int) getOption(ClickHouseClientOption.MAX_EXECUTION_TIME);
194194
this.maxQueuedBuffers = (int) getOption(ClickHouseClientOption.MAX_QUEUED_BUFFERS);
195195
this.maxQueuedRequests = (int) getOption(ClickHouseClientOption.MAX_QUEUED_REQUESTS);
196-
this.maxResultRows = (int) getOption(ClickHouseClientOption.MAX_RESULT_ROWS);
196+
this.maxResultRows = (long) getOption(ClickHouseClientOption.MAX_RESULT_ROWS);
197197
this.maxThreads = (int) getOption(ClickHouseClientOption.MAX_THREADS_PER_CLIENT);
198198
this.retry = (boolean) getOption(ClickHouseClientOption.RETRY);
199199
this.reuseValueWrapper = (boolean) getOption(ClickHouseClientOption.REUSE_VALUE_WRAPPER);
@@ -291,7 +291,7 @@ public int getMaxQueuedRequests() {
291291
return maxQueuedRequests;
292292
}
293293

294-
public int getMaxResultRows() {
294+
public long getMaxResultRows() {
295295
return maxResultRows;
296296
}
297297

0 commit comments

Comments
 (0)