feat(cmd): index

Use goroutines
goroutine
sundowndev 2020-04-14 16:22:34 +01:00
parent 6ff6cd13d0
commit c7a9de4191
6 changed files with 29 additions and 12 deletions

View File

@ -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)
}
}

View File

@ -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()
},
}

View File

@ -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
}

View File

@ -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")

View File

@ -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++

View File

@ -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) {