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 public class TransactionDBOptions extends RocksObject {
9 
TransactionDBOptions()10   public TransactionDBOptions() {
11     super(newTransactionDBOptions());
12   }
13 
14   /**
15    * Specifies the maximum number of keys that can be locked at the same time
16    * per column family.
17    *
18    * If the number of locked keys is greater than {@link #getMaxNumLocks()},
19    * transaction writes (or GetForUpdate) will return an error.
20    *
21    * @return The maximum number of keys that can be locked
22    */
getMaxNumLocks()23   public long getMaxNumLocks() {
24     assert(isOwningHandle());
25     return getMaxNumLocks(nativeHandle_);
26   }
27 
28   /**
29    * Specifies the maximum number of keys that can be locked at the same time
30    * per column family.
31    *
32    * If the number of locked keys is greater than {@link #getMaxNumLocks()},
33    * transaction writes (or GetForUpdate) will return an error.
34    *
35    * @param maxNumLocks The maximum number of keys that can be locked;
36    *     If this value is not positive, no limit will be enforced.
37    *
38    * @return this TransactionDBOptions instance
39    */
setMaxNumLocks(final long maxNumLocks)40   public TransactionDBOptions setMaxNumLocks(final long maxNumLocks) {
41     assert(isOwningHandle());
42     setMaxNumLocks(nativeHandle_, maxNumLocks);
43     return this;
44   }
45 
46   /**
47    * The number of sub-tables per lock table (per column family)
48    *
49    * @return The number of sub-tables
50    */
getNumStripes()51   public long getNumStripes() {
52     assert(isOwningHandle());
53     return getNumStripes(nativeHandle_);
54   }
55 
56   /**
57    * Increasing this value will increase the concurrency by dividing the lock
58    * table (per column family) into more sub-tables, each with their own
59    * separate mutex.
60    *
61    * Default: 16
62    *
63    * @param numStripes The number of sub-tables
64    *
65    * @return this TransactionDBOptions instance
66    */
setNumStripes(final long numStripes)67   public TransactionDBOptions setNumStripes(final long numStripes) {
68     assert(isOwningHandle());
69     setNumStripes(nativeHandle_, numStripes);
70     return this;
71   }
72 
73   /**
74    * The default wait timeout in milliseconds when
75    * a transaction attempts to lock a key if not specified by
76    * {@link TransactionOptions#setLockTimeout(long)}
77    *
78    * If 0, no waiting is done if a lock cannot instantly be acquired.
79    * If negative, there is no timeout.
80    *
81    * @return the default wait timeout in milliseconds
82    */
getTransactionLockTimeout()83   public long getTransactionLockTimeout() {
84     assert(isOwningHandle());
85     return getTransactionLockTimeout(nativeHandle_);
86   }
87 
88   /**
89    * If positive, specifies the default wait timeout in milliseconds when
90    * a transaction attempts to lock a key if not specified by
91    * {@link TransactionOptions#setLockTimeout(long)}
92    *
93    * If 0, no waiting is done if a lock cannot instantly be acquired.
94    * If negative, there is no timeout. Not using a timeout is not recommended
95    * as it can lead to deadlocks.  Currently, there is no deadlock-detection to
96    * recover from a deadlock.
97    *
98    * Default: 1000
99    *
100    * @param transactionLockTimeout the default wait timeout in milliseconds
101    *
102    * @return this TransactionDBOptions instance
103    */
setTransactionLockTimeout( final long transactionLockTimeout)104   public TransactionDBOptions setTransactionLockTimeout(
105       final long transactionLockTimeout) {
106     assert(isOwningHandle());
107     setTransactionLockTimeout(nativeHandle_, transactionLockTimeout);
108     return this;
109   }
110 
111   /**
112    * The wait timeout in milliseconds when writing a key
113    * OUTSIDE of a transaction (ie by calling {@link RocksDB#put},
114    * {@link RocksDB#merge}, {@link RocksDB#delete} or {@link RocksDB#write}
115    * directly).
116    *
117    * If 0, no waiting is done if a lock cannot instantly be acquired.
118    * If negative, there is no timeout and will block indefinitely when acquiring
119    * a lock.
120    *
121    * @return the timeout in milliseconds when writing a key OUTSIDE of a
122    *     transaction
123    */
getDefaultLockTimeout()124   public long getDefaultLockTimeout() {
125     assert(isOwningHandle());
126     return getDefaultLockTimeout(nativeHandle_);
127   }
128 
129   /**
130    * If positive, specifies the wait timeout in milliseconds when writing a key
131    * OUTSIDE of a transaction (ie by calling {@link RocksDB#put},
132    * {@link RocksDB#merge}, {@link RocksDB#delete} or {@link RocksDB#write}
133    * directly).
134    *
135    * If 0, no waiting is done if a lock cannot instantly be acquired.
136    * If negative, there is no timeout and will block indefinitely when acquiring
137    * a lock.
138    *
139    * Not using a timeout can lead to deadlocks. Currently, there
140    * is no deadlock-detection to recover from a deadlock.  While DB writes
141    * cannot deadlock with other DB writes, they can deadlock with a transaction.
142    * A negative timeout should only be used if all transactions have a small
143    * expiration set.
144    *
145    * Default: 1000
146    *
147    * @param defaultLockTimeout the timeout in milliseconds when writing a key
148    *     OUTSIDE of a transaction
149    * @return this TransactionDBOptions instance
150    */
setDefaultLockTimeout( final long defaultLockTimeout)151    public TransactionDBOptions setDefaultLockTimeout(
152        final long defaultLockTimeout) {
153      assert(isOwningHandle());
154      setDefaultLockTimeout(nativeHandle_, defaultLockTimeout);
155      return this;
156    }
157 
158 //  /**
159 //   * If set, the {@link TransactionDB} will use this implementation of a mutex
160 //   * and condition variable for all transaction locking instead of the default
161 //   * mutex/condvar implementation.
162 //   *
163 //   * @param transactionDbMutexFactory the mutex factory for the transactions
164 //   *
165 //   * @return this TransactionDBOptions instance
166 //   */
167 //  public TransactionDBOptions setCustomMutexFactory(
168 //      final TransactionDBMutexFactory transactionDbMutexFactory) {
169 //
170 //  }
171 
172   /**
173    * The policy for when to write the data into the DB. The default policy is to
174    * write only the committed data {@link TxnDBWritePolicy#WRITE_COMMITTED}.
175    * The data could be written before the commit phase. The DB then needs to
176    * provide the mechanisms to tell apart committed from uncommitted data.
177    *
178    * @return The write policy.
179    */
getWritePolicy()180   public TxnDBWritePolicy getWritePolicy() {
181     assert(isOwningHandle());
182     return TxnDBWritePolicy.getTxnDBWritePolicy(getWritePolicy(nativeHandle_));
183   }
184 
185   /**
186    * The policy for when to write the data into the DB. The default policy is to
187    * write only the committed data {@link TxnDBWritePolicy#WRITE_COMMITTED}.
188    * The data could be written before the commit phase. The DB then needs to
189    * provide the mechanisms to tell apart committed from uncommitted data.
190    *
191    * @param writePolicy The write policy.
192    *
193    * @return this TransactionDBOptions instance
194    */
setWritePolicy( final TxnDBWritePolicy writePolicy)195   public TransactionDBOptions setWritePolicy(
196       final TxnDBWritePolicy writePolicy) {
197     assert(isOwningHandle());
198     setWritePolicy(nativeHandle_, writePolicy.getValue());
199     return this;
200   }
201 
newTransactionDBOptions()202   private native static long newTransactionDBOptions();
getMaxNumLocks(final long handle)203   private native long getMaxNumLocks(final long handle);
setMaxNumLocks(final long handle, final long maxNumLocks)204   private native void setMaxNumLocks(final long handle,
205       final long maxNumLocks);
getNumStripes(final long handle)206   private native long getNumStripes(final long handle);
setNumStripes(final long handle, final long numStripes)207   private native void setNumStripes(final long handle, final long numStripes);
getTransactionLockTimeout(final long handle)208   private native long getTransactionLockTimeout(final long handle);
setTransactionLockTimeout(final long handle, final long transactionLockTimeout)209   private native void setTransactionLockTimeout(final long handle,
210       final long transactionLockTimeout);
getDefaultLockTimeout(final long handle)211   private native long getDefaultLockTimeout(final long handle);
setDefaultLockTimeout(final long handle, final long transactionLockTimeout)212   private native void setDefaultLockTimeout(final long handle,
213       final long transactionLockTimeout);
getWritePolicy(final long handle)214   private native byte getWritePolicy(final long handle);
setWritePolicy(final long handle, final byte writePolicy)215   private native void setWritePolicy(final long handle, final byte writePolicy);
disposeInternal(final long handle)216   @Override protected final native void disposeInternal(final long handle);
217 }
218