Skip to content
Merged
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
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.env
.env
dump.rdb
database.aof
zeno-build*
zeno-build*
local
24 changes: 14 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,22 @@ func main() {
}
hanlderFunc(args)
})
conn, err := l.Accept()
if err != nil {
fmt.Println(err)
return
for {
conn, err := l.Accept()
if err != nil {
fmt.Println(err)
continue
}
go handleConnection(conn, aof)
}
}

func handleConnection(conn net.Conn, aof *aof.Aof) {
defer conn.Close()
for {
response := resp.NewResp(conn)
value, err := response.Read()
fmt.Println(value)
if err != nil {
fmt.Println(err)
return
}
if value.Type != "array" {
Expand All @@ -56,23 +60,23 @@ func main() {
}
if len(value.Array) == 0 {
fmt.Println("Invalid Requested expected array len greater than 0")
continue
}
command := strings.ToUpper(value.Array[0].Bulk)
args := value.Array[1:]

writer := writer.NewWriter(conn)
w := writer.NewWriter(conn)
handlerResponse, ok := handler.Handlers[command]
if !ok {
fmt.Println("Invalid Command: ", command)
writer.Write(resp.Value{Type: "string", Str: ""})
w.Write(resp.Value{Type: "string", Str: ""})
continue

}
if command == "SET" || command == "HSET" {
aof.Write(value)
}

result := handlerResponse(args)
writer.Write(result)
w.Write(result)
}
}
8 changes: 4 additions & 4 deletions src/aof/aof.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func (aof *Aof) Read(callback func(value resp.Value)) error {
response := resp.NewResp(aof.File)
for {
value, err := response.Read()
if err == nil {
callback(value)
}
if err == io.EOF {
break
}
return err
if err != nil {
return err
}
callback(value)
}
return nil
}
8 changes: 4 additions & 4 deletions src/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ func hget(args []resp.Value) resp.Value {
}
hash := args[0].Bulk
key := args[1].Bulk
HSETsMu.Lock()
HSETsMu.RLock()
value, ok := HSETs[hash][key]
HSETsMu.Unlock()
HSETsMu.RUnlock()
if !ok {
return resp.Value{Type: "null"}
}
Expand All @@ -110,8 +110,8 @@ func hgetall(args []resp.Value) resp.Value {
}
hash := args[0].Bulk

HSETsMu.Lock()
defer HSETsMu.Unlock()
HSETsMu.RLock()
defer HSETsMu.RUnlock()

values, ok := HSETs[hash]
if !ok {
Expand Down
Loading