fix(engine): redis

Add password and db options to RedisClient; replace ZRevRangeByScore by ZRevRange
pull/3/head
sundowndev 2020-04-14 10:52:52 +01:00
parent 3f9ceb1977
commit 2e36a1d076
7 changed files with 21 additions and 18 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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