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).
5 
6 package org.rocksdb;
7 
8 import java.util.HashMap;
9 import java.util.Map;
10 import java.util.Objects;
11 
12 public class MutableDBOptions extends AbstractMutableOptions {
13 
14   /**
15    * User must use builder pattern, or parser.
16    *
17    * @param keys the keys
18    * @param values the values
19    *
20    * See {@link #builder()} and {@link #parse(String)}.
21    */
MutableDBOptions(final String[] keys, final String[] values)22   private MutableDBOptions(final String[] keys, final String[] values) {
23     super(keys, values);
24   }
25 
26   /**
27    * Creates a builder which allows you
28    * to set MutableDBOptions in a fluent
29    * manner
30    *
31    * @return A builder for MutableDBOptions
32    */
builder()33   public static MutableDBOptionsBuilder builder() {
34     return new MutableDBOptionsBuilder();
35   }
36 
37   /**
38    * Parses a String representation of MutableDBOptions
39    *
40    * The format is: key1=value1;key2=value2;key3=value3 etc
41    *
42    * For int[] values, each int should be separated by a comma, e.g.
43    *
44    * key1=value1;intArrayKey1=1,2,3
45    *
46    * @param str The string representation of the mutable db options
47    *
48    * @return A builder for the mutable db options
49    */
parse(final String str)50   public static MutableDBOptionsBuilder parse(final String str) {
51     Objects.requireNonNull(str);
52 
53     final MutableDBOptionsBuilder builder =
54         new MutableDBOptionsBuilder();
55 
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");
62       }
63 
64       final String key = option.substring(0, equalsOffset);
65       if(key.isEmpty()) {
66         throw new IllegalArgumentException("options string is invalid");
67       }
68 
69       final String value = option.substring(equalsOffset + 1);
70       if(value.isEmpty()) {
71         throw new IllegalArgumentException("options string is invalid");
72       }
73 
74       builder.fromString(key, value);
75     }
76 
77     return builder;
78   }
79 
80   private interface MutableDBOptionKey extends MutableOptionKey {}
81 
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     stats_persist_period_sec(ValueType.INT),
93     stats_history_buffer_size(ValueType.LONG),
94     max_open_files(ValueType.INT),
95     bytes_per_sync(ValueType.LONG),
96     wal_bytes_per_sync(ValueType.LONG),
97     strict_bytes_per_sync(ValueType.BOOLEAN),
98     compaction_readahead_size(ValueType.LONG);
99 
100     private final ValueType valueType;
DBOption(final ValueType valueType)101     DBOption(final ValueType valueType) {
102       this.valueType = valueType;
103     }
104 
105     @Override
getValueType()106     public ValueType getValueType() {
107       return valueType;
108     }
109   }
110 
111   public static class MutableDBOptionsBuilder
112       extends AbstractMutableOptionsBuilder<MutableDBOptions, MutableDBOptionsBuilder, MutableDBOptionKey>
113       implements MutableDBOptionsInterface<MutableDBOptionsBuilder> {
114 
115     private final static Map<String, MutableDBOptionKey> ALL_KEYS_LOOKUP = new HashMap<>();
116     static {
117       for(final MutableDBOptionKey key : DBOption.values()) {
key.name()118         ALL_KEYS_LOOKUP.put(key.name(), key);
119       }
120     }
121 
MutableDBOptionsBuilder()122     private MutableDBOptionsBuilder() {
123       super();
124     }
125 
126     @Override
self()127     protected MutableDBOptionsBuilder self() {
128       return this;
129     }
130 
131     @Override
allKeys()132     protected Map<String, MutableDBOptionKey> allKeys() {
133       return ALL_KEYS_LOOKUP;
134     }
135 
136     @Override
build(final String[] keys, final String[] values)137     protected MutableDBOptions build(final String[] keys,
138         final String[] values) {
139       return new MutableDBOptions(keys, values);
140     }
141 
142     @Override
setMaxBackgroundJobs( final int maxBackgroundJobs)143     public MutableDBOptionsBuilder setMaxBackgroundJobs(
144         final int maxBackgroundJobs) {
145       return setInt(DBOption.max_background_jobs, maxBackgroundJobs);
146     }
147 
148     @Override
maxBackgroundJobs()149     public int maxBackgroundJobs() {
150       return getInt(DBOption.max_background_jobs);
151     }
152 
153     @Override
154     @Deprecated
setBaseBackgroundCompactions( final int baseBackgroundCompactions)155     public void setBaseBackgroundCompactions(
156         final int baseBackgroundCompactions) {
157       setInt(DBOption.base_background_compactions,
158           baseBackgroundCompactions);
159     }
160 
161     @Override
baseBackgroundCompactions()162     public int baseBackgroundCompactions() {
163       return getInt(DBOption.base_background_compactions);
164     }
165 
166     @Override
167     @Deprecated
setMaxBackgroundCompactions( final int maxBackgroundCompactions)168     public MutableDBOptionsBuilder setMaxBackgroundCompactions(
169         final int maxBackgroundCompactions) {
170       return setInt(DBOption.max_background_compactions,
171           maxBackgroundCompactions);
172     }
173 
174     @Override
175     @Deprecated
maxBackgroundCompactions()176     public int maxBackgroundCompactions() {
177       return getInt(DBOption.max_background_compactions);
178     }
179 
180     @Override
setAvoidFlushDuringShutdown( final boolean avoidFlushDuringShutdown)181     public MutableDBOptionsBuilder setAvoidFlushDuringShutdown(
182         final boolean avoidFlushDuringShutdown) {
183       return setBoolean(DBOption.avoid_flush_during_shutdown,
184           avoidFlushDuringShutdown);
185     }
186 
187     @Override
avoidFlushDuringShutdown()188     public boolean avoidFlushDuringShutdown() {
189       return getBoolean(DBOption.avoid_flush_during_shutdown);
190     }
191 
192     @Override
setWritableFileMaxBufferSize( final long writableFileMaxBufferSize)193     public MutableDBOptionsBuilder setWritableFileMaxBufferSize(
194         final long writableFileMaxBufferSize) {
195       return setLong(DBOption.writable_file_max_buffer_size,
196           writableFileMaxBufferSize);
197     }
198 
199     @Override
writableFileMaxBufferSize()200     public long writableFileMaxBufferSize() {
201       return getLong(DBOption.writable_file_max_buffer_size);
202     }
203 
204     @Override
setDelayedWriteRate( final long delayedWriteRate)205     public MutableDBOptionsBuilder setDelayedWriteRate(
206         final long delayedWriteRate) {
207       return setLong(DBOption.delayed_write_rate,
208           delayedWriteRate);
209     }
210 
211     @Override
delayedWriteRate()212     public long delayedWriteRate() {
213       return getLong(DBOption.delayed_write_rate);
214     }
215 
216     @Override
setMaxTotalWalSize( final long maxTotalWalSize)217     public MutableDBOptionsBuilder setMaxTotalWalSize(
218         final long maxTotalWalSize) {
219       return setLong(DBOption.max_total_wal_size, maxTotalWalSize);
220     }
221 
222     @Override
maxTotalWalSize()223     public long maxTotalWalSize() {
224       return getLong(DBOption.max_total_wal_size);
225     }
226 
227     @Override
setDeleteObsoleteFilesPeriodMicros( final long micros)228     public MutableDBOptionsBuilder setDeleteObsoleteFilesPeriodMicros(
229         final long micros) {
230       return setLong(DBOption.delete_obsolete_files_period_micros, micros);
231     }
232 
233     @Override
deleteObsoleteFilesPeriodMicros()234     public long deleteObsoleteFilesPeriodMicros() {
235       return getLong(DBOption.delete_obsolete_files_period_micros);
236     }
237 
238     @Override
setStatsDumpPeriodSec( final int statsDumpPeriodSec)239     public MutableDBOptionsBuilder setStatsDumpPeriodSec(
240         final int statsDumpPeriodSec) {
241       return setInt(DBOption.stats_dump_period_sec, statsDumpPeriodSec);
242     }
243 
244     @Override
statsDumpPeriodSec()245     public int statsDumpPeriodSec() {
246       return getInt(DBOption.stats_dump_period_sec);
247     }
248 
249     @Override
setStatsPersistPeriodSec( final int statsPersistPeriodSec)250     public MutableDBOptionsBuilder setStatsPersistPeriodSec(
251         final int statsPersistPeriodSec) {
252       return setInt(DBOption.stats_persist_period_sec, statsPersistPeriodSec);
253     }
254 
255     @Override
statsPersistPeriodSec()256     public int statsPersistPeriodSec() {
257       return getInt(DBOption.stats_persist_period_sec);
258     }
259 
260     @Override
setStatsHistoryBufferSize( final long statsHistoryBufferSize)261     public MutableDBOptionsBuilder setStatsHistoryBufferSize(
262         final long statsHistoryBufferSize) {
263       return setLong(DBOption.stats_history_buffer_size, statsHistoryBufferSize);
264     }
265 
266     @Override
statsHistoryBufferSize()267     public long statsHistoryBufferSize() {
268       return getLong(DBOption.stats_history_buffer_size);
269     }
270 
271     @Override
setMaxOpenFiles(final int maxOpenFiles)272     public MutableDBOptionsBuilder setMaxOpenFiles(final int maxOpenFiles) {
273       return setInt(DBOption.max_open_files, maxOpenFiles);
274     }
275 
276     @Override
maxOpenFiles()277     public int maxOpenFiles() {
278       return getInt(DBOption.max_open_files);
279     }
280 
281     @Override
setBytesPerSync(final long bytesPerSync)282     public MutableDBOptionsBuilder setBytesPerSync(final long bytesPerSync) {
283       return setLong(DBOption.bytes_per_sync, bytesPerSync);
284     }
285 
286     @Override
bytesPerSync()287     public long bytesPerSync() {
288       return getLong(DBOption.bytes_per_sync);
289     }
290 
291     @Override
setWalBytesPerSync( final long walBytesPerSync)292     public MutableDBOptionsBuilder setWalBytesPerSync(
293         final long walBytesPerSync) {
294       return setLong(DBOption.wal_bytes_per_sync, walBytesPerSync);
295     }
296 
297     @Override
walBytesPerSync()298     public long walBytesPerSync() {
299       return getLong(DBOption.wal_bytes_per_sync);
300     }
301 
302     @Override
setStrictBytesPerSync( final boolean strictBytesPerSync)303     public MutableDBOptionsBuilder setStrictBytesPerSync(
304         final boolean strictBytesPerSync) {
305       return setBoolean(DBOption.strict_bytes_per_sync, strictBytesPerSync);
306     }
307 
308     @Override
strictBytesPerSync()309     public boolean strictBytesPerSync() {
310       return getBoolean(DBOption.strict_bytes_per_sync);
311     }
312 
313     @Override
setCompactionReadaheadSize( final long compactionReadaheadSize)314     public MutableDBOptionsBuilder setCompactionReadaheadSize(
315         final long compactionReadaheadSize) {
316       return setLong(DBOption.compaction_readahead_size,
317           compactionReadaheadSize);
318     }
319 
320     @Override
compactionReadaheadSize()321     public long compactionReadaheadSize() {
322       return getLong(DBOption.compaction_readahead_size);
323     }
324   }
325 }
326