Skip to content

Commit 86df0ae

Browse files
author
Paultagoras
committed
Tweaking based on discussion
1 parent 2632c6a commit 86df0ae

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import com.clickhouse.client.api.internal.ValidationUtils;
1010
import com.clickhouse.data.ClickHouseColumn;
1111

12+
import java.io.IOException;
1213
import java.io.InputStream;
14+
import java.lang.reflect.InvocationTargetException;
1315
import java.lang.reflect.Method;
1416
import java.math.BigDecimal;
1517
import java.math.BigInteger;
@@ -41,7 +43,8 @@ public class Client {
4143
private Set<String> endpoints;
4244
private Map<String, String> configuration;
4345
private List<ClickHouseNode> serverNodes = new ArrayList<>();
44-
private Map<Class<?>, List<POJOSerializer>> serializers = new HashMap<>();
46+
private Map<Class<?>, Map<ClickHouseColumn, POJOSerializer>> serializers;
47+
private Map<Class<?>, Map<String, Method>> getters;
4548
private static final Logger LOG = LoggerFactory.getLogger(Client.class);
4649

4750
private Client(Set<String> endpoints, Map<String,String> configuration) {
@@ -50,6 +53,8 @@ private Client(Set<String> endpoints, Map<String,String> configuration) {
5053
this.endpoints.forEach(endpoint -> {
5154
this.serverNodes.add(ClickHouseNode.of(endpoint, this.configuration));
5255
});
56+
this.serializers = new HashMap<>();
57+
this.getters = new HashMap<>();
5358
}
5459

5560
public static class Builder {
@@ -173,7 +178,7 @@ public boolean ping(int timeout) {
173178
public void register(Class<?> clazz, TableSchema schema) {
174179
//Create a new POJOSerializer with static .serialize(object, columns) methods
175180
Map<String, Method> getters = new HashMap<>();
176-
List<POJOSerializer> serializers = new ArrayList<>();
181+
Map<ClickHouseColumn, POJOSerializer> serializers = new HashMap<>();
177182

178183
//Retrieve all methods
179184
for (Method method: clazz.getMethods()) {
@@ -185,42 +190,50 @@ public void register(Class<?> clazz, TableSchema schema) {
185190
ClickHouseColumn column = schema.getColumnByName(fieldName);
186191
if(column != null) {//Check if the field is in the schema
187192
getters.put(fieldName, method);
188-
serializers.add((obj, stream, columns) -> {//Create the field serializer
189-
if (columns == null || columns.contains(fieldName)) {
190-
Method getter = getters.get(fieldName);
191-
Object value = getter.invoke(obj);
192-
193-
//Handle null values
194-
if (value == null) {
195-
BinaryStreamUtils.writeNull(stream);
196-
return;
197-
} else {//If nullable, we have to specify that the value is not null
198-
if (column.isNullable()) {
199-
BinaryStreamUtils.writeNonNull(stream);
200-
}
193+
serializers.put(column, (obj, stream) -> {//Create the field serializer
194+
Method getter = this.getters.get(clazz).get(fieldName);
195+
Object value = getter.invoke(obj);
196+
197+
//Handle null values
198+
if (value == null) {
199+
BinaryStreamUtils.writeNull(stream);
200+
return;
201+
} else {//If nullable, we have to specify that the value is not null
202+
if (column.isNullable()) {
203+
BinaryStreamUtils.writeNonNull(stream);
201204
}
202-
203-
//Handle the different types
204-
SerializerUtils.serializeData(stream, value, column);
205205
}
206+
207+
//Handle the different types
208+
SerializerUtils.serializeData(stream, value, column);
206209
});
207210
}
208211
}
209212
}
210213

211214
this.serializers.put(clazz, serializers);
215+
this.getters.put(clazz, getters);
212216
}
213217

214218
/**
215219
* Insert data into ClickHouse using a POJO
216220
*/
217221
public Future<InsertResponse> insert(String tableName,
218222
List<Object> data,
219-
InsertSettings settings,
220-
List<ClickHouseColumn> columns) throws ClickHouseException, SocketException {
223+
InsertSettings settings) throws ClickHouseException, IOException, InvocationTargetException, IllegalAccessException {
221224
//Lookup the Serializer for the POJO
222225
//Call the static .serialize method on the POJOSerializer for each object in the list
223-
return null;//This is just a placeholder
226+
List<POJOSerializer> serializers = this.serializers.get(data.get(0).getClass());
227+
if (serializers == null || serializers.isEmpty()) {
228+
throw new IllegalArgumentException("No serializer found for the given class. Please register() before calling this method.");
229+
}
230+
231+
for (Object obj: data) {
232+
for (POJOSerializer serializer: serializers) {
233+
serializer.serialize(data, null);
234+
}
235+
}
236+
224237
}
225238

226239
/**

client-v2/src/main/java/com/clickhouse/client/api/insert/POJOSerializer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import java.io.IOException;
44
import java.io.OutputStream;
55
import java.lang.reflect.InvocationTargetException;
6-
import java.util.List;
76

87
public interface POJOSerializer {
9-
void serialize(Object obj, OutputStream outputStream, List<String> columns) throws InvocationTargetException, IllegalAccessException, IOException;
8+
void serialize(Object obj, OutputStream outputStream) throws InvocationTargetException, IllegalAccessException, IOException;
109
}

client-v2/src/main/java/com/clickhouse/client/api/internal/SerializerUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private static void serializePrimitiveData(OutputStream stream, Object value, Cl
127127
case Date32:
128128
BinaryStreamUtils.writeDate32(stream, (LocalDate) value);
129129
break;
130-
case DateTime:
130+
case DateTime: //TODO: Discuss LocalDateTime vs ZonedDateTime and time zones (Who uses LocalDateTime?)
131131
BinaryStreamUtils.writeDateTime(stream, (LocalDateTime) value, column.getTimeZone());
132132
break;
133133
case DateTime64:

0 commit comments

Comments
 (0)