-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathtable_pattern.go
229 lines (214 loc) · 6.24 KB
/
table_pattern.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package backup
import (
"encoding/json"
"fmt"
"github.com/AlexAkulov/clickhouse-backup/pkg/common"
"github.com/AlexAkulov/clickhouse-backup/pkg/filesystemhelper"
"io"
"io/ioutil"
"net/url"
"os"
"path"
"path/filepath"
"sort"
"strings"
"github.com/AlexAkulov/clickhouse-backup/pkg/metadata"
)
type ListOfTables []metadata.TableMetadata
// Sort - sorting ListOfTables slice orderly by engine priority
func (lt ListOfTables) Sort(dropTable bool) {
sort.Slice(lt, func(i, j int) bool {
return getOrderByEngine(lt[i].Query, dropTable) < getOrderByEngine(lt[j].Query, dropTable)
})
}
func addTableToListIfNotExists(tables ListOfTables, table metadata.TableMetadata) ListOfTables {
for _, t := range tables {
if (t.Database == table.Database) && (t.Table == table.Table) {
return tables
}
}
return append(tables, table)
}
func getTableListByPatternLocal(metadataPath string, tablePattern string, skipTables []string, dropTable bool, partitionsFilter common.EmptyMap) (ListOfTables, error) {
result := ListOfTables{}
tablePatterns := []string{"*"}
if tablePattern != "" {
tablePatterns = strings.Split(tablePattern, ",")
}
if err := filepath.Walk(metadataPath, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !strings.HasSuffix(filePath, ".sql") &&
!strings.HasSuffix(filePath, ".json") &&
!info.Mode().IsRegular() {
return nil
}
p := filepath.ToSlash(filePath)
legacy := false
if strings.HasSuffix(p, ".sql") {
legacy = true
p = strings.TrimSuffix(p, ".sql")
} else {
p = strings.TrimSuffix(p, ".json")
}
p = strings.Trim(strings.TrimPrefix(p, metadataPath), "/")
parts := strings.Split(p, "/")
if len(parts) != 2 {
return nil
}
database, _ := url.PathUnescape(parts[0])
if IsInformationSchema(database) {
return nil
}
table, _ := url.PathUnescape(parts[1])
tableName := fmt.Sprintf("%s.%s", database, table)
shallSkipped := false
for _, skipPattern := range skipTables {
if shallSkipped, _ = filepath.Match(skipPattern, tableName); shallSkipped {
break
}
}
for _, p := range tablePatterns {
if matched, _ := filepath.Match(strings.Trim(p, " \t\r\n"), tableName); !matched || shallSkipped {
continue
}
data, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
if legacy {
result = addTableToListIfNotExists(result, metadata.TableMetadata{
Database: database,
Table: table,
Query: strings.Replace(string(data), "ATTACH", "CREATE", 1),
// Path: filePath,
})
return nil
}
var t metadata.TableMetadata
if err := json.Unmarshal(data, &t); err != nil {
return err
}
filterPartsByPartitionsFilter(t, partitionsFilter)
result = addTableToListIfNotExists(result, t)
return nil
}
return nil
}); err != nil {
return nil, err
}
result.Sort(dropTable)
return result, nil
}
func filterPartsByPartitionsFilter(tableMetadata metadata.TableMetadata, partitionsFilter common.EmptyMap) {
if len(partitionsFilter) > 0 {
for disk, parts := range tableMetadata.Parts {
for i, part := range parts {
if !filesystemhelper.IsPartInPartition(part.Name, partitionsFilter) {
parts = append(parts[:i], parts[i+1:]...)
}
}
tableMetadata.Parts[disk] = parts
}
}
}
func getTableListByPatternRemote(b *Backuper, remoteBackupMetadata *metadata.BackupMetadata, tablePattern string, skipTables []string, dropTable bool) (ListOfTables, error) {
result := ListOfTables{}
tablePatterns := []string{"*"}
if tablePattern != "" {
tablePatterns = strings.Split(tablePattern, ",")
}
metadataPath := path.Join(remoteBackupMetadata.BackupName, "metadata")
for _, t := range remoteBackupMetadata.Tables {
if IsInformationSchema(t.Database) {
continue
}
tableName := fmt.Sprintf("%s.%s", t.Database, t.Table)
shallSkipped := false
for _, skipPattern := range skipTables {
if shallSkipped, _ = filepath.Match(skipPattern, tableName); shallSkipped {
break
}
}
for _, p := range tablePatterns {
if matched, _ := filepath.Match(strings.Trim(p, " \t\r\n"), tableName); !matched || shallSkipped {
continue
}
tmReader, err := b.dst.GetFileReader(path.Join(metadataPath, common.TablePathEncode(t.Database), fmt.Sprintf("%s.json", common.TablePathEncode(t.Table))))
if err != nil {
return nil, err
}
data, err := io.ReadAll(tmReader)
if err != nil {
return nil, err
}
err = tmReader.Close()
if err != nil {
return nil, err
}
var t metadata.TableMetadata
if err = json.Unmarshal(data, &t); err != nil {
return nil, err
}
result = addTableToListIfNotExists(result, t)
break
}
}
result.Sort(dropTable)
return result, nil
}
func getOrderByEngine(query string, dropTable bool) int64 {
if strings.Contains(query, "ENGINE = Distributed") || strings.Contains(query, "ENGINE = Kafka") || strings.Contains(query, "ENGINE = RabbitMQ") {
return 4
}
if strings.HasPrefix(query, "CREATE DICTIONARY") {
return 3
}
if strings.HasPrefix(query, "CREATE VIEW") ||
strings.HasPrefix(query, "CREATE LIVE VIEW") ||
strings.HasPrefix(query, "CREATE WINDOW VIEW") ||
strings.HasPrefix(query, "ATTACH WINDOW VIEW") ||
strings.HasPrefix(query, "CREATE MATERIALIZED VIEW") ||
strings.HasPrefix(query, "ATTACH MATERIALIZED VIEW") {
if dropTable {
return 1
} else {
return 2
}
}
if strings.HasPrefix(query, "CREATE TABLE") &&
(strings.Contains(query, ".inner_id.") || strings.Contains(query, ".inner.")) {
if dropTable {
return 2
} else {
return 1
}
}
return 0
}
func parseTablePatternForDownload(tables []metadata.TableTitle, tablePattern string) []metadata.TableTitle {
tablePatterns := []string{"*"}
if tablePattern != "" {
tablePatterns = strings.Split(tablePattern, ",")
}
var result []metadata.TableTitle
for _, t := range tables {
for _, pattern := range tablePatterns {
tableName := fmt.Sprintf("%s.%s", t.Database, t.Table)
if matched, _ := filepath.Match(strings.Trim(pattern, " \t\r\n"), tableName); matched {
result = append(result, t)
break
}
}
}
return result
}
func IsInformationSchema(database string) bool {
for _, skipDatabase := range []string{"INFORMATION_SCHEMA", "information_schema"} {
if database == skipDatabase {
return true
}
}
return false
}