Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove guava #12

Merged
merged 7 commits into from
Aug 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import ru.yandex.clickhouse.util.ClickHouseHttpClientBuilder;
import ru.yandex.clickhouse.util.LogProxy;
import ru.yandex.clickhouse.util.TypeUtils;
import ru.yandex.clickhouse.util.Utils;
import ru.yandex.clickhouse.util.guava.StreamUtils;

import java.io.IOException;
Expand Down
45 changes: 28 additions & 17 deletions src/main/java/ru/yandex/clickhouse/ClickHouseUtil.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
package ru.yandex.clickhouse;


import com.google.common.escape.Escaper;
import com.google.common.escape.Escapers;
import java.util.Map;

public final class ClickHouseUtil {

private static final Map<Character, String> CHARS_MAP = Map.of(
'\\', "\\\\",
'\n', "\\n",
'\t', "\\t",
'\b', "\\b",
'\f', "\\f",
'\r', "\\r",
'\0', "\\0",
'\'', "\\'",
'`', "\\`"
);

private ClickHouseUtil() {
}

private final static Escaper CLICKHOUSE_ESCAPER = Escapers.builder()
.addEscape('\\', "\\\\")
.addEscape('\n', "\\n")
.addEscape('\t', "\\t")
.addEscape('\b', "\\b")
.addEscape('\f', "\\f")
.addEscape('\r', "\\r")
.addEscape('\0', "\\0")
.addEscape('\'', "\\'")
.addEscape('`', "\\`")
.build();

public static String escape(String s) {
if (s == null) {
return "\\N";
}
return CLICKHOUSE_ESCAPER.escape(s);

StringBuilder builder = new StringBuilder();

for (int i = 0; i < s.length(); i++) {
char currentChar = s.charAt(i);
if (CHARS_MAP.containsKey(currentChar)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use get and != null, otherwise key lookup happens twice.

String replacement = CHARS_MAP.get(currentChar);
builder.append(replacement);
} else {
builder.append(currentChar);
}
}

return builder.toString();
}

static String quoteIdentifier(String s) {
if (s == null) {
throw new IllegalArgumentException("Can't quote null as identifier");
}
String escaped = CLICKHOUSE_ESCAPER.escape(s);
String escaped = escape(s);
return '`' + escaped + '`';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.apache.http.conn.ConnectTimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.yandex.clickhouse.util.Utils;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,28 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import ru.yandex.clickhouse.Jackson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;


class ArrayToStringDeserializer extends JsonDeserializer<List<String>> {

private static final LoadingCache<DeserializationContext, JsonDeserializer<Object>> deserializers
= CacheBuilder.newBuilder()
.weakKeys()
.concurrencyLevel(16)
.maximumSize(10000)
.build(new CacheLoader<>() {
@Override
public JsonDeserializer<Object> load(DeserializationContext ctxt) throws Exception {
return ctxt.findContextualValueDeserializer(TypeFactory.defaultInstance()
.constructType(new TypeReference<List<Object>>() {
}), null);
}
});

@Override
public List<String> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
JsonDeserializer<Object> deserializer;
try {
deserializer = deserializers.get(ctxt);
} catch (ExecutionException e){
throw new RuntimeException(e);
}
JsonDeserializer<Object> deserializer = getJsonDeserializer(ctxt);

final Object deserialized = deserializer.deserialize(jp, ctxt);
if (!(deserialized instanceof List)){
throw new IllegalStateException();
}
//noinspection unchecked
final List<Object> deserializedList = (List) deserialized;

final List deserializedList = (List) deserialized;
List<String> result = new ArrayList<>();
for (Object x : deserializedList) {
String v = null;
Expand All @@ -65,4 +43,15 @@ public List<String> deserialize(JsonParser jp, DeserializationContext ctxt) thro
return result;
}

private JsonDeserializer<Object> getJsonDeserializer(DeserializationContext ctxt) {
try {
TypeReference<List<Object>> typeRef = new TypeReference<>() {
};
JavaType type = TypeFactory.defaultInstance().constructType(typeRef);

return ctxt.findContextualValueDeserializer(type, null);
} catch (JsonMappingException e) {
throw new RuntimeException(e);
}
}
}
25 changes: 21 additions & 4 deletions src/main/java/ru/yandex/clickhouse/response/ByteFragmentUtils.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
package ru.yandex.clickhouse.response;


import com.google.common.primitives.Primitives;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.Objects;

final class ByteFragmentUtils {

private static final char ARRAY_ELEMENTS_SEPARATOR = ',';
private static final char STRING_QUOTATION = '\'';

private static Map<Class, Class> WRAPPER_TO_PRIMITIVE_TYPE = Map.of(
Boolean.class, boolean.class,
Byte.class, byte.class,
Character.class, char.class,
Double.class, double.class,
Float.class, float.class,
Integer.class, int.class,
Long.class, long.class,
Short.class, short.class,
Void.class, void.class
);

private ByteFragmentUtils() {
}

Expand Down Expand Up @@ -172,7 +183,7 @@ static Object parseArray(ByteFragment value, Class elementClass, boolean useObje

int index = 0;
Object array = java.lang.reflect.Array.newInstance(
useObjects ? elementClass : Primitives.unwrap(elementClass),
useObjects ? elementClass : unwrap(elementClass),
getArrayLength(trim)
);
int fieldStart = 0;
Expand Down Expand Up @@ -305,4 +316,10 @@ private static int getArrayLength(ByteFragment value) {
}
return length;
}

private static Class unwrap(Class type) {
Objects.requireNonNull(type);
Class unwrapped = WRAPPER_TO_PRIMITIVE_TYPE.get(type);
return (unwrapped == null) ? type : unwrapped;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ru.yandex.clickhouse.util;

import com.google.common.base.Preconditions;
import com.google.common.io.LittleEndianDataOutputStream;
import com.google.common.primitives.UnsignedLong;
import ru.yandex.clickhouse.settings.ClickHouseProperties;
Expand All @@ -11,6 +10,7 @@
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Date;
import java.util.Objects;
import java.util.TimeZone;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -38,7 +38,10 @@ public ClickHouseRowBinaryStream(OutputStream outputStream, TimeZone timeZone, C
}

public void writeUnsignedLeb128(int value) throws IOException {
Preconditions.checkArgument(value >= 0);
if (value < 0) {
throw new IllegalArgumentException();
}

int remaining = value >>> 7;
while (remaining != 0) {
out.write((byte) ((value & 0x7f) | 0x80));
Expand Down Expand Up @@ -70,7 +73,7 @@ public void writeByte(byte b) throws IOException {
}

public void writeString(String string) throws IOException {
Preconditions.checkNotNull(string);
Objects.requireNonNull(string);
byte[] bytes = string.getBytes(StreamUtils.UTF_8);
writeUnsignedLeb128(bytes.length);
out.write(bytes);
Expand Down Expand Up @@ -144,12 +147,12 @@ public void writeUInt64(UnsignedLong value) throws IOException {
}

public void writeDateTime(Date date) throws IOException {
Preconditions.checkNotNull(date);
Objects.requireNonNull(date);
writeUInt32(TimeUnit.MILLISECONDS.toSeconds(date.getTime()));
}

public void writeDate(Date date) throws IOException {
Preconditions.checkNotNull(date);
Objects.requireNonNull(date);
long localMillis = date.getTime() + timeZone.getOffset(date.getTime());
int daysSinceEpoch = (int) (localMillis / MILLIS_IN_DAY);
writeUInt16(daysSinceEpoch);
Expand All @@ -165,110 +168,110 @@ public void writeFloat64(double value) throws IOException {


public void writeDateArray(Date[] dates) throws IOException {
Preconditions.checkNotNull(dates);
Objects.requireNonNull(dates);
writeUnsignedLeb128(dates.length);
for (Date date: dates) {
writeDate(date);
}
}

public void writeDateTimeArray(Date[] dates) throws IOException {
Preconditions.checkNotNull(dates);
Objects.requireNonNull(dates);
writeUnsignedLeb128(dates.length);
for (Date date: dates) {
writeDateTime(date);
}
}

public void writeStringArray(String[] strings) throws IOException {
Preconditions.checkNotNull(strings);
Objects.requireNonNull(strings);
writeUnsignedLeb128(strings.length);
for (String el : strings) {
writeString(el);
}
}

public void writeInt8Array(byte[] bytes) throws IOException {
Preconditions.checkNotNull(bytes);
Objects.requireNonNull(bytes);
writeUnsignedLeb128(bytes.length);
for (byte b: bytes) {
writeInt8(b);
}
}
public void writeInt8Array(int[] ints) throws IOException {
Preconditions.checkNotNull(ints);
Objects.requireNonNull(ints);
writeUnsignedLeb128(ints.length);
for (int i: ints) {
writeInt8(i);
}
}

public void writeUInt8Array(int[] ints) throws IOException {
Preconditions.checkNotNull(ints);
Objects.requireNonNull(ints);
writeUnsignedLeb128(ints.length);
for (int i: ints) {
writeUInt8(i);
}
}

public void writeInt16Array(short[] shorts) throws IOException {
Preconditions.checkNotNull(shorts);
Objects.requireNonNull(shorts);
writeUnsignedLeb128(shorts.length);
for (short s: shorts) {
writeInt16(s);
}
}

public void writeUInt16Array(int[] ints) throws IOException {
Preconditions.checkNotNull(ints);
Objects.requireNonNull(ints);
writeUnsignedLeb128(ints.length);
for (int i: ints) {
writeUInt16(i);
}
}

public void writeInt32Array(int[] ints) throws IOException {
Preconditions.checkNotNull(ints);
Objects.requireNonNull(ints);
writeUnsignedLeb128(ints.length);
for (int i: ints) {
writeInt32(i);
}
}

public void writeUInt32Array(long[] longs) throws IOException {
Preconditions.checkNotNull(longs);
Objects.requireNonNull(longs);
writeUnsignedLeb128(longs.length);
for (long l: longs) {
writeUInt32(l);
}
}

public void writeInt64Array(long[] longs) throws IOException {
Preconditions.checkNotNull(longs);
Objects.requireNonNull(longs);
writeUnsignedLeb128(longs.length);
for (long l: longs) {
writeInt64(l);
}
}

public void writeUInt64Array(long[] longs) throws IOException {
Preconditions.checkNotNull(longs);
Objects.requireNonNull(longs);
writeUnsignedLeb128(longs.length);
for (long l: longs) {
writeUInt64(l);
}
}

public void writeFloat32Array(float[] floats) throws IOException {
Preconditions.checkNotNull(floats);
Objects.requireNonNull(floats);
writeUnsignedLeb128(floats.length);
for (float f: floats) {
writeFloat32(f);
}
}

public void writeFloat64Array(double[] doubles) throws IOException {
Preconditions.checkNotNull(doubles);
Objects.requireNonNull(doubles);
writeUnsignedLeb128(doubles.length);
for (double d: doubles) {
writeFloat64(d);
Expand Down Expand Up @@ -298,7 +301,7 @@ void markNextNullable(boolean isNullable) throws IOException {
}

public void writeUUID(UUID uuid) throws IOException {
Preconditions.checkNotNull(uuid);
Objects.requireNonNull(uuid);
ByteBuffer bb = ByteBuffer.wrap(new byte[16]).order(ByteOrder.LITTLE_ENDIAN);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
Expand Down