diff --git a/cmd/drop.go b/cmd/drop.go index ff3b2ed..bee4333 100644 --- a/cmd/drop.go +++ b/cmd/drop.go @@ -17,7 +17,7 @@ var dropCmd = &cobra.Command{ Use: "drop", Short: "Drop all indexes", Run: func(cmd *cobra.Command, args []string) { - client, err := engine.NewRedisClient(redisAddr, redisPort) + client, err := engine.NewRedisClient(redisAddr, redisPort, redisPassword, redisDB) if err != nil { fmt.Println("Failed to connect to database", redisAddr, redisPort) os.Exit(1) diff --git a/cmd/dump.go b/cmd/dump.go index 88e1af3..39670a3 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -30,7 +30,7 @@ var dumpCmd = &cobra.Command{ Use: "dump", Short: "Dump database keys", Run: func(cmd *cobra.Command, args []string) { - client, err := engine.NewRedisClient(redisAddr, redisPort) + client, err := engine.NewRedisClient(redisAddr, redisPort, "", 0) if err != nil { fmt.Println("Failed to connect to database", redisAddr, redisPort) os.Exit(1) diff --git a/cmd/index.go b/cmd/index.go index 19c8730..5826dfb 100644 --- a/cmd/index.go +++ b/cmd/index.go @@ -19,7 +19,7 @@ var indexCmd = &cobra.Command{ Short: "Add files to database indexation", Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - client, err := engine.NewRedisClient(redisAddr, redisPort) + client, err := engine.NewRedisClient(redisAddr, redisPort, "", 0) if err != nil { fmt.Println("Failed to connect to database", redisAddr, redisPort) os.Exit(1) diff --git a/cmd/query.go b/cmd/query.go index bfafff1..a141381 100644 --- a/cmd/query.go +++ b/cmd/query.go @@ -20,7 +20,7 @@ var queryCmd = &cobra.Command{ Short: "Run a query against the database", Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - client, err := engine.NewRedisClient(redisAddr, redisPort) + client, err := engine.NewRedisClient(redisAddr, redisPort, "", 0) if err != nil { fmt.Println("Failed to connect to database", redisAddr, redisPort) os.Exit(1) @@ -31,14 +31,14 @@ var queryCmd = &cobra.Command{ fmt.Printf("Querying index for \"%s\":\n\n", word) - results, err := client.GetAllKeys() + files, err := client.GetAllKeys() if err != nil { log.Fatal(err) } var queryResults []*engine.QueryResult - for _, file := range results { + for _, file := range files { score := client.GetScore(file, word) if score == 0 { diff --git a/cmd/root.go b/cmd/root.go index bbf5ca0..b2d9645 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -9,11 +9,15 @@ import ( var redisAddr string var redisPort string +var redisPassword string +var redisDB int func init() { // Register flags rootCmd.PersistentFlags().StringVar(&redisAddr, "redis-addr", "localhost", "Redis server address") rootCmd.PersistentFlags().StringVar(&redisPort, "redis-port", "6379", "Redis server port") + rootCmd.PersistentFlags().StringVar(&redisPassword, "redis-password", "", "Redis server password") + rootCmd.PersistentFlags().IntVar(&redisDB, "redis-db", 0, "Redis DB") } var rootCmd = &cobra.Command{ diff --git a/engine/redis.go b/engine/redis.go index 6ea281c..9d56bec 100644 --- a/engine/redis.go +++ b/engine/redis.go @@ -13,11 +13,11 @@ type RedisClient struct { } // NewRedisClient returns a Redis client -func NewRedisClient(addr, port string) (*RedisClient, error) { +func NewRedisClient(addr, port, password string, db int) (*RedisClient, error) { client := redis.NewClient(&redis.Options{ Addr: addr + ":" + port, - Password: "", // no password set - DB: 0, // use default DB + Password: password, // no password set + DB: db, // use default DB }) err := client.Ping().Err() @@ -44,10 +44,7 @@ func (c *RedisClient) AddFile(file, content string) error { // GetKey search for a key func (c *RedisClient) GetKey(key string) ([]string, error) { - return c.conn.ZRevRangeByScore(key, &redis.ZRangeBy{ - Offset: 0, - Count: -1, - }).Result() + return c.conn.ZRevRange(key, 0, -1).Result() } // GetScore get score of element diff --git a/engine/redis_test.go b/engine/redis_test.go index 200766d..fab82c4 100644 --- a/engine/redis_test.go +++ b/engine/redis_test.go @@ -9,8 +9,10 @@ import ( ) const ( - addr = "0.0.0.0" - port = "6379" + addr = "0.0.0.0" + port = "6379" + password = "" + DB = 0 ) func TestRedisClient(t *testing.T) { @@ -18,13 +20,13 @@ func TestRedisClient(t *testing.T) { client := redis.NewClient(&redis.Options{ Addr: addr + ":" + port, - Password: "", // no password set - DB: 0, // use default DB + Password: password, // no password set + DB: DB, // use default DB }) defer client.FlushAll() defer client.Close() - redisClient, err := NewRedisClient(addr, port) + redisClient, err := NewRedisClient(addr, port, password, DB) if err != nil { log.Fatal(err) }