-
Notifications
You must be signed in to change notification settings - Fork 271
/
file.go
75 lines (65 loc) · 1.93 KB
/
file.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
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package binlog
import (
"os"
"sort"
"github.com/pingcap/tiflow/dm/pkg/log"
"github.com/pingcap/tiflow/dm/pkg/terror"
"github.com/pingcap/tiflow/dm/pkg/utils"
"go.uber.org/zap"
)
// ReadSortedBinlogFromDir reads and returns all binlog files (sorted ascending by binlog filename and sequence number).
func ReadSortedBinlogFromDir(dirpath string) ([]string, error) {
dir, err := os.Open(dirpath)
if err != nil {
return nil, terror.ErrReadDir.Delegate(err, dirpath)
}
defer dir.Close()
names, err := dir.Readdirnames(-1)
if err != nil {
return nil, terror.ErrReadDir.Delegate(err, dirpath)
}
if len(names) == 0 {
return nil, nil
}
// sorting bin.100000, ..., bin.1000000, ..., bin.999999
type tuple struct {
filename string
parsed utils.Filename
}
tmp := make([]tuple, 0, len(names)-1)
for _, f := range names {
p, err2 := utils.ParseFilename(f)
if err2 != nil {
// may contain some file that can't be parsed, like relay meta. ignore them
log.L().Info("collecting binlog file, ignore invalid file", zap.String("file", f))
continue
}
tmp = append(tmp, tuple{
filename: f,
parsed: p,
})
}
sort.Slice(tmp, func(i, j int) bool {
if tmp[i].parsed.BaseName != tmp[j].parsed.BaseName {
return tmp[i].parsed.BaseName < tmp[j].parsed.BaseName
}
return tmp[i].parsed.LessThan(tmp[j].parsed)
})
ret := make([]string, len(tmp))
for i := range tmp {
ret[i] = tmp[i].filename
}
return ret, nil
}