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 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 */ WriteOptions()18 public WriteOptions() { 19 super(newWriteOptions()); 20 21 } 22 23 // TODO(AR) consider ownership WriteOptions(final long nativeHandle)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 */ WriteOptions(WriteOptions other)37 public WriteOptions(WriteOptions other) { 38 super(copyWriteOptions(other.nativeHandle_)); 39 } 40 41 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 */ setSync(final boolean flag)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 */ sync()87 public boolean sync() { 88 return sync(nativeHandle_); 89 } 90 91 /** 92 * If true, writes will not first go to the write ahead log, 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. 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 */ setDisableWAL(final boolean flag)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, 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. 113 * 114 * @return boolean value indicating if WAL is disabled. 115 */ disableWAL()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 */ setIgnoreMissingColumnFamilies( final boolean ignoreMissingColumnFamilies)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 */ ignoreMissingColumnFamilies()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 */ setNoSlowdown(final boolean noSlowdown)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 */ noSlowdown()168 public boolean noSlowdown() { 169 return noSlowdown(nativeHandle_); 170 } 171 172 /** 173 * If true, this write request is of lower priority if compaction is 174 * behind. In this case that, {@link #noSlowdown()} == true, the request 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 */ setLowPri(final boolean lowPri)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 */ lowPri()199 public boolean lowPri() { 200 return lowPri(nativeHandle_); 201 } 202 newWriteOptions()203 private native static long newWriteOptions(); copyWriteOptions(long handle)204 private native static long copyWriteOptions(long handle); disposeInternal(final long handle)205 @Override protected final native void disposeInternal(final long handle); 206 setSync(long handle, boolean flag)207 private native void setSync(long handle, boolean flag); sync(long handle)208 private native boolean sync(long handle); setDisableWAL(long handle, boolean flag)209 private native void setDisableWAL(long handle, boolean flag); disableWAL(long handle)210 private native boolean disableWAL(long handle); setIgnoreMissingColumnFamilies(final long handle, final boolean ignoreMissingColumnFamilies)211 private native void setIgnoreMissingColumnFamilies(final long handle, 212 final boolean ignoreMissingColumnFamilies); ignoreMissingColumnFamilies(final long handle)213 private native boolean ignoreMissingColumnFamilies(final long handle); setNoSlowdown(final long handle, final boolean noSlowdown)214 private native void setNoSlowdown(final long handle, 215 final boolean noSlowdown); noSlowdown(final long handle)216 private native boolean noSlowdown(final long handle); setLowPri(final long handle, final boolean lowPri)217 private native void setLowPri(final long handle, final boolean lowPri); lowPri(final long handle)218 private native boolean lowPri(final long handle); 219 } 220