MCPcopy Create free account
hub / github.com/distributedio/titan / SPop

Function SPop

command/sets.go:87–114  ·  view source on GitHub ↗

SPop removes and returns one or more random elements from the set value store at key

(ctx *Context, txn *db.Transaction)

Source from the content-addressed store, hash-verified

85
86// SPop removes and returns one or more random elements from the set value store at key
87func 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
117func SRem(ctx *Context, txn *db.Transaction) (OnCommit, error) {

Callers

nothing calls this directly

Calls 4

BytesArrayFunction · 0.85
SPopMethod · 0.80
SetMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected