github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/internal/query/tx/settings.go (about)

     1  package tx
     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  	Option interface {
    30  		ApplyTxSettingsOption(a *allocator.Allocator, txSettings *Ydb_Query.TransactionSettings)
    31  	}
    32  	Settings []Option
    33  )
    34  
    35  func (opts Settings) applyTxSelector(a *allocator.Allocator, txControl *Ydb_Query.TransactionControl) {
    36  	beginTx := a.QueryTransactionControlBeginTx()
    37  	beginTx.BeginTx = a.QueryTransactionSettings()
    38  	for _, opt := range opts {
    39  		if opt != nil {
    40  			opt.ApplyTxSettingsOption(a, beginTx.BeginTx)
    41  		}
    42  	}
    43  	txControl.TxSelector = beginTx
    44  }
    45  
    46  func (opts Settings) ToYDB(a *allocator.Allocator) *Ydb_Query.TransactionSettings {
    47  	txSettings := a.QueryTransactionSettings()
    48  	for _, opt := range opts {
    49  		if opt != nil {
    50  			opt.ApplyTxSettingsOption(a, txSettings)
    51  		}
    52  	}
    53  
    54  	return txSettings
    55  }
    56  
    57  // NewSettings returns transaction settings
    58  func NewSettings(opts ...Option) Settings {
    59  	return opts
    60  }
    61  
    62  func WithDefaultTxMode() Option {
    63  	return WithSerializableReadWrite()
    64  }
    65  
    66  var _ Option = serializableReadWriteTxSettingsOption{}
    67  
    68  type serializableReadWriteTxSettingsOption struct{}
    69  
    70  func (o serializableReadWriteTxSettingsOption) ApplyTxSettingsOption(
    71  	a *allocator.Allocator, txSettings *Ydb_Query.TransactionSettings,
    72  ) {
    73  	txSettings.TxMode = serializableReadWrite
    74  }
    75  
    76  func WithSerializableReadWrite() Option {
    77  	return serializableReadWriteTxSettingsOption{}
    78  }
    79  
    80  var _ Option = snapshotReadOnlyTxSettingsOption{}
    81  
    82  type snapshotReadOnlyTxSettingsOption struct{}
    83  
    84  func (snapshotReadOnlyTxSettingsOption) ApplyTxSettingsOption(
    85  	a *allocator.Allocator, settings *Ydb_Query.TransactionSettings,
    86  ) {
    87  	settings.TxMode = snapshotReadOnly
    88  }
    89  
    90  func WithSnapshotReadOnly() Option {
    91  	return snapshotReadOnlyTxSettingsOption{}
    92  }
    93  
    94  var _ Option = staleReadOnlySettingsOption{}
    95  
    96  type staleReadOnlySettingsOption struct{}
    97  
    98  func (staleReadOnlySettingsOption) ApplyTxSettingsOption(
    99  	a *allocator.Allocator, settings *Ydb_Query.TransactionSettings,
   100  ) {
   101  	settings.TxMode = staleReadOnly
   102  }
   103  
   104  func WithStaleReadOnly() Option {
   105  	return staleReadOnlySettingsOption{}
   106  }
   107  
   108  type (
   109  	onlineReadOnly       bool
   110  	OnlineReadOnlyOption interface {
   111  		applyTxOnlineReadOnlyOption(opt *onlineReadOnly)
   112  	}
   113  )
   114  
   115  var _ OnlineReadOnlyOption = inconsistentReadsTxOnlineReadOnlyOption{}
   116  
   117  type inconsistentReadsTxOnlineReadOnlyOption struct{}
   118  
   119  func (i inconsistentReadsTxOnlineReadOnlyOption) applyTxOnlineReadOnlyOption(b *onlineReadOnly) {
   120  	*b = true
   121  }
   122  
   123  func WithInconsistentReads() OnlineReadOnlyOption {
   124  	return inconsistentReadsTxOnlineReadOnlyOption{}
   125  }
   126  
   127  var _ Option = onlineReadOnlySettingsOption{}
   128  
   129  type onlineReadOnlySettingsOption []OnlineReadOnlyOption
   130  
   131  func (opts onlineReadOnlySettingsOption) ApplyTxSettingsOption(
   132  	a *allocator.Allocator, settings *Ydb_Query.TransactionSettings,
   133  ) {
   134  	var ro onlineReadOnly
   135  	for _, opt := range opts {
   136  		if opt != nil {
   137  			opt.applyTxOnlineReadOnlyOption(&ro)
   138  		}
   139  	}
   140  	if ro {
   141  		settings.TxMode = onlineReadOnlyAllowInconsistentReads
   142  	} else {
   143  		settings.TxMode = onlineReadOnlyForbidInconsistentReads
   144  	}
   145  }
   146  
   147  func WithOnlineReadOnly(opts ...OnlineReadOnlyOption) onlineReadOnlySettingsOption {
   148  	return opts
   149  }