github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/query/transaction_settings.go (about)

     1  package query
     2  
     3  import (
     4  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Query"
     5  
     6  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
     7  )
     8  
     9  var (
    10  	serializableReadWrite = &Ydb_Query.TransactionSettings_SerializableReadWrite{
    11  		SerializableReadWrite: &Ydb_Query.SerializableModeSettings{},
    12  	}
    13  	staleReadOnly = &Ydb_Query.TransactionSettings_StaleReadOnly{
    14  		StaleReadOnly: &Ydb_Query.StaleModeSettings{},
    15  	}
    16  	snapshotReadOnly = &Ydb_Query.TransactionSettings_SnapshotReadOnly{
    17  		SnapshotReadOnly: &Ydb_Query.SnapshotModeSettings{},
    18  	}
    19  	onlineReadOnlyAllowInconsistentReads = &Ydb_Query.TransactionSettings_OnlineReadOnly{
    20  		OnlineReadOnly: &Ydb_Query.OnlineModeSettings{AllowInconsistentReads: true},
    21  	}
    22  	onlineReadOnlyForbidInconsistentReads = &Ydb_Query.TransactionSettings_OnlineReadOnly{
    23  		OnlineReadOnly: &Ydb_Query.OnlineModeSettings{AllowInconsistentReads: false},
    24  	}
    25  )
    26  
    27  // Transaction settings options
    28  type (
    29  	txSettingsOption interface {
    30  		applyTxSettingsOption(a *allocator.Allocator, txSettings *Ydb_Query.TransactionSettings)
    31  	}
    32  	TransactionSettings []txSettingsOption
    33  )
    34  
    35  func (opts TransactionSettings) applyTxSelector(a *allocator.Allocator, txControl *Ydb_Query.TransactionControl) {
    36  	beginTx := a.QueryTransactionControlBeginTx()
    37  	beginTx.BeginTx = a.QueryTransactionSettings()
    38  	for _, opt := range opts {
    39  		opt.applyTxSettingsOption(a, beginTx.BeginTx)
    40  	}
    41  	txControl.TxSelector = beginTx
    42  }
    43  
    44  func (opts TransactionSettings) ToYDB(a *allocator.Allocator) *Ydb_Query.TransactionSettings {
    45  	txSettings := a.QueryTransactionSettings()
    46  	for _, opt := range opts {
    47  		opt.applyTxSettingsOption(a, txSettings)
    48  	}
    49  
    50  	return txSettings
    51  }
    52  
    53  // TxSettings returns transaction settings
    54  func TxSettings(opts ...txSettingsOption) TransactionSettings {
    55  	return opts
    56  }
    57  
    58  func WithDefaultTxMode() txSettingsOption {
    59  	return WithSerializableReadWrite()
    60  }
    61  
    62  var _ txSettingsOption = serializableReadWriteTxSettingsOption{}
    63  
    64  type serializableReadWriteTxSettingsOption struct{}
    65  
    66  func (o serializableReadWriteTxSettingsOption) applyTxSettingsOption(
    67  	a *allocator.Allocator, txSettings *Ydb_Query.TransactionSettings,
    68  ) {
    69  	txSettings.TxMode = serializableReadWrite
    70  }
    71  
    72  func WithSerializableReadWrite() txSettingsOption {
    73  	return serializableReadWriteTxSettingsOption{}
    74  }
    75  
    76  var _ txSettingsOption = snapshotReadOnlyTxSettingsOption{}
    77  
    78  type snapshotReadOnlyTxSettingsOption struct{}
    79  
    80  func (snapshotReadOnlyTxSettingsOption) applyTxSettingsOption(
    81  	a *allocator.Allocator, settings *Ydb_Query.TransactionSettings,
    82  ) {
    83  	settings.TxMode = snapshotReadOnly
    84  }
    85  
    86  func WithSnapshotReadOnly() txSettingsOption {
    87  	return snapshotReadOnlyTxSettingsOption{}
    88  }
    89  
    90  var _ txSettingsOption = staleReadOnlySettingsOption{}
    91  
    92  type staleReadOnlySettingsOption struct{}
    93  
    94  func (staleReadOnlySettingsOption) applyTxSettingsOption(
    95  	a *allocator.Allocator, settings *Ydb_Query.TransactionSettings,
    96  ) {
    97  	settings.TxMode = staleReadOnly
    98  }
    99  
   100  func WithStaleReadOnly() txSettingsOption {
   101  	return staleReadOnlySettingsOption{}
   102  }
   103  
   104  type (
   105  	txOnlineReadOnly       bool
   106  	TxOnlineReadOnlyOption interface {
   107  		applyTxOnlineReadOnlyOption(opt *txOnlineReadOnly)
   108  	}
   109  )
   110  
   111  var _ TxOnlineReadOnlyOption = inconsistentReadsTxOnlineReadOnlyOption{}
   112  
   113  type inconsistentReadsTxOnlineReadOnlyOption struct{}
   114  
   115  func (i inconsistentReadsTxOnlineReadOnlyOption) applyTxOnlineReadOnlyOption(b *txOnlineReadOnly) {
   116  	*b = true
   117  }
   118  
   119  func WithInconsistentReads() TxOnlineReadOnlyOption {
   120  	return inconsistentReadsTxOnlineReadOnlyOption{}
   121  }
   122  
   123  var _ txSettingsOption = onlineReadOnlySettingsOption{}
   124  
   125  type onlineReadOnlySettingsOption []TxOnlineReadOnlyOption
   126  
   127  func (opts onlineReadOnlySettingsOption) applyTxSettingsOption(
   128  	a *allocator.Allocator, settings *Ydb_Query.TransactionSettings,
   129  ) {
   130  	var ro txOnlineReadOnly
   131  	for _, opt := range opts {
   132  		if opt != nil {
   133  			opt.applyTxOnlineReadOnlyOption(&ro)
   134  		}
   135  	}
   136  	if ro {
   137  		settings.TxMode = onlineReadOnlyAllowInconsistentReads
   138  	} else {
   139  		settings.TxMode = onlineReadOnlyForbidInconsistentReads
   140  	}
   141  }
   142  
   143  func WithOnlineReadOnly(opts ...TxOnlineReadOnlyOption) onlineReadOnlySettingsOption {
   144  	return opts
   145  }