Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for SMEMBERS.WATCH command #1289

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Next Next commit
feat(eval): notify subscribers when SADD is performed in evalSADD fun…
…ction
superiorsd10 committed Jan 11, 2025
commit 0dd5001dfce9f00259d7ceb74a5529c134f1f6fa
97 changes: 53 additions & 44 deletions internal/eval/store_eval.go
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you reformat this file? I'm seeing additional diffs here due to indentation changes which makes it harder to see the actual code changes.

Original file line number Diff line number Diff line change
@@ -4753,50 +4753,59 @@ func evalHDEL(args []string, store *dstore.Store) *EvalResponse {
// Returns an integer which represents the number of members that were added to the set, not including
// the members that were already present
func evalSADD(args []string, store *dstore.Store) *EvalResponse {
if len(args) < 2 {
return &EvalResponse{
Result: nil,
Error: diceerrors.ErrWrongArgumentCount("SADD"),
}
}
key := args[0]

// Get the set object from the store.
obj := store.Get(key)
lengthOfItems := len(args[1:])

var count = 0
if obj == nil {
var exDurationMs int64 = -1
var keepttl = false
// If the object does not exist, create a new set object.
value := make(map[string]struct{}, lengthOfItems)
// Create a new object.
obj = store.NewObj(value, exDurationMs, object.ObjTypeSet)
store.Put(key, obj, dstore.WithKeepTTL(keepttl))
}

if err := object.AssertType(obj.Type, object.ObjTypeSet); err != nil {
return &EvalResponse{
Result: nil,
Error: diceerrors.ErrWrongTypeOperation,
}
}

// Get the set object.
set := obj.Value.(map[string]struct{})

for _, arg := range args[1:] {
if _, ok := set[arg]; !ok {
set[arg] = struct{}{}
count++
}
}

return &EvalResponse{
Result: count,
Error: nil,
}
if len(args) < 2 {
return &EvalResponse{
Result: nil,
Error: diceerrors.ErrWrongArgumentCount("SADD"),
}
}
key := args[0]

// Get the set object from the store.
obj := store.Get(key)
lengthOfItems := len(args[1:])

var count = 0
var set map[string]struct{}

if obj == nil {
// If the object does not exist, create a new set
set = make(map[string]struct{}, lengthOfItems)
} else {
// Type and encoding checks
if err := object.AssertType(obj.TypeEncoding, object.ObjTypeSet); err != nil {
return &EvalResponse{
Result: nil,
Error: diceerrors.ErrWrongTypeOperation,
}
}

if err := object.AssertEncoding(obj.TypeEncoding, object.ObjEncodingSetStr); err != nil {
return &EvalResponse{
Result: nil,
Error: diceerrors.ErrWrongTypeOperation,
}
}

set = obj.Value.(map[string]struct{})
}

// Add elements to the set
for _, arg := range args[1:] {
if _, ok := set[arg]; !ok {
set[arg] = struct{}{}
count++
}
}

// Single Put operation at the end
obj = store.NewObj(set, -1, object.ObjTypeSet, object.ObjEncodingSetStr)
store.Put(key, obj, dstore.WithKeepTTL(false), dstore.WithPutCmd(dstore.SADD))

return &EvalResponse{
Result: count,
Error: nil,
}
}

// evalSREM removes one or more members from a set