diff --git a/cmd/dump.go b/cmd/dump.go index 5278271..0193989 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -52,7 +52,7 @@ var dumpCmd = &cobra.Command{ results[word] = make(map[string]int) } - results[word][file] = engine.CountWord(fileContent, word) + results[word][file] = engine.CountWordInText(fileContent, word) } } diff --git a/cmd/index.go b/cmd/index.go index 5826dfb..cb0bf8a 100644 --- a/cmd/index.go +++ b/cmd/index.go @@ -4,6 +4,7 @@ import ( "fmt" "io/ioutil" "os" + "sync" "github.com/spf13/cobra" "github.com/sundowndev/go-search/engine" @@ -30,7 +31,12 @@ var indexCmd = &cobra.Command{ fmt.Printf("Walking %v...\n", path) - for _, file := range engine.GetFilesFromDir(path) { + files := engine.GetFilesFromDir(path) + + var wg sync.WaitGroup + wg.Add(len(files) - 1) + + for _, file := range files { // Open File f, err := ioutil.ReadFile(file) if err != nil { @@ -43,9 +49,11 @@ var indexCmd = &cobra.Command{ content := string(f) - client.AddFile(file, content) + go client.AddFile(file, content, &wg) fmt.Println("Successfully indexed file", file) } + + wg.Wait() }, } diff --git a/engine/redis.go b/engine/redis.go index d5b89c8..575b95d 100644 --- a/engine/redis.go +++ b/engine/redis.go @@ -2,6 +2,7 @@ package engine import ( "strings" + "sync" redis "github.com/go-redis/redis/v7" ) @@ -29,11 +30,13 @@ func NewRedisClient(addr, port, password string, db int) (*RedisClient, error) { } // AddFile index a file -func (c *RedisClient) AddFile(file, content string) error { - for _, v := range GetWordsFromText(content) { +func (c *RedisClient) AddFile(file, content string, wg *sync.WaitGroup) error { + defer wg.Done() + + for _, word := range GetWordsFromText(content) { if err := c.conn.ZAdd(file, &redis.Z{ - Score: float64(CountWord(content, v)), - Member: strings.ToLower(v), + Score: float64(CountWordInText(content, word)), + Member: strings.ToLower(word), }).Err(); err != nil { return err } diff --git a/engine/redis_test.go b/engine/redis_test.go index ab407b7..1d23052 100644 --- a/engine/redis_test.go +++ b/engine/redis_test.go @@ -2,6 +2,7 @@ package engine import ( "log" + "sync" "testing" "github.com/go-redis/redis/v7" @@ -36,12 +37,17 @@ func TestRedisClient(t *testing.T) { t.Run("should use ZADD", func(t *testing.T) { client.FlushAll() - err := redisClient.AddFile("file1", "word") + var wg sync.WaitGroup + wg.Add(2) + + err := redisClient.AddFile("file1", "word", &wg) assert.Equal(nil, err, "should be equal") - err = redisClient.AddFile("file2", "word") + err = redisClient.AddFile("file2", "word", &wg) assert.Equal(nil, err, "should be equal") + wg.Wait() + values, err2 := client.ZRevRange("file1", 0, -1).Result() assert.Equal(nil, err2, "should be equal") diff --git a/engine/text.go b/engine/text.go index e948790..ae86a80 100644 --- a/engine/text.go +++ b/engine/text.go @@ -8,9 +8,9 @@ import ( "strings" ) -// CountWord returns the number of +// CountWordInText returns the number of // occurence for a word in a given text. -func CountWord(text, word string) (count int) { +func CountWordInText(text, word string) (count int) { for _, v := range GetWordsFromText(text) { if v == word { count++ diff --git a/engine/text_test.go b/engine/text_test.go index db357ae..38b4c57 100644 --- a/engine/text_test.go +++ b/engine/text_test.go @@ -12,7 +12,7 @@ func TestText(t *testing.T) { t.Run("CountWord", func(t *testing.T) { text := "Contrairement à une opinion répandue, le Lorem Ipsum n'est pas simplement du texte aléatoire.\nIl trouve ses racines dans une oeuvre de la littérature latine classique datant de 45 av. J.-C., le rendant vieux de 2000 ans.\n\nUn professeur du Hampden-Sydney College, en Virginie, s'est intéressé à un des mots latins les plus obscurs, consectetur, extrait d'un passage du Lorem Ipsum, et en étudiant tous les usages de ce mot dans la littérature classique, découvrit la source incontestable du Lorem Ipsum." - assert.Equal(3, CountWord(text, "la"), "should be equal") + assert.Equal(3, CountWordInText(text, "la"), "should be equal") }) t.Run("GetWordsFromText", func(t *testing.T) {