-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.go
More file actions
242 lines (220 loc) · 5.25 KB
/
generate.go
File metadata and controls
242 lines (220 loc) · 5.25 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"fmt"
"io/fs"
"log"
"path/filepath"
"github.com/codingsince1985/checksum"
"github.com/gammazero/workerpool"
"github.com/pkg/errors"
bolt "go.etcd.io/bbolt"
)
var writeCacheSize = 100
func generate(config *Config) {
db, err := bolt.Open(config.Db, 0600, nil)
if err != nil {
log.Printf("can't open db %s with error: %v", config.Db, err)
return
}
defer db.Close()
createBuckets(db, config)
checksum := config.Checksum
if checksum {
log.Print("check file checksum started")
checkAndSave(db, config)
} else {
log.Print("check file size started")
saveSize(db, config)
}
}
func createBuckets(db *bolt.DB, config *Config) error {
return db.Update(func(tx *bolt.Tx) error {
for k := range config.Dirs {
_, err := tx.CreateBucketIfNotExists([]byte(k))
if err != nil {
return err
}
}
return nil
})
}
func checkAndSave(db *bolt.DB, config *Config) error {
workers := config.Workers
checksumChanIn := make(chan Record, workers)
checksumChanOut := make(chan Record)
checksumChanFinish := make(chan int)
calculateChecksum(&checksumChanIn, workers, &checksumChanOut, &checksumChanFinish)
writeChanFinish := make(chan int)
writeRecord(db, &checksumChanOut, &writeChanFinish)
for bucket, rootDir := range config.Dirs {
filepath.Walk(rootDir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
log.Printf("loop dir error: %v", err)
return nil
}
if !info.IsDir() {
relpath, err := filepath.Rel(rootDir, path)
if err != nil {
log.Printf("calculate relpath error: %v", err)
return nil
}
if !config.FirstRun {
if isRecordExists(db, bucket, relpath) {
return nil
}
}
record := Record{
Bucket: bucket,
Path: relpath,
AbsPath: path,
}
checksumChanIn <- record
}
return nil
})
}
close(checksumChanIn)
<-checksumChanFinish
close(checksumChanOut)
<-writeChanFinish
return nil
}
const gb = 1073741824
func saveSize(db *bolt.DB, config *Config) error {
var logSize int64 = config.Logsize * gb
var currLogSize = logSize
var processedGB = 0
logProcess := func(size int64) {
currLogSize = currLogSize - size
for currLogSize < 0 {
processedGB = processedGB + int(config.Logsize)
currLogSize = currLogSize + logSize
log.Printf("%dGB of data processed", processedGB)
}
}
cache := make([]Record, 0, writeCacheSize)
writeR := func(record Record) {
if len(cache) < writeCacheSize {
cache = append(cache, record)
}
if len(cache) == writeCacheSize {
err := batchInsert(db, cache)
if err != nil {
log.Printf("error when insert records, error is %v", err)
}
cache = make([]Record, 0, writeCacheSize)
}
}
for bucket, rootDir := range config.Dirs {
filepath.Walk(rootDir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
log.Printf("loop dir error: %v", err)
return nil
}
if !info.IsDir() {
relpath, err := filepath.Rel(rootDir, path)
if err != nil {
log.Printf("calculate relpath error: %v", err)
return nil
}
if !config.FirstRun {
if isRecordExists(db, bucket, relpath) {
return nil
}
}
size := info.Size()
logProcess(size)
record := Record{
Bucket: bucket,
Path: relpath,
AbsPath: path,
Checksum: fmt.Sprintf("%d", size),
}
writeR(record)
}
return nil
})
}
if len(cache) > 0 {
err := batchInsert(db, cache)
if err != nil {
log.Printf("error when insert records, error is %v", err)
}
}
return nil
}
func isRecordExists(db *bolt.DB, bucket string, path string) bool {
exists := false
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
if b == nil {
return nil
}
data := b.Get([]byte(path))
if data != nil {
exists = true
}
return nil
})
return exists
}
func calculateChecksum(chanIn *chan Record, workers int, chanOut *chan Record, chanFinish *chan int) {
go func() {
wp := workerpool.New(workers)
for r := range *chanIn {
record := r
wp.Submit(func() {
abspath := record.AbsPath
md5, err := checksum.MD5sum(abspath)
if err != nil {
log.Printf("error in calculation checksum: %v", err)
return
}
record.Checksum = md5
*chanOut <- record
})
}
wp.StopWait()
*chanFinish <- 0
}()
}
func writeRecord(db *bolt.DB, chanIn *chan Record, chanFinish *chan int) {
go func() {
cache := make([]Record, 0, writeCacheSize)
for r := range *chanIn {
if len(cache) < writeCacheSize {
cache = append(cache, r)
}
if len(cache) == writeCacheSize {
err := batchInsert(db, cache)
if err != nil {
log.Printf("error when insert records, error is %v", err)
}
cache = make([]Record, 0, writeCacheSize)
}
}
if len(cache) > 0 {
err := batchInsert(db, cache)
if err != nil {
log.Printf("error when insert records, error is %v", err)
}
}
*chanFinish <- 0
}()
}
func batchInsert(db *bolt.DB, records []Record) error {
err := db.Update(func(tx *bolt.Tx) error {
for _, cr := range records {
bucket := tx.Bucket([]byte(cr.Bucket))
if bucket == nil {
return errors.New(fmt.Sprintf("bucket %s is nil", cr.Bucket))
}
err := bucket.Put([]byte(cr.Path), []byte(cr.Checksum))
if err != nil {
return err
}
}
return nil
})
return err
}