Repository navigation
Expand file tree
/
Copy pathConfig.java
More file actions
396 lines (346 loc) · 14.2 KB
/
Copy pathConfig.java
File metadata and controls
396 lines (346 loc) · 14.2 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/**
*
* Copyright (C) TidesDB
*
* Original Author: Alex Gaetano Padula
*
* Licensed under the Mozilla Public License, v. 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.mozilla.org/en-US/MPL/2.0/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tidesdb;
/**
* Configuration for opening a TidesDB instance.
*/
public class Config {
static {
NativeLibrary.load();
}
private String dbPath;
private int numFlushThreads;
private int numCompactionThreads;
private LogLevel logLevel;
private long blockCacheSize;
private long maxOpenSSTables;
private boolean logToFile;
private long logTruncationAt;
private long maxMemoryUsage;
private boolean unifiedMemtable;
private long unifiedMemtableWriteBufferSize;
private int unifiedMemtableSkipListMaxLevel;
private float unifiedMemtableSkipListProbability;
private int unifiedMemtableSyncMode;
private long unifiedMemtableSyncIntervalUs;
private String objectStoreFsPath;
private ObjectStoreConfig objectStoreConfig;
private S3Config objectStoreS3Config;
private int maxConcurrentFlushes;
private boolean finishCompactionsOnClose;
private Config(Builder builder) {
this.dbPath = builder.dbPath;
this.numFlushThreads = builder.numFlushThreads;
this.numCompactionThreads = builder.numCompactionThreads;
this.logLevel = builder.logLevel;
this.blockCacheSize = builder.blockCacheSize;
this.maxOpenSSTables = builder.maxOpenSSTables;
this.logToFile = builder.logToFile;
this.logTruncationAt = builder.logTruncationAt;
this.maxMemoryUsage = builder.maxMemoryUsage;
this.unifiedMemtable = builder.unifiedMemtable;
this.unifiedMemtableWriteBufferSize = builder.unifiedMemtableWriteBufferSize;
this.unifiedMemtableSkipListMaxLevel = builder.unifiedMemtableSkipListMaxLevel;
this.unifiedMemtableSkipListProbability = builder.unifiedMemtableSkipListProbability;
this.unifiedMemtableSyncMode = builder.unifiedMemtableSyncMode;
this.unifiedMemtableSyncIntervalUs = builder.unifiedMemtableSyncIntervalUs;
this.objectStoreFsPath = builder.objectStoreFsPath;
this.objectStoreConfig = builder.objectStoreConfig;
this.objectStoreS3Config = builder.objectStoreS3Config;
this.maxConcurrentFlushes = builder.maxConcurrentFlushes;
this.finishCompactionsOnClose = builder.finishCompactionsOnClose;
}
/**
* Creates a default configuration. The {@code maxConcurrentFlushes} default is
* sourced from the underlying C library via {@code tidesdb_default_config()} so
* the binding tracks the engine's defaults automatically.
*
* @return a new Config with default values
*/
public static Config defaultConfig() {
return new Builder()
.numFlushThreads(2)
.numCompactionThreads(2)
.logLevel(LogLevel.INFO)
.blockCacheSize(64 * 1024 * 1024)
.maxOpenSSTables(256)
.logToFile(false)
.logTruncationAt(24 * 1024 * 1024)
.maxMemoryUsage(0)
.maxConcurrentFlushes(nativeDefaultMaxConcurrentFlushes())
.build();
}
private static native int nativeDefaultMaxConcurrentFlushes();
/**
* Creates a new builder for Config.
*
* @param dbPath the database path
* @return a new Builder
*/
public static Builder builder(String dbPath) {
return new Builder().dbPath(dbPath);
}
public String getDbPath() {
return dbPath;
}
public int getNumFlushThreads() {
return numFlushThreads;
}
public int getNumCompactionThreads() {
return numCompactionThreads;
}
public LogLevel getLogLevel() {
return logLevel;
}
public long getBlockCacheSize() {
return blockCacheSize;
}
public long getMaxOpenSSTables() {
return maxOpenSSTables;
}
public boolean isLogToFile() {
return logToFile;
}
public long getLogTruncationAt() {
return logTruncationAt;
}
public long getMaxMemoryUsage() {
return maxMemoryUsage;
}
public boolean isUnifiedMemtable() {
return unifiedMemtable;
}
public long getUnifiedMemtableWriteBufferSize() {
return unifiedMemtableWriteBufferSize;
}
public int getUnifiedMemtableSkipListMaxLevel() {
return unifiedMemtableSkipListMaxLevel;
}
public float getUnifiedMemtableSkipListProbability() {
return unifiedMemtableSkipListProbability;
}
public int getUnifiedMemtableSyncMode() {
return unifiedMemtableSyncMode;
}
public long getUnifiedMemtableSyncIntervalUs() {
return unifiedMemtableSyncIntervalUs;
}
public String getObjectStoreFsPath() {
return objectStoreFsPath;
}
public ObjectStoreConfig getObjectStoreConfig() {
return objectStoreConfig;
}
/**
* Returns the S3-compatible object store connector configuration, or null if the database
* is not backed by S3.
*
* @return the S3 connector config, or null
*/
public S3Config getObjectStoreS3Config() {
return objectStoreS3Config;
}
public int getMaxConcurrentFlushes() {
return maxConcurrentFlushes;
}
/**
* Returns the close behavior for in-flight compactions.
*
* @return true if {@code close()} waits for in-flight compactions to finish;
* false (default) cancels them at their next checkpoint for a fast shutdown
*/
public boolean isFinishCompactionsOnClose() {
return finishCompactionsOnClose;
}
/**
* Builder for Config.
*/
public static class Builder {
private String dbPath = "";
private int numFlushThreads = 2;
private int numCompactionThreads = 2;
private LogLevel logLevel = LogLevel.INFO;
private long blockCacheSize = 64 * 1024 * 1024;
private long maxOpenSSTables = 256;
private boolean logToFile = false;
private long logTruncationAt = 24 * 1024 * 1024;
private long maxMemoryUsage = 0;
private boolean unifiedMemtable = false;
private long unifiedMemtableWriteBufferSize = 0;
private int unifiedMemtableSkipListMaxLevel = 0;
private float unifiedMemtableSkipListProbability = 0;
private int unifiedMemtableSyncMode = 0;
private long unifiedMemtableSyncIntervalUs = 0;
private String objectStoreFsPath = null;
private ObjectStoreConfig objectStoreConfig = null;
private S3Config objectStoreS3Config = null;
private int maxConcurrentFlushes = 0;
private boolean finishCompactionsOnClose = false;
public Builder dbPath(String dbPath) {
this.dbPath = dbPath;
return this;
}
public Builder numFlushThreads(int numFlushThreads) {
this.numFlushThreads = numFlushThreads;
return this;
}
public Builder numCompactionThreads(int numCompactionThreads) {
this.numCompactionThreads = numCompactionThreads;
return this;
}
public Builder logLevel(LogLevel logLevel) {
this.logLevel = logLevel;
return this;
}
public Builder blockCacheSize(long blockCacheSize) {
this.blockCacheSize = blockCacheSize;
return this;
}
public Builder maxOpenSSTables(long maxOpenSSTables) {
this.maxOpenSSTables = maxOpenSSTables;
return this;
}
public Builder logToFile(boolean logToFile) {
this.logToFile = logToFile;
return this;
}
public Builder logTruncationAt(long logTruncationAt) {
this.logTruncationAt = logTruncationAt;
return this;
}
public Builder maxMemoryUsage(long maxMemoryUsage) {
this.maxMemoryUsage = maxMemoryUsage;
return this;
}
public Builder unifiedMemtable(boolean unifiedMemtable) {
this.unifiedMemtable = unifiedMemtable;
return this;
}
public Builder unifiedMemtableWriteBufferSize(long unifiedMemtableWriteBufferSize) {
this.unifiedMemtableWriteBufferSize = unifiedMemtableWriteBufferSize;
return this;
}
public Builder unifiedMemtableSkipListMaxLevel(int unifiedMemtableSkipListMaxLevel) {
this.unifiedMemtableSkipListMaxLevel = unifiedMemtableSkipListMaxLevel;
return this;
}
public Builder unifiedMemtableSkipListProbability(float unifiedMemtableSkipListProbability) {
this.unifiedMemtableSkipListProbability = unifiedMemtableSkipListProbability;
return this;
}
public Builder unifiedMemtableSyncMode(int unifiedMemtableSyncMode) {
this.unifiedMemtableSyncMode = unifiedMemtableSyncMode;
return this;
}
public Builder unifiedMemtableSyncIntervalUs(long unifiedMemtableSyncIntervalUs) {
this.unifiedMemtableSyncIntervalUs = unifiedMemtableSyncIntervalUs;
return this;
}
public Builder objectStoreFsPath(String objectStoreFsPath) {
this.objectStoreFsPath = objectStoreFsPath;
return this;
}
public Builder objectStoreConfig(ObjectStoreConfig objectStoreConfig) {
this.objectStoreConfig = objectStoreConfig;
return this;
}
/**
* Backs the database with an S3-compatible object store connector (AWS S3, MinIO, etc.).
* Takes precedence over {@link #objectStoreFsPath(String)} when both are set. Pair with
* {@link #objectStoreConfig(ObjectStoreConfig)} to tune cache, multipart, and replication
* behavior. Requires the native library to be built with {@code TIDESDB_WITH_S3=ON}.
*
* @param objectStoreS3Config the S3 connector configuration, or null for none
* @return this builder
*/
public Builder objectStoreS3Config(S3Config objectStoreS3Config) {
this.objectStoreS3Config = objectStoreS3Config;
return this;
}
public Builder maxConcurrentFlushes(int maxConcurrentFlushes) {
this.maxConcurrentFlushes = maxConcurrentFlushes;
return this;
}
/**
* Sets the close behavior for in-flight compactions.
*
* @param finishCompactionsOnClose false (default) cancels in-flight compactions at their
* next checkpoint for a fast shutdown (no data is lost; recovery handles a mid-merge
* state). true lets in-flight compactions run to completion before {@code close()}
* returns.
* @return this builder
*/
public Builder finishCompactionsOnClose(boolean finishCompactionsOnClose) {
this.finishCompactionsOnClose = finishCompactionsOnClose;
return this;
}
public Config build() {
validate();
return new Config(this);
}
private void validate() throws IllegalArgumentException {
if (dbPath == null) {
throw new IllegalArgumentException("Database path cannot be null");
}
if (dbPath.isEmpty()) {
dbPath = "";
}
if (numFlushThreads <= 0) {
throw new IllegalArgumentException("Number of flush threads must be positive");
}
if (numCompactionThreads <= 0) {
throw new IllegalArgumentException("Number of compaction threads must be positive");
}
if (logLevel == null) {
throw new IllegalArgumentException("Log level cannot be null");
}
if (blockCacheSize < 0) {
throw new IllegalArgumentException("Block cache size cannot be negative");
}
if (maxOpenSSTables <= 0) {
throw new IllegalArgumentException("Max open SSTables must be positive");
}
if (logTruncationAt < 0) {
throw new IllegalArgumentException("logTruncationAt must not be negative, was: " + logTruncationAt);
}
if (maxMemoryUsage < 0) {
throw new IllegalArgumentException("maxMemoryUsage must not be negative, was: " + maxMemoryUsage);
}
if (unifiedMemtableWriteBufferSize < 0) {
throw new IllegalArgumentException("unifiedMemtableWriteBufferSize must not be negative, was: " + unifiedMemtableWriteBufferSize);
}
if (unifiedMemtableSyncIntervalUs < 0) {
throw new IllegalArgumentException("unifiedMemtableSyncIntervalUs must not be negative, was: " + unifiedMemtableSyncIntervalUs);
}
if (unifiedMemtableSkipListMaxLevel < 0) {
throw new IllegalArgumentException("unifiedMemtableSkipListMaxLevel must not be negative, was: " + unifiedMemtableSkipListMaxLevel);
}
if (Float.isNaN(unifiedMemtableSkipListProbability) || Float.isInfinite(unifiedMemtableSkipListProbability)
|| unifiedMemtableSkipListProbability < 0.0f || unifiedMemtableSkipListProbability > 1.0f) {
throw new IllegalArgumentException("unifiedMemtableSkipListProbability must be finite and in [0.0, 1.0], was: " + unifiedMemtableSkipListProbability);
}
if (unifiedMemtableSyncMode < 0) {
throw new IllegalArgumentException("unifiedMemtableSyncMode must not be negative, was: " + unifiedMemtableSyncMode);
}
if (maxConcurrentFlushes < 0) {
throw new IllegalArgumentException("maxConcurrentFlushes must not be negative, was: " + maxConcurrentFlushes);
}
}
}
}