优化 逻辑,时间复杂度

This commit is contained in:
dengsgo 2019-04-01 16:18:01 +08:00
parent 2eb994561d
commit 8d5c11d20d
2 changed files with 33 additions and 24 deletions

View File

@ -49,8 +49,11 @@ func parseConfig() {
if cfg.Core.Version > Version { if cfg.Core.Version > Version {
log.Panicln(PreError, "current fileboy support max version : ", Version) log.Panicln(PreError, "current fileboy support max version : ", Version)
} }
// types convert map // init map
cfg.Monitor.TypesMap = map[string]bool{} cfg.Monitor.TypesMap = map[string]bool{}
cfg.Monitor.IncludeDirsMap = map[string]bool{}
cfg.Monitor.ExceptDirsMap = map[string]bool{}
// convert to map
for _, v := range cfg.Monitor.Types { for _, v := range cfg.Monitor.Types {
cfg.Monitor.TypesMap[v] = true cfg.Monitor.TypesMap[v] = true
} }
@ -81,47 +84,50 @@ func eventDispatcher(event fsnotify.Event) {
func addWatcher() { func addWatcher() {
log.Println("collecting directory information...") log.Println("collecting directory information...")
dirs := make([]string, 0) dirsMap := map[string]bool{}
for i := 0; i < len(cfg.Monitor.IncludeDirs); i++ { for _, dir := range cfg.Monitor.IncludeDirs {
darr := dirParse2Array(cfg.Monitor.IncludeDirs[i]) darr := dirParse2Array(dir)
if len(darr) < 1 || len(darr) > 2 { if len(darr) < 1 || len(darr) > 2 {
log.Fatalln(PreError, "filegirl section monitor dirs is error. ", cfg.Monitor.IncludeDirs[i]) log.Fatalln(PreError, "filegirl section monitor dirs is error. ", dir)
} }
if strings.HasPrefix(darr[0], "/") { if strings.HasPrefix(darr[0], "/") {
log.Fatalln(PreError, "dirs must be relative paths ! err path:", cfg.Monitor.IncludeDirs[i]) log.Fatalln(PreError, "dirs must be relative paths ! err path:", dir)
} }
if darr[0] == "." { if darr[0] == "." {
if len(darr) == 2 && darr[1] == "*" { if len(darr) == 2 && darr[1] == "*" {
dirs = make([]string, 0) // The highest priority
dirs = append(dirs, ".") dirsMap = map[string]bool{
projectFolder: true,
}
listFile(projectFolder, func(d string) { listFile(projectFolder, func(d string) {
dirs = arrayUniqueAdd(dirs, d) dirsMap[d] = true
}) })
break
} else { } else {
dirs = arrayUniqueAdd(dirs, projectFolder) dirsMap[projectFolder] = true
} }
break
} else { } else {
md := projectFolder + "/" + darr[0] md := projectFolder + "/" + darr[0]
dirsMap[md] = true
if len(darr) == 2 && darr[1] == "*" { if len(darr) == 2 && darr[1] == "*" {
dirs = arrayUniqueAdd(dirs, md)
listFile(md, func(d string) { listFile(md, func(d string) {
dirs = arrayUniqueAdd(dirs, d) dirsMap[d] = true
}) })
} else {
dirs = arrayUniqueAdd(dirs, md)
} }
} }
} }
for i := 0; i < len(cfg.Monitor.ExceptDirs); i++ { for _, dir := range cfg.Monitor.ExceptDirs {
p := projectFolder + "/" + cfg.Monitor.ExceptDirs[i] if dir == "." {
dirs = arrayRemoveElement(dirs, p) log.Fatalln(PreError, "exceptDirs must is not project root path ! err path:", dir)
}
p := projectFolder + "/" + dir
delete(dirsMap, p)
listFile(p, func(d string) { listFile(p, func(d string) {
dirs = arrayRemoveElement(dirs, d) delete(dirsMap, d)
}) })
} }
for _, dir := range dirs { for dir := range dirsMap {
log.Println("watcher add -> ", dir) log.Println("watcher add -> ", dir)
err := watcher.Add(dir) err := watcher.Add(dir)
if err != nil { if err != nil {

View File

@ -5,10 +5,13 @@ type FileGirl struct {
Version int `yaml:"version"` Version int `yaml:"version"`
} }
Monitor struct { Monitor struct {
Types []string `yaml:"types"` Types []string `yaml:"types"`
TypesMap map[string]bool `yaml:"-"` IncludeDirs []string `yaml:"includeDirs"`
IncludeDirs []string `yaml:"includeDirs"` ExceptDirs []string `yaml:"exceptDirs"`
ExceptDirs []string `yaml:"exceptDirs"` // convert to
TypesMap map[string]bool `yaml:"-"`
IncludeDirsMap map[string]bool `yaml:"-"`
ExceptDirsMap map[string]bool `yaml:"-"`
} }
Command struct { Command struct {
Exec []string `yaml:"exec"` Exec []string `yaml:"exec"`