Skip to content

Commit

Permalink
Added SISMEMBER command
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin MacDonald authored and Martin MacDonald committed Jun 25, 2024
1 parent 8f4b824 commit fe90318
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Currently supported Redis Commands
- LINDEX
- SADD
- SMEMBERS
- SISMEMBER
- PERSIST
- EXPIRE
- EXPIREAT
Expand Down
1 change: 1 addition & 0 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var Handlers = map[string]Handler{
"LINDEX": lindex,
"SADD": sadd,
"SMEMBERS": smembers,
"SISMEMBER": sismember,
"PERSIST": persist,
"EXPIRE": setExpiry,
"EXPIREAT": setExpiry,
Expand Down
34 changes: 34 additions & 0 deletions internal/handlers/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,37 @@ func smembers(h handlerArgs) handlerResponse {
resp: generateSetResponse(members),
}
}

func sismember(h handlerArgs) handlerResponse {
if len(h.args) != 2 {
return handlerResponse{
err: fmt.Errorf("wrong amount of arguments to 'sismember' command"),
}
}

key := h.args[0].Bulk
value := h.args[1].Bulk
s, ok, err := h.store.GetByKey(storage.KV{Key: key})

if err != nil {
return handlerResponse{
err: err,
}
}

if !ok || s.Typ != SET {
return handlerResponse{
resp: generateIntegerResponse(0),
}
}

if _, exists := s.Set[value]; !exists {
return handlerResponse{
resp: generateIntegerResponse(0),
}
}

return handlerResponse{
resp: generateIntegerResponse(1),
}
}

0 comments on commit fe90318

Please sign in to comment.