github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/table/options/models.go (about) 1 package options 2 3 import ( 4 "bytes" 5 "fmt" 6 "time" 7 8 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Table" 9 10 "github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator" 11 "github.com/ydb-platform/ydb-go-sdk/v3/internal/feature" 12 "github.com/ydb-platform/ydb-go-sdk/v3/internal/types" 13 "github.com/ydb-platform/ydb-go-sdk/v3/internal/value" 14 ) 15 16 type Column struct { 17 Name string 18 Type types.Type 19 Family string 20 } 21 22 func (c Column) toYDB(a *allocator.Allocator) *Ydb_Table.ColumnMeta { 23 return &Ydb_Table.ColumnMeta{ 24 Name: c.Name, 25 Type: types.TypeToYDB(c.Type, a), 26 Family: c.Family, 27 } 28 } 29 30 func NewTableColumn(name string, typ types.Type, family string) Column { 31 return Column{ 32 Name: name, 33 Type: typ, 34 Family: family, 35 } 36 } 37 38 type IndexDescription struct { 39 Name string 40 IndexColumns []string 41 DataColumns []string 42 Status Ydb_Table.TableIndexDescription_Status 43 Type IndexType 44 } 45 46 //nolint:unused 47 func (i IndexDescription) toYDB() *Ydb_Table.TableIndexDescription { 48 return &Ydb_Table.TableIndexDescription{ 49 Name: i.Name, 50 IndexColumns: i.IndexColumns, 51 Status: i.Status, 52 } 53 } 54 55 type Description struct { 56 Name string 57 Columns []Column 58 PrimaryKey []string 59 KeyRanges []KeyRange 60 Stats *TableStats 61 ColumnFamilies []ColumnFamily 62 Attributes map[string]string 63 ReadReplicaSettings ReadReplicasSettings 64 StorageSettings StorageSettings 65 KeyBloomFilter FeatureFlag 66 PartitioningSettings PartitioningSettings 67 Indexes []IndexDescription 68 TimeToLiveSettings *TimeToLiveSettings 69 Changefeeds []ChangefeedDescription 70 Tiering string 71 } 72 73 type TableStats struct { 74 PartitionStats []PartitionStats 75 RowsEstimate uint64 76 StoreSize uint64 77 Partitions uint64 78 CreationTime time.Time 79 ModificationTime time.Time 80 } 81 82 type PartitionStats struct { 83 RowsEstimate uint64 84 StoreSize uint64 85 } 86 87 type ColumnFamily struct { 88 Name string 89 Data StoragePool 90 Compression ColumnFamilyCompression 91 KeepInMemory FeatureFlag 92 } 93 94 func (c ColumnFamily) toYDB() *Ydb_Table.ColumnFamily { 95 return &Ydb_Table.ColumnFamily{ 96 Name: c.Name, 97 Data: c.Data.toYDB(), 98 Compression: c.Compression.toYDB(), 99 KeepInMemory: c.KeepInMemory.ToYDB(), 100 } 101 } 102 103 func NewColumnFamily(c *Ydb_Table.ColumnFamily) ColumnFamily { 104 return ColumnFamily{ 105 Name: c.GetName(), 106 Data: storagePool(c.GetData()), 107 Compression: columnFamilyCompression(c.GetCompression()), 108 KeepInMemory: feature.FromYDB(c.GetKeepInMemory()), 109 } 110 } 111 112 type StoragePool struct { 113 Media string 114 } 115 116 func (s StoragePool) toYDB() *Ydb_Table.StoragePool { 117 if s.Media == "" { 118 return nil 119 } 120 121 return &Ydb_Table.StoragePool{ 122 Media: s.Media, 123 } 124 } 125 126 func storagePool(s *Ydb_Table.StoragePool) StoragePool { 127 return StoragePool{ 128 Media: s.GetMedia(), 129 } 130 } 131 132 type ColumnFamilyCompression byte 133 134 const ( 135 ColumnFamilyCompressionUnknown ColumnFamilyCompression = iota 136 ColumnFamilyCompressionNone 137 ColumnFamilyCompressionLZ4 138 ) 139 140 func (c ColumnFamilyCompression) String() string { 141 switch c { 142 case ColumnFamilyCompressionNone: 143 return "none" 144 case ColumnFamilyCompressionLZ4: 145 return "lz4" 146 default: 147 return fmt.Sprintf("unknown_column_family_compression_%d", c) 148 } 149 } 150 151 func (c ColumnFamilyCompression) toYDB() Ydb_Table.ColumnFamily_Compression { 152 switch c { 153 case ColumnFamilyCompressionNone: 154 return Ydb_Table.ColumnFamily_COMPRESSION_NONE 155 case ColumnFamilyCompressionLZ4: 156 return Ydb_Table.ColumnFamily_COMPRESSION_LZ4 157 default: 158 return Ydb_Table.ColumnFamily_COMPRESSION_UNSPECIFIED 159 } 160 } 161 162 func columnFamilyCompression(c Ydb_Table.ColumnFamily_Compression) ColumnFamilyCompression { 163 switch c { 164 case Ydb_Table.ColumnFamily_COMPRESSION_NONE: 165 return ColumnFamilyCompressionNone 166 case Ydb_Table.ColumnFamily_COMPRESSION_LZ4: 167 return ColumnFamilyCompressionLZ4 168 default: 169 return ColumnFamilyCompressionUnknown 170 } 171 } 172 173 type ( 174 DescribeTableDesc Ydb_Table.DescribeTableRequest 175 DescribeTableOption func(d *DescribeTableDesc) 176 ) 177 178 type ReadReplicasSettings struct { 179 Type ReadReplicasType 180 Count uint64 181 } 182 183 func (rr ReadReplicasSettings) ToYDB() *Ydb_Table.ReadReplicasSettings { 184 switch rr.Type { 185 case ReadReplicasPerAzReadReplicas: 186 return &Ydb_Table.ReadReplicasSettings{ 187 Settings: &Ydb_Table.ReadReplicasSettings_PerAzReadReplicasCount{ 188 PerAzReadReplicasCount: rr.Count, 189 }, 190 } 191 192 default: 193 return &Ydb_Table.ReadReplicasSettings{ 194 Settings: &Ydb_Table.ReadReplicasSettings_AnyAzReadReplicasCount{ 195 AnyAzReadReplicasCount: rr.Count, 196 }, 197 } 198 } 199 } 200 201 func NewReadReplicasSettings(rr *Ydb_Table.ReadReplicasSettings) ReadReplicasSettings { 202 t := ReadReplicasPerAzReadReplicas 203 var c uint64 204 205 if c = rr.GetPerAzReadReplicasCount(); c != 0 { 206 t = ReadReplicasPerAzReadReplicas 207 } else if c = rr.GetAnyAzReadReplicasCount(); c != 0 { 208 t = ReadReplicasAnyAzReadReplicas 209 } 210 211 return ReadReplicasSettings{ 212 Type: t, 213 Count: c, 214 } 215 } 216 217 type ReadReplicasType byte 218 219 const ( 220 ReadReplicasPerAzReadReplicas ReadReplicasType = iota 221 ReadReplicasAnyAzReadReplicas 222 ) 223 224 type StorageSettings struct { 225 TableCommitLog0 StoragePool 226 TableCommitLog1 StoragePool 227 External StoragePool 228 StoreExternalBlobs FeatureFlag 229 } 230 231 func (ss StorageSettings) ToYDB() *Ydb_Table.StorageSettings { 232 return &Ydb_Table.StorageSettings{ 233 TabletCommitLog0: ss.TableCommitLog0.toYDB(), 234 TabletCommitLog1: ss.TableCommitLog1.toYDB(), 235 External: ss.External.toYDB(), 236 StoreExternalBlobs: ss.StoreExternalBlobs.ToYDB(), 237 } 238 } 239 240 func NewStorageSettings(ss *Ydb_Table.StorageSettings) StorageSettings { 241 return StorageSettings{ 242 TableCommitLog0: storagePool(ss.GetTabletCommitLog0()), 243 TableCommitLog1: storagePool(ss.GetTabletCommitLog1()), 244 External: storagePool(ss.GetExternal()), 245 StoreExternalBlobs: feature.FromYDB(ss.GetStoreExternalBlobs()), 246 } 247 } 248 249 type Partitions interface { 250 CreateTableOption 251 252 isPartitions() 253 } 254 255 type PartitioningSettings struct { 256 PartitioningBySize FeatureFlag 257 PartitionSizeMb uint64 258 PartitioningByLoad FeatureFlag 259 MinPartitionsCount uint64 260 MaxPartitionsCount uint64 261 } 262 263 func (ps PartitioningSettings) toYDB() *Ydb_Table.PartitioningSettings { 264 return &Ydb_Table.PartitioningSettings{ 265 PartitioningBySize: ps.PartitioningBySize.ToYDB(), 266 PartitionSizeMb: ps.PartitionSizeMb, 267 PartitioningByLoad: ps.PartitioningByLoad.ToYDB(), 268 MinPartitionsCount: ps.MinPartitionsCount, 269 MaxPartitionsCount: ps.MaxPartitionsCount, 270 } 271 } 272 273 func NewPartitioningSettings(ps *Ydb_Table.PartitioningSettings) PartitioningSettings { 274 return PartitioningSettings{ 275 PartitioningBySize: feature.FromYDB(ps.GetPartitioningBySize()), 276 PartitionSizeMb: ps.GetPartitionSizeMb(), 277 PartitioningByLoad: feature.FromYDB(ps.GetPartitioningByLoad()), 278 MinPartitionsCount: ps.GetMinPartitionsCount(), 279 MaxPartitionsCount: ps.GetMaxPartitionsCount(), 280 } 281 } 282 283 type ( 284 IndexType uint8 285 ) 286 287 const ( 288 IndexTypeGlobal = IndexType(iota) 289 IndexTypeGlobalAsync 290 ) 291 292 func (t IndexType) ApplyIndexOption(d *indexDesc) { 293 switch t { 294 case IndexTypeGlobal: 295 d.Type = &Ydb_Table.TableIndex_GlobalIndex{ 296 GlobalIndex: &Ydb_Table.GlobalIndex{}, 297 } 298 case IndexTypeGlobalAsync: 299 d.Type = &Ydb_Table.TableIndex_GlobalAsyncIndex{ 300 GlobalAsyncIndex: &Ydb_Table.GlobalAsyncIndex{}, 301 } 302 } 303 } 304 305 func GlobalIndex() IndexType { 306 return IndexTypeGlobal 307 } 308 309 func GlobalAsyncIndex() IndexType { 310 return IndexTypeGlobalAsync 311 } 312 313 type PartitioningMode byte 314 315 const ( 316 PartitioningUnknown PartitioningMode = iota 317 PartitioningDisabled 318 PartitioningAutoSplit 319 PartitioningAutoSplitMerge 320 ) 321 322 func (p PartitioningMode) toYDB() Ydb_Table.PartitioningPolicy_AutoPartitioningPolicy { 323 switch p { 324 case PartitioningDisabled: 325 return Ydb_Table.PartitioningPolicy_DISABLED 326 case PartitioningAutoSplit: 327 return Ydb_Table.PartitioningPolicy_AUTO_SPLIT 328 case PartitioningAutoSplitMerge: 329 return Ydb_Table.PartitioningPolicy_AUTO_SPLIT_MERGE 330 default: 331 panic("ydb: unknown partitioning mode") 332 } 333 } 334 335 type ExecuteScanQueryRequestMode byte 336 337 const ( 338 ExecuteScanQueryRequestModeExec ExecuteScanQueryRequestMode = iota 339 ExecuteScanQueryRequestModeExplain 340 ) 341 342 func (p ExecuteScanQueryRequestMode) toYDB() Ydb_Table.ExecuteScanQueryRequest_Mode { 343 switch p { 344 case ExecuteScanQueryRequestModeExec: 345 return Ydb_Table.ExecuteScanQueryRequest_MODE_EXEC 346 case ExecuteScanQueryRequestModeExplain: 347 return Ydb_Table.ExecuteScanQueryRequest_MODE_EXPLAIN 348 default: 349 panic("ydb: unknown execute scan query mode") 350 } 351 } 352 353 type TableOptionsDescription struct { 354 TableProfilePresets []TableProfileDescription 355 StoragePolicyPresets []StoragePolicyDescription 356 CompactionPolicyPresets []CompactionPolicyDescription 357 PartitioningPolicyPresets []PartitioningPolicyDescription 358 ExecutionPolicyPresets []ExecutionPolicyDescription 359 ReplicationPolicyPresets []ReplicationPolicyDescription 360 CachingPolicyPresets []CachingPolicyDescription 361 } 362 363 type ( 364 TableProfileDescription struct { 365 Name string 366 Labels map[string]string 367 368 DefaultStoragePolicy string 369 DefaultCompactionPolicy string 370 DefaultPartitioningPolicy string 371 DefaultExecutionPolicy string 372 DefaultReplicationPolicy string 373 DefaultCachingPolicy string 374 375 AllowedStoragePolicies []string 376 AllowedCompactionPolicies []string 377 AllowedPartitioningPolicies []string 378 AllowedExecutionPolicies []string 379 AllowedReplicationPolicies []string 380 AllowedCachingPolicies []string 381 } 382 StoragePolicyDescription struct { 383 Name string 384 Labels map[string]string 385 } 386 CompactionPolicyDescription struct { 387 Name string 388 Labels map[string]string 389 } 390 PartitioningPolicyDescription struct { 391 Name string 392 Labels map[string]string 393 } 394 ExecutionPolicyDescription struct { 395 Name string 396 Labels map[string]string 397 } 398 ReplicationPolicyDescription struct { 399 Name string 400 Labels map[string]string 401 } 402 CachingPolicyDescription struct { 403 Name string 404 Labels map[string]string 405 } 406 ) 407 408 type KeyRange struct { 409 From value.Value 410 To value.Value 411 } 412 413 func (kr KeyRange) String() string { 414 var buf bytes.Buffer 415 buf.WriteString("[") 416 if kr.From == nil { 417 buf.WriteString("NULL") 418 } else { 419 buf.WriteString(kr.From.Yql()) 420 } 421 buf.WriteString(",") 422 if kr.To == nil { 423 buf.WriteString("NULL") 424 } else { 425 buf.WriteString(kr.To.Yql()) 426 } 427 buf.WriteString("]") 428 429 return buf.String() 430 } 431 432 // Deprecated: use TimeToLiveSettings instead. 433 // Will be removed after Jan 2022. 434 type TTLSettings struct { 435 DateTimeColumn string 436 TTLSeconds uint32 437 } 438 439 type TimeToLiveSettings struct { 440 ColumnName string 441 442 // Mode specified mode 443 Mode TimeToLiveMode 444 445 // ExpireAfterSeconds specified expiration in seconds 446 ExpireAfterSeconds uint32 447 448 // ColumnUnit valid with Mode = TimeToLiveModeValueSinceUnixEpoch 449 ColumnUnit *TimeToLiveUnit 450 } 451 452 type TimeToLiveMode byte 453 454 const ( 455 TimeToLiveModeDateType TimeToLiveMode = iota 456 TimeToLiveModeValueSinceUnixEpoch 457 ) 458 459 func NewTTLSettings() TimeToLiveSettings { 460 return TimeToLiveSettings{ 461 Mode: TimeToLiveModeDateType, 462 } 463 } 464 465 func (ttl TimeToLiveSettings) ColumnDateType(columnName string) TimeToLiveSettings { 466 ttl.Mode = TimeToLiveModeDateType 467 ttl.ColumnName = columnName 468 469 return ttl 470 } 471 472 func unitToPointer(unit TimeToLiveUnit) *TimeToLiveUnit { 473 return &unit 474 } 475 476 func (ttl TimeToLiveSettings) ColumnSeconds(columnName string) TimeToLiveSettings { 477 ttl.Mode = TimeToLiveModeValueSinceUnixEpoch 478 ttl.ColumnName = columnName 479 ttl.ColumnUnit = unitToPointer(TimeToLiveUnitSeconds) 480 481 return ttl 482 } 483 484 func (ttl TimeToLiveSettings) ColumnMilliseconds(columnName string) TimeToLiveSettings { 485 ttl.Mode = TimeToLiveModeValueSinceUnixEpoch 486 ttl.ColumnName = columnName 487 ttl.ColumnUnit = unitToPointer(TimeToLiveUnitMilliseconds) 488 489 return ttl 490 } 491 492 func (ttl TimeToLiveSettings) ColumnMicroseconds(columnName string) TimeToLiveSettings { 493 ttl.Mode = TimeToLiveModeValueSinceUnixEpoch 494 ttl.ColumnName = columnName 495 ttl.ColumnUnit = unitToPointer(TimeToLiveUnitMicroseconds) 496 497 return ttl 498 } 499 500 func (ttl TimeToLiveSettings) ColumnNanoseconds(columnName string) TimeToLiveSettings { 501 ttl.Mode = TimeToLiveModeValueSinceUnixEpoch 502 ttl.ColumnName = columnName 503 ttl.ColumnUnit = unitToPointer(TimeToLiveUnitNanoseconds) 504 505 return ttl 506 } 507 508 func (ttl TimeToLiveSettings) ExpireAfter(expireAfter time.Duration) TimeToLiveSettings { 509 ttl.ExpireAfterSeconds = uint32(expireAfter.Seconds()) 510 511 return ttl 512 } 513 514 func (ttl *TimeToLiveSettings) ToYDB() *Ydb_Table.TtlSettings { 515 if ttl == nil { 516 return nil 517 } 518 switch ttl.Mode { 519 case TimeToLiveModeValueSinceUnixEpoch: 520 return &Ydb_Table.TtlSettings{ 521 Mode: &Ydb_Table.TtlSettings_ValueSinceUnixEpoch{ 522 ValueSinceUnixEpoch: &Ydb_Table.ValueSinceUnixEpochModeSettings{ 523 ColumnName: ttl.ColumnName, 524 ColumnUnit: ttl.ColumnUnit.ToYDB(), 525 ExpireAfterSeconds: ttl.ExpireAfterSeconds, 526 }, 527 }, 528 } 529 default: // currently use TimeToLiveModeDateType mode as default 530 return &Ydb_Table.TtlSettings{ 531 Mode: &Ydb_Table.TtlSettings_DateTypeColumn{ 532 DateTypeColumn: &Ydb_Table.DateTypeColumnModeSettings{ 533 ColumnName: ttl.ColumnName, 534 ExpireAfterSeconds: ttl.ExpireAfterSeconds, 535 }, 536 }, 537 } 538 } 539 } 540 541 type TimeToLiveUnit int32 542 543 const ( 544 TimeToLiveUnitUnspecified TimeToLiveUnit = iota 545 TimeToLiveUnitSeconds 546 TimeToLiveUnitMilliseconds 547 TimeToLiveUnitMicroseconds 548 TimeToLiveUnitNanoseconds 549 ) 550 551 func (unit *TimeToLiveUnit) ToYDB() Ydb_Table.ValueSinceUnixEpochModeSettings_Unit { 552 if unit == nil { 553 return Ydb_Table.ValueSinceUnixEpochModeSettings_UNIT_UNSPECIFIED 554 } 555 switch *unit { 556 case TimeToLiveUnitSeconds: 557 return Ydb_Table.ValueSinceUnixEpochModeSettings_UNIT_SECONDS 558 case TimeToLiveUnitMilliseconds: 559 return Ydb_Table.ValueSinceUnixEpochModeSettings_UNIT_MILLISECONDS 560 case TimeToLiveUnitMicroseconds: 561 return Ydb_Table.ValueSinceUnixEpochModeSettings_UNIT_MICROSECONDS 562 case TimeToLiveUnitNanoseconds: 563 return Ydb_Table.ValueSinceUnixEpochModeSettings_UNIT_NANOSECONDS 564 case TimeToLiveUnitUnspecified: 565 return Ydb_Table.ValueSinceUnixEpochModeSettings_UNIT_UNSPECIFIED 566 default: 567 panic("ydb: unknown unit for value since epoch") 568 } 569 } 570 571 type ChangefeedDescription struct { 572 Name string 573 Mode ChangefeedMode 574 Format ChangefeedFormat 575 State ChangefeedState 576 } 577 578 func NewChangefeedDescription(proto *Ydb_Table.ChangefeedDescription) ChangefeedDescription { 579 return ChangefeedDescription{ 580 Name: proto.GetName(), 581 Mode: ChangefeedMode(proto.GetMode()), 582 Format: ChangefeedFormat(proto.GetFormat()), 583 State: ChangefeedState(proto.GetState()), 584 } 585 } 586 587 type ChangefeedState int 588 589 const ( 590 ChangefeedStateUnspecified = ChangefeedState(Ydb_Table.ChangefeedDescription_STATE_UNSPECIFIED) 591 ChangefeedStateEnabled = ChangefeedState(Ydb_Table.ChangefeedDescription_STATE_ENABLED) 592 ChangefeedStateDisabled = ChangefeedState(Ydb_Table.ChangefeedDescription_STATE_DISABLED) 593 ) 594 595 type ChangefeedMode int 596 597 const ( 598 ChangefeedModeUnspecified = ChangefeedMode(Ydb_Table.ChangefeedMode_MODE_UNSPECIFIED) 599 ChangefeedModeKeysOnly = ChangefeedMode(Ydb_Table.ChangefeedMode_MODE_KEYS_ONLY) 600 ChangefeedModeUpdates = ChangefeedMode(Ydb_Table.ChangefeedMode_MODE_UPDATES) 601 ChangefeedModeNewImage = ChangefeedMode(Ydb_Table.ChangefeedMode_MODE_NEW_IMAGE) 602 ChangefeedModeOldImage = ChangefeedMode(Ydb_Table.ChangefeedMode_MODE_OLD_IMAGE) 603 ChangefeedModeNewAndOldImages = ChangefeedMode(Ydb_Table.ChangefeedMode_MODE_NEW_AND_OLD_IMAGES) 604 ) 605 606 type ChangefeedFormat int 607 608 const ( 609 ChangefeedFormatUnspecified = ChangefeedFormat(Ydb_Table.ChangefeedFormat_FORMAT_UNSPECIFIED) 610 ChangefeedFormatJSON = ChangefeedFormat(Ydb_Table.ChangefeedFormat_FORMAT_JSON) 611 ChangefeedFormatDynamoDBStreamsJSON = ChangefeedFormat(Ydb_Table.ChangefeedFormat_FORMAT_DYNAMODB_STREAMS_JSON) 612 )