From 553278f2d8534ee110c2c958a5b33ea0a878c1d7 Mon Sep 17 00:00:00 2001 From: Vipin Rai P Date: Tue, 31 Dec 2024 09:55:37 +0530 Subject: [PATCH 1/2] 1401: Update leaderbard example to use zrange watch --- examples/leaderboard-go/main.go | 42 +++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/examples/leaderboard-go/main.go b/examples/leaderboard-go/main.go index 92899b97c..cc7930c54 100644 --- a/examples/leaderboard-go/main.go +++ b/examples/leaderboard-go/main.go @@ -20,8 +20,10 @@ import ( "context" "encoding/json" "fmt" - "github.com/dicedb/dicedb-go" "log" + + "github.com/dicedb/dicedb-go" + "math/rand" "net/http" "os" @@ -65,8 +67,8 @@ func main() { MaxRetries: 10, }) - go updateScores() go watchLeaderboard() + go updateScores() // Serve static files for the frontend http.Handle("/", http.FileServer(http.Dir("."))) @@ -78,31 +80,35 @@ func main() { func updateScores() { ctx := context.Background() + key := "match:100" for { - entry := LeaderboardEntry{ - PlayerID: fmt.Sprintf("player:%d", rand.Intn(10)), - Score: rand.Intn(100), - Timestamp: time.Now(), - } - lentry, _ := json.Marshal(entry) - dice.JSONSet(ctx, entry.PlayerID, "$", lentry).Err() + dice.ZAdd(ctx, key, dicedb.Z{Score: rand.Float64() * 100, Member: fmt.Sprintf("player:%d", rand.Intn(5))}) + time.Sleep(time.Duration(2 * time.Second)) } } func watchLeaderboard() { ctx := context.Background() - qwatch := dice.QWatch(ctx) - qwatch.WatchQuery(ctx, `SELECT $key, $value - WHERE $key LIKE 'player:*' AND '$value.score' > 10 - ORDER BY $value.score DESC - LIMIT 5;`) - defer qwatch.Close() - - ch := qwatch.Channel() + watchConn := dice.WatchConn(ctx) + key := "match:100" + _, err := watchConn.ZRangeWatch(ctx, key, "0", "4", "REV", "WITHSCORES") + if err != nil { + log.Println("failed to create watch connection:", err) + return + } + + defer watchConn.Close() + + ch := watchConn.Channel() for { select { case msg := <-ch: - entries := toEntries(msg.Updates) + var entries []LeaderboardEntry + for _, dicedbZ := range msg.Data.([]dicedb.Z) { + entry := LeaderboardEntry{Score: int(dicedbZ.Score), PlayerID: dicedbZ.Member.(string), + Timestamp: time.Now()} + entries = append(entries, entry) + } broadcast(entries) case <-ctx.Done(): return From a6840a7196b031e406fce9d741cb79017a445191 Mon Sep 17 00:00:00 2001 From: Jyotinder Singh Date: Tue, 7 Jan 2025 16:11:48 +0530 Subject: [PATCH 2/2] formatting fixes --- examples/leaderboard-go/main.go | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/examples/leaderboard-go/main.go b/examples/leaderboard-go/main.go index cc7930c54..65c4e4107 100644 --- a/examples/leaderboard-go/main.go +++ b/examples/leaderboard-go/main.go @@ -82,8 +82,11 @@ func updateScores() { ctx := context.Background() key := "match:100" for { - dice.ZAdd(ctx, key, dicedb.Z{Score: rand.Float64() * 100, Member: fmt.Sprintf("player:%d", rand.Intn(5))}) - time.Sleep(time.Duration(2 * time.Second)) + dice.ZAdd(ctx, key, dicedb.Z{ + Score: rand.Float64() * 100, + Member: fmt.Sprintf("player:%d", rand.Intn(5)), + }) + time.Sleep(2 * time.Second) } } @@ -105,8 +108,11 @@ func watchLeaderboard() { case msg := <-ch: var entries []LeaderboardEntry for _, dicedbZ := range msg.Data.([]dicedb.Z) { - entry := LeaderboardEntry{Score: int(dicedbZ.Score), PlayerID: dicedbZ.Member.(string), - Timestamp: time.Now()} + entry := LeaderboardEntry{ + Score: int(dicedbZ.Score), + PlayerID: dicedbZ.Member.(string), + Timestamp: time.Now(), + } entries = append(entries, entry) } broadcast(entries) @@ -116,16 +122,6 @@ func watchLeaderboard() { } } -func toEntries(updates []dicedb.KV) []LeaderboardEntry { - var entries []LeaderboardEntry - for _, update := range updates { - var entry LeaderboardEntry - json.Unmarshal([]byte(update.Value.(string)), &entry) - entries = append(entries, entry) - } - return entries -} - func broadcast(entries []LeaderboardEntry) { cMux.Lock() defer cMux.Unlock()