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

feat: implement hset command #6

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions internal/commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func (s RequestHandler) Run(data []byte, conn net.Conn, store storage.Storage) {
GetHandler(items, store, conn)
case "set":
SetHandler(items, store, conn)
case "hset":
go HSetHandler(items[2:], store, conn)
case "client":
log.Println("going to execute client options command")
default:
Expand Down
99 changes: 99 additions & 0 deletions internal/commands/handler_hset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package commands

import (
"bytes"
"encoding/gob"
"fmt"
"log"
"net"
"pedis/internal/storage"
"strings"
)

// HSetHandler
func HSetHandler(items [][]byte, store storage.Storage, conn net.Conn) {
hs := chunkSlice(items[3:], 4)

data, err := hs.ToBytes()

if err != nil {
_, _ = conn.Write([]byte("-ERR future error message\r\n"))
return
}

_, err = store.HSet(string(items[0]), data, 0)

if err != nil {
_, _ = conn.Write([]byte("-ERR future error message\r\n"))
return
}

err = hs.FromBytes(data)
log.Println(err)
_, _ = conn.Write([]byte(fmt.Sprintf(":%d\r\n", hs.Len())))
}

type hasharray [][]byte

func (ha hasharray) Key() string {
return string(ha[1])
}

func (ha hasharray) Value() string {
return string(ha[3])
}

func (ha hasharray) String() string {
sb := strings.Builder{}

sb.WriteString("hasharray[")
for idx, item := range ha {
str := fmt.Sprintf("(i=%d v=%s),", idx, string(item))
sb.WriteString(str)
}
sb.WriteString("]")

return sb.String()
}

type hset []hasharray

func (hs hset) Len() int {
return len(hs)
}

func (hs hset) ToBytes() ([]byte, error) {
log.Println(hs)
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
if err := enc.Encode(hs); err != nil {
return nil, err
}
return buf.Bytes(), nil
}

func (hs hset) FromBytes(data []byte) error {
buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf)

if err := dec.Decode(&hs); err != nil {
return err
}

return nil
}

func chunkSlice(slice [][]byte, chunkSize int) hset {
var chunks hset

for i := 0; i < len(slice); i += chunkSize {
end := i + chunkSize
if end > len(slice) {
end = len(slice)
}

chunks = append(chunks, slice[i:end])
}

return chunks
}
22 changes: 20 additions & 2 deletions internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (

func TestServerSetAndGet(t *testing.T) {
s, err := NewRedisServer(storage.NewSimpleStorage(), Config{ServerAddr: "localhost:6379"})

require.NoError(t, err)

go s.Start()

client := redis.NewClient(&redis.Options{
Expand Down Expand Up @@ -54,3 +52,23 @@ func TestServerSetAndGet(t *testing.T) {
assert.Equal(t, err.Error(), "ERR key not found")
})
}

func TestServerHSetAndHGet(t *testing.T) {
s, err := NewRedisServer(storage.NewSimpleStorage(), Config{ServerAddr: "localhost:6379"})
require.NoError(t, err)
go s.Start()

client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})

t.Run("Can set and get a hash", func(t *testing.T) {
// m := map[string]interface{}{"key-one": "one value", "key-two": "two value"}
// err = client.HMSet(context.Background(), "myhash", m, 0).Err()
result, err := client.HSet(context.Background(), "user", "name", "Pathe", "country", "Senegal", 221).Result()
require.NoError(t, err)
assert.Equal(t, int64(3), result)
})
}
14 changes: 14 additions & 0 deletions internal/storage/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,17 @@ func (ss *SimpleStorage) Get(key string) (string, error) {

return string(v.data), nil
}

func (ss *SimpleStorage) HSet(key string, value []byte, expires int64) (int, error) {
data := datatype{t: 'm', data: value}

ss.Lock()
ss.data[key] = data
ss.Unlock()

return 0, nil
}

func (ss *SimpleStorage) HGet(key string) ([]byte, error) {
return []byte{}, nil
}
5 changes: 5 additions & 0 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package storage

type Storage interface {
// Simple strings
Set(key string, value string, expires int64) error
Get(key string) (string, error)

// Maps
HGet(key string) ([]byte, error)
HSet(key string, value []byte, expires int64) (int, error)
}
Loading