-
Notifications
You must be signed in to change notification settings - Fork 623
Expand file tree
/
Copy pathCommonSettings.java
More file actions
300 lines (260 loc) · 9.95 KB
/
CommonSettings.java
File metadata and controls
300 lines (260 loc) · 9.95 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
package com.clickhouse.client.api.internal;
import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.ClientConfigProperties;
import com.clickhouse.client.api.Session;
import com.clickhouse.client.api.http.ClickHouseHttpProto;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* Class representing very common logic across all setting objects
* like setting database, getting option
*
*/
public class CommonSettings {
private String operationId;
private String logComment;
protected Map<String, Object> settings;
public CommonSettings() {
settings = new HashMap<>();
}
/**
* Gets a configuration option.
*
* @param option - configuration option name
* @return configuration option value
*/
public Object getOption(String option) {
return settings.get(option);
}
/**
* Gets a configuration option. If not set then defaultValue is returned.
*
* @param option - config option key
* @param defaultValue - default option to return when option is not set
* @return configuration option value
*/
public Object getOption(String option, Object defaultValue) {
return settings.getOrDefault(option, defaultValue);
}
public boolean hasOption(String option) {
return settings.containsKey(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 CommonSettings setOption(String option, Object value) {
settings.put(option, value);
if (option.equals(ClientConfigProperties.PRODUCT_NAME.getKey())) {
settings.put(ClientConfigProperties.CLIENT_NAME.getKey(), value);
}
return this;
}
public CommonSettings resetOption(String option) {
settings.remove(option);
return this;
}
/**
* Get all settings as an unmodifiable map.
*
* @return all settings
*/
public Map<String, Object> getAllSettings() {
return settings;
}
public String getQueryId() {
return (String) settings.get(ClientConfigProperties.QUERY_ID.getKey());
}
/**
* Sets the query id. This id will be sent to the server and can be used to identify the query.
*/
public CommonSettings setQueryId(String queryId) {
settings.put(ClientConfigProperties.QUERY_ID.getKey(), queryId);
return this;
}
public CommonSettings setSessionId(String sessionId) {
ValidationUtils.checkNonBlank(sessionId, ClickHouseHttpProto.QPARAM_SESSION_ID);
serverSetting(ClickHouseHttpProto.QPARAM_SESSION_ID, sessionId);
return this;
}
public String getSessionId() {
return (String) settings.get(ClientConfigProperties.serverSetting(ClickHouseHttpProto.QPARAM_SESSION_ID));
}
public CommonSettings setSessionCheck(boolean sessionCheck) {
serverSetting(ClickHouseHttpProto.QPARAM_SESSION_CHECK, sessionCheck ? "1" : "0");
return this;
}
public Boolean getSessionCheck() {
String value = (String) settings.get(ClientConfigProperties.serverSetting(ClickHouseHttpProto.QPARAM_SESSION_CHECK));
return value == null ? null : ("1".equals(value) || Boolean.parseBoolean(value));
}
public CommonSettings setSessionTimeout(int timeoutInSeconds) {
ValidationUtils.checkPositive(timeoutInSeconds, ClickHouseHttpProto.QPARAM_SESSION_TIMEOUT);
serverSetting(ClickHouseHttpProto.QPARAM_SESSION_TIMEOUT, String.valueOf(timeoutInSeconds));
return this;
}
public Integer getSessionTimeout() {
String value = (String) settings.get(ClientConfigProperties.serverSetting(ClickHouseHttpProto.QPARAM_SESSION_TIMEOUT));
return value == null ? null : Integer.valueOf(value);
}
public CommonSettings setSessionTimezone(String timezone) {
ValidationUtils.checkNonBlank(timezone, ClickHouseHttpProto.QPARAM_SESSION_TIMEZONE);
serverSetting(ClickHouseHttpProto.QPARAM_SESSION_TIMEZONE, timezone);
return this;
}
public String getSessionTimezone() {
return (String) settings.get(ClientConfigProperties.serverSetting(ClickHouseHttpProto.QPARAM_SESSION_TIMEZONE));
}
public CommonSettings use(Session session) {
ValidationUtils.checkNotNull(session, "session");
session.applyTo(settings);
return this;
}
/**
* Operation id. Used internally to register new operation.
* Should not be called directly.
*/
public String getOperationId() {
return this.operationId;
}
/**
* Operation id. Used internally to register new operation.
* Should not be called directly.
*
* @param operationId - operation id
*/
public CommonSettings setOperationId(String operationId) {
this.operationId = operationId;
return this;
}
/**
* Sets database to be used for a request.
*/
public CommonSettings setDatabase(String database) {
ValidationUtils.checkNonBlank(database, ClientConfigProperties.DATABASE.getKey());
settings.put(ClientConfigProperties.DATABASE.getKey(), database);
return this;
}
public String getDatabase() {
return (String) settings.get(ClientConfigProperties.DATABASE.getKey());
}
/**
* 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.
*
* @see Client.Builder#httpHeaders(Map)
* @param key - header name.
* @param value - header value.
* @return same instance of the builder
*/
public CommonSettings httpHeader(String key, String value) {
settings.put(ClientConfigProperties.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 CommonSettings httpHeader(String key, Collection<String> values) {
settings.put(ClientConfigProperties.httpHeader(key), ClientConfigProperties.commaSeparated(values));
return this;
}
/**
* {@see #httpHeader(String, String)} but for multiple headers.
* @param headers - map of headers
* @return same instance of the builder
*/
public CommonSettings httpHeaders(Map<String, String> headers) {
headers.forEach(this::httpHeader);
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.
*
* @see Client.Builder#serverSetting(String, Collection)
* @param name - name of the setting
* @param value - value of the setting
* @return same instance of the builder
*/
public CommonSettings serverSetting(String name, String value) {
settings.put(ClientConfigProperties.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 CommonSettings serverSetting(String name, Collection<String> values) {
settings.put(ClientConfigProperties.serverSetting(name), ClientConfigProperties.commaSeparated(values));
return this;
}
/**
* Sets DB roles for an operation. Roles that were set by {@link Client#setDBRoles(Collection)} will be overridden.
*
* @param dbRoles
*/
public CommonSettings setDBRoles(Collection<String> dbRoles) {
settings.put(ClientConfigProperties.SESSION_DB_ROLES.getKey(), dbRoles);
return this;
}
/**
* Gets DB roles for an operation.
*
* @return list of DB roles
*/
public Collection<String> getDBRoles() {
return (Collection<String>) settings.get(ClientConfigProperties.SESSION_DB_ROLES.getKey());
}
/**
* 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 CommonSettings logComment(String logComment) {
this.logComment = logComment;
if (logComment != null && !logComment.isEmpty()) {
settings.put(ClientConfigProperties.SETTING_LOG_COMMENT.getKey(), logComment);
}
return this;
}
public String getLogComment() {
return logComment;
}
/**
* Sets a network operation timeout.
* @param timeout
* @param unit
*/
public void setNetworkTimeout(long timeout, ChronoUnit unit) {
settings.put(ClientConfigProperties.SOCKET_OPERATION_TIMEOUT.getKey(), Duration.of(timeout, unit).toMillis());
}
/**
* Returns network timeout. Zero value is returned if no timeout is set.
* @return timeout in ms.
*/
public Long getNetworkTimeout() {
// Socket operation timeout must be integer because of OS interface
// Network timeout may be something else in the future. So we need to cast it to Long.
return ((Number) getOption(ClientConfigProperties.SOCKET_OPERATION_TIMEOUT.getKey(),
ClientConfigProperties.SOCKET_OPERATION_TIMEOUT.getDefObjVal())).longValue();
}
public CommonSettings copyAndMerge(CommonSettings override) {
CommonSettings copy = new CommonSettings();
copy.settings.putAll(settings);
copy.logComment = logComment;
copy.settings.putAll(override.settings);
return copy;
}
}