Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | package redis |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sync" |
| 6 | "sync/atomic" |
| 7 | ) |
| 8 | |
| 9 | func (c *ClusterClient) DBSize(ctx context.Context) *IntCmd { |
| 10 | cmd := NewIntCmd(ctx, "dbsize") |
| 11 | _ = c.hooks.process(ctx, cmd, func(ctx context.Context, _ Cmder) error { |
| 12 | var size int64 |
| 13 | err := c.ForEachMaster(ctx, func(ctx context.Context, master *Client) error { |
| 14 | n, err := master.DBSize(ctx).Result() |
| 15 | if err != nil { |
| 16 | return err |
| 17 | } |
| 18 | atomic.AddInt64(&size, n) |
| 19 | return nil |
| 20 | }) |
| 21 | if err != nil { |
| 22 | cmd.SetErr(err) |
| 23 | } else { |
| 24 | cmd.val = size |
| 25 | } |
| 26 | return nil |
| 27 | }) |
| 28 | return cmd |
| 29 | } |
| 30 | |
| 31 | func (c *ClusterClient) ScriptLoad(ctx context.Context, script string) *StringCmd { |
| 32 | cmd := NewStringCmd(ctx, "script", "load", script) |
| 33 | _ = c.hooks.process(ctx, cmd, func(ctx context.Context, _ Cmder) error { |
| 34 | mu := &sync.Mutex{} |
| 35 | err := c.ForEachShard(ctx, func(ctx context.Context, shard *Client) error { |
| 36 | val, err := shard.ScriptLoad(ctx, script).Result() |
| 37 | if err != nil { |
| 38 | return err |
| 39 | } |
| 40 | |
| 41 | mu.Lock() |
| 42 | if cmd.Val() == "" { |
| 43 | cmd.val = val |
| 44 | } |
| 45 | mu.Unlock() |
| 46 | |
| 47 | return nil |
| 48 | }) |
| 49 | if err != nil { |
| 50 | cmd.SetErr(err) |
| 51 | } |
| 52 | return nil |
| 53 | }) |
| 54 | return cmd |
| 55 | } |
| 56 | |
| 57 | func (c *ClusterClient) ScriptFlush(ctx context.Context) *StatusCmd { |
| 58 | cmd := NewStatusCmd(ctx, "script", "flush") |
| 59 | _ = c.hooks.process(ctx, cmd, func(ctx context.Context, _ Cmder) error { |
| 60 | err := c.ForEachShard(ctx, func(ctx context.Context, shard *Client) error { |
| 61 | return shard.ScriptFlush(ctx).Err() |
| 62 | }) |
| 63 | if err != nil { |
| 64 | cmd.SetErr(err) |
| 65 | } |
| 66 | return nil |
| 67 | }) |
| 68 | return cmd |
| 69 | } |
| 70 | |
| 71 | func (c *ClusterClient) ScriptExists(ctx context.Context, hashes ...string) *BoolSliceCmd { |
| 72 | args := make([]interface{}, 2+len(hashes)) |
| 73 | args[0] = "script" |
| 74 | args[1] = "exists" |
| 75 | for i, hash := range hashes { |
| 76 | args[2+i] = hash |
| 77 | } |
| 78 | cmd := NewBoolSliceCmd(ctx, args...) |
| 79 | |
| 80 | result := make([]bool, len(hashes)) |
| 81 | for i := range result { |
| 82 | result[i] = true |
| 83 | } |
| 84 | |
| 85 | _ = c.hooks.process(ctx, cmd, func(ctx context.Context, _ Cmder) error { |
| 86 | mu := &sync.Mutex{} |
| 87 | err := c.ForEachShard(ctx, func(ctx context.Context, shard *Client) error { |
| 88 | val, err := shard.ScriptExists(ctx, hashes...).Result() |
| 89 | if err != nil { |
| 90 | return err |
| 91 | } |
| 92 | |
| 93 | mu.Lock() |
| 94 | for i, v := range val { |
| 95 | result[i] = result[i] && v |
| 96 | } |
| 97 | mu.Unlock() |
| 98 | |
| 99 | return nil |
| 100 | }) |
| 101 | if err != nil { |
| 102 | cmd.SetErr(err) |
| 103 | } else { |
| 104 | cmd.val = result |
| 105 | } |
| 106 | return nil |
| 107 | }) |
| 108 | return cmd |
| 109 | } |