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).
8 public class TransactionDBOptions
extends RocksObject
{
10 public TransactionDBOptions() {
11 super(newTransactionDBOptions());
15 * Specifies the maximum number of keys that can be locked at the same time
18 * If the number of locked keys is greater than {@link #getMaxNumLocks()},
19 * transaction writes (or GetForUpdate) will return an error.
21 * @return The maximum number of keys that can be locked
23 public long getMaxNumLocks() {
24 assert(isOwningHandle());
25 return getMaxNumLocks(nativeHandle_
);
29 * Specifies the maximum number of keys that can be locked at the same time
32 * If the number of locked keys is greater than {@link #getMaxNumLocks()},
33 * transaction writes (or GetForUpdate) will return an error.
35 * @param maxNumLocks The maximum number of keys that can be locked;
36 * If this value is not positive, no limit will be enforced.
38 * @return this TransactionDBOptions instance
40 public TransactionDBOptions
setMaxNumLocks(final long maxNumLocks
) {
41 assert(isOwningHandle());
42 setMaxNumLocks(nativeHandle_
, maxNumLocks
);
47 * The number of sub-tables per lock table (per column family)
49 * @return The number of sub-tables
51 public long getNumStripes() {
52 assert(isOwningHandle());
53 return getNumStripes(nativeHandle_
);
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
63 * @param numStripes The number of sub-tables
65 * @return this TransactionDBOptions instance
67 public TransactionDBOptions
setNumStripes(final long numStripes
) {
68 assert(isOwningHandle());
69 setNumStripes(nativeHandle_
, numStripes
);
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)}
78 * If 0, no waiting is done if a lock cannot instantly be acquired.
79 * If negative, there is no timeout.
81 * @return the default wait timeout in milliseconds
83 public long getTransactionLockTimeout() {
84 assert(isOwningHandle());
85 return getTransactionLockTimeout(nativeHandle_
);
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)}
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.
100 * @param transactionLockTimeout the default wait timeout in milliseconds
102 * @return this TransactionDBOptions instance
104 public TransactionDBOptions
setTransactionLockTimeout(
105 final long transactionLockTimeout
) {
106 assert(isOwningHandle());
107 setTransactionLockTimeout(nativeHandle_
, transactionLockTimeout
);
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#remove} or {@link RocksDB#write}
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
121 * @return the timeout in milliseconds when writing a key OUTSIDE of a
124 public long getDefaultLockTimeout() {
125 assert(isOwningHandle());
126 return getDefaultLockTimeout(nativeHandle_
);
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#remove} or {@link RocksDB#write}
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
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
147 * @param defaultLockTimeout the timeout in milliseconds when writing a key
148 * OUTSIDE of a transaction
149 * @return this TransactionDBOptions instance
151 public TransactionDBOptions
setDefaultLockTimeout(
152 final long defaultLockTimeout
) {
153 assert(isOwningHandle());
154 setDefaultLockTimeout(nativeHandle_
, defaultLockTimeout
);
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.
163 // * @param transactionDbMutexFactory the mutex factory for the transactions
165 // * @return this TransactionDBOptions instance
167 // public TransactionDBOptions setCustomMutexFactory(
168 // final TransactionDBMutexFactory transactionDbMutexFactory) {
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.
178 * @return The write policy.
180 public TxnDBWritePolicy
getWritePolicy() {
181 assert(isOwningHandle());
182 return TxnDBWritePolicy
.getTxnDBWritePolicy(getWritePolicy(nativeHandle_
));
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.
191 * @param writePolicy The write policy.
193 * @return this TransactionDBOptions instance
195 public TransactionDBOptions
setWritePolicy(
196 final TxnDBWritePolicy writePolicy
) {
197 assert(isOwningHandle());
198 setWritePolicy(nativeHandle_
, writePolicy
.getValue());
202 private native static long newTransactionDBOptions();
203 private native long getMaxNumLocks(final long handle
);
204 private native void setMaxNumLocks(final long handle
,
205 final long maxNumLocks
);
206 private native long getNumStripes(final long handle
);
207 private native void setNumStripes(final long handle
, final long numStripes
);
208 private native long getTransactionLockTimeout(final long handle
);
209 private native void setTransactionLockTimeout(final long handle
,
210 final long transactionLockTimeout
);
211 private native long getDefaultLockTimeout(final long handle
);
212 private native void setDefaultLockTimeout(final long handle
,
213 final long transactionLockTimeout
);
214 private native byte getWritePolicy(final long handle
);
215 private native void setWritePolicy(final long handle
, final byte writePolicy
);
216 @Override protected final native void disposeInternal(final long handle
);