Guest User

Untitled

a guest
Nov 6th, 2026
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. package tech.ydb.yoj.repository.db.internal;
  2.  
  3. import lombok.AccessLevel;
  4. import lombok.NoArgsConstructor;
  5. import lombok.NonNull;
  6. import lombok.RequiredArgsConstructor;
  7. import tech.ydb.yoj.ExperimentalApi;
  8. import tech.ydb.yoj.InternalApi;
  9. import tech.ydb.yoj.repository.db.IsolationLevel;
  10. import tech.ydb.yoj.repository.db.RepositoryTransaction;
  11. import tech.ydb.yoj.repository.db.bulk.BulkParams;
  12. import tech.ydb.yoj.repository.db.readtable.ReadTableParams;
  13.  
  14. import javax.annotation.Nullable;
  15.  
  16. /**
  17.  * Describes a single request to the database performed by a {@link RepositoryTransaction},
  18.  * typically in a transaction <em>but not always</em>.
  19.  * <p>For ease of use, requests that don't take any arguments are available as constants:
  20.  * <ul>
  21.  * <li>{@link Commit#INSTANCE Commit.INSTANCE - commit()}</li>
  22.  * <li>{@link Rollback#INSTANCE Rollback.INSTANCE - rollback()}</li>
  23.  * <li>{@link GetSession#INSTANCE GetSession.INSTANCE - getSession()}</li>
  24.  * </ul>
  25.  */
  26. @InternalApi
  27. @ExperimentalApi(issue = "https://github.com/ydb-platform/yoj-project/issues/165")
  28. public sealed interface DbRequest permits
  29.         DbRequest.Commit,
  30.         DbRequest.Rollback,
  31.         DbRequest.GetSession,
  32.         DbRequest.DataQuery,
  33.         DbRequest.ScanQuery,
  34.         DbRequest.ReadTable,
  35.         DbRequest.BulkUpsert
  36. {
  37.     /**
  38.      * Returns a human-readable representation of the database request.
  39.      */
  40.     String toLogString();
  41.  
  42.     /**
  43.      * Commit the transaction.
  44.      */
  45.     @NoArgsConstructor(access = AccessLevel.PRIVATE)
  46.     final class Commit implements DbRequest {
  47.         public static final Commit INSTANCE = new Commit();
  48.  
  49.         @Override
  50.         public String toLogString() {
  51.             return ".commit()";
  52.         }
  53.     }
  54.  
  55.     /**
  56.      * Rollback the transaction.
  57.      */
  58.     @NoArgsConstructor(access = AccessLevel.PRIVATE)
  59.     final class Rollback implements DbRequest {
  60.         public static final Rollback INSTANCE = new Rollback();
  61.  
  62.         @Override
  63.         public String toLogString() {
  64.             return ".rollback()";
  65.         }
  66.     }
  67.  
  68.     /**
  69.      * Obtain a database session (connection), most likely from a pool.
  70.      * Not all {@link RepositoryTransaction} implementations use this request type.
  71.      */
  72.     @NoArgsConstructor(access = AccessLevel.PRIVATE)
  73.     final class GetSession implements DbRequest {
  74.         public static final GetSession INSTANCE = new GetSession();
  75.  
  76.         @Override
  77.         public String toLogString() {
  78.             return ".getSession()";
  79.         }
  80.     }
  81.  
  82.     /**
  83.      * Execute a single statement in ordinary query mode. Consistency and allowed operations (reads and writes,
  84.      * or only reads) depend on the transaction's isolation level.
  85.      */
  86.     @RequiredArgsConstructor
  87.     final class DataQuery implements DbRequest {
  88.         @Nullable private final String log;
  89.  
  90.         @Override
  91.         public String toLogString() {
  92.             return ".dataQuery()" + log;
  93.         }
  94.     }
  95.  
  96.     /**
  97.      * Execute a single statement in scan query mode. Not quite a transactional action, but users may consider
  98.      * scan query isolation to be that of the {@link IsolationLevel#SNAPSHOT_READ_ONLY} isolation level.
  99.      */
  100.     @RequiredArgsConstructor
  101.     final class ScanQuery implements DbRequest {
  102.         @Nullable private final String log;
  103.  
  104.         @Override
  105.         public String toLogString() {
  106.             return ".scanQuery()" + log;
  107.         }
  108.     }
  109.  
  110.     /**
  111.      * Stream the whole table or a range of it (non-transactional).
  112.      */
  113.     @RequiredArgsConstructor
  114.     final class ReadTable implements DbRequest {
  115.         @NonNull private final String tableName;
  116.         @NonNull private final ReadTableParams<?> params;
  117.  
  118.         @Override
  119.         public String toLogString() {
  120.             return ".readTable()" + tableName + ", " + params;
  121.         }
  122.     }
  123.  
  124.     /**
  125.      * Bulk upsert data (non-transactional).
  126.      */
  127.     @RequiredArgsConstructor
  128.     final class BulkUpsert implements DbRequest {
  129.         @NonNull private final String tableName;
  130.         private final long size;
  131.         @NonNull private final BulkParams bulkParams;
  132.  
  133.         @Override
  134.         public String toLogString() {
  135.             return ".bulkUpsert()" + tableName + ", [" + size + "], " + bulkParams;
  136.         }
  137.     }
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment