|
1 | 1 | package org.tarantool;
|
2 | 2 |
|
| 3 | +public class TarantoolClientConfig { |
3 | 4 |
|
4 |
| -import java.util.concurrent.TimeUnit; |
| 5 | + private String username; |
| 6 | + private String password; |
5 | 7 |
|
6 |
| -public class TarantoolClientConfig { |
| 8 | + private int defaultRequestSize; |
| 9 | + private int predictedFutures; |
7 | 10 |
|
8 |
| - /** |
9 |
| - * username and password for authorization |
10 |
| - */ |
11 |
| - public String username; |
| 11 | + private int writerThreadPriority; |
| 12 | + private int readerThreadPriority; |
| 13 | + |
| 14 | + private int sharedBufferSize; |
| 15 | + private double directWriteFactor; |
12 | 16 |
|
13 |
| - public String password; |
| 17 | + private boolean useNewCall; |
| 18 | + |
| 19 | + private long initTimeoutMillis; |
| 20 | + private long writeTimeoutMillis; |
| 21 | + |
| 22 | + public static TarantoolClientConfigBuilder builder() { |
| 23 | + return new TarantoolClientConfigBuilder(); |
| 24 | + } |
| 25 | + |
| 26 | + protected TarantoolClientConfig(TarantoolClientConfigBuilder builder) { |
| 27 | + this.username = builder.username; |
| 28 | + this.password = builder.password; |
| 29 | + this.defaultRequestSize = builder.defaultRequestSize; |
| 30 | + this.predictedFutures = builder.predictedFutures; |
| 31 | + this.writerThreadPriority = builder.writerThreadPriority; |
| 32 | + this.readerThreadPriority = builder.readerThreadPriority; |
| 33 | + this.sharedBufferSize = builder.sharedBufferSize; |
| 34 | + this.directWriteFactor = builder.directWriteFactor; |
| 35 | + this.useNewCall = builder.useNewCall; |
| 36 | + this.initTimeoutMillis = builder.initTimeoutMillis; |
| 37 | + this.writeTimeoutMillis = builder.writeTimeoutMillis; |
| 38 | + } |
14 | 39 |
|
15 | 40 | /**
|
16 |
| - * default ByteArrayOutputStream size when make query serialization |
| 41 | + * Gets an username for auth |
| 42 | + * |
| 43 | + * @return username |
17 | 44 | */
|
18 |
| - public int defaultRequestSize = 4096; |
| 45 | + public String getUsername() { |
| 46 | + return username; |
| 47 | + } |
19 | 48 |
|
20 | 49 | /**
|
21 |
| - * initial size for map which holds futures of sent request |
| 50 | + * Gets a password for auth |
| 51 | + * |
| 52 | + * @return password |
22 | 53 | */
|
23 |
| - public int predictedFutures = (int) ((1024 * 1024) / 0.75) + 1; |
| 54 | + public String getPassword() { |
| 55 | + return password; |
| 56 | + } |
24 | 57 |
|
| 58 | + /** |
| 59 | + * Gets a default request size when make query serialization |
| 60 | + * |
| 61 | + * @return |
| 62 | + */ |
| 63 | + public int getDefaultRequestSize() { |
| 64 | + return defaultRequestSize; |
| 65 | + } |
25 | 66 |
|
26 |
| - public int writerThreadPriority = Thread.NORM_PRIORITY; |
| 67 | + /** |
| 68 | + * Gets an initial capacity for the map which holds futures of sent request |
| 69 | + * |
| 70 | + * @return initial capacity |
| 71 | + */ |
| 72 | + public int getPredictedFutures() { |
| 73 | + return predictedFutures; |
| 74 | + } |
27 | 75 |
|
28 |
| - public int readerThreadPriority = Thread.NORM_PRIORITY; |
| 76 | + /** |
| 77 | + * Gets a writer thread priority |
| 78 | + * |
| 79 | + * @return thread priority |
| 80 | + */ |
| 81 | + public int getWriterThreadPriority() { |
| 82 | + return writerThreadPriority; |
| 83 | + } |
29 | 84 |
|
| 85 | + /** |
| 86 | + * Gets a reader thread priority |
| 87 | + * |
| 88 | + * @return thread priority |
| 89 | + */ |
| 90 | + public int getReaderThreadPriority() { |
| 91 | + return readerThreadPriority; |
| 92 | + } |
30 | 93 |
|
31 | 94 | /**
|
32 |
| - * shared buffer is place where client collect requests when socket is busy on write |
| 95 | + * Gets a shared buffer size (place where client collects requests |
| 96 | + * when socket is busy on write) |
| 97 | + * |
| 98 | + * @return buffer size |
33 | 99 | */
|
34 |
| - public int sharedBufferSize = 8 * 1024 * 1024; |
| 100 | + public int getSharedBufferSize() { |
| 101 | + return sharedBufferSize; |
| 102 | + } |
| 103 | + |
35 | 104 | /**
|
36 |
| - * not put request into the shared buffer if request size is ge directWriteFactor * sharedBufferSize |
| 105 | + * Gets a factor to calculate a threshold whether request will be accommodated |
| 106 | + * in the shared buffer. |
| 107 | + * if request size exceeds <code>directWriteFactor * sharedBufferSize</code> |
| 108 | + * request is sent directly. |
| 109 | + * |
| 110 | + * @return buffer factor |
37 | 111 | */
|
38 |
| - public double directWriteFactor = 0.5d; |
| 112 | + public double getDirectWriteFactor() { |
| 113 | + return directWriteFactor; |
| 114 | + } |
39 | 115 |
|
40 | 116 | /**
|
| 117 | + * Gets a flag whether the client will use new CALL version. |
| 118 | + * |
41 | 119 | * Use old call command https://github.com/tarantool/doc/issues/54,
|
42 | 120 | * please ensure that you server supports new call command
|
| 121 | + * |
| 122 | + * @return flag indicating CALL command version |
43 | 123 | */
|
44 |
| - public boolean useNewCall = false; |
| 124 | + public boolean isUseNewCall() { |
| 125 | + return useNewCall; |
| 126 | + } |
45 | 127 |
|
46 | 128 | /**
|
47 |
| - * Any blocking ops timeout |
| 129 | + * Gets an init timeout for synchronous operations |
| 130 | + * |
| 131 | + * @return init timeout |
48 | 132 | */
|
49 |
| - public long initTimeoutMillis = 60*1000L; |
| 133 | + public long getInitTimeoutMillis() { |
| 134 | + return initTimeoutMillis; |
| 135 | + } |
50 | 136 |
|
51 |
| - public long writeTimeoutMillis = 60*1000L; |
| 137 | + /** |
| 138 | + * Gets a write timeout for synchronous operations |
| 139 | + * |
| 140 | + * @return write timeout |
| 141 | + */ |
| 142 | + public long getWriteTimeoutMillis() { |
| 143 | + return writeTimeoutMillis; |
| 144 | + } |
| 145 | + |
| 146 | + public static class TarantoolClientConfigBuilder<T extends TarantoolClientConfigBuilder<T>> { |
| 147 | + |
| 148 | + private String username; |
| 149 | + private String password; |
| 150 | + private int defaultRequestSize = 4096; |
| 151 | + private int predictedFutures = (int) ((1024 * 1024) / 0.75) + 1; |
| 152 | + private int writerThreadPriority = Thread.NORM_PRIORITY; |
| 153 | + private int readerThreadPriority = Thread.NORM_PRIORITY; |
| 154 | + private int sharedBufferSize = 8 * 1024 * 1024; |
| 155 | + private double directWriteFactor = 0.5d; |
| 156 | + private boolean useNewCall = false; |
| 157 | + private long initTimeoutMillis = 60 * 1000L; |
| 158 | + private long writeTimeoutMillis = 60 * 1000L; |
| 159 | + |
| 160 | + public T setUsername(String username) { |
| 161 | + this.username = username; |
| 162 | + return self(); |
| 163 | + } |
| 164 | + |
| 165 | + public T setPassword(String password) { |
| 166 | + this.password = password; |
| 167 | + return self(); |
| 168 | + } |
| 169 | + |
| 170 | + public T setDefaultRequestSize(int defaultRequestSize) { |
| 171 | + this.defaultRequestSize = defaultRequestSize; |
| 172 | + return self(); |
| 173 | + } |
| 174 | + |
| 175 | + public T setPredictedFutures(int predictedFutures) { |
| 176 | + this.predictedFutures = predictedFutures; |
| 177 | + return self(); |
| 178 | + } |
| 179 | + |
| 180 | + public TarantoolClientConfigBuilder setWriterThreadPriority(int writerThreadPriority) { |
| 181 | + this.writerThreadPriority = writerThreadPriority; |
| 182 | + return self(); |
| 183 | + } |
| 184 | + |
| 185 | + public T setReaderThreadPriority(int readerThreadPriority) { |
| 186 | + this.readerThreadPriority = readerThreadPriority; |
| 187 | + return self(); |
| 188 | + } |
| 189 | + |
| 190 | + public T setSharedBufferSize(int sharedBufferSize) { |
| 191 | + this.sharedBufferSize = sharedBufferSize; |
| 192 | + return self(); |
| 193 | + } |
| 194 | + |
| 195 | + public T setDirectWriteFactor(double directWriteFactor) { |
| 196 | + this.directWriteFactor = directWriteFactor; |
| 197 | + return self(); |
| 198 | + } |
| 199 | + |
| 200 | + public T setUseNewCall(boolean useNewCall) { |
| 201 | + this.useNewCall = useNewCall; |
| 202 | + return self(); |
| 203 | + } |
| 204 | + |
| 205 | + public T setInitTimeoutMillis(long initTimeoutMillis) { |
| 206 | + this.initTimeoutMillis = initTimeoutMillis; |
| 207 | + return self(); |
| 208 | + } |
| 209 | + |
| 210 | + public T setWriteTimeoutMillis(long writeTimeoutMillis) { |
| 211 | + this.writeTimeoutMillis = writeTimeoutMillis; |
| 212 | + return self(); |
| 213 | + } |
| 214 | + |
| 215 | + public TarantoolClientConfig build() { |
| 216 | + return new TarantoolClientConfig(this); |
| 217 | + } |
| 218 | + |
| 219 | + @SuppressWarnings("unchecked") |
| 220 | + protected T self() { |
| 221 | + return (T) this; |
| 222 | + } |
| 223 | + |
| 224 | + } |
52 | 225 |
|
53 | 226 | }
|
0 commit comments