fix(engine): redis
Add password and db options to RedisClient; replace ZRevRangeByScore by ZRevRangepull/3/head
parent
3f9ceb1977
commit
2e36a1d076
|
@ -17,7 +17,7 @@ var dropCmd = &cobra.Command{
|
||||||
Use: "drop",
|
Use: "drop",
|
||||||
Short: "Drop all indexes",
|
Short: "Drop all indexes",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
client, err := engine.NewRedisClient(redisAddr, redisPort)
|
client, err := engine.NewRedisClient(redisAddr, redisPort, redisPassword, redisDB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Failed to connect to database", redisAddr, redisPort)
|
fmt.Println("Failed to connect to database", redisAddr, redisPort)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -30,7 +30,7 @@ var dumpCmd = &cobra.Command{
|
||||||
Use: "dump",
|
Use: "dump",
|
||||||
Short: "Dump database keys",
|
Short: "Dump database keys",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
client, err := engine.NewRedisClient(redisAddr, redisPort)
|
client, err := engine.NewRedisClient(redisAddr, redisPort, "", 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Failed to connect to database", redisAddr, redisPort)
|
fmt.Println("Failed to connect to database", redisAddr, redisPort)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -19,7 +19,7 @@ var indexCmd = &cobra.Command{
|
||||||
Short: "Add files to database indexation",
|
Short: "Add files to database indexation",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
client, err := engine.NewRedisClient(redisAddr, redisPort)
|
client, err := engine.NewRedisClient(redisAddr, redisPort, "", 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Failed to connect to database", redisAddr, redisPort)
|
fmt.Println("Failed to connect to database", redisAddr, redisPort)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -20,7 +20,7 @@ var queryCmd = &cobra.Command{
|
||||||
Short: "Run a query against the database",
|
Short: "Run a query against the database",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
client, err := engine.NewRedisClient(redisAddr, redisPort)
|
client, err := engine.NewRedisClient(redisAddr, redisPort, "", 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Failed to connect to database", redisAddr, redisPort)
|
fmt.Println("Failed to connect to database", redisAddr, redisPort)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -31,14 +31,14 @@ var queryCmd = &cobra.Command{
|
||||||
|
|
||||||
fmt.Printf("Querying index for \"%s\":\n\n", word)
|
fmt.Printf("Querying index for \"%s\":\n\n", word)
|
||||||
|
|
||||||
results, err := client.GetAllKeys()
|
files, err := client.GetAllKeys()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var queryResults []*engine.QueryResult
|
var queryResults []*engine.QueryResult
|
||||||
|
|
||||||
for _, file := range results {
|
for _, file := range files {
|
||||||
score := client.GetScore(file, word)
|
score := client.GetScore(file, word)
|
||||||
|
|
||||||
if score == 0 {
|
if score == 0 {
|
||||||
|
|
|
@ -9,11 +9,15 @@ import (
|
||||||
|
|
||||||
var redisAddr string
|
var redisAddr string
|
||||||
var redisPort string
|
var redisPort string
|
||||||
|
var redisPassword string
|
||||||
|
var redisDB int
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Register flags
|
// Register flags
|
||||||
rootCmd.PersistentFlags().StringVar(&redisAddr, "redis-addr", "localhost", "Redis server address")
|
rootCmd.PersistentFlags().StringVar(&redisAddr, "redis-addr", "localhost", "Redis server address")
|
||||||
rootCmd.PersistentFlags().StringVar(&redisPort, "redis-port", "6379", "Redis server port")
|
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{
|
var rootCmd = &cobra.Command{
|
||||||
|
|
|
@ -13,11 +13,11 @@ type RedisClient struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRedisClient returns a Redis client
|
// 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{
|
client := redis.NewClient(&redis.Options{
|
||||||
Addr: addr + ":" + port,
|
Addr: addr + ":" + port,
|
||||||
Password: "", // no password set
|
Password: password, // no password set
|
||||||
DB: 0, // use default DB
|
DB: db, // use default DB
|
||||||
})
|
})
|
||||||
|
|
||||||
err := client.Ping().Err()
|
err := client.Ping().Err()
|
||||||
|
@ -44,10 +44,7 @@ func (c *RedisClient) AddFile(file, content string) error {
|
||||||
|
|
||||||
// GetKey search for a key
|
// GetKey search for a key
|
||||||
func (c *RedisClient) GetKey(key string) ([]string, error) {
|
func (c *RedisClient) GetKey(key string) ([]string, error) {
|
||||||
return c.conn.ZRevRangeByScore(key, &redis.ZRangeBy{
|
return c.conn.ZRevRange(key, 0, -1).Result()
|
||||||
Offset: 0,
|
|
||||||
Count: -1,
|
|
||||||
}).Result()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetScore get score of element
|
// GetScore get score of element
|
||||||
|
|
|
@ -9,8 +9,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
addr = "0.0.0.0"
|
addr = "0.0.0.0"
|
||||||
port = "6379"
|
port = "6379"
|
||||||
|
password = ""
|
||||||
|
DB = 0
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRedisClient(t *testing.T) {
|
func TestRedisClient(t *testing.T) {
|
||||||
|
@ -18,13 +20,13 @@ func TestRedisClient(t *testing.T) {
|
||||||
|
|
||||||
client := redis.NewClient(&redis.Options{
|
client := redis.NewClient(&redis.Options{
|
||||||
Addr: addr + ":" + port,
|
Addr: addr + ":" + port,
|
||||||
Password: "", // no password set
|
Password: password, // no password set
|
||||||
DB: 0, // use default DB
|
DB: DB, // use default DB
|
||||||
})
|
})
|
||||||
defer client.FlushAll()
|
defer client.FlushAll()
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
|
||||||
redisClient, err := NewRedisClient(addr, port)
|
redisClient, err := NewRedisClient(addr, port, password, DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue