Skip to content

Commit

Permalink
Add SINTERSTORE command
Browse files Browse the repository at this point in the history
Signed-off-by: Devansh Singh <[email protected]>
  • Loading branch information
Devansh3712 committed Feb 16, 2024
1 parent 0348334 commit 3b345b4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
CMD_SMEMBERS = "SMEMBERS"
CMD_SISMEMBER = "SISMEMBER"
CMD_SDIFFSTORE = "SDIFFSTORE"
CMD_SINTERSTORE = "SINTERSTORE"
)

var ErrInvalidCmd = errors.New("invalid command")
Expand Down Expand Up @@ -133,6 +134,8 @@ func (s *Server) HandleCommand() {
s.sIsMember(cmd)
case CMD_SDIFFSTORE:
s.sDiffStore(cmd)
case CMD_SINTERSTORE:
s.sInterStore(cmd)
default:
cmd.error(ErrInvalidCmd)
}
Expand Down
11 changes: 11 additions & 0 deletions server/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,14 @@ func (s *Server) sInter(cmd Command) {
cmd.write(fmt.Sprintf("%d) %s", index+1, element))
}
}

func (s *Server) sInterStore(cmd Command) {
if len(cmd.Args) < 3 {
cmd.error(ErrNotEnoughArgs)
return
}
err := s.DB.SInterStore(cmd.Args[0], cmd.Args[1], cmd.Args[2])
if err != nil {
cmd.error(err)
}
}
11 changes: 11 additions & 0 deletions store/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,14 @@ func (s *Store) SInter(s1, s2 string) ([]string, error) {
}
return elements, nil
}

func (s *Store) SInterStore(s1, s2, s3 string) error {
elements, err := s.SInter(s1, s2)
if err != nil {
return err
}
for _, element := range elements {
s.SAdd(s3, element)
}
return nil
}

0 comments on commit 3b345b4

Please sign in to comment.