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.nio.ByteBuffer; 9 10 /** 11 * SstFileWriter is used to create sst files that can be added to the 12 * database later. All keys in files generated by SstFileWriter will have 13 * sequence number = 0. 14 */ 15 public class SstFileWriter extends RocksObject { 16 static { RocksDB.loadLibrary()17 RocksDB.loadLibrary(); 18 } 19 20 /** 21 * SstFileWriter Constructor. 22 * 23 * @param envOptions {@link org.rocksdb.EnvOptions} instance. 24 * @param options {@link org.rocksdb.Options} instance. 25 * @param comparator the comparator to specify the ordering of keys. 26 * 27 * @deprecated Use {@link #SstFileWriter(EnvOptions, Options)}. 28 * Passing an explicit comparator is deprecated in lieu of passing the 29 * comparator as part of options. Use the other constructor instead. 30 */ 31 @Deprecated SstFileWriter(final EnvOptions envOptions, final Options options, final AbstractComparator comparator)32 public SstFileWriter(final EnvOptions envOptions, final Options options, 33 final AbstractComparator comparator) { 34 super(newSstFileWriter( 35 envOptions.nativeHandle_, options.nativeHandle_, comparator.nativeHandle_, 36 comparator.getComparatorType().getValue())); 37 } 38 39 /** 40 * SstFileWriter Constructor. 41 * 42 * @param envOptions {@link org.rocksdb.EnvOptions} instance. 43 * @param options {@link org.rocksdb.Options} instance. 44 */ SstFileWriter(final EnvOptions envOptions, final Options options)45 public SstFileWriter(final EnvOptions envOptions, final Options options) { 46 super(newSstFileWriter( 47 envOptions.nativeHandle_, options.nativeHandle_)); 48 } 49 50 /** 51 * Prepare SstFileWriter to write to a file. 52 * 53 * @param filePath the location of file 54 * 55 * @throws RocksDBException thrown if error happens in underlying 56 * native library. 57 */ open(final String filePath)58 public void open(final String filePath) throws RocksDBException { 59 open(nativeHandle_, filePath); 60 } 61 62 /** 63 * Add a Put key with value to currently opened file. 64 * 65 * @param key the specified key to be inserted. 66 * @param value the value associated with the specified key. 67 * 68 * @throws RocksDBException thrown if error happens in underlying 69 * native library. 70 * 71 * @deprecated Use {@link #put(Slice, Slice)} 72 */ 73 @Deprecated add(final Slice key, final Slice value)74 public void add(final Slice key, final Slice value) 75 throws RocksDBException { 76 put(nativeHandle_, key.getNativeHandle(), value.getNativeHandle()); 77 } 78 79 /** 80 * Add a Put key with value to currently opened file. 81 * 82 * @param key the specified key to be inserted. 83 * @param value the value associated with the specified key. 84 * 85 * @throws RocksDBException thrown if error happens in underlying 86 * native library. 87 * 88 * @deprecated Use {@link #put(DirectSlice, DirectSlice)} 89 */ 90 @Deprecated add(final DirectSlice key, final DirectSlice value)91 public void add(final DirectSlice key, final DirectSlice value) 92 throws RocksDBException { 93 put(nativeHandle_, key.getNativeHandle(), value.getNativeHandle()); 94 } 95 96 /** 97 * Add a Put key with value to currently opened file. 98 * 99 * @param key the specified key to be inserted. 100 * @param value the value associated with the specified key. 101 * 102 * @throws RocksDBException thrown if error happens in underlying 103 * native library. 104 */ put(final Slice key, final Slice value)105 public void put(final Slice key, final Slice value) throws RocksDBException { 106 put(nativeHandle_, key.getNativeHandle(), value.getNativeHandle()); 107 } 108 109 /** 110 * Add a Put key with value to currently opened file. 111 * 112 * @param key the specified key to be inserted. 113 * @param value the value associated with the specified key. 114 * 115 * @throws RocksDBException thrown if error happens in underlying 116 * native library. 117 */ put(final DirectSlice key, final DirectSlice value)118 public void put(final DirectSlice key, final DirectSlice value) 119 throws RocksDBException { 120 put(nativeHandle_, key.getNativeHandle(), value.getNativeHandle()); 121 } 122 123 /** 124 * Add a Put key with value to currently opened file. 125 * 126 * @param key the specified key to be inserted. 127 * @param value the value associated with the specified key. 128 * 129 * @throws RocksDBException thrown if error happens in underlying 130 * native library. 131 */ put(final ByteBuffer key, final ByteBuffer value)132 public void put(final ByteBuffer key, final ByteBuffer value) throws RocksDBException { 133 assert key.isDirect() && value.isDirect(); 134 putDirect(nativeHandle_, key, key.position(), key.remaining(), value, value.position(), 135 value.remaining()); 136 key.position(key.limit()); 137 value.position(value.limit()); 138 } 139 140 /** 141 * Add a Put key with value to currently opened file. 142 * 143 * @param key the specified key to be inserted. 144 * @param value the value associated with the specified key. 145 * 146 * @throws RocksDBException thrown if error happens in underlying 147 * native library. 148 */ put(final byte[] key, final byte[] value)149 public void put(final byte[] key, final byte[] value) throws RocksDBException { 150 put(nativeHandle_, key, value); 151 } 152 153 /** 154 * Add a Merge key with value to currently opened file. 155 * 156 * @param key the specified key to be merged. 157 * @param value the value to be merged with the current value for 158 * the specified key. 159 * 160 * @throws RocksDBException thrown if error happens in underlying 161 * native library. 162 */ merge(final Slice key, final Slice value)163 public void merge(final Slice key, final Slice value) 164 throws RocksDBException { 165 merge(nativeHandle_, key.getNativeHandle(), value.getNativeHandle()); 166 } 167 168 /** 169 * Add a Merge key with value to currently opened file. 170 * 171 * @param key the specified key to be merged. 172 * @param value the value to be merged with the current value for 173 * the specified key. 174 * 175 * @throws RocksDBException thrown if error happens in underlying 176 * native library. 177 */ merge(final byte[] key, final byte[] value)178 public void merge(final byte[] key, final byte[] value) 179 throws RocksDBException { 180 merge(nativeHandle_, key, value); 181 } 182 183 /** 184 * Add a Merge key with value to currently opened file. 185 * 186 * @param key the specified key to be merged. 187 * @param value the value to be merged with the current value for 188 * the specified key. 189 * 190 * @throws RocksDBException thrown if error happens in underlying 191 * native library. 192 */ merge(final DirectSlice key, final DirectSlice value)193 public void merge(final DirectSlice key, final DirectSlice value) 194 throws RocksDBException { 195 merge(nativeHandle_, key.getNativeHandle(), value.getNativeHandle()); 196 } 197 198 /** 199 * Add a deletion key to currently opened file. 200 * 201 * @param key the specified key to be deleted. 202 * 203 * @throws RocksDBException thrown if error happens in underlying 204 * native library. 205 */ delete(final Slice key)206 public void delete(final Slice key) throws RocksDBException { 207 delete(nativeHandle_, key.getNativeHandle()); 208 } 209 210 /** 211 * Add a deletion key to currently opened file. 212 * 213 * @param key the specified key to be deleted. 214 * 215 * @throws RocksDBException thrown if error happens in underlying 216 * native library. 217 */ delete(final DirectSlice key)218 public void delete(final DirectSlice key) throws RocksDBException { 219 delete(nativeHandle_, key.getNativeHandle()); 220 } 221 222 /** 223 * Add a deletion key to currently opened file. 224 * 225 * @param key the specified key to be deleted. 226 * 227 * @throws RocksDBException thrown if error happens in underlying 228 * native library. 229 */ delete(final byte[] key)230 public void delete(final byte[] key) throws RocksDBException { 231 delete(nativeHandle_, key); 232 } 233 234 /** 235 * Finish the process and close the sst file. 236 * 237 * @throws RocksDBException thrown if error happens in underlying 238 * native library. 239 */ finish()240 public void finish() throws RocksDBException { 241 finish(nativeHandle_); 242 } 243 244 /** 245 * Return the current file size. 246 * 247 * @throws RocksDBException thrown if error happens in underlying 248 * native library. 249 */ fileSize()250 public long fileSize() throws RocksDBException { 251 return fileSize(nativeHandle_); 252 } 253 newSstFileWriter( final long envOptionsHandle, final long optionsHandle, final long userComparatorHandle, final byte comparatorType)254 private native static long newSstFileWriter( 255 final long envOptionsHandle, final long optionsHandle, 256 final long userComparatorHandle, final byte comparatorType); 257 newSstFileWriter(final long envOptionsHandle, final long optionsHandle)258 private native static long newSstFileWriter(final long envOptionsHandle, 259 final long optionsHandle); 260 open(final long handle, final String filePath)261 private native void open(final long handle, final String filePath) 262 throws RocksDBException; 263 put(final long handle, final long keyHandle, final long valueHandle)264 private native void put(final long handle, final long keyHandle, 265 final long valueHandle) throws RocksDBException; 266 put(final long handle, final byte[] key, final byte[] value)267 private native void put(final long handle, final byte[] key, 268 final byte[] value) throws RocksDBException; 269 putDirect(long handle, ByteBuffer key, int keyOffset, int keyLength, ByteBuffer value, int valueOffset, int valueLength)270 private native void putDirect(long handle, ByteBuffer key, int keyOffset, int keyLength, 271 ByteBuffer value, int valueOffset, int valueLength) throws RocksDBException; 272 fileSize(long handle)273 private native long fileSize(long handle) throws RocksDBException; 274 merge(final long handle, final long keyHandle, final long valueHandle)275 private native void merge(final long handle, final long keyHandle, 276 final long valueHandle) throws RocksDBException; 277 merge(final long handle, final byte[] key, final byte[] value)278 private native void merge(final long handle, final byte[] key, 279 final byte[] value) throws RocksDBException; 280 delete(final long handle, final long keyHandle)281 private native void delete(final long handle, final long keyHandle) 282 throws RocksDBException; 283 delete(final long handle, final byte[] key)284 private native void delete(final long handle, final byte[] key) 285 throws RocksDBException; 286 finish(final long handle)287 private native void finish(final long handle) throws RocksDBException; 288 disposeInternal(final long handle)289 @Override protected final native void disposeInternal(final long handle); 290 } 291