Skip to content

Commit a3b167d

Browse files
Add support for Cassandra 5.0 table options
1 parent 34fdeeb commit a3b167d

File tree

1 file changed

+191
-51
lines changed

1 file changed

+191
-51
lines changed

metadata.go

Lines changed: 191 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ type ViewMetadata struct {
9898
type MaterializedViewMetadata struct {
9999
Keyspace string
100100
Name string
101+
AdditionalWritePolicy string
101102
BaseTableId UUID
102103
BaseTable *TableMetadata
103104
BloomFilterFpChance float64
@@ -115,6 +116,7 @@ type MaterializedViewMetadata struct {
115116
MaxIndexInterval int
116117
MemtableFlushPeriodInMs int
117118
MinIndexInterval int
119+
ReadRepair string
118120
ReadRepairChance float64
119121
SpeculativeRetry string
120122

@@ -975,69 +977,207 @@ func getViewsMetadata(session *Session, keyspaceName string) ([]ViewMetadata, er
975977
return views, nil
976978
}
977979

980+
func extensionsBlobToStrings(value interface{}) (map[string]string, error) {
981+
const errorMessage = "gocql.extensionsBlobToStrings failed to read column %s"
982+
byteData, ok := value.(map[string][]byte)
983+
if !ok {
984+
return nil, fmt.Errorf(errorMessage, "extentions")
985+
}
986+
987+
extensions := make(map[string]string, len(byteData))
988+
for key, rowByte := range byteData {
989+
extensions[key] = string(rowByte)
990+
}
991+
992+
return extensions, nil
993+
}
994+
995+
func materializedViewMetadataFromMap(currentObject map[string]interface{}, materializedView *MaterializedViewMetadata) error {
996+
const errorMessage = "gocql.materializedViewMetadataFromMap failed to read column %s"
997+
var ok bool
998+
for key, value := range currentObject {
999+
switch key {
1000+
case "keyspace_name":
1001+
materializedView.Keyspace, ok = value.(string)
1002+
if !ok {
1003+
return fmt.Errorf(errorMessage, key)
1004+
}
1005+
1006+
case "view_name":
1007+
materializedView.Name, ok = value.(string)
1008+
if !ok {
1009+
return fmt.Errorf(errorMessage, key)
1010+
}
1011+
1012+
case "additional_write_policy":
1013+
materializedView.AdditionalWritePolicy, ok = value.(string)
1014+
if !ok {
1015+
return fmt.Errorf(errorMessage, key)
1016+
}
1017+
1018+
case "base_table_id":
1019+
materializedView.BaseTableId, ok = value.(UUID)
1020+
if !ok {
1021+
return fmt.Errorf(errorMessage, key)
1022+
}
1023+
1024+
case "base_table_name":
1025+
materializedView.baseTableName, ok = value.(string)
1026+
if !ok {
1027+
return fmt.Errorf(errorMessage, key)
1028+
}
1029+
1030+
case "bloom_filter_fp_chance":
1031+
materializedView.BloomFilterFpChance, ok = value.(float64)
1032+
if !ok {
1033+
return fmt.Errorf(errorMessage, key)
1034+
}
1035+
1036+
case "caching":
1037+
materializedView.Caching, ok = value.(map[string]string)
1038+
if !ok {
1039+
return fmt.Errorf(errorMessage, key)
1040+
}
1041+
1042+
case "comment":
1043+
materializedView.Comment, ok = value.(string)
1044+
if !ok {
1045+
return fmt.Errorf(errorMessage, key)
1046+
}
1047+
1048+
case "compaction":
1049+
materializedView.Compaction, ok = value.(map[string]string)
1050+
if !ok {
1051+
return fmt.Errorf(errorMessage, key)
1052+
}
1053+
1054+
case "compression":
1055+
materializedView.Compression, ok = value.(map[string]string)
1056+
if !ok {
1057+
return fmt.Errorf(errorMessage, key)
1058+
}
1059+
1060+
case "crc_check_chance":
1061+
materializedView.CrcCheckChance, ok = value.(float64)
1062+
if !ok {
1063+
return fmt.Errorf(errorMessage, key)
1064+
}
1065+
1066+
case "dclocal_read_repair_chance":
1067+
materializedView.DcLocalReadRepairChance, ok = value.(float64)
1068+
if !ok {
1069+
return fmt.Errorf(errorMessage, key)
1070+
}
1071+
1072+
case "default_time_to_live":
1073+
materializedView.DefaultTimeToLive, ok = value.(int)
1074+
if !ok {
1075+
return fmt.Errorf(errorMessage, key)
1076+
}
1077+
1078+
case "extensions":
1079+
extensions, err := extensionsBlobToStrings(value)
1080+
if err != nil {
1081+
return err
1082+
}
1083+
1084+
materializedView.Extensions = extensions
1085+
1086+
case "gc_grace_seconds":
1087+
materializedView.GcGraceSeconds, ok = value.(int)
1088+
if !ok {
1089+
return fmt.Errorf(errorMessage, key)
1090+
}
1091+
1092+
case "id":
1093+
materializedView.Id, ok = value.(UUID)
1094+
if !ok {
1095+
return fmt.Errorf(errorMessage, key)
1096+
}
1097+
1098+
case "include_all_columns":
1099+
materializedView.IncludeAllColumns, ok = value.(bool)
1100+
if !ok {
1101+
return fmt.Errorf(errorMessage, key)
1102+
}
1103+
1104+
case "max_index_interval":
1105+
materializedView.MaxIndexInterval, ok = value.(int)
1106+
if !ok {
1107+
return fmt.Errorf(errorMessage, key)
1108+
}
1109+
1110+
case "memtable_flush_period_in_ms":
1111+
materializedView.MemtableFlushPeriodInMs, ok = value.(int)
1112+
if !ok {
1113+
return fmt.Errorf(errorMessage, key)
1114+
}
1115+
1116+
case "min_index_interval":
1117+
materializedView.MinIndexInterval, ok = value.(int)
1118+
if !ok {
1119+
return fmt.Errorf(errorMessage, key)
1120+
}
1121+
1122+
case "read_repair":
1123+
materializedView.ReadRepair, ok = value.(string)
1124+
if !ok {
1125+
return fmt.Errorf(errorMessage, key)
1126+
}
1127+
1128+
case "read_repair_chance":
1129+
materializedView.ReadRepairChance, ok = value.(float64)
1130+
if !ok {
1131+
return fmt.Errorf(errorMessage, key)
1132+
}
1133+
1134+
case "speculative_retry":
1135+
materializedView.SpeculativeRetry, ok = value.(string)
1136+
if !ok {
1137+
return fmt.Errorf(errorMessage, key)
1138+
}
1139+
1140+
}
1141+
}
1142+
return nil
1143+
}
1144+
1145+
func parseSystemSchemaViews(iter *Iter) ([]MaterializedViewMetadata, error) {
1146+
var materializedViews []MaterializedViewMetadata
1147+
s, err := iter.SliceMap()
1148+
if err != nil {
1149+
return nil, err
1150+
}
1151+
1152+
for _, row := range s {
1153+
var materializedView MaterializedViewMetadata
1154+
err = materializedViewMetadataFromMap(row, &materializedView)
1155+
if err != nil {
1156+
return nil, err
1157+
}
1158+
1159+
materializedViews = append(materializedViews, materializedView)
1160+
}
1161+
1162+
return materializedViews, nil
1163+
}
1164+
9781165
func getMaterializedViewsMetadata(session *Session, keyspaceName string) ([]MaterializedViewMetadata, error) {
9791166
if !session.useSystemSchema {
9801167
return nil, nil
9811168
}
9821169
var tableName = "system_schema.views"
9831170
stmt := fmt.Sprintf(`
984-
SELECT
985-
view_name,
986-
base_table_id,
987-
base_table_name,
988-
bloom_filter_fp_chance,
989-
caching,
990-
comment,
991-
compaction,
992-
compression,
993-
crc_check_chance,
994-
dclocal_read_repair_chance,
995-
default_time_to_live,
996-
extensions,
997-
gc_grace_seconds,
998-
id,
999-
include_all_columns,
1000-
max_index_interval,
1001-
memtable_flush_period_in_ms,
1002-
min_index_interval,
1003-
read_repair_chance,
1004-
speculative_retry
1171+
SELECT *
10051172
FROM %s
10061173
WHERE keyspace_name = ?`, tableName)
10071174

10081175
var materializedViews []MaterializedViewMetadata
10091176

1010-
rows := session.control.query(stmt, keyspaceName).Scanner()
1011-
for rows.Next() {
1012-
materializedView := MaterializedViewMetadata{Keyspace: keyspaceName}
1013-
err := rows.Scan(&materializedView.Name,
1014-
&materializedView.BaseTableId,
1015-
&materializedView.baseTableName,
1016-
&materializedView.BloomFilterFpChance,
1017-
&materializedView.Caching,
1018-
&materializedView.Comment,
1019-
&materializedView.Compaction,
1020-
&materializedView.Compression,
1021-
&materializedView.CrcCheckChance,
1022-
&materializedView.DcLocalReadRepairChance,
1023-
&materializedView.DefaultTimeToLive,
1024-
&materializedView.Extensions,
1025-
&materializedView.GcGraceSeconds,
1026-
&materializedView.Id,
1027-
&materializedView.IncludeAllColumns,
1028-
&materializedView.MaxIndexInterval,
1029-
&materializedView.MemtableFlushPeriodInMs,
1030-
&materializedView.MinIndexInterval,
1031-
&materializedView.ReadRepairChance,
1032-
&materializedView.SpeculativeRetry,
1033-
)
1034-
if err != nil {
1035-
return nil, err
1036-
}
1037-
materializedViews = append(materializedViews, materializedView)
1038-
}
1177+
iter := session.control.query(stmt, keyspaceName)
10391178

1040-
if err := rows.Err(); err != nil {
1179+
materializedViews, err := parseSystemSchemaViews(iter)
1180+
if err != nil {
10411181
return nil, err
10421182
}
10431183

0 commit comments

Comments
 (0)