Skip to content

Commit

Permalink
Merge pull request #841 from zhicwu/exception-handling
Browse files Browse the repository at this point in the history
More changes for patch5
  • Loading branch information
zhicwu authored Feb 22, 2022
2 parents ea49d7f + db4e219 commit 3d921d4
Show file tree
Hide file tree
Showing 31 changed files with 2,058 additions and 494 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ jobs:
matrix:
# most recent LTS releases as well as latest stable builds
clickhouse: ["21.3", "21.8", "latest"]
protocol: ["http", "grpc"]
# http2 here represents http protocol + JDK HttpClient(http_connection_provider=HTTP_CLIENT)
protocol: ["http", "http2", "grpc"]
exclude:
- clickhouse: "21.3"
protocol: grpc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package com.clickhouse.client;

import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.Arrays;

/**
* Lite version of {@link java.nio.ByteBuffer}.
*/
public class ClickHouseByteBuffer implements Serializable {
private static final long serialVersionUID = -8178041799873465082L;

/**
* Empty byte array.
*/
public static final byte[] EMPTY_BYTES = new byte[0];

/**
* Empty and read-only byte buffer.
*/
public static final ByteBuffer EMPTY_BUFFER = ByteBuffer.wrap(EMPTY_BYTES).asReadOnlyBuffer();

/**
* Creates an empty byte buffer.
*
* @return empty byte buffer
*/
public static ClickHouseByteBuffer newInstance() {
return new ClickHouseByteBuffer(EMPTY_BYTES, 0, 0);
}

/**
* Wraps given byte array as byte buffer.
*
* @param bytes byte array
* @return byte buffer
*/
public static ClickHouseByteBuffer of(byte[] bytes) {
return bytes == null || bytes.length == 0 ? newInstance() : new ClickHouseByteBuffer(bytes, 0, bytes.length);
}

/**
* Wraps given byte array as byte buffer.
*
* @param bytes byte array
* @param offset offset
* @param length length
* @return byte buffer
*/
public static ClickHouseByteBuffer of(byte[] bytes, int offset, int length) {
if (bytes == null || bytes.length == 0 || length == 0) {
return newInstance();
} else {
validate(bytes, offset, length);
}

return new ClickHouseByteBuffer(bytes, offset, length);
}

static void validate(byte[] bytes, int offset, int length) {
int len = ClickHouseChecker.nonNull(bytes, "Byte array").length;
if (ClickHouseChecker.between(offset, "Offset", 0, len)
+ ClickHouseChecker.between(length, "Length", 0, len) > len) {
throw new IllegalArgumentException(
ClickHouseUtils.format("Offset(%d) plus length(%d) should not greater than %d", offset, length,
len));
}
}

protected byte[] array;
protected int position;
protected int length;

protected ClickHouseByteBuffer(byte[] bytes, int offset, int length) {
this.array = bytes;
this.position = offset;
this.length = length;
}

public boolean isEmpty() {
return length < 1;
}

public ClickHouseByteBuffer reset() {
array = EMPTY_BYTES;
position = 0;
length = 0;
return this;
}

public ClickHouseByteBuffer update(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
reset();
} else {
array = bytes;
position = 0;
length = bytes.length;
}

return this;
}

public ClickHouseByteBuffer update(byte[] bytes, int offset, int length) {
if (bytes == null || bytes.length == 0 || length == 0) {
return reset();
} else {
validate(bytes, offset, length);
}

this.array = bytes;
this.position = offset;
this.length = length;
return this;
}

public byte[] array() {
return array;
}

public int position() {
return position;
}

public int length() {
return length;
}

public int limit() {
return position + length;
}

@Override
public int hashCode() {
final int prime = 31;
int result = prime + Arrays.hashCode(array);
result = prime * result + position;
result = prime * result + length;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj == null || getClass() != obj.getClass()) {
return false;
}

ClickHouseByteBuffer other = (ClickHouseByteBuffer) obj;
return Arrays.equals(array, other.array) && position == other.position && length == other.length;
}

@Override
public String toString() {
return new StringBuilder().append(getClass().getSimpleName()).append("array=").append(array)
.append(", position=").append(position).append(", length=").append(length).append(')').toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected static final Object mergeMetricRegistry(List<ClickHouseConfig> list) {
private final int maxExecutionTime;
private final int maxQueuedBuffers;
private final int maxQueuedRequests;
private final int maxResultRows;
private final long maxResultRows;
private final int maxThreads;
private final boolean retry;
private final boolean reuseValueWrapper;
Expand Down Expand Up @@ -193,7 +193,7 @@ public ClickHouseConfig(Map<ClickHouseOption, Serializable> options, ClickHouseC
this.maxExecutionTime = (int) getOption(ClickHouseClientOption.MAX_EXECUTION_TIME);
this.maxQueuedBuffers = (int) getOption(ClickHouseClientOption.MAX_QUEUED_BUFFERS);
this.maxQueuedRequests = (int) getOption(ClickHouseClientOption.MAX_QUEUED_REQUESTS);
this.maxResultRows = (int) getOption(ClickHouseClientOption.MAX_RESULT_ROWS);
this.maxResultRows = (long) getOption(ClickHouseClientOption.MAX_RESULT_ROWS);
this.maxThreads = (int) getOption(ClickHouseClientOption.MAX_THREADS_PER_CLIENT);
this.retry = (boolean) getOption(ClickHouseClientOption.RETRY);
this.reuseValueWrapper = (boolean) getOption(ClickHouseClientOption.REUSE_VALUE_WRAPPER);
Expand Down Expand Up @@ -291,7 +291,7 @@ public int getMaxQueuedRequests() {
return maxQueuedRequests;
}

public int getMaxResultRows() {
public long getMaxResultRows() {
return maxResultRows;
}

Expand Down
Loading

0 comments on commit 3d921d4

Please sign in to comment.