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 TransactionOptions extends RocksObject
9     implements TransactionalOptions<TransactionOptions> {
10 
TransactionOptions()11   public TransactionOptions() {
12     super(newTransactionOptions());
13   }
14 
15   @Override
isSetSnapshot()16   public boolean isSetSnapshot() {
17     assert(isOwningHandle());
18     return isSetSnapshot(nativeHandle_);
19   }
20 
21   @Override
setSetSnapshot(final boolean setSnapshot)22   public TransactionOptions setSetSnapshot(final boolean setSnapshot) {
23     assert(isOwningHandle());
24     setSetSnapshot(nativeHandle_, setSnapshot);
25     return this;
26   }
27 
28   /**
29    * True means that before acquiring locks, this transaction will
30    * check if doing so will cause a deadlock. If so, it will return with
31    * {@link Status.Code#Busy}. The user should retry their transaction.
32    *
33    * @return true if a deadlock is detected.
34    */
isDeadlockDetect()35   public boolean isDeadlockDetect() {
36     assert(isOwningHandle());
37     return isDeadlockDetect(nativeHandle_);
38   }
39 
40   /**
41    * Setting to true means that before acquiring locks, this transaction will
42    * check if doing so will cause a deadlock. If so, it will return with
43    * {@link Status.Code#Busy}. The user should retry their transaction.
44    *
45    * @param deadlockDetect true if we should detect deadlocks.
46    *
47    * @return this TransactionOptions instance
48    */
setDeadlockDetect(final boolean deadlockDetect)49   public TransactionOptions setDeadlockDetect(final boolean deadlockDetect) {
50     assert(isOwningHandle());
51     setDeadlockDetect(nativeHandle_, deadlockDetect);
52     return this;
53   }
54 
55   /**
56    * The wait timeout in milliseconds when a transaction attempts to lock a key.
57    *
58    * If 0, no waiting is done if a lock cannot instantly be acquired.
59    * If negative, {@link TransactionDBOptions#getTransactionLockTimeout(long)}
60    * will be used
61    *
62    * @return the lock timeout in milliseconds
63    */
getLockTimeout()64   public long getLockTimeout() {
65     assert(isOwningHandle());
66     return getLockTimeout(nativeHandle_);
67   }
68 
69   /**
70    * If positive, specifies the wait timeout in milliseconds when
71    * a transaction attempts to lock a key.
72    *
73    * If 0, no waiting is done if a lock cannot instantly be acquired.
74    * If negative, {@link TransactionDBOptions#getTransactionLockTimeout(long)}
75    * will be used
76    *
77    * Default: -1
78    *
79    * @param lockTimeout the lock timeout in milliseconds
80    *
81    * @return this TransactionOptions instance
82    */
setLockTimeout(final long lockTimeout)83   public TransactionOptions setLockTimeout(final long lockTimeout) {
84     assert(isOwningHandle());
85     setLockTimeout(nativeHandle_, lockTimeout);
86     return this;
87   }
88 
89   /**
90    * Expiration duration in milliseconds.
91    *
92    * If non-negative, transactions that last longer than this many milliseconds
93    * will fail to commit. If not set, a forgotten transaction that is never
94    * committed, rolled back, or deleted will never relinquish any locks it
95    * holds. This could prevent keys from being written by other writers.
96    *
97    * @return expiration the expiration duration in milliseconds
98    */
getExpiration()99   public long getExpiration() {
100     assert(isOwningHandle());
101     return getExpiration(nativeHandle_);
102   }
103 
104   /**
105    * Expiration duration in milliseconds.
106    *
107    * If non-negative, transactions that last longer than this many milliseconds
108    * will fail to commit. If not set, a forgotten transaction that is never
109    * committed, rolled back, or deleted will never relinquish any locks it
110    * holds. This could prevent keys from being written by other writers.
111    *
112    * Default: -1
113    *
114    * @param expiration the expiration duration in milliseconds
115    *
116    * @return this TransactionOptions instance
117    */
setExpiration(final long expiration)118   public TransactionOptions setExpiration(final long expiration) {
119     assert(isOwningHandle());
120     setExpiration(nativeHandle_, expiration);
121     return this;
122   }
123 
124   /**
125    * Gets the number of traversals to make during deadlock detection.
126    *
127    * @return the number of traversals to make during
128    *     deadlock detection
129    */
getDeadlockDetectDepth()130   public long getDeadlockDetectDepth() {
131     return getDeadlockDetectDepth(nativeHandle_);
132   }
133 
134   /**
135    * Sets the number of traversals to make during deadlock detection.
136    *
137    * Default: 50
138    *
139    * @param deadlockDetectDepth the number of traversals to make during
140    *     deadlock detection
141    *
142    * @return this TransactionOptions instance
143    */
setDeadlockDetectDepth( final long deadlockDetectDepth)144   public TransactionOptions setDeadlockDetectDepth(
145       final long deadlockDetectDepth) {
146     setDeadlockDetectDepth(nativeHandle_, deadlockDetectDepth);
147     return this;
148   }
149 
150   /**
151    * Get the maximum number of bytes that may be used for the write batch.
152    *
153    * @return the maximum number of bytes, 0 means no limit.
154    */
getMaxWriteBatchSize()155   public long getMaxWriteBatchSize() {
156     return getMaxWriteBatchSize(nativeHandle_);
157   }
158 
159   /**
160    * Set the maximum number of bytes that may be used for the write batch.
161    *
162    * @param maxWriteBatchSize the maximum number of bytes, 0 means no limit.
163    *
164    * @return this TransactionOptions instance
165    */
setMaxWriteBatchSize(final long maxWriteBatchSize)166   public TransactionOptions setMaxWriteBatchSize(final long maxWriteBatchSize) {
167     setMaxWriteBatchSize(nativeHandle_, maxWriteBatchSize);
168     return this;
169   }
170 
newTransactionOptions()171   private native static long newTransactionOptions();
isSetSnapshot(final long handle)172   private native boolean isSetSnapshot(final long handle);
setSetSnapshot(final long handle, final boolean setSnapshot)173   private native void setSetSnapshot(final long handle,
174       final boolean setSnapshot);
isDeadlockDetect(final long handle)175   private native boolean isDeadlockDetect(final long handle);
setDeadlockDetect(final long handle, final boolean deadlockDetect)176   private native void setDeadlockDetect(final long handle,
177       final boolean deadlockDetect);
getLockTimeout(final long handle)178   private native long getLockTimeout(final long handle);
setLockTimeout(final long handle, final long lockTimeout)179   private native void setLockTimeout(final long handle, final long lockTimeout);
getExpiration(final long handle)180   private native long getExpiration(final long handle);
setExpiration(final long handle, final long expiration)181   private native void setExpiration(final long handle, final long expiration);
getDeadlockDetectDepth(final long handle)182   private native long getDeadlockDetectDepth(final long handle);
setDeadlockDetectDepth(final long handle, final long deadlockDetectDepth)183   private native void setDeadlockDetectDepth(final long handle,
184       final long deadlockDetectDepth);
getMaxWriteBatchSize(final long handle)185   private native long getMaxWriteBatchSize(final long handle);
setMaxWriteBatchSize(final long handle, final long maxWriteBatchSize)186   private native void setMaxWriteBatchSize(final long handle,
187       final long maxWriteBatchSize);
disposeInternal(final long handle)188   @Override protected final native void disposeInternal(final long handle);
189 }
190