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
;
10 import java
.util
.Objects
;
12 public class MutableDBOptions
extends AbstractMutableOptions
{
15 * User must use builder pattern, or parser.
17 * @param keys the keys
18 * @param values the values
20 * See {@link #builder()} and {@link #parse(String)}.
22 private MutableDBOptions(final String
[] keys
, final String
[] values
) {
27 * Creates a builder which allows you
28 * to set MutableDBOptions in a fluent
31 * @return A builder for MutableDBOptions
33 public static MutableDBOptionsBuilder
builder() {
34 return new MutableDBOptionsBuilder();
38 * Parses a String representation of MutableDBOptions
40 * The format is: key1=value1;key2=value2;key3=value3 etc
42 * For int[] values, each int should be separated by a comma, e.g.
44 * key1=value1;intArrayKey1=1,2,3
46 * @param str The string representation of the mutable db options
48 * @return A builder for the mutable db options
50 public static MutableDBOptionsBuilder
parse(final String str
) {
51 Objects
.requireNonNull(str
);
53 final MutableDBOptionsBuilder builder
=
54 new MutableDBOptionsBuilder();
56 final String
[] options
= str
.trim().split(KEY_VALUE_PAIR_SEPARATOR
);
57 for(final String option
: options
) {
58 final int equalsOffset
= option
.indexOf(KEY_VALUE_SEPARATOR
);
59 if(equalsOffset
<= 0) {
60 throw new IllegalArgumentException(
61 "options string has an invalid key=value pair");
64 final String key
= option
.substring(0, equalsOffset
);
66 throw new IllegalArgumentException("options string is invalid");
69 final String value
= option
.substring(equalsOffset
+ 1);
71 throw new IllegalArgumentException("options string is invalid");
74 builder
.fromString(key
, value
);
80 private interface MutableDBOptionKey
extends MutableOptionKey
{}
82 public enum DBOption
implements MutableDBOptionKey
{
83 max_background_jobs(ValueType
.INT
),
84 base_background_compactions(ValueType
.INT
),
85 max_background_compactions(ValueType
.INT
),
86 avoid_flush_during_shutdown(ValueType
.BOOLEAN
),
87 writable_file_max_buffer_size(ValueType
.LONG
),
88 delayed_write_rate(ValueType
.LONG
),
89 max_total_wal_size(ValueType
.LONG
),
90 delete_obsolete_files_period_micros(ValueType
.LONG
),
91 stats_dump_period_sec(ValueType
.INT
),
92 max_open_files(ValueType
.INT
),
93 bytes_per_sync(ValueType
.LONG
),
94 wal_bytes_per_sync(ValueType
.LONG
),
95 compaction_readahead_size(ValueType
.LONG
);
97 private final ValueType valueType
;
98 DBOption(final ValueType valueType
) {
99 this.valueType
= valueType
;
103 public ValueType
getValueType() {
108 public static class MutableDBOptionsBuilder
109 extends AbstractMutableOptionsBuilder
<MutableDBOptions
, MutableDBOptionsBuilder
, MutableDBOptionKey
>
110 implements MutableDBOptionsInterface
<MutableDBOptionsBuilder
> {
112 private final static Map
<String
, MutableDBOptionKey
> ALL_KEYS_LOOKUP
= new HashMap
<>();
114 for(final MutableDBOptionKey key
: DBOption
.values()) {
115 ALL_KEYS_LOOKUP
.put(key
.name(), key
);
119 private MutableDBOptionsBuilder() {
124 protected MutableDBOptionsBuilder
self() {
129 protected Map
<String
, MutableDBOptionKey
> allKeys() {
130 return ALL_KEYS_LOOKUP
;
134 protected MutableDBOptions
build(final String
[] keys
,
135 final String
[] values
) {
136 return new MutableDBOptions(keys
, values
);
140 public MutableDBOptionsBuilder
setMaxBackgroundJobs(
141 final int maxBackgroundJobs
) {
142 return setInt(DBOption
.max_background_jobs
, maxBackgroundJobs
);
146 public int maxBackgroundJobs() {
147 return getInt(DBOption
.max_background_jobs
);
151 public void setBaseBackgroundCompactions(
152 final int baseBackgroundCompactions
) {
153 setInt(DBOption
.base_background_compactions
,
154 baseBackgroundCompactions
);
158 public int baseBackgroundCompactions() {
159 return getInt(DBOption
.base_background_compactions
);
163 public MutableDBOptionsBuilder
setMaxBackgroundCompactions(
164 final int maxBackgroundCompactions
) {
165 return setInt(DBOption
.max_background_compactions
,
166 maxBackgroundCompactions
);
170 public int maxBackgroundCompactions() {
171 return getInt(DBOption
.max_background_compactions
);
175 public MutableDBOptionsBuilder
setAvoidFlushDuringShutdown(
176 final boolean avoidFlushDuringShutdown
) {
177 return setBoolean(DBOption
.avoid_flush_during_shutdown
,
178 avoidFlushDuringShutdown
);
182 public boolean avoidFlushDuringShutdown() {
183 return getBoolean(DBOption
.avoid_flush_during_shutdown
);
187 public MutableDBOptionsBuilder
setWritableFileMaxBufferSize(
188 final long writableFileMaxBufferSize
) {
189 return setLong(DBOption
.writable_file_max_buffer_size
,
190 writableFileMaxBufferSize
);
194 public long writableFileMaxBufferSize() {
195 return getLong(DBOption
.writable_file_max_buffer_size
);
199 public MutableDBOptionsBuilder
setDelayedWriteRate(
200 final long delayedWriteRate
) {
201 return setLong(DBOption
.delayed_write_rate
,
206 public long delayedWriteRate() {
207 return getLong(DBOption
.delayed_write_rate
);
211 public MutableDBOptionsBuilder
setMaxTotalWalSize(
212 final long maxTotalWalSize
) {
213 return setLong(DBOption
.max_total_wal_size
, maxTotalWalSize
);
217 public long maxTotalWalSize() {
218 return getLong(DBOption
.max_total_wal_size
);
222 public MutableDBOptionsBuilder
setDeleteObsoleteFilesPeriodMicros(
224 return setLong(DBOption
.delete_obsolete_files_period_micros
, micros
);
228 public long deleteObsoleteFilesPeriodMicros() {
229 return getLong(DBOption
.delete_obsolete_files_period_micros
);
233 public MutableDBOptionsBuilder
setStatsDumpPeriodSec(
234 final int statsDumpPeriodSec
) {
235 return setInt(DBOption
.stats_dump_period_sec
, statsDumpPeriodSec
);
239 public int statsDumpPeriodSec() {
240 return getInt(DBOption
.stats_dump_period_sec
);
244 public MutableDBOptionsBuilder
setMaxOpenFiles(final int maxOpenFiles
) {
245 return setInt(DBOption
.max_open_files
, maxOpenFiles
);
249 public int maxOpenFiles() {
250 return getInt(DBOption
.max_open_files
);
254 public MutableDBOptionsBuilder
setBytesPerSync(final long bytesPerSync
) {
255 return setLong(DBOption
.bytes_per_sync
, bytesPerSync
);
259 public long bytesPerSync() {
260 return getLong(DBOption
.bytes_per_sync
);
264 public MutableDBOptionsBuilder
setWalBytesPerSync(
265 final long walBytesPerSync
) {
266 return setLong(DBOption
.wal_bytes_per_sync
, walBytesPerSync
);
270 public long walBytesPerSync() {
271 return getLong(DBOption
.wal_bytes_per_sync
);
275 public MutableDBOptionsBuilder
setCompactionReadaheadSize(
276 final long compactionReadaheadSize
) {
277 return setLong(DBOption
.compaction_readahead_size
,
278 compactionReadaheadSize
);
282 public long compactionReadaheadSize() {
283 return getLong(DBOption
.compaction_readahead_size
);