2222import com .scalar .db .api .Upsert ;
2323import com .scalar .db .common .AbstractDistributedTransactionManager ;
2424import com .scalar .db .common .AbstractTransactionManagerCrudOperableScanner ;
25+ import com .scalar .db .common .ReadOnlyDistributedTransaction ;
2526import com .scalar .db .config .DatabaseConfig ;
2627import com .scalar .db .exception .transaction .CommitConflictException ;
2728import com .scalar .db .exception .transaction .CrudConflictException ;
@@ -137,22 +138,24 @@ private CommitHandler createCommitHandler() {
137138
138139 @ Override
139140 public DistributedTransaction begin () {
140- return begin (config .getIsolation ());
141+ String txId = UUID .randomUUID ().toString ();
142+ return begin (txId );
141143 }
142144
143145 @ Override
144146 public DistributedTransaction begin (String txId ) {
145- return begin (txId , config .getIsolation ());
147+ return begin (txId , config .getIsolation (), false );
146148 }
147149
148150 @ Override
149151 public DistributedTransaction beginReadOnly () {
150- throw new UnsupportedOperationException ("implement later" );
152+ String txId = UUID .randomUUID ().toString ();
153+ return beginReadOnly (txId );
151154 }
152155
153156 @ Override
154157 public DistributedTransaction beginReadOnly (String txId ) {
155- throw new UnsupportedOperationException ( "implement later" );
158+ return begin ( txId , config . getIsolation (), true );
156159 }
157160
158161 /** @deprecated As of release 2.4.0. Will be removed in release 4.0.0. */
@@ -166,7 +169,7 @@ public DistributedTransaction start(com.scalar.db.api.Isolation isolation) {
166169 @ Deprecated
167170 @ Override
168171 public DistributedTransaction start (String txId , com .scalar .db .api .Isolation isolation ) {
169- return begin (txId , Isolation .valueOf (isolation .name ()));
172+ return begin (txId , Isolation .valueOf (isolation .name ()), false );
170173 }
171174
172175 /** @deprecated As of release 2.4.0. Will be removed in release 4.0.0. */
@@ -189,7 +192,7 @@ public DistributedTransaction start(com.scalar.db.api.SerializableStrategy strat
189192 @ Override
190193 public DistributedTransaction start (
191194 String txId , com .scalar .db .api .SerializableStrategy strategy ) {
192- return begin (txId , Isolation .SERIALIZABLE );
195+ return begin (txId , Isolation .SERIALIZABLE , false );
193196 }
194197
195198 /** @deprecated As of release 2.4.0. Will be removed in release 4.0.0. */
@@ -199,17 +202,23 @@ public DistributedTransaction start(
199202 String txId ,
200203 com .scalar .db .api .Isolation isolation ,
201204 com .scalar .db .api .SerializableStrategy strategy ) {
202- return begin (txId , Isolation .valueOf (isolation .name ()));
205+ return begin (txId , Isolation .valueOf (isolation .name ()), false );
203206 }
204207
205208 @ VisibleForTesting
206209 DistributedTransaction begin (Isolation isolation ) {
207210 String txId = UUID .randomUUID ().toString ();
208- return begin (txId , isolation );
211+ return begin (txId , isolation , false );
212+ }
213+
214+ @ VisibleForTesting
215+ DistributedTransaction beginReadOnly (Isolation isolation ) {
216+ String txId = UUID .randomUUID ().toString ();
217+ return begin (txId , isolation , true );
209218 }
210219
211220 @ VisibleForTesting
212- DistributedTransaction begin (String txId , Isolation isolation ) {
221+ DistributedTransaction begin (String txId , Isolation isolation , boolean readOnly ) {
213222 checkArgument (!Strings .isNullOrEmpty (txId ));
214223 checkNotNull (isolation );
215224 if (isGroupCommitEnabled ()) {
@@ -224,27 +233,35 @@ DistributedTransaction begin(String txId, Isolation isolation) {
224233 Snapshot snapshot = new Snapshot (txId , isolation , tableMetadataManager , parallelExecutor );
225234 CrudHandler crud =
226235 new CrudHandler (
227- storage , snapshot , tableMetadataManager , isIncludeMetadataEnabled , parallelExecutor );
228- ConsensusCommit consensus =
236+ storage ,
237+ snapshot ,
238+ tableMetadataManager ,
239+ isIncludeMetadataEnabled ,
240+ parallelExecutor ,
241+ readOnly );
242+ DistributedTransaction transaction =
229243 new ConsensusCommit (crud , commit , recovery , mutationOperationChecker , groupCommitter );
230- getNamespace ().ifPresent (consensus ::withNamespace );
231- getTable ().ifPresent (consensus ::withTable );
232- return consensus ;
244+ if (readOnly ) {
245+ transaction = new ReadOnlyDistributedTransaction (transaction );
246+ }
247+ getNamespace ().ifPresent (transaction ::withNamespace );
248+ getTable ().ifPresent (transaction ::withTable );
249+ return transaction ;
233250 }
234251
235252 @ Override
236253 public Optional <Result > get (Get get ) throws CrudException , UnknownTransactionStatusException {
237- return executeTransaction (t -> t .get (copyAndSetTargetToIfNot (get )));
254+ return executeTransaction (t -> t .get (copyAndSetTargetToIfNot (get )), true );
238255 }
239256
240257 @ Override
241258 public List <Result > scan (Scan scan ) throws CrudException , UnknownTransactionStatusException {
242- return executeTransaction (t -> t .scan (copyAndSetTargetToIfNot (scan )));
259+ return executeTransaction (t -> t .scan (copyAndSetTargetToIfNot (scan )), true );
243260 }
244261
245262 @ Override
246263 public Scanner getScanner (Scan scan ) throws CrudException {
247- DistributedTransaction transaction = begin ();
264+ DistributedTransaction transaction = beginReadOnly ();
248265
249266 TransactionCrudOperable .Scanner scanner ;
250267 try {
@@ -331,7 +348,8 @@ public void put(Put put) throws CrudException, UnknownTransactionStatusException
331348 t -> {
332349 t .put (copyAndSetTargetToIfNot (put ));
333350 return null ;
334- });
351+ },
352+ false );
335353 }
336354
337355 /** @deprecated As of release 3.13.0. Will be removed in release 5.0.0. */
@@ -342,7 +360,8 @@ public void put(List<Put> puts) throws CrudException, UnknownTransactionStatusEx
342360 t -> {
343361 t .put (copyAndSetTargetToIfNot (puts ));
344362 return null ;
345- });
363+ },
364+ false );
346365 }
347366
348367 @ Override
@@ -351,7 +370,8 @@ public void insert(Insert insert) throws CrudException, UnknownTransactionStatus
351370 t -> {
352371 t .insert (copyAndSetTargetToIfNot (insert ));
353372 return null ;
354- });
373+ },
374+ false );
355375 }
356376
357377 @ Override
@@ -360,7 +380,8 @@ public void upsert(Upsert upsert) throws CrudException, UnknownTransactionStatus
360380 t -> {
361381 t .upsert (copyAndSetTargetToIfNot (upsert ));
362382 return null ;
363- });
383+ },
384+ false );
364385 }
365386
366387 @ Override
@@ -369,7 +390,8 @@ public void update(Update update) throws CrudException, UnknownTransactionStatus
369390 t -> {
370391 t .update (copyAndSetTargetToIfNot (update ));
371392 return null ;
372- });
393+ },
394+ false );
373395 }
374396
375397 @ Override
@@ -378,7 +400,8 @@ public void delete(Delete delete) throws CrudException, UnknownTransactionStatus
378400 t -> {
379401 t .delete (copyAndSetTargetToIfNot (delete ));
380402 return null ;
381- });
403+ },
404+ false );
382405 }
383406
384407 /** @deprecated As of release 3.13.0. Will be removed in release 5.0.0. */
@@ -389,7 +412,8 @@ public void delete(List<Delete> deletes) throws CrudException, UnknownTransactio
389412 t -> {
390413 t .delete (copyAndSetTargetToIfNot (deletes ));
391414 return null ;
392- });
415+ },
416+ false );
393417 }
394418
395419 @ Override
@@ -399,13 +423,21 @@ public void mutate(List<? extends Mutation> mutations)
399423 t -> {
400424 t .mutate (copyAndSetTargetToIfNot (mutations ));
401425 return null ;
402- });
426+ },
427+ false );
403428 }
404429
405430 private <R > R executeTransaction (
406- ThrowableFunction <DistributedTransaction , R , TransactionException > throwableFunction )
431+ ThrowableFunction <DistributedTransaction , R , TransactionException > throwableFunction ,
432+ boolean readOnly )
407433 throws CrudException , UnknownTransactionStatusException {
408- DistributedTransaction transaction = begin ();
434+ DistributedTransaction transaction ;
435+ if (readOnly ) {
436+ transaction = beginReadOnly ();
437+ } else {
438+ transaction = begin ();
439+ }
440+
409441 try {
410442 R result = throwableFunction .apply (transaction );
411443 transaction .commit ();
0 commit comments