From bc9cfcde08714da0856f47fa1199894bcaa7c1ca Mon Sep 17 00:00:00 2001 From: Mason Dye <108563824+MasonDye@users.noreply.github.com> Date: Fri, 22 Mar 2024 07:33:37 +0800 Subject: [PATCH] V1.0.0 --- Hpkse-1.0.0.go | 103 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 Hpkse-1.0.0.go diff --git a/Hpkse-1.0.0.go b/Hpkse-1.0.0.go new file mode 100644 index 0000000..648f525 --- /dev/null +++ b/Hpkse-1.0.0.go @@ -0,0 +1,103 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "os" + "path/filepath" + "regexp" + "sync" +) + +func searchInFile(filepath string, query string, wg *sync.WaitGroup, ch chan<- bool) { + defer wg.Done() + file, err := os.Open(filepath) + if err != nil { + fmt.Printf("Error opening file %s: %v\n", filepath, err) + return + } + defer file.Close() + + reader := bufio.NewReaderSize(file, 1024*1024) // 1MB 缓冲区大小 + + found := false + for { + line, err := reader.ReadString('\n') + if err != nil { + if err != io.EOF { + fmt.Printf("Error reading file %s: %v\n", filepath, err) + } + break + } + if regexp.MustCompile(regexp.QuoteMeta(query)).MatchString(line) { + fmt.Printf("%s: %s\n", filepath, line) + found = true + } + } + + if found { + ch <- true + } +} + +func searchInDirectory(directory string, query string) { + var wg sync.WaitGroup + ch := make(chan bool) + + filepath.Walk(directory, func(path string, info os.FileInfo, err error) error { + if err != nil { + fmt.Printf("Error accessing path %s: %v\n", path, err) + return nil + } + if !info.IsDir() && filepath.Ext(path) == ".txt" { + wg.Add(1) + go searchInFile(path, query, &wg, ch) + } + return nil + }) + + go func() { + wg.Wait() + close(ch) + }() + + found := false + for range ch { + found = true + } + + if !found { + fmt.Println("Not found keyword") + } +} + +func main() { + fmt.Println("Hpkse High performance keyword search engine") + fmt.Println("Version : 1.0.0") + fmt.Println("Author by MasonDye\n") + + var queryDirectory string + + if len(os.Args) > 1 && os.Args[1] == "-data" { + if len(os.Args) > 2 { + queryDirectory = os.Args[2] + } else { + fmt.Println("Folder not provided") + return + } + } else { + fmt.Print("Search folder (relative path): ") + fmt.Scanln(&queryDirectory) + } + + var query string + fmt.Print("Search keyword: ") + fmt.Scanln(&query) + + if fileInfo, err := os.Stat(queryDirectory); err == nil && fileInfo.IsDir() { + searchInDirectory(queryDirectory, query) + } else { + fmt.Println("input path is not a folder.") + } +}