github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/dtables/binlog_table.go (about) 1 // Copyright 2022 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package dtables 16 17 import ( 18 "github.com/dolthub/go-mysql-server/sql" 19 "github.com/dolthub/go-mysql-server/sql/types" 20 "github.com/dolthub/vitess/go/sqltypes" 21 22 "github.com/dolthub/dolt/go/libraries/doltcore/branch_control" 23 24 "github.com/dolthub/dolt/go/libraries/doltcore/sqle/index" 25 ) 26 27 const ( 28 AccessBinlogTableName = AccessTableName + "_binlog" 29 NamespaceBinlogTableName = NamespaceTableName + "_binlog" 30 ) 31 32 // accessBinlogSchema is the schema for the "dolt_branch_control_binlog" table. 33 var accessBinlogSchema = sql.Schema{ 34 &sql.Column{ 35 Name: "index", 36 Type: types.Int64, 37 Source: AccessBinlogTableName, 38 PrimaryKey: true, 39 }, 40 &sql.Column{ 41 Name: "operation", 42 Type: types.MustCreateEnumType([]string{"insert", "delete"}, sql.Collation_utf8mb4_0900_bin), 43 Source: AccessBinlogTableName, 44 PrimaryKey: false, 45 }, 46 &sql.Column{ 47 Name: "branch", 48 Type: types.MustCreateString(sqltypes.VarChar, 16383, sql.Collation_utf8mb4_0900_ai_ci), 49 Source: AccessBinlogTableName, 50 PrimaryKey: false, 51 }, 52 &sql.Column{ 53 Name: "user", 54 Type: types.MustCreateString(sqltypes.VarChar, 16383, sql.Collation_utf8mb4_0900_bin), 55 Source: AccessBinlogTableName, 56 PrimaryKey: false, 57 }, 58 &sql.Column{ 59 Name: "host", 60 Type: types.MustCreateString(sqltypes.VarChar, 16383, sql.Collation_utf8mb4_0900_ai_ci), 61 Source: AccessBinlogTableName, 62 PrimaryKey: false, 63 }, 64 &sql.Column{ 65 Name: "permissions", 66 Type: types.MustCreateSetType(PermissionsStrings, sql.Collation_utf8mb4_0900_ai_ci), 67 Source: AccessBinlogTableName, 68 PrimaryKey: false, 69 }, 70 } 71 72 // namespaceBinlogSchema is the schema for the "dolt_branch_namespace_control_binlog" table. 73 var namespaceBinlogSchema = sql.Schema{ 74 &sql.Column{ 75 Name: "index", 76 Type: types.Int64, 77 Source: NamespaceBinlogTableName, 78 PrimaryKey: true, 79 }, 80 &sql.Column{ 81 Name: "operation", 82 Type: types.MustCreateEnumType([]string{"insert", "delete"}, sql.Collation_utf8mb4_0900_bin), 83 Source: NamespaceBinlogTableName, 84 PrimaryKey: false, 85 }, 86 &sql.Column{ 87 Name: "branch", 88 Type: types.MustCreateString(sqltypes.VarChar, 16383, sql.Collation_utf8mb4_0900_ai_ci), 89 Source: NamespaceBinlogTableName, 90 PrimaryKey: false, 91 }, 92 &sql.Column{ 93 Name: "user", 94 Type: types.MustCreateString(sqltypes.VarChar, 16383, sql.Collation_utf8mb4_0900_bin), 95 Source: NamespaceBinlogTableName, 96 PrimaryKey: false, 97 }, 98 &sql.Column{ 99 Name: "host", 100 Type: types.MustCreateString(sqltypes.VarChar, 16383, sql.Collation_utf8mb4_0900_ai_ci), 101 Source: NamespaceBinlogTableName, 102 PrimaryKey: false, 103 }, 104 } 105 106 // BinlogTable provides a queryable view over the Binlog. 107 type BinlogTable struct { 108 Log *branch_control.Binlog 109 IsAccess bool 110 } 111 112 var _ sql.Table = BinlogTable{} 113 114 // Name implements the interface sql.Table. 115 func (b BinlogTable) Name() string { 116 if b.IsAccess { 117 return AccessBinlogTableName 118 } else { 119 return NamespaceBinlogTableName 120 } 121 } 122 123 // String implements the interface sql.Table. 124 func (b BinlogTable) String() string { 125 if b.IsAccess { 126 return AccessBinlogTableName 127 } else { 128 return NamespaceBinlogTableName 129 } 130 } 131 132 // Schema implements the interface sql.Table. 133 func (b BinlogTable) Schema() sql.Schema { 134 if b.IsAccess { 135 return accessBinlogSchema 136 } else { 137 return namespaceBinlogSchema 138 } 139 } 140 141 // Collation implements the interface sql.Table. 142 func (b BinlogTable) Collation() sql.CollationID { 143 return sql.Collation_Default 144 } 145 146 // Partitions implements the interface sql.Table. 147 func (b BinlogTable) Partitions(context *sql.Context) (sql.PartitionIter, error) { 148 return index.SinglePartitionIterFromNomsMap(nil), nil 149 } 150 151 // PartitionRows implements the interface sql.Table. 152 func (b BinlogTable) PartitionRows(context *sql.Context, partition sql.Partition) (sql.RowIter, error) { 153 b.Log.RWMutex.RLock() 154 defer b.Log.RWMutex.RUnlock() 155 156 binlogRows := b.Log.Rows() 157 rows := make([]sql.Row, len(binlogRows)) 158 for i := 0; i < len(binlogRows); i++ { 159 logRow := binlogRows[i] 160 operation := uint16(1) 161 if !logRow.IsInsert { 162 operation = 2 163 } 164 165 if b.IsAccess { 166 rows[i] = sql.Row{ 167 int64(i), 168 operation, 169 logRow.Branch, 170 logRow.User, 171 logRow.Host, 172 logRow.Permissions, 173 } 174 } else { 175 rows[i] = sql.Row{ 176 int64(i), 177 operation, 178 logRow.Branch, 179 logRow.User, 180 logRow.Host, 181 } 182 } 183 } 184 return sql.RowsToRowIter(rows...), nil 185 }