]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
5 | |
6 | package org.rocksdb; | |
7 | ||
8 | /** | |
9 | * Options that control write operations. | |
10 | * | |
11 | * Note that developers should call WriteOptions.dispose() to release the | |
12 | * c++ side memory before a WriteOptions instance runs out of scope. | |
13 | */ | |
14 | public class WriteOptions extends RocksObject { | |
15 | /** | |
16 | * Construct WriteOptions instance. | |
17 | */ | |
18 | public WriteOptions() { | |
19 | super(newWriteOptions()); | |
20 | ||
21 | } | |
22 | ||
11fdf7f2 TL |
23 | // TODO(AR) consider ownership |
24 | WriteOptions(final long nativeHandle) { | |
25 | super(nativeHandle); | |
26 | disOwnNativeHandle(); | |
27 | } | |
28 | ||
29 | /** | |
30 | * Copy constructor for WriteOptions. | |
31 | * | |
32 | * NOTE: This does a shallow copy, which means comparator, merge_operator, compaction_filter, | |
33 | * compaction_filter_factory and other pointers will be cloned! | |
34 | * | |
35 | * @param other The ColumnFamilyOptions to copy. | |
36 | */ | |
37 | public WriteOptions(WriteOptions other) { | |
38 | super(copyWriteOptions(other.nativeHandle_)); | |
39 | } | |
40 | ||
41 | ||
7c673cae FG |
42 | /** |
43 | * If true, the write will be flushed from the operating system | |
44 | * buffer cache (by calling WritableFile::Sync()) before the write | |
45 | * is considered complete. If this flag is true, writes will be | |
46 | * slower. | |
47 | * | |
48 | * If this flag is false, and the machine crashes, some recent | |
49 | * writes may be lost. Note that if it is just the process that | |
50 | * crashes (i.e., the machine does not reboot), no writes will be | |
51 | * lost even if sync==false. | |
52 | * | |
53 | * In other words, a DB write with sync==false has similar | |
54 | * crash semantics as the "write()" system call. A DB write | |
55 | * with sync==true has similar crash semantics to a "write()" | |
56 | * system call followed by "fdatasync()". | |
57 | * | |
58 | * Default: false | |
59 | * | |
60 | * @param flag a boolean flag to indicate whether a write | |
61 | * should be synchronized. | |
62 | * @return the instance of the current WriteOptions. | |
63 | */ | |
64 | public WriteOptions setSync(final boolean flag) { | |
65 | setSync(nativeHandle_, flag); | |
66 | return this; | |
67 | } | |
68 | ||
69 | /** | |
70 | * If true, the write will be flushed from the operating system | |
71 | * buffer cache (by calling WritableFile::Sync()) before the write | |
72 | * is considered complete. If this flag is true, writes will be | |
73 | * slower. | |
74 | * | |
75 | * If this flag is false, and the machine crashes, some recent | |
76 | * writes may be lost. Note that if it is just the process that | |
77 | * crashes (i.e., the machine does not reboot), no writes will be | |
78 | * lost even if sync==false. | |
79 | * | |
80 | * In other words, a DB write with sync==false has similar | |
81 | * crash semantics as the "write()" system call. A DB write | |
82 | * with sync==true has similar crash semantics to a "write()" | |
83 | * system call followed by "fdatasync()". | |
84 | * | |
85 | * @return boolean value indicating if sync is active. | |
86 | */ | |
87 | public boolean sync() { | |
88 | return sync(nativeHandle_); | |
89 | } | |
90 | ||
91 | /** | |
92 | * If true, writes will not first go to the write ahead log, | |
494da23a TL |
93 | * and the write may got lost after a crash. The backup engine |
94 | * relies on write-ahead logs to back up the memtable, so if | |
95 | * you disable write-ahead logs, you must create backups with | |
96 | * flush_before_backup=true to avoid losing unflushed memtable data. | |
7c673cae FG |
97 | * |
98 | * @param flag a boolean flag to specify whether to disable | |
99 | * write-ahead-log on writes. | |
100 | * @return the instance of the current WriteOptions. | |
101 | */ | |
102 | public WriteOptions setDisableWAL(final boolean flag) { | |
103 | setDisableWAL(nativeHandle_, flag); | |
104 | return this; | |
105 | } | |
106 | ||
107 | /** | |
108 | * If true, writes will not first go to the write ahead log, | |
494da23a TL |
109 | * and the write may got lost after a crash. The backup engine |
110 | * relies on write-ahead logs to back up the memtable, so if | |
111 | * you disable write-ahead logs, you must create backups with | |
112 | * flush_before_backup=true to avoid losing unflushed memtable data. | |
7c673cae FG |
113 | * |
114 | * @return boolean value indicating if WAL is disabled. | |
115 | */ | |
116 | public boolean disableWAL() { | |
117 | return disableWAL(nativeHandle_); | |
118 | } | |
119 | ||
120 | /** | |
121 | * If true and if user is trying to write to column families that don't exist | |
122 | * (they were dropped), ignore the write (don't return an error). If there | |
123 | * are multiple writes in a WriteBatch, other writes will succeed. | |
124 | * | |
125 | * Default: false | |
126 | * | |
127 | * @param ignoreMissingColumnFamilies true to ignore writes to column families | |
128 | * which don't exist | |
129 | * @return the instance of the current WriteOptions. | |
130 | */ | |
131 | public WriteOptions setIgnoreMissingColumnFamilies( | |
132 | final boolean ignoreMissingColumnFamilies) { | |
133 | setIgnoreMissingColumnFamilies(nativeHandle_, ignoreMissingColumnFamilies); | |
134 | return this; | |
135 | } | |
136 | ||
137 | /** | |
138 | * If true and if user is trying to write to column families that don't exist | |
139 | * (they were dropped), ignore the write (don't return an error). If there | |
140 | * are multiple writes in a WriteBatch, other writes will succeed. | |
141 | * | |
142 | * Default: false | |
143 | * | |
144 | * @return true if writes to column families which don't exist are ignored | |
145 | */ | |
146 | public boolean ignoreMissingColumnFamilies() { | |
147 | return ignoreMissingColumnFamilies(nativeHandle_); | |
148 | } | |
149 | ||
150 | /** | |
151 | * If true and we need to wait or sleep for the write request, fails | |
152 | * immediately with {@link Status.Code#Incomplete}. | |
153 | * | |
154 | * @param noSlowdown true to fail write requests if we need to wait or sleep | |
155 | * @return the instance of the current WriteOptions. | |
156 | */ | |
157 | public WriteOptions setNoSlowdown(final boolean noSlowdown) { | |
158 | setNoSlowdown(nativeHandle_, noSlowdown); | |
159 | return this; | |
160 | } | |
161 | ||
162 | /** | |
163 | * If true and we need to wait or sleep for the write request, fails | |
164 | * immediately with {@link Status.Code#Incomplete}. | |
165 | * | |
166 | * @return true when write requests are failed if we need to wait or sleep | |
167 | */ | |
168 | public boolean noSlowdown() { | |
169 | return noSlowdown(nativeHandle_); | |
170 | } | |
171 | ||
494da23a TL |
172 | /** |
173 | * If true, this write request is of lower priority if compaction is | |
1e59de90 | 174 | * behind. In the case that, {@link #noSlowdown()} == true, the request |
494da23a TL |
175 | * will be cancelled immediately with {@link Status.Code#Incomplete} returned. |
176 | * Otherwise, it will be slowed down. The slowdown value is determined by | |
177 | * RocksDB to guarantee it introduces minimum impacts to high priority writes. | |
178 | * | |
179 | * Default: false | |
180 | * | |
181 | * @param lowPri true if the write request should be of lower priority than | |
182 | * compactions which are behind. | |
183 | * | |
184 | * @return the instance of the current WriteOptions. | |
185 | */ | |
186 | public WriteOptions setLowPri(final boolean lowPri) { | |
187 | setLowPri(nativeHandle_, lowPri); | |
188 | return this; | |
189 | } | |
190 | ||
191 | /** | |
192 | * Returns true if this write request is of lower priority if compaction is | |
193 | * behind. | |
194 | * | |
195 | * See {@link #setLowPri(boolean)}. | |
196 | * | |
197 | * @return true if this write request is of lower priority, false otherwise. | |
198 | */ | |
199 | public boolean lowPri() { | |
200 | return lowPri(nativeHandle_); | |
201 | } | |
202 | ||
1e59de90 TL |
203 | /** |
204 | * If true, this writebatch will maintain the last insert positions of each | |
205 | * memtable as hints in concurrent write. It can improve write performance | |
206 | * in concurrent writes if keys in one writebatch are sequential. In | |
207 | * non-concurrent writes (when {@code concurrent_memtable_writes} is false) this | |
208 | * option will be ignored. | |
209 | * | |
210 | * Default: false | |
211 | * | |
212 | * @return true if writebatch will maintain the last insert positions of each memtable as hints in | |
213 | * concurrent write. | |
214 | */ | |
215 | public boolean memtableInsertHintPerBatch() { | |
216 | return memtableInsertHintPerBatch(nativeHandle_); | |
217 | } | |
218 | ||
219 | /** | |
220 | * If true, this writebatch will maintain the last insert positions of each | |
221 | * memtable as hints in concurrent write. It can improve write performance | |
222 | * in concurrent writes if keys in one writebatch are sequential. In | |
223 | * non-concurrent writes (when {@code concurrent_memtable_writes} is false) this | |
224 | * option will be ignored. | |
225 | * | |
226 | * Default: false | |
227 | * | |
228 | * @param memtableInsertHintPerBatch true if writebatch should maintain the last insert positions | |
229 | * of each memtable as hints in concurrent write. | |
230 | * @return the instance of the current WriteOptions. | |
231 | */ | |
232 | public WriteOptions setMemtableInsertHintPerBatch(final boolean memtableInsertHintPerBatch) { | |
233 | setMemtableInsertHintPerBatch(nativeHandle_, memtableInsertHintPerBatch); | |
234 | return this; | |
235 | } | |
236 | ||
7c673cae | 237 | private native static long newWriteOptions(); |
11fdf7f2 | 238 | private native static long copyWriteOptions(long handle); |
494da23a TL |
239 | @Override protected final native void disposeInternal(final long handle); |
240 | ||
7c673cae FG |
241 | private native void setSync(long handle, boolean flag); |
242 | private native boolean sync(long handle); | |
243 | private native void setDisableWAL(long handle, boolean flag); | |
244 | private native boolean disableWAL(long handle); | |
245 | private native void setIgnoreMissingColumnFamilies(final long handle, | |
246 | final boolean ignoreMissingColumnFamilies); | |
247 | private native boolean ignoreMissingColumnFamilies(final long handle); | |
248 | private native void setNoSlowdown(final long handle, | |
249 | final boolean noSlowdown); | |
250 | private native boolean noSlowdown(final long handle); | |
494da23a TL |
251 | private native void setLowPri(final long handle, final boolean lowPri); |
252 | private native boolean lowPri(final long handle); | |
1e59de90 TL |
253 | private native boolean memtableInsertHintPerBatch(final long handle); |
254 | private native void setMemtableInsertHintPerBatch( | |
255 | final long handle, final boolean memtableInsertHintPerBatch); | |
7c673cae | 256 | } |