github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/table/options/options.go (about)

     1  package options
     2  
     3  import (
     4  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
     5  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Table"
     6  	"google.golang.org/grpc"
     7  
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
    10  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
    11  )
    12  
    13  func WithShardKeyBounds() DescribeTableOption {
    14  	return func(d *DescribeTableDesc) {
    15  		d.IncludeShardKeyBounds = true
    16  	}
    17  }
    18  
    19  func WithTableStats() DescribeTableOption {
    20  	return func(d *DescribeTableDesc) {
    21  		d.IncludeTableStats = true
    22  	}
    23  }
    24  
    25  func WithPartitionStats() DescribeTableOption {
    26  	return func(d *DescribeTableDesc) {
    27  		d.IncludePartitionStats = true
    28  	}
    29  }
    30  
    31  type (
    32  	CreateTableDesc   Ydb_Table.CreateTableRequest
    33  	CreateTableOption interface {
    34  		ApplyCreateTableOption(d *CreateTableDesc, a *allocator.Allocator)
    35  	}
    36  )
    37  
    38  type (
    39  	profile       Ydb_Table.TableProfile
    40  	ProfileOption interface {
    41  		ApplyProfileOption(p *profile, a *allocator.Allocator)
    42  	}
    43  )
    44  
    45  type (
    46  	storagePolicy      Ydb_Table.StoragePolicy
    47  	compactionPolicy   Ydb_Table.CompactionPolicy
    48  	partitioningPolicy Ydb_Table.PartitioningPolicy
    49  	executionPolicy    Ydb_Table.ExecutionPolicy
    50  	replicationPolicy  Ydb_Table.ReplicationPolicy
    51  	cachingPolicy      Ydb_Table.CachingPolicy
    52  )
    53  
    54  type column struct {
    55  	name string
    56  	typ  types.Type
    57  }
    58  
    59  func (c column) ApplyAlterTableOption(d *AlterTableDesc, a *allocator.Allocator) {
    60  	d.AddColumns = append(d.AddColumns, &Ydb_Table.ColumnMeta{
    61  		Name: c.name,
    62  		Type: types.TypeToYDB(c.typ, a),
    63  	})
    64  }
    65  
    66  func (c column) ApplyCreateTableOption(d *CreateTableDesc, a *allocator.Allocator) {
    67  	d.Columns = append(d.Columns, &Ydb_Table.ColumnMeta{
    68  		Name: c.name,
    69  		Type: types.TypeToYDB(c.typ, a),
    70  	})
    71  }
    72  
    73  func WithColumn(name string, typ types.Type) CreateTableOption {
    74  	return column{
    75  		name: name,
    76  		typ:  typ,
    77  	}
    78  }
    79  
    80  type columnMeta Column
    81  
    82  func (c columnMeta) ApplyAlterTableOption(d *AlterTableDesc, a *allocator.Allocator) {
    83  	d.AddColumns = append(d.AddColumns, Column(c).toYDB(a))
    84  }
    85  
    86  func (c columnMeta) ApplyCreateTableOption(d *CreateTableDesc, a *allocator.Allocator) {
    87  	d.Columns = append(d.Columns, Column(c).toYDB(a))
    88  }
    89  
    90  func WithColumnMeta(column Column) CreateTableOption {
    91  	return columnMeta(column)
    92  }
    93  
    94  type primaryKeyColumn []string
    95  
    96  func (columns primaryKeyColumn) ApplyCreateTableOption(d *CreateTableDesc, a *allocator.Allocator) {
    97  	d.PrimaryKey = append(d.PrimaryKey, columns...)
    98  }
    99  
   100  func WithPrimaryKeyColumn(columns ...string) CreateTableOption {
   101  	return primaryKeyColumn(columns)
   102  }
   103  
   104  type timeToLiveSettings TimeToLiveSettings
   105  
   106  func (settings timeToLiveSettings) ApplyAlterTableOption(d *AlterTableDesc, a *allocator.Allocator) {
   107  	d.TtlAction = &Ydb_Table.AlterTableRequest_SetTtlSettings{
   108  		SetTtlSettings: (*TimeToLiveSettings)(&settings).ToYDB(),
   109  	}
   110  }
   111  
   112  func (settings timeToLiveSettings) ApplyCreateTableOption(d *CreateTableDesc, a *allocator.Allocator) {
   113  	d.TtlSettings = (*TimeToLiveSettings)(&settings).ToYDB()
   114  }
   115  
   116  // WithTimeToLiveSettings defines TTL settings in CreateTable request
   117  func WithTimeToLiveSettings(settings TimeToLiveSettings) CreateTableOption {
   118  	return timeToLiveSettings(settings)
   119  }
   120  
   121  type attribute struct {
   122  	key   string
   123  	value string
   124  }
   125  
   126  func (a attribute) ApplyAlterTableOption(d *AlterTableDesc, _ *allocator.Allocator) {
   127  	if d.AlterAttributes == nil {
   128  		d.AlterAttributes = make(map[string]string)
   129  	}
   130  	d.AlterAttributes[a.key] = a.value
   131  }
   132  
   133  func (a attribute) ApplyCreateTableOption(d *CreateTableDesc, _ *allocator.Allocator) {
   134  	if d.Attributes == nil {
   135  		d.Attributes = make(map[string]string)
   136  	}
   137  	d.Attributes[a.key] = a.value
   138  }
   139  
   140  func WithAttribute(key, value string) CreateTableOption {
   141  	return attribute{
   142  		key:   key,
   143  		value: value,
   144  	}
   145  }
   146  
   147  type (
   148  	indexDesc   Ydb_Table.TableIndex
   149  	IndexOption interface {
   150  		ApplyIndexOption(d *indexDesc)
   151  	}
   152  )
   153  
   154  type index struct {
   155  	name string
   156  	opts []IndexOption
   157  }
   158  
   159  func (i index) ApplyAlterTableOption(d *AlterTableDesc, a *allocator.Allocator) {
   160  	x := &Ydb_Table.TableIndex{
   161  		Name: i.name,
   162  	}
   163  	for _, opt := range i.opts {
   164  		if opt != nil {
   165  			opt.ApplyIndexOption((*indexDesc)(x))
   166  		}
   167  	}
   168  	d.AddIndexes = append(d.AddIndexes, x)
   169  }
   170  
   171  func (i index) ApplyCreateTableOption(d *CreateTableDesc, a *allocator.Allocator) {
   172  	x := &Ydb_Table.TableIndex{
   173  		Name: i.name,
   174  	}
   175  	for _, opt := range i.opts {
   176  		if opt != nil {
   177  			opt.ApplyIndexOption((*indexDesc)(x))
   178  		}
   179  	}
   180  	d.Indexes = append(d.Indexes, x)
   181  }
   182  
   183  func WithIndex(name string, opts ...IndexOption) CreateTableOption {
   184  	return index{
   185  		name: name,
   186  		opts: opts,
   187  	}
   188  }
   189  
   190  func WithAddIndex(name string, opts ...IndexOption) AlterTableOption {
   191  	return index{
   192  		name: name,
   193  		opts: opts,
   194  	}
   195  }
   196  
   197  type dropIndex string
   198  
   199  func (i dropIndex) ApplyAlterTableOption(d *AlterTableDesc, a *allocator.Allocator) {
   200  	d.DropIndexes = append(d.DropIndexes, string(i))
   201  }
   202  
   203  func WithDropIndex(name string) AlterTableOption {
   204  	return dropIndex(name)
   205  }
   206  
   207  type indexColumns []string
   208  
   209  func (columns indexColumns) ApplyIndexOption(d *indexDesc) {
   210  	d.IndexColumns = append(d.IndexColumns, columns...)
   211  }
   212  
   213  func WithIndexColumns(columns ...string) IndexOption {
   214  	return indexColumns(columns)
   215  }
   216  
   217  type dataColumns []string
   218  
   219  func (columns dataColumns) ApplyIndexOption(d *indexDesc) {
   220  	d.DataColumns = append(d.DataColumns, columns...)
   221  }
   222  
   223  func WithDataColumns(columns ...string) IndexOption {
   224  	return dataColumns(columns)
   225  }
   226  
   227  func WithIndexType(t IndexType) IndexOption {
   228  	return t
   229  }
   230  
   231  type columnFamilies []ColumnFamily
   232  
   233  func (cf columnFamilies) ApplyAlterTableOption(d *AlterTableDesc, a *allocator.Allocator) {
   234  	d.AddColumnFamilies = make([]*Ydb_Table.ColumnFamily, len(cf))
   235  	for i := range cf {
   236  		d.AddColumnFamilies[i] = cf[i].toYDB()
   237  	}
   238  }
   239  
   240  func (cf columnFamilies) ApplyCreateTableOption(d *CreateTableDesc, a *allocator.Allocator) {
   241  	d.ColumnFamilies = make([]*Ydb_Table.ColumnFamily, len(cf))
   242  	for i := range cf {
   243  		d.ColumnFamilies[i] = cf[i].toYDB()
   244  	}
   245  }
   246  
   247  func WithColumnFamilies(cf ...ColumnFamily) CreateTableOption {
   248  	return columnFamilies(cf)
   249  }
   250  
   251  type readReplicasSettings ReadReplicasSettings
   252  
   253  func (rr readReplicasSettings) ApplyAlterTableOption(d *AlterTableDesc, a *allocator.Allocator) {
   254  	d.SetReadReplicasSettings = ReadReplicasSettings(rr).ToYDB()
   255  }
   256  
   257  func (rr readReplicasSettings) ApplyCreateTableOption(d *CreateTableDesc, a *allocator.Allocator) {
   258  	d.ReadReplicasSettings = ReadReplicasSettings(rr).ToYDB()
   259  }
   260  
   261  func WithReadReplicasSettings(rr ReadReplicasSettings) CreateTableOption {
   262  	return readReplicasSettings(rr)
   263  }
   264  
   265  type storageSettings StorageSettings
   266  
   267  func (ss storageSettings) ApplyAlterTableOption(d *AlterTableDesc, a *allocator.Allocator) {
   268  	d.AlterStorageSettings = StorageSettings(ss).ToYDB()
   269  }
   270  
   271  func (ss storageSettings) ApplyCreateTableOption(d *CreateTableDesc, a *allocator.Allocator) {
   272  	d.StorageSettings = StorageSettings(ss).ToYDB()
   273  }
   274  
   275  func WithStorageSettings(ss StorageSettings) CreateTableOption {
   276  	return storageSettings(ss)
   277  }
   278  
   279  type keyBloomFilter FeatureFlag
   280  
   281  func (f keyBloomFilter) ApplyAlterTableOption(d *AlterTableDesc, a *allocator.Allocator) {
   282  	d.SetKeyBloomFilter = FeatureFlag(f).ToYDB()
   283  }
   284  
   285  func (f keyBloomFilter) ApplyCreateTableOption(d *CreateTableDesc, a *allocator.Allocator) {
   286  	d.KeyBloomFilter = FeatureFlag(f).ToYDB()
   287  }
   288  
   289  func WithKeyBloomFilter(f FeatureFlag) CreateTableOption {
   290  	return keyBloomFilter(f)
   291  }
   292  
   293  func WithPartitions(p Partitions) CreateTableOption {
   294  	return p
   295  }
   296  
   297  type uniformPartitions uint64
   298  
   299  func (u uniformPartitions) ApplyCreateTableOption(d *CreateTableDesc, a *allocator.Allocator) {
   300  	d.Partitions = &Ydb_Table.CreateTableRequest_UniformPartitions{
   301  		UniformPartitions: uint64(u),
   302  	}
   303  }
   304  
   305  func (u uniformPartitions) isPartitions() {}
   306  
   307  func WithUniformPartitions(n uint64) Partitions {
   308  	return uniformPartitions(n)
   309  }
   310  
   311  type explicitPartitions []value.Value
   312  
   313  func (e explicitPartitions) ApplyCreateTableOption(d *CreateTableDesc, a *allocator.Allocator) {
   314  	values := make([]*Ydb.TypedValue, len(e))
   315  	for i := range values {
   316  		values[i] = value.ToYDB(e[i], a)
   317  	}
   318  	d.Partitions = &Ydb_Table.CreateTableRequest_PartitionAtKeys{
   319  		PartitionAtKeys: &Ydb_Table.ExplicitPartitions{
   320  			SplitPoints: values,
   321  		},
   322  	}
   323  }
   324  
   325  func (e explicitPartitions) isPartitions() {}
   326  
   327  func WithExplicitPartitions(splitPoints ...value.Value) Partitions {
   328  	return explicitPartitions(splitPoints)
   329  }
   330  
   331  type profileOption []ProfileOption
   332  
   333  func (opts profileOption) ApplyCreateTableOption(d *CreateTableDesc, a *allocator.Allocator) {
   334  	if d.Profile == nil {
   335  		d.Profile = new(Ydb_Table.TableProfile)
   336  	}
   337  	for _, opt := range opts {
   338  		if opt != nil {
   339  			opt.ApplyProfileOption((*profile)(d.Profile), a)
   340  		}
   341  	}
   342  }
   343  
   344  func WithProfile(opts ...ProfileOption) CreateTableOption {
   345  	return profileOption(opts)
   346  }
   347  
   348  type profilePresetProfileOption string
   349  
   350  func (preset profilePresetProfileOption) ApplyProfileOption(p *profile, a *allocator.Allocator) {
   351  	p.PresetName = string(preset)
   352  }
   353  
   354  func WithProfilePreset(name string) ProfileOption {
   355  	return profilePresetProfileOption(name)
   356  }
   357  
   358  type storagePolicyProfileOption []StoragePolicyOption
   359  
   360  func (opts storagePolicyProfileOption) ApplyProfileOption(p *profile, a *allocator.Allocator) {
   361  	if p.StoragePolicy == nil {
   362  		p.StoragePolicy = new(Ydb_Table.StoragePolicy)
   363  	}
   364  	for _, opt := range opts {
   365  		if opt != nil {
   366  			opt((*storagePolicy)(p.StoragePolicy))
   367  		}
   368  	}
   369  }
   370  
   371  func WithStoragePolicy(opts ...StoragePolicyOption) ProfileOption {
   372  	return storagePolicyProfileOption(opts)
   373  }
   374  
   375  type compactionPolicyProfileOption []CompactionPolicyOption
   376  
   377  func (opts compactionPolicyProfileOption) ApplyProfileOption(p *profile, a *allocator.Allocator) {
   378  	if p.CompactionPolicy == nil {
   379  		p.CompactionPolicy = new(Ydb_Table.CompactionPolicy)
   380  	}
   381  	for _, opt := range opts {
   382  		if opt != nil {
   383  			opt((*compactionPolicy)(p.CompactionPolicy))
   384  		}
   385  	}
   386  }
   387  
   388  func WithCompactionPolicy(opts ...CompactionPolicyOption) ProfileOption {
   389  	return compactionPolicyProfileOption(opts)
   390  }
   391  
   392  type partitioningPolicyProfileOption []PartitioningPolicyOption
   393  
   394  func (opts partitioningPolicyProfileOption) ApplyProfileOption(p *profile, a *allocator.Allocator) {
   395  	if p.PartitioningPolicy == nil {
   396  		p.PartitioningPolicy = new(Ydb_Table.PartitioningPolicy)
   397  	}
   398  	for _, opt := range opts {
   399  		if opt != nil {
   400  			opt((*partitioningPolicy)(p.PartitioningPolicy), a)
   401  		}
   402  	}
   403  }
   404  
   405  func WithPartitioningPolicy(opts ...PartitioningPolicyOption) ProfileOption {
   406  	return partitioningPolicyProfileOption(opts)
   407  }
   408  
   409  type executionPolicyProfileOption []ExecutionPolicyOption
   410  
   411  func (opts executionPolicyProfileOption) ApplyProfileOption(p *profile, a *allocator.Allocator) {
   412  	if p.ExecutionPolicy == nil {
   413  		p.ExecutionPolicy = new(Ydb_Table.ExecutionPolicy)
   414  	}
   415  	for _, opt := range opts {
   416  		if opt != nil {
   417  			opt((*executionPolicy)(p.ExecutionPolicy))
   418  		}
   419  	}
   420  }
   421  
   422  func WithExecutionPolicy(opts ...ExecutionPolicyOption) ProfileOption {
   423  	return executionPolicyProfileOption(opts)
   424  }
   425  
   426  type replicationPolicyProfileOption []ReplicationPolicyOption
   427  
   428  func (opts replicationPolicyProfileOption) ApplyProfileOption(p *profile, a *allocator.Allocator) {
   429  	if p.ReplicationPolicy == nil {
   430  		p.ReplicationPolicy = new(Ydb_Table.ReplicationPolicy)
   431  	}
   432  	for _, opt := range opts {
   433  		if opt != nil {
   434  			opt((*replicationPolicy)(p.ReplicationPolicy))
   435  		}
   436  	}
   437  }
   438  
   439  func WithReplicationPolicy(opts ...ReplicationPolicyOption) ProfileOption {
   440  	return replicationPolicyProfileOption(opts)
   441  }
   442  
   443  type cachingPolicyProfileOption []CachingPolicyOption
   444  
   445  func (opts cachingPolicyProfileOption) ApplyProfileOption(p *profile, a *allocator.Allocator) {
   446  	if p.CachingPolicy == nil {
   447  		p.CachingPolicy = new(Ydb_Table.CachingPolicy)
   448  	}
   449  	for _, opt := range opts {
   450  		if opt != nil {
   451  			opt((*cachingPolicy)(p.CachingPolicy))
   452  		}
   453  	}
   454  }
   455  
   456  func WithCachingPolicy(opts ...CachingPolicyOption) ProfileOption {
   457  	return cachingPolicyProfileOption(opts)
   458  }
   459  
   460  type (
   461  	StoragePolicyOption      func(*storagePolicy)
   462  	CompactionPolicyOption   func(*compactionPolicy)
   463  	PartitioningPolicyOption func(*partitioningPolicy, *allocator.Allocator)
   464  	ExecutionPolicyOption    func(*executionPolicy)
   465  	ReplicationPolicyOption  func(*replicationPolicy)
   466  	CachingPolicyOption      func(*cachingPolicy)
   467  )
   468  
   469  func WithStoragePolicyPreset(name string) StoragePolicyOption {
   470  	return func(s *storagePolicy) {
   471  		s.PresetName = name
   472  	}
   473  }
   474  
   475  func WithStoragePolicySyslog(kind string) StoragePolicyOption {
   476  	return func(s *storagePolicy) {
   477  		s.Syslog = &Ydb_Table.StoragePool{Media: kind}
   478  	}
   479  }
   480  
   481  func WithStoragePolicyLog(kind string) StoragePolicyOption {
   482  	return func(s *storagePolicy) {
   483  		s.Log = &Ydb_Table.StoragePool{Media: kind}
   484  	}
   485  }
   486  
   487  func WithStoragePolicyData(kind string) StoragePolicyOption {
   488  	return func(s *storagePolicy) {
   489  		s.Data = &Ydb_Table.StoragePool{Media: kind}
   490  	}
   491  }
   492  
   493  func WithStoragePolicyExternal(kind string) StoragePolicyOption {
   494  	return func(s *storagePolicy) {
   495  		s.External = &Ydb_Table.StoragePool{Media: kind}
   496  	}
   497  }
   498  
   499  func WithStoragePolicyKeepInMemory(flag FeatureFlag) StoragePolicyOption {
   500  	return func(s *storagePolicy) {
   501  		s.KeepInMemory = flag.ToYDB()
   502  	}
   503  }
   504  
   505  func WithCompactionPolicyPreset(name string) CompactionPolicyOption {
   506  	return func(c *compactionPolicy) { c.PresetName = name }
   507  }
   508  
   509  func WithPartitioningPolicyPreset(name string) PartitioningPolicyOption {
   510  	return func(p *partitioningPolicy, a *allocator.Allocator) {
   511  		p.PresetName = name
   512  	}
   513  }
   514  
   515  func WithPartitioningPolicyMode(mode PartitioningMode) PartitioningPolicyOption {
   516  	return func(p *partitioningPolicy, a *allocator.Allocator) {
   517  		p.AutoPartitioning = mode.toYDB()
   518  	}
   519  }
   520  
   521  // Deprecated: use WithUniformPartitions instead.
   522  // Will be removed after Oct 2024.
   523  // Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
   524  func WithPartitioningPolicyUniformPartitions(n uint64) PartitioningPolicyOption {
   525  	return func(p *partitioningPolicy, a *allocator.Allocator) {
   526  		p.Partitions = &Ydb_Table.PartitioningPolicy_UniformPartitions{
   527  			UniformPartitions: n,
   528  		}
   529  	}
   530  }
   531  
   532  // Deprecated: use WithExplicitPartitions instead.
   533  // Will be removed after Oct 2024.
   534  // Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
   535  func WithPartitioningPolicyExplicitPartitions(splitPoints ...value.Value) PartitioningPolicyOption {
   536  	return func(p *partitioningPolicy, a *allocator.Allocator) {
   537  		values := make([]*Ydb.TypedValue, len(splitPoints))
   538  		for i := range values {
   539  			values[i] = value.ToYDB(splitPoints[i], a)
   540  		}
   541  		p.Partitions = &Ydb_Table.PartitioningPolicy_ExplicitPartitions{
   542  			ExplicitPartitions: &Ydb_Table.ExplicitPartitions{
   543  				SplitPoints: values,
   544  			},
   545  		}
   546  	}
   547  }
   548  
   549  func WithReplicationPolicyPreset(name string) ReplicationPolicyOption {
   550  	return func(e *replicationPolicy) {
   551  		e.PresetName = name
   552  	}
   553  }
   554  
   555  func WithReplicationPolicyReplicasCount(n uint32) ReplicationPolicyOption {
   556  	return func(e *replicationPolicy) {
   557  		e.ReplicasCount = n
   558  	}
   559  }
   560  
   561  func WithReplicationPolicyCreatePerAZ(flag FeatureFlag) ReplicationPolicyOption {
   562  	return func(e *replicationPolicy) {
   563  		e.CreatePerAvailabilityZone = flag.ToYDB()
   564  	}
   565  }
   566  
   567  func WithReplicationPolicyAllowPromotion(flag FeatureFlag) ReplicationPolicyOption {
   568  	return func(e *replicationPolicy) {
   569  		e.AllowPromotion = flag.ToYDB()
   570  	}
   571  }
   572  
   573  func WithExecutionPolicyPreset(name string) ExecutionPolicyOption {
   574  	return func(e *executionPolicy) {
   575  		e.PresetName = name
   576  	}
   577  }
   578  
   579  func WithCachingPolicyPreset(name string) CachingPolicyOption {
   580  	return func(e *cachingPolicy) {
   581  		e.PresetName = name
   582  	}
   583  }
   584  
   585  type partitioningSettingsObject PartitioningSettings
   586  
   587  func (ps partitioningSettingsObject) ApplyAlterTableOption(d *AlterTableDesc, a *allocator.Allocator) {
   588  	d.AlterPartitioningSettings = PartitioningSettings(ps).toYDB()
   589  }
   590  
   591  func (ps partitioningSettingsObject) ApplyCreateTableOption(d *CreateTableDesc, a *allocator.Allocator) {
   592  	d.PartitioningSettings = PartitioningSettings(ps).toYDB()
   593  }
   594  
   595  func WithPartitioningSettingsObject(ps PartitioningSettings) CreateTableOption {
   596  	return partitioningSettingsObject(ps)
   597  }
   598  
   599  type partitioningSettings []PartitioningSettingsOption
   600  
   601  func (opts partitioningSettings) ApplyCreateTableOption(d *CreateTableDesc, a *allocator.Allocator) {
   602  	settings := &ydbPartitioningSettings{}
   603  	for _, opt := range opts {
   604  		if opt != nil {
   605  			opt.ApplyPartitioningSettingsOption(settings)
   606  		}
   607  	}
   608  	d.PartitioningSettings = (*Ydb_Table.PartitioningSettings)(settings)
   609  }
   610  
   611  func WithPartitioningSettings(opts ...PartitioningSettingsOption) CreateTableOption {
   612  	return partitioningSettings(opts)
   613  }
   614  
   615  type (
   616  	ydbPartitioningSettings    Ydb_Table.PartitioningSettings
   617  	PartitioningSettingsOption interface {
   618  		ApplyPartitioningSettingsOption(settings *ydbPartitioningSettings)
   619  	}
   620  )
   621  
   622  type partitioningBySizePartitioningSettingsOption FeatureFlag
   623  
   624  func (flag partitioningBySizePartitioningSettingsOption) ApplyPartitioningSettingsOption(
   625  	settings *ydbPartitioningSettings,
   626  ) {
   627  	settings.PartitioningBySize = FeatureFlag(flag).ToYDB()
   628  }
   629  
   630  func WithPartitioningBySize(flag FeatureFlag) PartitioningSettingsOption {
   631  	return partitioningBySizePartitioningSettingsOption(flag)
   632  }
   633  
   634  type partitionSizeMbPartitioningSettingsOption uint64
   635  
   636  func (partitionSizeMb partitionSizeMbPartitioningSettingsOption) ApplyPartitioningSettingsOption(
   637  	settings *ydbPartitioningSettings,
   638  ) {
   639  	settings.PartitionSizeMb = uint64(partitionSizeMb)
   640  }
   641  
   642  func WithPartitionSizeMb(partitionSizeMb uint64) PartitioningSettingsOption {
   643  	return partitionSizeMbPartitioningSettingsOption(partitionSizeMb)
   644  }
   645  
   646  type partitioningByLoadPartitioningSettingsOption FeatureFlag
   647  
   648  func (flag partitioningByLoadPartitioningSettingsOption) ApplyPartitioningSettingsOption(
   649  	settings *ydbPartitioningSettings,
   650  ) {
   651  	settings.PartitioningByLoad = FeatureFlag(flag).ToYDB()
   652  }
   653  
   654  func WithPartitioningByLoad(flag FeatureFlag) PartitioningSettingsOption {
   655  	return partitioningByLoadPartitioningSettingsOption(flag)
   656  }
   657  
   658  type partitioningByPartitioningSettingsOption []string
   659  
   660  func (columns partitioningByPartitioningSettingsOption) ApplyPartitioningSettingsOption(
   661  	settings *ydbPartitioningSettings,
   662  ) {
   663  	settings.PartitionBy = columns
   664  }
   665  
   666  func WithPartitioningBy(columns []string) PartitioningSettingsOption {
   667  	return partitioningByPartitioningSettingsOption(columns)
   668  }
   669  
   670  type minPartitionsCountPartitioningSettingsOption uint64
   671  
   672  func (minPartitionsCount minPartitionsCountPartitioningSettingsOption) ApplyPartitioningSettingsOption(
   673  	settings *ydbPartitioningSettings,
   674  ) {
   675  	settings.MinPartitionsCount = uint64(minPartitionsCount)
   676  }
   677  
   678  func WithMinPartitionsCount(minPartitionsCount uint64) PartitioningSettingsOption {
   679  	return minPartitionsCountPartitioningSettingsOption(minPartitionsCount)
   680  }
   681  
   682  type maxPartitionsCountPartitioningSettingsOption uint64
   683  
   684  func (maxPartitionsCount maxPartitionsCountPartitioningSettingsOption) ApplyPartitioningSettingsOption(
   685  	settings *ydbPartitioningSettings,
   686  ) {
   687  	settings.MaxPartitionsCount = uint64(maxPartitionsCount)
   688  }
   689  
   690  func WithMaxPartitionsCount(maxPartitionsCount uint64) PartitioningSettingsOption {
   691  	return maxPartitionsCountPartitioningSettingsOption(maxPartitionsCount)
   692  }
   693  
   694  type (
   695  	DropTableDesc   Ydb_Table.DropTableRequest
   696  	DropTableOption interface {
   697  		ApplyDropTableOption(desc *DropTableDesc)
   698  	}
   699  )
   700  
   701  type (
   702  	AlterTableDesc   Ydb_Table.AlterTableRequest
   703  	AlterTableOption interface {
   704  		ApplyAlterTableOption(desc *AlterTableDesc, a *allocator.Allocator)
   705  	}
   706  )
   707  
   708  // WithAddColumn adds column in AlterTable request
   709  func WithAddColumn(name string, typ types.Type) AlterTableOption {
   710  	return column{
   711  		name: name,
   712  		typ:  typ,
   713  	}
   714  }
   715  
   716  // WithAlterAttribute changes attribute in AlterTable request
   717  func WithAlterAttribute(key, value string) AlterTableOption {
   718  	return attribute{
   719  		key:   key,
   720  		value: value,
   721  	}
   722  }
   723  
   724  // WithAddAttribute adds attribute to table in AlterTable request
   725  func WithAddAttribute(key, value string) AlterTableOption {
   726  	return attribute{
   727  		key:   key,
   728  		value: value,
   729  	}
   730  }
   731  
   732  // WithDropAttribute drops attribute from table in AlterTable request
   733  func WithDropAttribute(key string) AlterTableOption {
   734  	return attribute{
   735  		key: key,
   736  	}
   737  }
   738  
   739  func WithAddColumnMeta(column Column) AlterTableOption {
   740  	return columnMeta(column)
   741  }
   742  
   743  type dropColumn string
   744  
   745  func (name dropColumn) ApplyAlterTableOption(d *AlterTableDesc, a *allocator.Allocator) {
   746  	d.DropColumns = append(d.DropColumns, string(name))
   747  }
   748  
   749  func WithDropColumn(name string) AlterTableOption {
   750  	return dropColumn(name)
   751  }
   752  
   753  func WithAddColumnFamilies(cf ...ColumnFamily) AlterTableOption {
   754  	return columnFamilies(cf)
   755  }
   756  
   757  func WithAlterColumnFamilies(cf ...ColumnFamily) AlterTableOption {
   758  	return columnFamilies(cf)
   759  }
   760  
   761  func WithAlterReadReplicasSettings(rr ReadReplicasSettings) AlterTableOption {
   762  	return readReplicasSettings(rr)
   763  }
   764  
   765  func WithAlterStorageSettings(ss StorageSettings) AlterTableOption {
   766  	return storageSettings(ss)
   767  }
   768  
   769  func WithAlterKeyBloomFilter(f FeatureFlag) AlterTableOption {
   770  	return keyBloomFilter(f)
   771  }
   772  
   773  func WithAlterPartitionSettingsObject(ps PartitioningSettings) AlterTableOption {
   774  	return partitioningSettingsObject(ps)
   775  }
   776  
   777  // WithSetTimeToLiveSettings appends TTL settings in AlterTable request
   778  func WithSetTimeToLiveSettings(settings TimeToLiveSettings) AlterTableOption {
   779  	return timeToLiveSettings(settings)
   780  }
   781  
   782  type dropTimeToLive struct{}
   783  
   784  func (dropTimeToLive) ApplyAlterTableOption(d *AlterTableDesc, a *allocator.Allocator) {
   785  	d.TtlAction = &Ydb_Table.AlterTableRequest_DropTtlSettings{}
   786  }
   787  
   788  // WithDropTimeToLive drops TTL settings in AlterTable request
   789  func WithDropTimeToLive() AlterTableOption {
   790  	return dropTimeToLive{}
   791  }
   792  
   793  type (
   794  	CopyTableDesc   Ydb_Table.CopyTableRequest
   795  	CopyTableOption func(*CopyTableDesc)
   796  )
   797  
   798  type (
   799  	CopyTablesDesc   Ydb_Table.CopyTablesRequest
   800  	CopyTablesOption func(*CopyTablesDesc)
   801  )
   802  
   803  func CopyTablesItem(src, dst string, omitIndexes bool) CopyTablesOption {
   804  	return func(desc *CopyTablesDesc) {
   805  		desc.Tables = append(desc.Tables, &Ydb_Table.CopyTableItem{
   806  			SourcePath:      src,
   807  			DestinationPath: dst,
   808  			OmitIndexes:     omitIndexes,
   809  		})
   810  	}
   811  }
   812  
   813  type (
   814  	RenameTablesDesc   Ydb_Table.RenameTablesRequest
   815  	RenameTablesOption func(desc *RenameTablesDesc)
   816  )
   817  
   818  func RenameTablesItem(src, dst string, replaceDestination bool) RenameTablesOption {
   819  	return func(desc *RenameTablesDesc) {
   820  		desc.Tables = append(desc.Tables, &Ydb_Table.RenameTableItem{
   821  			SourcePath:         src,
   822  			DestinationPath:    dst,
   823  			ReplaceDestination: replaceDestination,
   824  		})
   825  	}
   826  }
   827  
   828  type (
   829  	ExecuteSchemeQueryDesc   Ydb_Table.ExecuteSchemeQueryRequest
   830  	ExecuteSchemeQueryOption func(*ExecuteSchemeQueryDesc)
   831  )
   832  
   833  type (
   834  	ExecuteDataQueryDesc struct {
   835  		*Ydb_Table.ExecuteDataQueryRequest
   836  
   837  		IgnoreTruncated bool
   838  	}
   839  	ExecuteDataQueryOption interface {
   840  		ApplyExecuteDataQueryOption(d *ExecuteDataQueryDesc, a *allocator.Allocator) []grpc.CallOption
   841  	}
   842  	executeDataQueryOptionFunc func(d *ExecuteDataQueryDesc, a *allocator.Allocator) []grpc.CallOption
   843  )
   844  
   845  func (f executeDataQueryOptionFunc) ApplyExecuteDataQueryOption(
   846  	d *ExecuteDataQueryDesc, a *allocator.Allocator,
   847  ) []grpc.CallOption {
   848  	return f(d, a)
   849  }
   850  
   851  var _ ExecuteDataQueryOption = executeDataQueryOptionFunc(nil)
   852  
   853  type (
   854  	CommitTransactionDesc   Ydb_Table.CommitTransactionRequest
   855  	CommitTransactionOption func(*CommitTransactionDesc)
   856  )
   857  
   858  type (
   859  	queryCachePolicy       Ydb_Table.QueryCachePolicy
   860  	QueryCachePolicyOption func(*queryCachePolicy, *allocator.Allocator)
   861  )
   862  
   863  // WithKeepInCache manages keep-in-cache flag in query cache policy
   864  //
   865  // By default all data queries executes with keep-in-cache policy
   866  func WithKeepInCache(keepInCache bool) ExecuteDataQueryOption {
   867  	return withQueryCachePolicy(
   868  		withQueryCachePolicyKeepInCache(keepInCache),
   869  	)
   870  }
   871  
   872  type withCallOptions []grpc.CallOption
   873  
   874  func (opts withCallOptions) ApplyExecuteScanQueryOption(d *ExecuteScanQueryDesc) []grpc.CallOption {
   875  	return opts
   876  }
   877  
   878  func (opts withCallOptions) ApplyBulkUpsertOption() []grpc.CallOption {
   879  	return opts
   880  }
   881  
   882  func (opts withCallOptions) ApplyExecuteDataQueryOption(
   883  	d *ExecuteDataQueryDesc, a *allocator.Allocator,
   884  ) []grpc.CallOption {
   885  	return opts
   886  }
   887  
   888  // WithCallOptions appends flag of commit transaction with executing query
   889  func WithCallOptions(opts ...grpc.CallOption) withCallOptions {
   890  	return opts
   891  }
   892  
   893  // WithCommit appends flag of commit transaction with executing query
   894  func WithCommit() ExecuteDataQueryOption {
   895  	return executeDataQueryOptionFunc(func(desc *ExecuteDataQueryDesc, a *allocator.Allocator) []grpc.CallOption {
   896  		desc.TxControl.CommitTx = true
   897  
   898  		return nil
   899  	})
   900  }
   901  
   902  // WithIgnoreTruncated mark truncated result as good (without error)
   903  func WithIgnoreTruncated() ExecuteDataQueryOption {
   904  	return executeDataQueryOptionFunc(func(desc *ExecuteDataQueryDesc, a *allocator.Allocator) []grpc.CallOption {
   905  		desc.IgnoreTruncated = true
   906  
   907  		return nil
   908  	})
   909  }
   910  
   911  // WithQueryCachePolicyKeepInCache manages keep-in-cache policy
   912  //
   913  // Deprecated: data queries always executes with enabled keep-in-cache policy.
   914  // Will be removed after Oct 2024.
   915  // Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
   916  func WithQueryCachePolicyKeepInCache() QueryCachePolicyOption {
   917  	return withQueryCachePolicyKeepInCache(true)
   918  }
   919  
   920  func withQueryCachePolicyKeepInCache(keepInCache bool) QueryCachePolicyOption {
   921  	return func(p *queryCachePolicy, a *allocator.Allocator) {
   922  		p.KeepInCache = keepInCache
   923  	}
   924  }
   925  
   926  // WithQueryCachePolicy manages query cache policy
   927  //
   928  // Deprecated: use WithKeepInCache for disabling keep-in-cache policy.
   929  // Will be removed after Oct 2024.
   930  // Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
   931  func WithQueryCachePolicy(opts ...QueryCachePolicyOption) ExecuteDataQueryOption {
   932  	return withQueryCachePolicy(opts...)
   933  }
   934  
   935  func withQueryCachePolicy(opts ...QueryCachePolicyOption) ExecuteDataQueryOption {
   936  	return executeDataQueryOptionFunc(func(d *ExecuteDataQueryDesc, a *allocator.Allocator) []grpc.CallOption {
   937  		if d.QueryCachePolicy == nil {
   938  			d.QueryCachePolicy = a.TableQueryCachePolicy()
   939  			d.QueryCachePolicy.KeepInCache = true
   940  		}
   941  		for _, opt := range opts {
   942  			if opt != nil {
   943  				opt((*queryCachePolicy)(d.QueryCachePolicy), a)
   944  			}
   945  		}
   946  
   947  		return nil
   948  	})
   949  }
   950  
   951  func WithCommitCollectStatsModeNone() CommitTransactionOption {
   952  	return func(d *CommitTransactionDesc) {
   953  		d.CollectStats = Ydb_Table.QueryStatsCollection_STATS_COLLECTION_NONE
   954  	}
   955  }
   956  
   957  func WithCommitCollectStatsModeBasic() CommitTransactionOption {
   958  	return func(d *CommitTransactionDesc) {
   959  		d.CollectStats = Ydb_Table.QueryStatsCollection_STATS_COLLECTION_BASIC
   960  	}
   961  }
   962  
   963  func WithCollectStatsModeNone() ExecuteDataQueryOption {
   964  	return executeDataQueryOptionFunc(func(d *ExecuteDataQueryDesc, a *allocator.Allocator) []grpc.CallOption {
   965  		d.CollectStats = Ydb_Table.QueryStatsCollection_STATS_COLLECTION_NONE
   966  
   967  		return nil
   968  	})
   969  }
   970  
   971  func WithCollectStatsModeBasic() ExecuteDataQueryOption {
   972  	return executeDataQueryOptionFunc(func(d *ExecuteDataQueryDesc, a *allocator.Allocator) []grpc.CallOption {
   973  		d.CollectStats = Ydb_Table.QueryStatsCollection_STATS_COLLECTION_BASIC
   974  
   975  		return nil
   976  	})
   977  }
   978  
   979  type (
   980  	BulkUpsertOption interface {
   981  		ApplyBulkUpsertOption() []grpc.CallOption
   982  	}
   983  )
   984  
   985  type (
   986  	ExecuteScanQueryDesc   Ydb_Table.ExecuteScanQueryRequest
   987  	ExecuteScanQueryOption interface {
   988  		ApplyExecuteScanQueryOption(d *ExecuteScanQueryDesc) []grpc.CallOption
   989  	}
   990  	executeScanQueryOptionFunc func(*ExecuteScanQueryDesc) []grpc.CallOption
   991  )
   992  
   993  func (f executeScanQueryOptionFunc) ApplyExecuteScanQueryOption(d *ExecuteScanQueryDesc) []grpc.CallOption {
   994  	return f(d)
   995  }
   996  
   997  var _ ExecuteScanQueryOption = executeScanQueryOptionFunc(nil)
   998  
   999  // WithExecuteScanQueryMode defines scan query mode: execute or explain
  1000  func WithExecuteScanQueryMode(m ExecuteScanQueryRequestMode) ExecuteScanQueryOption {
  1001  	return executeScanQueryOptionFunc(func(desc *ExecuteScanQueryDesc) []grpc.CallOption {
  1002  		desc.Mode = m.toYDB()
  1003  
  1004  		return nil
  1005  	})
  1006  }
  1007  
  1008  // ExecuteScanQueryStatsType specified scan query mode
  1009  type ExecuteScanQueryStatsType uint32
  1010  
  1011  const (
  1012  	ExecuteScanQueryStatsTypeNone = iota
  1013  	ExecuteScanQueryStatsTypeBasic
  1014  	ExecuteScanQueryStatsTypeFull
  1015  )
  1016  
  1017  func (stats ExecuteScanQueryStatsType) toYDB() Ydb_Table.QueryStatsCollection_Mode {
  1018  	switch stats {
  1019  	case ExecuteScanQueryStatsTypeNone:
  1020  		return Ydb_Table.QueryStatsCollection_STATS_COLLECTION_NONE
  1021  	case ExecuteScanQueryStatsTypeBasic:
  1022  		return Ydb_Table.QueryStatsCollection_STATS_COLLECTION_BASIC
  1023  	case ExecuteScanQueryStatsTypeFull:
  1024  		return Ydb_Table.QueryStatsCollection_STATS_COLLECTION_FULL
  1025  	default:
  1026  		return Ydb_Table.QueryStatsCollection_STATS_COLLECTION_UNSPECIFIED
  1027  	}
  1028  }
  1029  
  1030  // WithExecuteScanQueryStats defines query statistics mode
  1031  func WithExecuteScanQueryStats(stats ExecuteScanQueryStatsType) ExecuteScanQueryOption {
  1032  	return executeScanQueryOptionFunc(func(desc *ExecuteScanQueryDesc) []grpc.CallOption {
  1033  		desc.CollectStats = stats.toYDB()
  1034  
  1035  		return nil
  1036  	})
  1037  }
  1038  
  1039  var (
  1040  	_ ReadRowsOption  = readColumnsOption{}
  1041  	_ ReadTableOption = readOrderedOption{}
  1042  	_ ReadTableOption = readKeyRangeOption{}
  1043  	_ ReadTableOption = readGreaterOrEqualOption{}
  1044  	_ ReadTableOption = readLessOrEqualOption{}
  1045  	_ ReadTableOption = readLessOption{}
  1046  	_ ReadTableOption = readGreaterOption{}
  1047  	_ ReadTableOption = readRowLimitOption(0)
  1048  )
  1049  
  1050  type (
  1051  	ReadRowsDesc   Ydb_Table.ReadRowsRequest
  1052  	ReadRowsOption interface {
  1053  		ApplyReadRowsOption(desc *ReadRowsDesc, a *allocator.Allocator)
  1054  	}
  1055  
  1056  	ReadTableDesc   Ydb_Table.ReadTableRequest
  1057  	ReadTableOption interface {
  1058  		ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator)
  1059  	}
  1060  
  1061  	readColumnsOption        []string
  1062  	readOrderedOption        struct{}
  1063  	readSnapshotOption       bool
  1064  	readKeyRangeOption       KeyRange
  1065  	readGreaterOrEqualOption struct{ value.Value }
  1066  	readLessOrEqualOption    struct{ value.Value }
  1067  	readLessOption           struct{ value.Value }
  1068  	readGreaterOption        struct{ value.Value }
  1069  	readRowLimitOption       uint64
  1070  )
  1071  
  1072  func (n readRowLimitOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
  1073  	desc.RowLimit = uint64(n)
  1074  }
  1075  
  1076  func (x readGreaterOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
  1077  	desc.initKeyRange()
  1078  	desc.KeyRange.FromBound = &Ydb_Table.KeyRange_Greater{
  1079  		Greater: value.ToYDB(x, a),
  1080  	}
  1081  }
  1082  
  1083  func (x readLessOrEqualOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
  1084  	desc.initKeyRange()
  1085  	desc.KeyRange.ToBound = &Ydb_Table.KeyRange_LessOrEqual{
  1086  		LessOrEqual: value.ToYDB(x, a),
  1087  	}
  1088  }
  1089  
  1090  func (x readLessOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
  1091  	desc.initKeyRange()
  1092  	desc.KeyRange.ToBound = &Ydb_Table.KeyRange_Less{
  1093  		Less: value.ToYDB(x, a),
  1094  	}
  1095  }
  1096  
  1097  func (columns readColumnsOption) ApplyReadRowsOption(desc *ReadRowsDesc, a *allocator.Allocator) {
  1098  	desc.Columns = append(desc.Columns, columns...)
  1099  }
  1100  
  1101  func (columns readColumnsOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
  1102  	desc.Columns = append(desc.Columns, columns...)
  1103  }
  1104  
  1105  func (readOrderedOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
  1106  	desc.Ordered = true
  1107  }
  1108  
  1109  func (b readSnapshotOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
  1110  	if b {
  1111  		desc.UseSnapshot = FeatureEnabled.ToYDB()
  1112  	} else {
  1113  		desc.UseSnapshot = FeatureDisabled.ToYDB()
  1114  	}
  1115  }
  1116  
  1117  func (x readKeyRangeOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
  1118  	desc.initKeyRange()
  1119  	if x.From != nil {
  1120  		desc.KeyRange.FromBound = &Ydb_Table.KeyRange_GreaterOrEqual{
  1121  			GreaterOrEqual: value.ToYDB(x.From, a),
  1122  		}
  1123  	}
  1124  	if x.To != nil {
  1125  		desc.KeyRange.ToBound = &Ydb_Table.KeyRange_Less{
  1126  			Less: value.ToYDB(x.To, a),
  1127  		}
  1128  	}
  1129  }
  1130  
  1131  func (x readGreaterOrEqualOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
  1132  	desc.initKeyRange()
  1133  	desc.KeyRange.FromBound = &Ydb_Table.KeyRange_GreaterOrEqual{
  1134  		GreaterOrEqual: value.ToYDB(x, a),
  1135  	}
  1136  }
  1137  
  1138  func ReadColumn(name string) readColumnsOption {
  1139  	return []string{name}
  1140  }
  1141  
  1142  func ReadColumns(names ...string) readColumnsOption {
  1143  	return names
  1144  }
  1145  
  1146  func ReadOrdered() ReadTableOption {
  1147  	return readOrderedOption{}
  1148  }
  1149  
  1150  func ReadFromSnapshot(b bool) ReadTableOption {
  1151  	return readSnapshotOption(b)
  1152  }
  1153  
  1154  // ReadKeyRange returns ReadTableOption which makes ReadTable read values
  1155  // in range [x.From, x.To).
  1156  //
  1157  // Both x.From and x.To may be nil.
  1158  func ReadKeyRange(x KeyRange) ReadTableOption {
  1159  	return readKeyRangeOption(x)
  1160  }
  1161  
  1162  func ReadGreater(x value.Value) ReadTableOption {
  1163  	return readGreaterOption{x}
  1164  }
  1165  
  1166  func ReadGreaterOrEqual(x value.Value) ReadTableOption {
  1167  	return readGreaterOrEqualOption{x}
  1168  }
  1169  
  1170  func ReadLess(x value.Value) ReadTableOption {
  1171  	return readLessOption{x}
  1172  }
  1173  
  1174  func ReadLessOrEqual(x value.Value) ReadTableOption {
  1175  	return readLessOrEqualOption{x}
  1176  }
  1177  
  1178  func ReadRowLimit(n uint64) ReadTableOption {
  1179  	return readRowLimitOption(n)
  1180  }
  1181  
  1182  func (d *ReadTableDesc) initKeyRange() {
  1183  	if d.KeyRange == nil {
  1184  		d.KeyRange = new(Ydb_Table.KeyRange)
  1185  	}
  1186  }