From 7d0e7ec8e7048c5f74217647d3c820c25f75dbf5 Mon Sep 17 00:00:00 2001 From: tanganyu <569386693@qq.com> Date: Tue, 29 Dec 2020 14:26:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9write=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=9A=84goroutine=E6=A8=A1=E5=BC=8F=EF=BC=8C=E5=B0=9D=E8=AF=95?= =?UTF-8?q?=E6=8F=90=E9=AB=98=E5=A4=A7=E6=89=B9=E9=87=8F=E5=8D=95=E4=B8=AA?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E6=96=87=E4=BB=B6=E7=9A=84=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/model.go | 184 ------------------------------------------------- write/write.go | 57 +++++++-------- 2 files changed, 29 insertions(+), 212 deletions(-) diff --git a/model/model.go b/model/model.go index bb77eab..287cd33 100644 --- a/model/model.go +++ b/model/model.go @@ -148,187 +148,3 @@ func NewLink(stat bool, src, dist string) *link { dist: dist, } } - -/*type fileMap map[string]struct { - files []string - size int64 -} - -type FileMaps interface { - Select(hash string) bool - Insert(hash, file string, size int) -} - -func (f fileMap) Select(hash string) bool { - _, ok := f[hash] - return ok -} - -func (f fileMap) Insert(record Record) { - if record.Select().Stats { - f[record.Select().Hash] = struct { - files []string - size int64 - }{ - files: append(f[record.Select().Hash].files, record.Select().File), - size: int64(record.Select().Size)} - } -} - -type PathResult interface { - AddTotalNum() - AddTotalSize(size int64) - AddErrInfo(path string) -} - -type pathResult struct { - totalNum int - totalSize int64 - errNum int - errPath []string -} - -func NewPathResult() *pathResult { - return &pathResult{ - totalNum: 0, - totalSize: 0, - errNum: 0, - errPath: nil, - } -} - -func (p pathResult) AddTotalSize(size int64) { - p.totalSize += size -} - -func (p pathResult) AddTotalNum() { - p.totalNum += 1 -} - -func (p pathResult) AddErrInfo(path string) { - p.errNum += 1 - p.errPath = append(p.errPath, path) -} - -type Record interface { - Select() *record -} - -type record struct { - Stats bool - Hash string - File string - Size int -} - -func NewRecord(stats bool, hash, file string, size int) *record { - return &record{ - Stats: stats, - Hash: hash, - File: file, - Size: size, - } -} - -func (r record) Select() *record { - return &r -} - -type Result interface { - Insert(record Record) - Select() (totalSize int64, totalNum int, errNum int, errFile []string) -} - -type path struct { - totalNum int - errNum int - errPath []string -} - -type read struct { - totalSize int - totalNum int - errNum int - errFile []string -} - -func NewRead() *read { - return &read{ - totalSize: 0, - totalNum: 0, - errNum: 0, - errFile: make([]string, 0), - } -} - -type write struct { - totalSize int - totalNum int - errNum int - errFile []string -} - -func NewWrite() *write { - return &write{ - totalSize: 0, - totalNum: 0, - errNum: 0, - errFile: make([]string, 0), - } -} - -type link struct { - totalSize int - totalNum int - errNum int - errFile []string -} - -func NewLink() *link { - return &link{ - totalSize: 0, - totalNum: 0, - errNum: 0, - errFile: make([]string, 0), - } -} - -func (r read) Select() (totalSize int, totalNum int, errNum int, errFile []string) { - return r.totalSize, r.totalNum, r.errNum, r.errFile -} - -func (r write) Select() (totalSize int, totalNum int, errNum int, errFile []string) { - return r.totalSize, r.totalNum, r.errNum, r.errFile -} - -func (r link) Select() (totalSize int, totalNum int, errNum int, errFile []string) { - return r.totalSize, r.totalNum, r.errNum, r.errFile -} - -func (r read) Insert(record Record) { - r.totalNum += 1 - r.totalSize += record.Select().Size - if !record.Select().Stats { - r.errNum += 1 - r.errFile = append(r.errFile, record.Select().File) - } -} - -func (r write) Insert(record Record) { - r.totalNum += 1 - r.totalSize += record.Select().Size - if !record.Select().Stats { - r.errNum += 1 - r.errFile = append(r.errFile, record.Select().File) - } -} - -func (r link) Insert(record Record) { - r.totalNum += 1 - r.totalSize += record.Select().Size - if !record.Select().Stats { - r.errNum += 1 - r.errFile = append(r.errFile, record.Select().File) - } -} -*/ diff --git a/write/write.go b/write/write.go index 3ac034d..0f40d5d 100644 --- a/write/write.go +++ b/write/write.go @@ -40,19 +40,19 @@ func Write(dm string) { func dryRunFile() { wg := sync.WaitGroup{} for hash, files := range model.FileMap { - wg.Add(1) - model.ControlCH <- 1 - go func(hash string, files []string) { - defer wg.Done() - defer func() { <-model.ControlCH }() - if len(files) > 1 { - for _, file := range files[1:] { + if len(files) > 1 { + for _, file := range files[1:] { + wg.Add(1) + model.ControlCH <- 1 + go func(hash string, file string) { + defer wg.Done() + defer func() { <-model.ControlCH }() fmt.Printf("[INFO]: Duplicate File: %s\n", file) rd := model.NewWrite(true, hash, file, model.FileSize[hash]) model.RecordCH <- rd - } + }(hash, file) } - }(hash, files) + } } wg.Wait() } @@ -60,13 +60,13 @@ func dryRunFile() { func removeFile() { wg := sync.WaitGroup{} for hash, files := range model.FileMap { - wg.Add(1) - model.ControlCH <- 1 - go func(hash string, files []string) { - defer wg.Done() - defer func() { <-model.ControlCH }() - if len(files) > 1 { - for _, file := range files[1:] { + if len(files) > 1 { + for _, file := range files[1:] { + wg.Add(1) + model.ControlCH <- 1 + go func(hash string, file string) { + defer wg.Done() + defer func() { <-model.ControlCH }() var stat = true err := os.Remove(file) if err != nil { @@ -77,10 +77,9 @@ func removeFile() { } rd := model.NewWrite(stat, hash, file, model.FileSize[hash]) model.RecordCH <- rd - } + }(hash, file) } - - }(hash, files) + } } wg.Wait() } @@ -88,13 +87,14 @@ func removeFile() { func linkFile() { wg := sync.WaitGroup{} for hash, files := range model.FileMap { - wg.Add(1) - model.ControlCH <- 1 - go func(hash string, files []string) { - defer wg.Done() - defer func() { <-model.ControlCH }() - if len(files) > 1 { - for _, file := range files[1:] { + if len(files) > 1 { + for _, file := range files[1:] { + wg.Add(1) + model.ControlCH <- 1 + go func(hash string, file string) { + defer wg.Done() + defer func() { <-model.ControlCH }() + var stat = true err := os.Link(files[0], file) if err != nil { @@ -105,9 +105,10 @@ func linkFile() { } rd := model.NewLink(stat, files[0], file) model.RecordCH <- rd - } + + }(hash, file) } - }(hash, files) + } } wg.Wait() }