Skip to content

Commit

Permalink
Merge pull request #1327 from pan3793/con-map
Browse files Browse the repository at this point in the history
Use ConcurrentHashMap for DataType SerDe cache
  • Loading branch information
zhicwu authored Apr 24, 2023
2 parents 92f7fc6 + 47535ed commit 355cca0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;

import com.clickhouse.data.ClickHouseArraySequence;
import com.clickhouse.data.ClickHouseChecker;
Expand Down Expand Up @@ -139,11 +139,11 @@ public void serialize(ClickHouseValue value, ClickHouseOutputStream output) thro
}

static class DateSerDe implements ClickHouseDeserializer, ClickHouseSerializer {
private static final Map<TimeZone, DateSerDe> cache = new HashMap<>();
private static final Map<TimeZone, DateSerDe> cache = new ConcurrentHashMap<>();

public static final DateSerDe of(ClickHouseDataConfig config) {
TimeZone tz = ClickHouseChecker.nonNull(config, ClickHouseDataConfig.TYPE_NAME).getTimeZoneForDate();
return cache.computeIfAbsent(tz, DateSerDe::new);
return cache.computeIfAbsent(tz == null ? ClickHouseValues.SYS_TIMEZONE : tz, DateSerDe::new);
}

protected final ZoneId zoneId;
Expand Down Expand Up @@ -174,11 +174,11 @@ public void serialize(ClickHouseValue value, ClickHouseOutputStream output) thro
}

static class Date32SerDe implements ClickHouseDeserializer, ClickHouseSerializer {
private static final Map<TimeZone, Date32SerDe> cache = new HashMap<>();
private static final Map<TimeZone, Date32SerDe> cache = new ConcurrentHashMap<>();

public static final Date32SerDe of(ClickHouseDataConfig config) {
TimeZone tz = ClickHouseChecker.nonNull(config, ClickHouseDataConfig.TYPE_NAME).getTimeZoneForDate();
return cache.computeIfAbsent(tz, Date32SerDe::new);
return cache.computeIfAbsent(tz == null ? ClickHouseValues.SYS_TIMEZONE : tz, Date32SerDe::new);
}

protected final ZoneId zoneId;
Expand Down Expand Up @@ -210,7 +210,7 @@ public void serialize(ClickHouseValue value, ClickHouseOutputStream output) thro
}

static class DateTime32SerDe implements ClickHouseDeserializer, ClickHouseSerializer {
private static final Map<TimeZone, DateTime32SerDe> cache = new HashMap<>();
private static final Map<TimeZone, DateTime32SerDe> cache = new ConcurrentHashMap<>();

public static final DateTime32SerDe of(ClickHouseDataConfig config, ClickHouseColumn column) {
TimeZone tz = ClickHouseChecker.nonNull(column, ClickHouseColumn.TYPE_NAME).hasTimeZone()
Expand Down Expand Up @@ -246,7 +246,7 @@ static class DateTime64SerDe implements ClickHouseDeserializer, ClickHouseSerial
private static final int[] BASES = new int[] { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
1000000000 };
// use combined key for all timezones?
private static final Map<Integer, DateTime64SerDe> cache = new HashMap<>();
private static final Map<Integer, DateTime64SerDe> cache = new ConcurrentHashMap<>();

public static final DateTime64SerDe of(ClickHouseDataConfig config, ClickHouseColumn column) {
TimeZone tz = ClickHouseChecker.nonNull(column, ClickHouseColumn.TYPE_NAME).hasTimeZone()
Expand Down Expand Up @@ -341,7 +341,7 @@ public void serialize(ClickHouseValue value, ClickHouseOutputStream output) thro
}

static class Decimal32SerDe extends DecimalSerDe {
private static final Map<Integer, DecimalSerDe> cache = new HashMap<>();
private static final Map<Integer, DecimalSerDe> cache = new ConcurrentHashMap<>();

public static final DecimalSerDe of(ClickHouseColumn column) {
int scale = ClickHouseChecker.nonNull(column, ClickHouseColumn.TYPE_NAME).getScale();
Expand Down Expand Up @@ -369,7 +369,7 @@ public void serialize(ClickHouseValue value, ClickHouseOutputStream output) thro
}

static class Decimal64SerDe extends DecimalSerDe {
private static final Map<Integer, DecimalSerDe> cache = new HashMap<>();
private static final Map<Integer, DecimalSerDe> cache = new ConcurrentHashMap<>();

public static final DecimalSerDe of(ClickHouseColumn column) {
int scale = ClickHouseChecker.nonNull(column, ClickHouseColumn.TYPE_NAME).getScale();
Expand Down Expand Up @@ -401,7 +401,7 @@ public void serialize(ClickHouseValue value, ClickHouseOutputStream output) thro
}

static class Decimal128SerDe extends DecimalSerDe {
private static final Map<Integer, DecimalSerDe> cache = new HashMap<>();
private static final Map<Integer, DecimalSerDe> cache = new ConcurrentHashMap<>();

public static final DecimalSerDe of(ClickHouseColumn column) {
int scale = ClickHouseChecker.nonNull(column, ClickHouseColumn.TYPE_NAME).getScale();
Expand Down Expand Up @@ -433,7 +433,7 @@ public void serialize(ClickHouseValue value, ClickHouseOutputStream output) thro
}

static class Decimal256SerDe extends DecimalSerDe {
private static final Map<Integer, DecimalSerDe> cache = new HashMap<>();
private static final Map<Integer, DecimalSerDe> cache = new ConcurrentHashMap<>();

public static final DecimalSerDe of(ClickHouseColumn column) {
int scale = ClickHouseChecker.nonNull(column, ClickHouseColumn.TYPE_NAME).getScale();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.clickhouse.data.ClickHouseByteBuffer;
import com.clickhouse.data.ClickHouseChecker;
Expand All @@ -17,7 +17,7 @@

public interface TextDataProcessor {
static class TextSerDe implements ClickHouseDeserializer, ClickHouseSerializer {
private static final Map<String, TextSerDe> cache = new HashMap<>();
private static final Map<String, TextSerDe> cache = new ConcurrentHashMap<>();

public static final TextSerDe of(byte escapeChar, byte recordSeparator, byte valueSeparator, String nullValue) {
String key = new StringBuffer().append((char) escapeChar).append((char) recordSeparator)
Expand Down

0 comments on commit 355cca0

Please sign in to comment.