1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
8 import java
.util
.HashMap
;
11 import java
.util
.Objects
;
13 public class MutableDBOptions
extends AbstractMutableOptions
{
16 * User must use builder pattern, or parser.
18 * @param keys the keys
19 * @param values the values
21 * See {@link #builder()} and {@link #parse(String)}.
23 private MutableDBOptions(final String
[] keys
, final String
[] values
) {
28 * Creates a builder which allows you
29 * to set MutableDBOptions in a fluent
32 * @return A builder for MutableDBOptions
34 public static MutableDBOptionsBuilder
builder() {
35 return new MutableDBOptionsBuilder();
39 * Parses a String representation of MutableDBOptions
41 * The format is: key1=value1;key2=value2;key3=value3 etc
43 * For int[] values, each int should be separated by a comma, e.g.
45 * key1=value1;intArrayKey1=1:2:3
47 * @param str The string representation of the mutable db options
48 * @param ignoreUnknown what to do if the key is not one of the keys we expect
50 * @return A builder for the mutable db options
52 public static MutableDBOptionsBuilder
parse(final String str
, boolean ignoreUnknown
) {
53 Objects
.requireNonNull(str
);
55 final List
<OptionString
.Entry
> parsedOptions
= OptionString
.Parser
.parse(str
);
56 return new MutableDBOptions
.MutableDBOptionsBuilder().fromParsed(parsedOptions
, ignoreUnknown
);
59 public static MutableDBOptionsBuilder
parse(final String str
) {
60 return parse(str
, false);
63 private interface MutableDBOptionKey
extends MutableOptionKey
{}
65 public enum DBOption
implements MutableDBOptionKey
{
66 max_background_jobs(ValueType
.INT
),
67 max_background_compactions(ValueType
.INT
),
68 avoid_flush_during_shutdown(ValueType
.BOOLEAN
),
69 writable_file_max_buffer_size(ValueType
.LONG
),
70 delayed_write_rate(ValueType
.LONG
),
71 max_total_wal_size(ValueType
.LONG
),
72 delete_obsolete_files_period_micros(ValueType
.LONG
),
73 stats_dump_period_sec(ValueType
.INT
),
74 stats_persist_period_sec(ValueType
.INT
),
75 stats_history_buffer_size(ValueType
.LONG
),
76 max_open_files(ValueType
.INT
),
77 bytes_per_sync(ValueType
.LONG
),
78 wal_bytes_per_sync(ValueType
.LONG
),
79 strict_bytes_per_sync(ValueType
.BOOLEAN
),
80 compaction_readahead_size(ValueType
.LONG
);
82 private final ValueType valueType
;
83 DBOption(final ValueType valueType
) {
84 this.valueType
= valueType
;
88 public ValueType
getValueType() {
93 public static class MutableDBOptionsBuilder
94 extends AbstractMutableOptionsBuilder
<MutableDBOptions
, MutableDBOptionsBuilder
, MutableDBOptionKey
>
95 implements MutableDBOptionsInterface
<MutableDBOptionsBuilder
> {
97 private final static Map
<String
, MutableDBOptionKey
> ALL_KEYS_LOOKUP
= new HashMap
<>();
99 for(final MutableDBOptionKey key
: DBOption
.values()) {
100 ALL_KEYS_LOOKUP
.put(key
.name(), key
);
104 private MutableDBOptionsBuilder() {
109 protected MutableDBOptionsBuilder
self() {
114 protected Map
<String
, MutableDBOptionKey
> allKeys() {
115 return ALL_KEYS_LOOKUP
;
119 protected MutableDBOptions
build(final String
[] keys
,
120 final String
[] values
) {
121 return new MutableDBOptions(keys
, values
);
125 public MutableDBOptionsBuilder
setMaxBackgroundJobs(
126 final int maxBackgroundJobs
) {
127 return setInt(DBOption
.max_background_jobs
, maxBackgroundJobs
);
131 public int maxBackgroundJobs() {
132 return getInt(DBOption
.max_background_jobs
);
137 public MutableDBOptionsBuilder
setMaxBackgroundCompactions(
138 final int maxBackgroundCompactions
) {
139 return setInt(DBOption
.max_background_compactions
,
140 maxBackgroundCompactions
);
145 public int maxBackgroundCompactions() {
146 return getInt(DBOption
.max_background_compactions
);
150 public MutableDBOptionsBuilder
setAvoidFlushDuringShutdown(
151 final boolean avoidFlushDuringShutdown
) {
152 return setBoolean(DBOption
.avoid_flush_during_shutdown
,
153 avoidFlushDuringShutdown
);
157 public boolean avoidFlushDuringShutdown() {
158 return getBoolean(DBOption
.avoid_flush_during_shutdown
);
162 public MutableDBOptionsBuilder
setWritableFileMaxBufferSize(
163 final long writableFileMaxBufferSize
) {
164 return setLong(DBOption
.writable_file_max_buffer_size
,
165 writableFileMaxBufferSize
);
169 public long writableFileMaxBufferSize() {
170 return getLong(DBOption
.writable_file_max_buffer_size
);
174 public MutableDBOptionsBuilder
setDelayedWriteRate(
175 final long delayedWriteRate
) {
176 return setLong(DBOption
.delayed_write_rate
,
181 public long delayedWriteRate() {
182 return getLong(DBOption
.delayed_write_rate
);
186 public MutableDBOptionsBuilder
setMaxTotalWalSize(
187 final long maxTotalWalSize
) {
188 return setLong(DBOption
.max_total_wal_size
, maxTotalWalSize
);
192 public long maxTotalWalSize() {
193 return getLong(DBOption
.max_total_wal_size
);
197 public MutableDBOptionsBuilder
setDeleteObsoleteFilesPeriodMicros(
199 return setLong(DBOption
.delete_obsolete_files_period_micros
, micros
);
203 public long deleteObsoleteFilesPeriodMicros() {
204 return getLong(DBOption
.delete_obsolete_files_period_micros
);
208 public MutableDBOptionsBuilder
setStatsDumpPeriodSec(
209 final int statsDumpPeriodSec
) {
210 return setInt(DBOption
.stats_dump_period_sec
, statsDumpPeriodSec
);
214 public int statsDumpPeriodSec() {
215 return getInt(DBOption
.stats_dump_period_sec
);
219 public MutableDBOptionsBuilder
setStatsPersistPeriodSec(
220 final int statsPersistPeriodSec
) {
221 return setInt(DBOption
.stats_persist_period_sec
, statsPersistPeriodSec
);
225 public int statsPersistPeriodSec() {
226 return getInt(DBOption
.stats_persist_period_sec
);
230 public MutableDBOptionsBuilder
setStatsHistoryBufferSize(
231 final long statsHistoryBufferSize
) {
232 return setLong(DBOption
.stats_history_buffer_size
, statsHistoryBufferSize
);
236 public long statsHistoryBufferSize() {
237 return getLong(DBOption
.stats_history_buffer_size
);
241 public MutableDBOptionsBuilder
setMaxOpenFiles(final int maxOpenFiles
) {
242 return setInt(DBOption
.max_open_files
, maxOpenFiles
);
246 public int maxOpenFiles() {
247 return getInt(DBOption
.max_open_files
);
251 public MutableDBOptionsBuilder
setBytesPerSync(final long bytesPerSync
) {
252 return setLong(DBOption
.bytes_per_sync
, bytesPerSync
);
256 public long bytesPerSync() {
257 return getLong(DBOption
.bytes_per_sync
);
261 public MutableDBOptionsBuilder
setWalBytesPerSync(
262 final long walBytesPerSync
) {
263 return setLong(DBOption
.wal_bytes_per_sync
, walBytesPerSync
);
267 public long walBytesPerSync() {
268 return getLong(DBOption
.wal_bytes_per_sync
);
272 public MutableDBOptionsBuilder
setStrictBytesPerSync(
273 final boolean strictBytesPerSync
) {
274 return setBoolean(DBOption
.strict_bytes_per_sync
, strictBytesPerSync
);
278 public boolean strictBytesPerSync() {
279 return getBoolean(DBOption
.strict_bytes_per_sync
);
283 public MutableDBOptionsBuilder
setCompactionReadaheadSize(
284 final long compactionReadaheadSize
) {
285 return setLong(DBOption
.compaction_readahead_size
,
286 compactionReadaheadSize
);
290 public long compactionReadaheadSize() {
291 return getLong(DBOption
.compaction_readahead_size
);