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