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