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

#1020 Migrate SADD, SREM, SCARD, SMEMBERS command to store_eval #1068

Merged
merged 44 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
9ede787
initial commit
sahoss Oct 11, 2024
fba9506
fix tests
sahoss Oct 11, 2024
29f0396
add comments
sahoss Oct 11, 2024
848d1f6
Merge branch 'DiceDB:master' into sadd-command-migration
sahoss Oct 13, 2024
2452a72
code review suggestions
sahoss Oct 13, 2024
98278d0
migrate srem
sahoss Oct 13, 2024
3e9a5c2
Merge branch 'DiceDB:master' into sadd-command-migration
sahoss Oct 13, 2024
ed3576f
migrate scard
sahoss Oct 13, 2024
e8c9772
migrate smembers
sahoss Oct 13, 2024
f1a030c
Merge branch 'master' of sahoss:sahoss/dice into sadd-command-migration
sahoss Oct 15, 2024
0015e5d
lint
sahoss Oct 15, 2024
6905e6b
add comment back
sahoss Oct 15, 2024
4a30ffb
add comment back
sahoss Oct 15, 2024
0af629f
merge master
sahoss Oct 15, 2024
e77feab
add unit test for sadd
sahoss Oct 15, 2024
af774c9
add srem unit test
sahoss Oct 16, 2024
b6c11d1
unit test scard
sahoss Oct 16, 2024
adec65b
Merge branch 'master' into sadd-command-migration
sahoss Oct 16, 2024
21233c1
unit test smembers
sahoss Oct 16, 2024
ed858e4
Merge branch 'master' into sadd-command-migration
sahoss Oct 20, 2024
e90d07e
commit
sahoss Oct 20, 2024
d8a502d
migrate integ tests
sahoss Oct 20, 2024
fcb713c
fix test
sahoss Oct 20, 2024
40a4fa4
Merge branch 'master' into sadd-command-migration
sahoss Oct 22, 2024
bd29a02
fix missing import
sahoss Oct 22, 2024
849e698
lint
sahoss Oct 22, 2024
b11def2
Update internal/eval/store_eval.go
sahoss Oct 22, 2024
037ec10
Update internal/eval/store_eval.go
sahoss Oct 22, 2024
052051a
fix failing test
sahoss Oct 24, 2024
b3baf98
Merge branch 'master' into sadd-command-migration
sahoss Oct 24, 2024
94768bb
fix
sahoss Oct 24, 2024
b1e422f
Merge branch 'master' into sadd-command-migration
sahoss Oct 28, 2024
ebd9e63
fix
sahoss Oct 28, 2024
0f02583
Merge branch 'master' into sadd-command-migration
sahoss Oct 28, 2024
b0c0256
fix failing test
sahoss Oct 30, 2024
e397ab5
Merge branch 'master' into sadd-command-migration
sahoss Oct 30, 2024
9187b7c
fix
sahoss Oct 30, 2024
636a78e
Merge branch 'master' into sadd-command-migration
apoorvyadav1111 Nov 5, 2024
577c815
Merge branch 'master' into sadd-command-migration
sahoss Nov 6, 2024
ac792a4
merge
sahoss Nov 6, 2024
411a818
fix
sahoss Nov 6, 2024
3ef322e
fix
sahoss Nov 6, 2024
d7f8870
Update deque_test.go fix for assert function
apoorvyadav1111 Nov 6, 2024
184170d
Update deque_test.go
apoorvyadav1111 Nov 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions internal/eval/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,9 +839,10 @@ var (
Specified members that are already a member of this set are ignored
Non existing keys are treated as empty sets.
An error is returned when the value stored at key is not a set.`,
Eval: evalSADD,
Arity: -3,
KeySpecs: KeySpecs{BeginIndex: 1},
NewEval: evalSADD,
Arity: -3,
KeySpecs: KeySpecs{BeginIndex: 1},
IsMigrated: true,
}
smembersCmdMeta = DiceCmdMeta{
Name: "SMEMBERS",
Expand Down
44 changes: 1 addition & 43 deletions internal/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ func evalJSONDEL(args []string, store *dstore.Store) []byte {

hasBrackets := strings.Contains(path, "[") && strings.Contains(path, "]")

//If the command has square brackets then we have to delete an element inside an array
// If the command has square brackets then we have to delete an element inside an array
if hasBrackets {
_, err = expr.Remove(jsonData)
} else {
Expand Down Expand Up @@ -3705,48 +3705,6 @@ func evalFLUSHDB(args []string, store *dstore.Store) []byte {
return clientio.RespOK
}

func evalSADD(args []string, store *dstore.Store) []byte {
if len(args) < 2 {
return diceerrors.NewErrArity("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, object.ObjEncodingSetStr)
store.Put(key, obj, dstore.WithKeepTTL(keepttl))
}

if err := object.AssertType(obj.TypeEncoding, object.ObjTypeSet); err != nil {
return diceerrors.NewErrWithFormattedMessage(diceerrors.WrongTypeErr)
}

if err := object.AssertEncoding(obj.TypeEncoding, object.ObjEncodingSetStr); err != nil {
return diceerrors.NewErrWithFormattedMessage(diceerrors.WrongTypeErr)
}

// 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 clientio.Encode(count, false)
}

func evalSMEMBERS(args []string, store *dstore.Store) []byte {
if len(args) != 1 {
return diceerrors.NewErrArity("SMEMBERS")
Expand Down
60 changes: 60 additions & 0 deletions internal/eval/store_eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,63 @@ func evalSETEX(args []string, store *dstore.Store) *EvalResponse {

return evalSET(newArgs, store)
}

// evalSADD adds one or more members to a set
// args must contain a key and one or more members to add the set
// If the set does not exist, a new set is created and members are added to it
// An error response is returned if the command is used on a key that contains a non-set value(eg: string)
// 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, object.ObjEncodingSetStr)
store.Put(key, obj, dstore.WithKeepTTL(keepttl))
}

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,
}
}

// 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: float64(count),
sahoss marked this conversation as resolved.
Show resolved Hide resolved
Error: nil,
}
}
5 changes: 5 additions & 0 deletions internal/server/cmd_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ var (
Cmd: "SETEX",
CmdType: SingleShard,
}
saddCmdMeta = CmdsMeta{
Cmd: "SADD",
CmdType: SingleShard,
}

// Metadata for multishard commands would go here.
// These commands require both breakup and gather logic.
Expand All @@ -79,6 +83,7 @@ func init() {
WorkerCmdsMeta["GET"] = getCmdMeta
WorkerCmdsMeta["GETSET"] = getsetCmdMeta
WorkerCmdsMeta["SETEX"] = setexCmdMeta
WorkerCmdsMeta["SADD"] = saddCmdMeta

// Additional commands (multishard, custom) can be added here as needed.
}
4 changes: 4 additions & 0 deletions internal/worker/cmd_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
CmdGetSet = "GETSET"
CmdGetWatch = "GET.WATCH"
CmdZRangeWatch = "ZRANGE.WATCH"
CmdSadd = "SADD"
)

type CmdMeta struct {
Expand Down Expand Up @@ -80,6 +81,9 @@ var CommandsMeta = map[string]CmdMeta{
CmdGetSet: {
CmdType: SingleShard,
},
CmdSadd: {
CmdType: SingleShard,
},

// Custom commands.
CmdAbort: {
Expand Down
Loading