diff --git a/cmd/drop.go b/cmd/drop.go index 1227609..1231b13 100644 --- a/cmd/drop.go +++ b/cmd/drop.go @@ -26,7 +26,7 @@ var dropCmd = &cobra.Command{ fmt.Println("Dropping database...") - err = client.FlushAll() + err = engine.FlushAll(client) if err != nil { panic(err) } diff --git a/cmd/dump.go b/cmd/dump.go index b6a3e1e..1281837 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -29,7 +29,7 @@ var dumpCmd = &cobra.Command{ var results = make(map[string]map[string]int) - files, err := client.GetFiles() + files, err := engine.GetFiles(client) if err != nil { log.Fatalf("error: %v", err) } diff --git a/cmd/index.go b/cmd/index.go index fe44980..d4301ac 100644 --- a/cmd/index.go +++ b/cmd/index.go @@ -24,7 +24,7 @@ var indexCmd = &cobra.Command{ fmt.Println("Failed to connect to database", redisAddr, redisPort) os.Exit(1) } - defer client.Close() + defer engine.Close(client) path := args[0] @@ -48,7 +48,7 @@ var indexCmd = &cobra.Command{ content := string(f) - err = client.AddFile(file, content) + err = engine.AddFile(client, file, content) if err != nil { panic(err) } diff --git a/cmd/query.go b/cmd/query.go index 5fa19dd..aab6926 100644 --- a/cmd/query.go +++ b/cmd/query.go @@ -31,7 +31,7 @@ var queryCmd = &cobra.Command{ fmt.Printf("Querying index for \"%s\":\n\n", word) - files, err := client.GetFiles() + files, err := engine.GetFiles(client) if err != nil { log.Fatal(err) } @@ -39,7 +39,7 @@ var queryCmd = &cobra.Command{ var queryResults []*engine.QueryResult for _, file := range files { - score := client.GetWordScoreFromFile(file, word) + score := engine.GetWordScoreFromFile(client, file, word) // If score is 0 then ignore it if score == 0 { @@ -60,6 +60,8 @@ var queryCmd = &cobra.Command{ }) } - engine.ShowResults(engine.SortResultsByScore(queryResults)) + sorted := engine.SortResultsByScore(queryResults) + + engine.ShowResults(sorted) }, } diff --git a/engine/redis.go b/engine/redis.go index bab242a..abf1274 100644 --- a/engine/redis.go +++ b/engine/redis.go @@ -6,14 +6,8 @@ import ( redis "github.com/go-redis/redis/v7" ) -// RedisClient is an idiomatic interface for the Redis client, -// adding few methods to interact with the file system. -type RedisClient struct { - conn *redis.Client -} - // NewRedisClient returns a Redis client -func NewRedisClient(addr, port, password string, db int) (*RedisClient, error) { +func NewRedisClient(addr, port, password string, db int) (*redis.Client, error) { client := redis.NewClient(&redis.Options{ Addr: addr + ":" + port, Password: password, // no password set @@ -25,13 +19,13 @@ func NewRedisClient(addr, port, password string, db int) (*RedisClient, error) { return nil, err } - return &RedisClient{conn: client}, nil + return client, nil } -// AddFile index a file -func (c *RedisClient) AddFile(file, content string) error { +// AddFile indexes a file and its words in the database +func AddFile(c *redis.Client, file, content string) error { for w, s := range Scan(content) { - if err := c.conn.ZAdd(file, &redis.Z{ + if err := c.ZAdd(file, &redis.Z{ Score: float64(s), Member: strings.ToLower(w), }).Err(); err != nil { @@ -43,8 +37,8 @@ func (c *RedisClient) AddFile(file, content string) error { } // Get search for a key -func (c *RedisClient) Get(key string) ([]string, error) { - return c.conn.ZRevRangeByScore(key, &redis.ZRangeBy{ +func Get(c *redis.Client, key string) ([]string, error) { + return c.ZRevRangeByScore(key, &redis.ZRangeBy{ Min: "-inf", Max: "+inf", Offset: 0, @@ -52,23 +46,23 @@ func (c *RedisClient) Get(key string) ([]string, error) { }).Result() } -// GetWordScoreFromFile get score of element -func (c *RedisClient) GetWordScoreFromFile(key, member string) float64 { - return c.conn.ZScore(key, member).Val() +// GetWordScoreFromFile retreive the score of a file's word +func GetWordScoreFromFile(c *redis.Client, key, member string) float64 { + return c.ZScore(key, member).Val() } -// GetFiles returns a key value -func (c *RedisClient) GetFiles() (keys []string, err error) { - keys, _, err = c.conn.Scan(0, "*", -1).Result() +// GetFiles returns all registered files +func GetFiles(c *redis.Client) (keys []string, err error) { + keys, _, err = c.Scan(0, "*", -1).Result() return } -// FlushAll drop the database -func (c *RedisClient) FlushAll() error { - return c.conn.FlushAll().Err() +// FlushAll drops the database +func FlushAll(c *redis.Client) error { + return c.FlushAll().Err() } // Close closes the Redis connection -func (c *RedisClient) Close() error { - return c.conn.Close() +func Close(c *redis.Client) error { + return c.Close() } diff --git a/engine/redis_test.go b/engine/redis_test.go index b3f05cb..1c56d3a 100644 --- a/engine/redis_test.go +++ b/engine/redis_test.go @@ -1,10 +1,9 @@ package engine import ( - "log" "testing" - "github.com/go-redis/redis/v7" + redis "github.com/go-redis/redis/v7" "github.com/stretchr/testify/assert" ) @@ -26,20 +25,14 @@ func TestRedisClient(t *testing.T) { defer client.FlushAll() defer client.Close() - redisClient, err := NewRedisClient(addr, port, password, DB) - if err != nil { - log.Fatal(err) - } - defer redisClient.Close() - t.Run("RedisClient", func(t *testing.T) { t.Run("should use ZADD", func(t *testing.T) { client.FlushAll() - err := redisClient.AddFile("file1", "word") + err := AddFile(client, "file1", "word") assert.Equal(nil, err, "should be equal") - err = redisClient.AddFile("file2", "word") + err = AddFile(client, "file2", "word") assert.Equal(nil, err, "should be equal") values, err2 := client.ZRevRange("file1", 0, -1).Result() @@ -57,7 +50,7 @@ func TestRedisClient(t *testing.T) { }).Err() assert.Equal(nil, err, "should be equal") - values, err2 := redisClient.Get("word") + values, err2 := Get(client, "word") assert.Equal(nil, err2, "should be equal") assert.Equal([]string{"file"}, values, "should be equal") @@ -69,7 +62,7 @@ func TestRedisClient(t *testing.T) { err := client.Set("test", "value", 0).Err() assert.Equal(nil, err, "should be equal") - err = redisClient.FlushAll() + err = FlushAll(client) assert.Equal(nil, err, "should be equal") err = client.Get("test").Err()