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 /**
9  * Options while opening a file to read/write
10  */
11 public class EnvOptions extends RocksObject {
12   static {
RocksDB.loadLibrary()13     RocksDB.loadLibrary();
14   }
15 
16   /**
17    * Construct with default Options
18    */
EnvOptions()19   public EnvOptions() {
20     super(newEnvOptions());
21   }
22 
23   /**
24    * Construct from {@link DBOptions}.
25    *
26    * @param dbOptions the database options.
27    */
EnvOptions(final DBOptions dbOptions)28   public EnvOptions(final DBOptions dbOptions) {
29     super(newEnvOptions(dbOptions.nativeHandle_));
30   }
31 
32   /**
33    * Enable/Disable memory mapped reads.
34    *
35    * Default: false
36    *
37    * @param useMmapReads true to enable memory mapped reads, false to disable.
38    *
39    * @return the reference to these options.
40    */
setUseMmapReads(final boolean useMmapReads)41   public EnvOptions setUseMmapReads(final boolean useMmapReads) {
42     setUseMmapReads(nativeHandle_, useMmapReads);
43     return this;
44   }
45 
46   /**
47    * Determine if memory mapped reads are in-use.
48    *
49    * @return true if memory mapped reads are in-use, false otherwise.
50    */
useMmapReads()51   public boolean useMmapReads() {
52     assert(isOwningHandle());
53     return useMmapReads(nativeHandle_);
54   }
55 
56   /**
57    * Enable/Disable memory mapped Writes.
58    *
59    * Default: true
60    *
61    * @param useMmapWrites true to enable memory mapped writes, false to disable.
62    *
63    * @return the reference to these options.
64    */
setUseMmapWrites(final boolean useMmapWrites)65   public EnvOptions setUseMmapWrites(final boolean useMmapWrites) {
66     setUseMmapWrites(nativeHandle_, useMmapWrites);
67     return this;
68   }
69 
70   /**
71    * Determine if memory mapped writes are in-use.
72    *
73    * @return true if memory mapped writes are in-use, false otherwise.
74    */
useMmapWrites()75   public boolean useMmapWrites() {
76     assert(isOwningHandle());
77     return useMmapWrites(nativeHandle_);
78   }
79 
80   /**
81    * Enable/Disable direct reads, i.e. {@code O_DIRECT}.
82    *
83    * Default: false
84    *
85    * @param useDirectReads true to enable direct reads, false to disable.
86    *
87    * @return the reference to these options.
88    */
setUseDirectReads(final boolean useDirectReads)89   public EnvOptions setUseDirectReads(final boolean useDirectReads) {
90     setUseDirectReads(nativeHandle_, useDirectReads);
91     return this;
92   }
93 
94   /**
95    * Determine if direct reads are in-use.
96    *
97    * @return true if direct reads are in-use, false otherwise.
98    */
useDirectReads()99   public boolean useDirectReads() {
100     assert(isOwningHandle());
101     return useDirectReads(nativeHandle_);
102   }
103 
104   /**
105    * Enable/Disable direct writes, i.e. {@code O_DIRECT}.
106    *
107    * Default: false
108    *
109    * @param useDirectWrites true to enable direct writes, false to disable.
110    *
111    * @return the reference to these options.
112    */
setUseDirectWrites(final boolean useDirectWrites)113   public EnvOptions setUseDirectWrites(final boolean useDirectWrites) {
114     setUseDirectWrites(nativeHandle_, useDirectWrites);
115     return this;
116   }
117 
118   /**
119    * Determine if direct writes are in-use.
120    *
121    * @return true if direct writes are in-use, false otherwise.
122    */
useDirectWrites()123   public boolean useDirectWrites() {
124     assert(isOwningHandle());
125     return useDirectWrites(nativeHandle_);
126   }
127 
128   /**
129    * Enable/Disable fallocate calls.
130    *
131    * Default: true
132    *
133    * If false, {@code fallocate()} calls are bypassed.
134    *
135    * @param allowFallocate true to enable fallocate calls, false to disable.
136    *
137    * @return the reference to these options.
138    */
setAllowFallocate(final boolean allowFallocate)139   public EnvOptions setAllowFallocate(final boolean allowFallocate) {
140     setAllowFallocate(nativeHandle_, allowFallocate);
141     return this;
142   }
143 
144   /**
145    * Determine if fallocate calls are used.
146    *
147    * @return true if fallocate calls are used, false otherwise.
148    */
allowFallocate()149   public boolean allowFallocate() {
150     assert(isOwningHandle());
151     return allowFallocate(nativeHandle_);
152   }
153 
154   /**
155    * Enable/Disable the {@code FD_CLOEXEC} bit when opening file descriptors.
156    *
157    * Default: true
158    *
159    * @param setFdCloexec true to enable the {@code FB_CLOEXEC} bit,
160    *     false to disable.
161    *
162    * @return the reference to these options.
163    */
setSetFdCloexec(final boolean setFdCloexec)164   public EnvOptions setSetFdCloexec(final boolean setFdCloexec) {
165     setSetFdCloexec(nativeHandle_, setFdCloexec);
166     return this;
167   }
168 
169   /**
170    * Determine i fthe {@code FD_CLOEXEC} bit is set when opening file
171    * descriptors.
172    *
173    * @return true if the {@code FB_CLOEXEC} bit is enabled, false otherwise.
174    */
setFdCloexec()175   public boolean setFdCloexec() {
176     assert(isOwningHandle());
177     return setFdCloexec(nativeHandle_);
178   }
179 
180   /**
181    * Allows OS to incrementally sync files to disk while they are being
182    * written, in the background. Issue one request for every
183    * {@code bytesPerSync} written.
184    *
185    * Default: 0
186    *
187    * @param bytesPerSync 0 to disable, otherwise the number of bytes.
188    *
189    * @return the reference to these options.
190    */
setBytesPerSync(final long bytesPerSync)191   public EnvOptions setBytesPerSync(final long bytesPerSync) {
192     setBytesPerSync(nativeHandle_, bytesPerSync);
193     return this;
194   }
195 
196   /**
197    * Get the number of incremental bytes per sync written in the background.
198    *
199    * @return 0 if disabled, otherwise the number of bytes.
200    */
bytesPerSync()201   public long bytesPerSync() {
202     assert(isOwningHandle());
203     return bytesPerSync(nativeHandle_);
204   }
205 
206   /**
207    * If true, we will preallocate the file with {@code FALLOC_FL_KEEP_SIZE}
208    * flag, which means that file size won't change as part of preallocation.
209    * If false, preallocation will also change the file size. This option will
210    * improve the performance in workloads where you sync the data on every
211    * write. By default, we set it to true for MANIFEST writes and false for
212    * WAL writes
213    *
214    * @param fallocateWithKeepSize true to preallocate, false otherwise.
215    *
216    * @return the reference to these options.
217    */
setFallocateWithKeepSize( final boolean fallocateWithKeepSize)218   public EnvOptions setFallocateWithKeepSize(
219       final boolean fallocateWithKeepSize) {
220     setFallocateWithKeepSize(nativeHandle_, fallocateWithKeepSize);
221     return this;
222   }
223 
224   /**
225    * Determine if file is preallocated.
226    *
227    * @return true if the file is preallocated, false otherwise.
228    */
fallocateWithKeepSize()229   public boolean fallocateWithKeepSize() {
230     assert(isOwningHandle());
231     return fallocateWithKeepSize(nativeHandle_);
232   }
233 
234   /**
235    * See {@link DBOptions#setCompactionReadaheadSize(long)}.
236    *
237    * @param compactionReadaheadSize the compaction read-ahead size.
238    *
239    * @return the reference to these options.
240    */
setCompactionReadaheadSize( final long compactionReadaheadSize)241   public EnvOptions setCompactionReadaheadSize(
242       final long compactionReadaheadSize) {
243     setCompactionReadaheadSize(nativeHandle_, compactionReadaheadSize);
244     return this;
245   }
246 
247   /**
248    * See {@link DBOptions#compactionReadaheadSize()}.
249    *
250    * @return the compaction read-ahead size.
251    */
compactionReadaheadSize()252   public long compactionReadaheadSize() {
253     assert(isOwningHandle());
254     return compactionReadaheadSize(nativeHandle_);
255   }
256 
257   /**
258    * See {@link DBOptions#setRandomAccessMaxBufferSize(long)}.
259    *
260    * @param randomAccessMaxBufferSize the max buffer size for random access.
261    *
262    * @return the reference to these options.
263    */
setRandomAccessMaxBufferSize( final long randomAccessMaxBufferSize)264   public EnvOptions setRandomAccessMaxBufferSize(
265       final long randomAccessMaxBufferSize) {
266     setRandomAccessMaxBufferSize(nativeHandle_, randomAccessMaxBufferSize);
267     return this;
268   }
269 
270   /**
271    * See {@link DBOptions#randomAccessMaxBufferSize()}.
272    *
273    * @return the max buffer size for random access.
274    */
randomAccessMaxBufferSize()275   public long randomAccessMaxBufferSize() {
276     assert(isOwningHandle());
277     return randomAccessMaxBufferSize(nativeHandle_);
278   }
279 
280   /**
281    * See {@link DBOptions#setWritableFileMaxBufferSize(long)}.
282    *
283    * @param writableFileMaxBufferSize the max buffer size.
284    *
285    * @return the reference to these options.
286    */
setWritableFileMaxBufferSize( final long writableFileMaxBufferSize)287   public EnvOptions setWritableFileMaxBufferSize(
288       final long writableFileMaxBufferSize) {
289     setWritableFileMaxBufferSize(nativeHandle_, writableFileMaxBufferSize);
290     return this;
291   }
292 
293   /**
294    * See {@link DBOptions#writableFileMaxBufferSize()}.
295    *
296    * @return the max buffer size.
297    */
writableFileMaxBufferSize()298   public long writableFileMaxBufferSize() {
299     assert(isOwningHandle());
300     return writableFileMaxBufferSize(nativeHandle_);
301   }
302 
303   /**
304    * Set the write rate limiter for flush and compaction.
305    *
306    * @param rateLimiter the rate limiter.
307    *
308    * @return the reference to these options.
309    */
setRateLimiter(final RateLimiter rateLimiter)310   public EnvOptions setRateLimiter(final RateLimiter rateLimiter) {
311     this.rateLimiter = rateLimiter;
312     setRateLimiter(nativeHandle_, rateLimiter.nativeHandle_);
313     return this;
314   }
315 
316   /**
317    * Get the write rate limiter for flush and compaction.
318    *
319    * @return the rate limiter.
320    */
rateLimiter()321   public RateLimiter rateLimiter() {
322     assert(isOwningHandle());
323     return rateLimiter;
324   }
325 
newEnvOptions()326   private native static long newEnvOptions();
newEnvOptions(final long dboptions_handle)327   private native static long newEnvOptions(final long dboptions_handle);
disposeInternal(final long handle)328   @Override protected final native void disposeInternal(final long handle);
329 
setUseMmapReads(final long handle, final boolean useMmapReads)330   private native void setUseMmapReads(final long handle,
331       final boolean useMmapReads);
useMmapReads(final long handle)332   private native boolean useMmapReads(final long handle);
setUseMmapWrites(final long handle, final boolean useMmapWrites)333   private native void setUseMmapWrites(final long handle,
334       final boolean useMmapWrites);
useMmapWrites(final long handle)335   private native boolean useMmapWrites(final long handle);
setUseDirectReads(final long handle, final boolean useDirectReads)336   private native void setUseDirectReads(final long handle,
337       final boolean useDirectReads);
useDirectReads(final long handle)338   private native boolean useDirectReads(final long handle);
setUseDirectWrites(final long handle, final boolean useDirectWrites)339   private native void setUseDirectWrites(final long handle,
340       final boolean useDirectWrites);
useDirectWrites(final long handle)341   private native boolean useDirectWrites(final long handle);
setAllowFallocate(final long handle, final boolean allowFallocate)342   private native void setAllowFallocate(final long handle,
343       final boolean allowFallocate);
allowFallocate(final long handle)344   private native boolean allowFallocate(final long handle);
setSetFdCloexec(final long handle, final boolean setFdCloexec)345   private native void setSetFdCloexec(final long handle,
346       final boolean setFdCloexec);
setFdCloexec(final long handle)347   private native boolean setFdCloexec(final long handle);
setBytesPerSync(final long handle, final long bytesPerSync)348   private native void setBytesPerSync(final long handle,
349       final long bytesPerSync);
bytesPerSync(final long handle)350   private native long bytesPerSync(final long handle);
setFallocateWithKeepSize( final long handle, final boolean fallocateWithKeepSize)351   private native void setFallocateWithKeepSize(
352       final long handle, final boolean fallocateWithKeepSize);
fallocateWithKeepSize(final long handle)353   private native boolean fallocateWithKeepSize(final long handle);
setCompactionReadaheadSize( final long handle, final long compactionReadaheadSize)354   private native void setCompactionReadaheadSize(
355       final long handle, final long compactionReadaheadSize);
compactionReadaheadSize(final long handle)356   private native long compactionReadaheadSize(final long handle);
setRandomAccessMaxBufferSize( final long handle, final long randomAccessMaxBufferSize)357   private native void setRandomAccessMaxBufferSize(
358       final long handle, final long randomAccessMaxBufferSize);
randomAccessMaxBufferSize(final long handle)359   private native long randomAccessMaxBufferSize(final long handle);
setWritableFileMaxBufferSize( final long handle, final long writableFileMaxBufferSize)360   private native void setWritableFileMaxBufferSize(
361       final long handle, final long writableFileMaxBufferSize);
writableFileMaxBufferSize(final long handle)362   private native long writableFileMaxBufferSize(final long handle);
setRateLimiter(final long handle, final long rateLimiterHandle)363   private native void setRateLimiter(final long handle,
364       final long rateLimiterHandle);
365   private RateLimiter rateLimiter;
366 }
367