Skip to content

Commit 0fcf2e2

Browse files
committed
Code cleanup
1 parent fb139dd commit 0fcf2e2

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

logic/bulk.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package logic
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"github.com/redis/go-redis/v9"
8+
"log"
9+
"redisjson4gophers/domain"
10+
"strconv"
11+
)
12+
13+
func IndexMoviesAsDocuments(ctx context.Context, redisClient *redis.Client, movies []domain.Movie) {
14+
pipeline := redisClient.Pipeline()
15+
for movieID, movie := range movies {
16+
movieAsJSON, err := json.Marshal(movie)
17+
if err != nil {
18+
log.Printf("Error marshaling movie into JSON: %v", err)
19+
}
20+
pipeline.JSONSet(ctx, keyPrefix+strconv.Itoa(movieID+1), "$", string(movieAsJSON))
21+
}
22+
23+
_, err := pipeline.Exec(ctx)
24+
if err != nil {
25+
log.Printf("Error writing JSON documents into Redis: %v", err)
26+
}
27+
28+
fmt.Printf("🟥 Movies stored on Redis: %d \n", len(movies))
29+
}

logic/index.go

+5-19
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@ package logic
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"github.com/redis/go-redis/v9"
87
"log"
9-
"redisjson4gophers/domain"
10-
"strconv"
118
)
129

1310
const (
14-
indexName = "json_movies_index"
11+
indexName = "movies_index"
1512
keyPrefix = "movie:"
1613
)
1714

18-
func IndexMoviesAsDocuments(ctx context.Context, redisClient *redis.Client, movies []domain.Movie) {
15+
func CreateMoviesIndexOnRedis(ctx context.Context, redisClient *redis.Client) {
1916
redisClient.FTDropIndexWithArgs(ctx, indexName, &redis.FTDropIndexOptions{DeleteDocs: true})
2017

2118
titleField := &redis.FieldSchema{FieldName: "$.title", FieldType: redis.SearchFieldTypeText, As: "title"}
@@ -28,24 +25,13 @@ func IndexMoviesAsDocuments(ctx context.Context, redisClient *redis.Client, movi
2825
actorsField := &redis.FieldSchema{FieldName: "$.actors.*", FieldType: redis.SearchFieldTypeTag, As: "actors"}
2926
directorsField := &redis.FieldSchema{FieldName: "$.directors.*", FieldType: redis.SearchFieldTypeTag, As: "directors"}
3027

31-
redisClient.FTCreate(ctx, indexName,
28+
_, err := redisClient.FTCreate(ctx, indexName,
3229
&redis.FTCreateOptions{OnJSON: true, Prefix: []interface{}{keyPrefix}},
3330
titleField, yearField, plotField, runningTimeField, releaseDateField,
3431
ratingField, genresField, actorsField, directorsField).Result()
35-
36-
pipeline := redisClient.Pipeline()
37-
for movieID, movie := range movies {
38-
movieAsJSON, err := json.Marshal(movie)
39-
if err != nil {
40-
log.Printf("Error marshaling movie into JSON: %v", err)
41-
}
42-
pipeline.JSONSet(ctx, keyPrefix+strconv.Itoa(movieID), "$", string(movieAsJSON))
43-
}
44-
45-
_, err := pipeline.Exec(ctx)
4632
if err != nil {
47-
log.Printf("Error writing JSON documents into Redis: %v", err)
33+
log.Printf("Error creating the index: %v", err)
4834
}
4935

50-
fmt.Printf("🟥 Movies indexed on Redis: %d \n", len(movies))
36+
fmt.Println("🟥 Index created successfully")
5137
}

logic/movies.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,6 @@ func LoadMoviesFromFile(fileName string) ([]domain.Movie, error) {
9393
return movies, errors.New(errorMsg)
9494
}
9595

96-
fmt.Printf("🟥 Movies loaded from the file: %d \n", len(movies))
96+
fmt.Printf("🟥 Movies loaded from file: %d \n", len(movies))
9797
return movies, nil
9898
}

main.go

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ func main() {
2121
}
2222
}(redisClient)
2323

24+
logic.CreateMoviesIndexOnRedis(ctx, redisClient)
25+
2426
movies, err := logic.LoadMoviesFromFile("movies.json")
2527
if err != nil {
2628
log.Fatalf("Error loading movies from file: %v", err)

0 commit comments

Comments
 (0)