Skip to content

Commit 30d84af

Browse files
committed
added common settings to reduce code duplication
1 parent c9b62cb commit 30d84af

File tree

4 files changed

+313
-108
lines changed

4 files changed

+313
-108
lines changed
Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
package com.clickhouse.client.api.insert;
22

3-
import com.clickhouse.client.ClickHouseProtocol;
43
import com.clickhouse.client.api.Client;
54
import com.clickhouse.client.api.ClientConfigProperties;
6-
import com.clickhouse.client.api.enums.Protocol;
7-
import com.clickhouse.client.api.internal.ValidationUtils;
5+
import com.clickhouse.client.api.internal.CommonSettings;
86
import org.apache.hc.core5.http.HttpHeaders;
97

108
import java.util.Collection;
11-
import java.util.HashMap;
129
import java.util.Map;
1310

1411
public class InsertSettings {
1512
private static final int DEFAULT_INPUT_STREAM_BATCH_SIZE = 8196;
1613

1714
private int inputStreamCopyBufferSize;
18-
private String operationId;
19-
Map<String, Object> rawSettings;
15+
CommonSettings settings;
2016

2117
public InsertSettings() {
22-
rawSettings = new HashMap<>();
18+
settings = new CommonSettings();
2319
setDefaults();
2420
}
2521

2622
public InsertSettings(Map<String, Object> settings) {
27-
rawSettings = new HashMap<>();
23+
this.settings = new CommonSettings();
2824
setDefaults();
29-
rawSettings.putAll(settings);
25+
for (Map.Entry<String, Object> entry : settings.entrySet()) {
26+
this.settings.setOption(entry.getKey(), entry.getValue());
27+
}
3028
}
3129

3230
private void setDefaults() {// Default settings, for now a very small list
@@ -40,7 +38,7 @@ private void setDefaults() {// Default settings, for now a very small list
4038
* @return configuration option value
4139
*/
4240
public Object getOption(String option) {
43-
return rawSettings.get(option);
41+
return settings.getOption(option);
4442
}
4543

4644
/**
@@ -51,10 +49,7 @@ public Object getOption(String option) {
5149
* @param value - configuration option value
5250
*/
5351
public InsertSettings setOption(String option, Object value) {
54-
rawSettings.put(option, value);
55-
if (option.equals(ClientConfigProperties.PRODUCT_NAME.getKey())) {
56-
rawSettings.put(ClientConfigProperties.CLIENT_NAME.getKey(), value);
57-
}
52+
settings.setOption(option, value);
5853
return this;
5954
}
6055

@@ -64,7 +59,7 @@ public InsertSettings setOption(String option, Object value) {
6459
* @return all settings
6560
*/
6661
public Map<String, Object> getAllSettings() {
67-
return rawSettings;
62+
return settings.getAllSettings();
6863
}
6964

7065
/**
@@ -79,14 +74,14 @@ public InsertSettings setDeduplicationToken(String token) {
7974
}
8075

8176
public String getQueryId() {
82-
return (String) rawSettings.get(ClientConfigProperties.QUERY_ID.getKey());
77+
return settings.getQueryId();
8378
}
8479

8580
/**
8681
* Sets the query id. This id will be sent to the server and can be used to identify the query.
8782
*/
8883
public InsertSettings setQueryId(String queryId) {
89-
rawSettings.put(ClientConfigProperties.QUERY_ID.getKey(), queryId);
84+
settings.setQueryId(queryId);
9085
return this;
9186
}
9287

@@ -108,7 +103,7 @@ public InsertSettings setInputStreamCopyBufferSize(int size) {
108103
* Should not be called directly.
109104
*/
110105
public String getOperationId() {
111-
return this.operationId;
106+
return settings.getOperationId();
112107
}
113108

114109
/**
@@ -118,21 +113,20 @@ public String getOperationId() {
118113
* @param operationId - operation id
119114
*/
120115
public InsertSettings setOperationId(String operationId) {
121-
this.operationId = operationId;
116+
settings.setOperationId(operationId);
122117
return this;
123118
}
124119

125120
/**
126121
* Sets database to be used for a request.
127122
*/
128123
public InsertSettings setDatabase(String database) {
129-
ValidationUtils.checkNonBlank(database, "database");
130-
rawSettings.put("database", database);
124+
settings.setDatabase(database);
131125
return this;
132126
}
133127

134128
public String getDatabase() {
135-
return (String) rawSettings.get("database");
129+
return settings.getDatabase();
136130
}
137131

138132
/**
@@ -141,12 +135,12 @@ public String getDatabase() {
141135
* @param enabled - indicates if client request compression is enabled
142136
*/
143137
public InsertSettings compressClientRequest(boolean enabled) {
144-
this.rawSettings.put(ClientConfigProperties.COMPRESS_CLIENT_REQUEST.getKey(), enabled);
138+
settings.setOption(ClientConfigProperties.COMPRESS_CLIENT_REQUEST.getKey(), enabled);
145139
return this;
146140
}
147141

148142
public InsertSettings useHttpCompression(boolean enabled) {
149-
this.rawSettings.put(ClientConfigProperties.USE_HTTP_COMPRESSION.getKey(), enabled);
143+
settings.setOption(ClientConfigProperties.USE_HTTP_COMPRESSION.getKey(), enabled);
150144
return this;
151145
}
152146

@@ -156,48 +150,50 @@ public InsertSettings useHttpCompression(boolean enabled) {
156150
* @param enabled - if application provides compressed data
157151
*/
158152
public InsertSettings appCompressedData(boolean enabled, String compressionMethod) {
159-
this.rawSettings.put(ClientConfigProperties.APP_COMPRESSED_DATA.getKey(), enabled);
153+
settings.setOption(ClientConfigProperties.APP_COMPRESSED_DATA.getKey(), enabled);
160154
useHttpCompression(true);
161155
httpHeader(HttpHeaders.CONTENT_ENCODING, compressionMethod);
162156
return this;
163157
}
164158

165159
public boolean isClientRequestEnabled() {
166-
return (Boolean) rawSettings.get("decompress");
160+
return (Boolean) settings.getOption(ClientConfigProperties.COMPRESS_CLIENT_REQUEST.getKey());
167161
}
168162

169163
/**
170164
* Defines list of headers that should be sent with current request. The Client will use a header value
171165
* defined in {@code headers} instead of any other.
172166
*
173-
* @see Client.Builder#httpHeaders(Map)
174-
* @param key - header name.
167+
* @param key - header name.
175168
* @param value - header value.
176169
* @return same instance of the builder
170+
* @see Client.Builder#httpHeaders(Map)
177171
*/
178172
public InsertSettings httpHeader(String key, String value) {
179-
rawSettings.put(ClientConfigProperties.httpHeader(key), value);
173+
settings.httpHeader(key, value);
180174
return this;
181175
}
182176

183177
/**
184178
* {@see #httpHeader(String, String)} but for multiple values.
185-
* @param key - name of the header
179+
*
180+
* @param key - name of the header
186181
* @param values - collection of values
187182
* @return same instance of the builder
188183
*/
189184
public InsertSettings httpHeader(String key, Collection<String> values) {
190-
rawSettings.put(ClientConfigProperties.httpHeader(key), ClientConfigProperties.commaSeparated(values));
185+
settings.httpHeader(key, values);
191186
return this;
192187
}
193188

194189
/**
195190
* {@see #httpHeader(String, String)} but for multiple headers.
191+
*
196192
* @param headers - map of headers
197193
* @return same instance of the builder
198194
*/
199195
public InsertSettings httpHeaders(Map<String, String> headers) {
200-
headers.forEach(this::httpHeader);
196+
settings.httpHeaders(headers);
201197
return this;
202198
}
203199

@@ -206,24 +202,25 @@ public InsertSettings httpHeaders(Map<String, String> headers) {
206202
* defined in {@code settings} instead of any other.
207203
* Operation settings may override these values.
208204
*
209-
* @see Client.Builder#serverSetting(String, Collection)
210-
* @param name - name of the setting
205+
* @param name - name of the setting
211206
* @param value - value of the setting
212207
* @return same instance of the builder
208+
* @see Client.Builder#serverSetting(String, Collection)
213209
*/
214210
public InsertSettings serverSetting(String name, String value) {
215-
rawSettings.put(ClientConfigProperties.serverSetting(name), value);
211+
settings.serverSetting(name, value);
216212
return this;
217213
}
218214

219215
/**
220216
* {@see #serverSetting(String, String)} but for multiple values.
221-
* @param name - name of the setting without special prefix
217+
*
218+
* @param name - name of the setting without special prefix
222219
* @param values - collection of values
223220
* @return same instance of the builder
224221
*/
225222
public InsertSettings serverSetting(String name, Collection<String> values) {
226-
rawSettings.put(ClientConfigProperties.serverSetting(name), ClientConfigProperties.commaSeparated(values));
223+
settings.serverSetting(name, values);
227224
return this;
228225
}
229226

@@ -233,7 +230,7 @@ public InsertSettings serverSetting(String name, Collection<String> values) {
233230
* @param dbRoles
234231
*/
235232
public InsertSettings setDBRoles(Collection<String> dbRoles) {
236-
rawSettings.put(ClientConfigProperties.SESSION_DB_ROLES.getKey(), dbRoles);
233+
settings.setDBRoles(dbRoles);
237234
return this;
238235
}
239236

@@ -243,25 +240,21 @@ public InsertSettings setDBRoles(Collection<String> dbRoles) {
243240
* @return list of DB roles
244241
*/
245242
public Collection<String> getDBRoles() {
246-
return (Collection<String>) rawSettings.get(ClientConfigProperties.SESSION_DB_ROLES.getKey());
243+
return settings.getDBRoles();
247244
}
248245

249246
/**
250247
* Sets the comment that will be added to the query log record associated with the query.
248+
*
251249
* @param logComment - comment to be added to the log
252250
* @return same instance of the builder
253251
*/
254252
public InsertSettings logComment(String logComment) {
255-
this.logComment = logComment;
256-
if (logComment != null && !logComment.isEmpty()) {
257-
rawSettings.put(ClientConfigProperties.SETTING_LOG_COMMENT.getKey(), logComment);
258-
}
253+
settings.logComment(logComment);
259254
return this;
260255
}
261256

262-
private String logComment = null;
263-
264257
public String getLogComment() {
265-
return logComment;
258+
return settings.getLogComment();
266259
}
267260
}

0 commit comments

Comments
 (0)