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