SPop removes and returns one or more random elements from the set value store at key
(ctx *Context, txn *db.Transaction)
| 85 | |
| 86 | // SPop removes and returns one or more random elements from the set value store at key |
| 87 | func SPop(ctx *Context, txn *db.Transaction) (OnCommit, error) { |
| 88 | if len(ctx.Args) > 2 { |
| 89 | return nil, ErrSyntax |
| 90 | } |
| 91 | |
| 92 | count := 1 |
| 93 | var err error |
| 94 | key := []byte(ctx.Args[0]) |
| 95 | if len(ctx.Args) == 2 { |
| 96 | count, err = strconv.Atoi(ctx.Args[1]) |
| 97 | if err != nil { |
| 98 | return nil, ErrInteger |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | set, err := txn.Set(key) |
| 103 | if err != nil { |
| 104 | if err == db.ErrTypeMismatch { |
| 105 | return nil, ErrTypeMismatch |
| 106 | } |
| 107 | return nil, errors.New("ERR " + err.Error()) |
| 108 | } |
| 109 | members, err := set.SPop(int64(count)) |
| 110 | if err != nil { |
| 111 | return nil, errors.New("ERR " + err.Error()) |
| 112 | } |
| 113 | return BytesArray(ctx.Out, members), nil |
| 114 | } |
| 115 | |
| 116 | // SRem removes the specified members from the set stored at key |
| 117 | func SRem(ctx *Context, txn *db.Transaction) (OnCommit, error) { |
nothing calls this directly
no test coverage detected