-
Notifications
You must be signed in to change notification settings - Fork 623
Expand file tree
/
Copy pathInsertSettings.java
More file actions
361 lines (315 loc) · 10.5 KB
/
InsertSettings.java
File metadata and controls
361 lines (315 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package com.clickhouse.client.api.insert;
import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.ClientConfigProperties;
import com.clickhouse.client.api.Session;
import com.clickhouse.client.api.internal.CommonSettings;
import org.apache.hc.core5.http.HttpHeaders;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.Map;
public class InsertSettings {
private static final int DEFAULT_INPUT_STREAM_BATCH_SIZE = 8196;
private int inputStreamCopyBufferSize;
CommonSettings settings;
public InsertSettings() {
settings = new CommonSettings();
setDefaults();
}
public InsertSettings(Map<String, Object> settings) {
this.settings = new CommonSettings();
setDefaults();
for (Map.Entry<String, Object> entry : settings.entrySet()) {
this.settings.setOption(entry.getKey(), entry.getValue());
}
}
private InsertSettings(CommonSettings settings) {
this.settings = settings;
setDefaults();
}
private void setDefaults() {// Default settings, for now a very small list
this.setInputStreamCopyBufferSize(DEFAULT_INPUT_STREAM_BATCH_SIZE);
}
/**
* Gets a configuration option.
*
* @param option - configuration option name
* @return configuration option value
*/
public Object getOption(String option) {
return settings.getOption(option);
}
/**
* Sets a configuration option. This method can be used to set any configuration option.
* There is no specific validation is done on the key or value.
*
* @param option - configuration option name
* @param value - configuration option value
*/
public InsertSettings setOption(String option, Object value) {
settings.setOption(option, value);
return this;
}
/**
* Get all settings as an unmodifiable map.
*
* @return all settings
*/
public Map<String, Object> getAllSettings() {
return settings.getAllSettings();
}
/**
* Sets the deduplication token. This token will be sent to the server and can be used to identify the query.
*
* @param token - deduplication token
* @return
*/
public InsertSettings setDeduplicationToken(String token) {
serverSetting("insert_deduplication_token", token);
return this;
}
public String getQueryId() {
return settings.getQueryId();
}
/**
* Sets the query id. This id will be sent to the server and can be used to identify the query.
*/
public InsertSettings setQueryId(String queryId) {
settings.setQueryId(queryId);
return this;
}
/**
* Sets ClickHouse session id for this operation.
*/
public InsertSettings setSessionId(String sessionId) {
settings.setSessionId(sessionId);
return this;
}
public String getSessionId() {
return settings.getSessionId();
}
/**
* Sets ClickHouse session check flag for this operation.
*/
public InsertSettings setSessionCheck(boolean sessionCheck) {
settings.setSessionCheck(sessionCheck);
return this;
}
public Boolean getSessionCheck() {
return settings.getSessionCheck();
}
/**
* Sets ClickHouse session timeout (seconds) for this operation.
*/
public InsertSettings setSessionTimeout(int timeoutInSeconds) {
settings.setSessionTimeout(timeoutInSeconds);
return this;
}
public Integer getSessionTimeout() {
return settings.getSessionTimeout();
}
/**
* Sets ClickHouse session timezone for this operation.
*/
public InsertSettings setSessionTimezone(String timezone) {
settings.setSessionTimezone(timezone);
return this;
}
public String getSessionTimezone() {
return settings.getSessionTimezone();
}
public InsertSettings use(Session session) {
settings.use(session);
return this;
}
public int getInputStreamCopyBufferSize() {
return this.inputStreamCopyBufferSize;
}
/**
* Copy buffer size. The buffer is used while write operation to copy data from user provided input stream
* to an output stream.
*/
public InsertSettings setInputStreamCopyBufferSize(int size) {
this.inputStreamCopyBufferSize = size;
return this;
}
/**
* Operation id. Used internally to register new operation.
* Should not be called directly.
*/
public String getOperationId() {
return settings.getOperationId();
}
/**
* Operation id. Used internally to register new operation.
* Should not be called directly.
*
* @param operationId - operation id
*/
public InsertSettings setOperationId(String operationId) {
settings.setOperationId(operationId);
return this;
}
/**
* Sets database to be used for a request.
*/
public InsertSettings setDatabase(String database) {
settings.setDatabase(database);
return this;
}
public String getDatabase() {
return settings.getDatabase();
}
/**
* Client request compression. If set to true client will compress the request.
*
* @param enabled - indicates if client request compression is enabled
*/
public InsertSettings compressClientRequest(boolean enabled) {
settings.setOption(ClientConfigProperties.COMPRESS_CLIENT_REQUEST.getKey(), enabled);
return this;
}
public InsertSettings useHttpCompression(boolean enabled) {
settings.setOption(ClientConfigProperties.USE_HTTP_COMPRESSION.getKey(), enabled);
return this;
}
/**
* Sets flag that indicates if application provides already compressed data
*
* @param enabled - if application provides compressed data
*/
public InsertSettings appCompressedData(boolean enabled, String compressionMethod) {
settings.setOption(ClientConfigProperties.APP_COMPRESSED_DATA.getKey(), enabled);
useHttpCompression(true);
httpHeader(HttpHeaders.CONTENT_ENCODING, compressionMethod);
return this;
}
/**
*
* @return true if client compression is enabled
* @deprecated because of typo
*/
public boolean isClientRequestEnabled() {
return isClientCompressionEnabled();
}
/**
* Returns indication if client request should be compressed (client side compression).
*
* @return true if client compression is enabled
*/
public boolean isClientCompressionEnabled() {
return (boolean) settings.getOption(
ClientConfigProperties.COMPRESS_CLIENT_REQUEST.getKey(),
false
);
}
/**
* Defines list of headers that should be sent with current request. The Client will use a header value
* defined in {@code headers} instead of any other.
*
* @param key - header name.
* @param value - header value.
* @return same instance of the builder
* @see Client.Builder#httpHeaders(Map)
*/
public InsertSettings httpHeader(String key, String value) {
settings.httpHeader(key, value);
return this;
}
/**
* {@see #httpHeader(String, String)} but for multiple values.
*
* @param key - name of the header
* @param values - collection of values
* @return same instance of the builder
*/
public InsertSettings httpHeader(String key, Collection<String> values) {
settings.httpHeader(key, values);
return this;
}
/**
* {@see #httpHeader(String, String)} but for multiple headers.
*
* @param headers - map of headers
* @return same instance of the builder
*/
public InsertSettings httpHeaders(Map<String, String> headers) {
settings.httpHeaders(headers);
return this;
}
/**
* Defines list of server settings that should be sent with each request. The Client will use a setting value
* defined in {@code settings} instead of any other.
* Operation settings may override these values.
*
* @param name - name of the setting
* @param value - value of the setting
* @return same instance of the builder
* @see Client.Builder#serverSetting(String, Collection)
*/
public InsertSettings serverSetting(String name, String value) {
settings.serverSetting(name, value);
return this;
}
/**
* {@see #serverSetting(String, String)} but for multiple values.
*
* @param name - name of the setting without special prefix
* @param values - collection of values
* @return same instance of the builder
*/
public InsertSettings serverSetting(String name, Collection<String> values) {
settings.serverSetting(name, values);
return this;
}
/**
* Sets DB roles for an operation. Roles that were set by {@link Client#setDBRoles(Collection)} will be overridden.
*
* @param dbRoles
*/
public InsertSettings setDBRoles(Collection<String> dbRoles) {
settings.setDBRoles(dbRoles);
return this;
}
/**
* Gets DB roles for an operation.
*
* @return list of DB roles
*/
public Collection<String> getDBRoles() {
return settings.getDBRoles();
}
/**
* Sets the comment that will be added to the query log record associated with the query.
*
* @param logComment - comment to be added to the log
* @return same instance of the builder
*/
public InsertSettings logComment(String logComment) {
settings.logComment(logComment);
return this;
}
public String getLogComment() {
return settings.getLogComment();
}
public static InsertSettings merge(InsertSettings source, InsertSettings override) {
CommonSettings mergedSettings = source.settings.copyAndMerge(override.settings);
InsertSettings insertSettings = new InsertSettings(mergedSettings);
insertSettings.setInputStreamCopyBufferSize(override.getInputStreamCopyBufferSize());
return insertSettings;
}
/**
* Sets a network operation timeout.
* @param timeout
* @param unit
*/
public void setNetworkTimeout(long timeout, ChronoUnit unit) {
settings.setNetworkTimeout(timeout, unit);
}
/**
* Returns network timeout. Zero value is returned if no timeout is set.
* @return timeout in ms.
*/
public Long getNetworkTimeout() {
return settings.getNetworkTimeout();
}
}