Serialize everything

main
Mari 4 years ago
parent c7a91fa9ae
commit 72929d5636
  1. 81
      drive-demuxer.go

@ -3,25 +3,21 @@ package main
import ( import (
"bytes" "bytes"
"flag" "flag"
"github.com/cheggaaa/pb/v3"
"io" "io"
"log" "log"
"os" "os"
"path" "path"
"sync"
"github.com/cheggaaa/pb/v3"
) )
type TopLevel struct { type TopLevel struct {
LeftInput string LeftInput string
RightInput string RightInput string
LeftOutput string LeftOutput string
CombinedOutput string CombinedOutput string
RightOutput string RightOutput string
OpenHandles chan int DryRun bool
OpenDirectories chan int Bar *pb.ProgressBar
DryRun bool
Bar *pb.ProgressBar
} }
type Step struct { type Step struct {
@ -191,10 +187,6 @@ func (s *Step) RemoveInputDirs(child string) {
} }
func (s *Step) ListChildren() []string { func (s *Step) ListChildren() []string {
s.OpenHandles <- 1
defer func() {
<-s.OpenHandles
}()
leftInPath := s.InputPath("", LEFT) leftInPath := s.InputPath("", LEFT)
rightInPath := s.InputPath("", RIGHT) rightInPath := s.InputPath("", RIGHT)
results := make(map[string]bool) results := make(map[string]bool)
@ -220,10 +212,6 @@ func (s *Step) ListChildren() []string {
} }
func (s *Step) AreFilesIdentical(child string) bool { func (s *Step) AreFilesIdentical(child string) bool {
s.OpenHandles <- 1
defer func() {
<-s.OpenHandles
}()
leftInPath := s.InputPath(child, LEFT) leftInPath := s.InputPath(child, LEFT)
rightInPath := s.InputPath(child, RIGHT) rightInPath := s.InputPath(child, RIGHT)
leftInfo, err := os.Stat(leftInPath) leftInfo, err := os.Stat(leftInPath)
@ -290,8 +278,6 @@ func (s *Step) AreFilesIdentical(child string) bool {
} }
func (s *Step) Walk() { func (s *Step) Walk() {
wg := sync.WaitGroup{}
children := s.ListChildren() children := s.ListChildren()
s.Bar.AddTotal(int64(len(children))) s.Bar.AddTotal(int64(len(children)))
for _, child := range children { for _, child := range children {
@ -330,43 +316,28 @@ func (s *Step) Walk() {
switch rightState { switch rightState {
case FILE: case FILE:
if leftState == FILE { if leftState == FILE {
wg.Add(1) if s.AreFilesIdentical(child) {
go func(child string) { s.Combine(child)
defer func() { s.Bar.Increment()
wg.Done() } else {
}() s.Separate(child)
if s.AreFilesIdentical(child) { s.Bar.Increment()
s.Combine(child) }
s.Bar.Increment()
} else {
s.Separate(child)
s.Bar.Increment()
}
}(child)
} else { } else {
s.Separate(child) s.Separate(child)
s.Bar.Increment() s.Bar.Increment()
} }
case DIRECTORY: case DIRECTORY:
if leftState == DIRECTORY { if leftState == DIRECTORY {
s.OpenDirectories <- 1 substep := Step{
wg.Add(1) TopLevel: s.TopLevel,
go func(child string) { Subpath: path.Join(s.Subpath, child),
defer func() { }
wg.Done() s.MakeCombinedDir(child)
<-s.OpenDirectories substep.Walk()
}() s.RemoveInputDirs(child)
substep := Step{ s.Bar.Increment()
TopLevel: s.TopLevel,
Subpath: path.Join(s.Subpath, child),
}
s.MakeCombinedDir(child)
substep.Walk()
s.RemoveInputDirs(child)
s.Bar.Increment()
}(child)
} else { } else {
wg.Add(1)
s.Separate(child) s.Separate(child)
s.Bar.Increment() s.Bar.Increment()
} }
@ -374,15 +345,11 @@ func (s *Step) Walk() {
panic("Unexpected state") panic("Unexpected state")
} }
} }
wg.Wait()
} }
func main() { func main() {
settings := TopLevel{ settings := TopLevel{
OpenHandles: make(chan int, 450), Bar: pb.StartNew(1),
OpenDirectories: make(chan int, 1000),
Bar: pb.StartNew(1),
} }
flag.StringVar(&settings.LeftInput, flag.StringVar(&settings.LeftInput,
"left-input", "./input/left", "The name of the left side of the input.") "left-input", "./input/left", "The name of the left side of the input.")

Loading…
Cancel
Save