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
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

const port = ":9000" // TODO configs
const walFilePath = "bin/wal.log" // TODO configs
const fsyncStrategy = wal.EVERY_SEC // TODO configs
const fsyncStrategy = wal.ALWAYS // TODO configs
const maxWalEntryLength = 1 << 30 // TODO configs

func main() {
Expand Down
26 changes: 18 additions & 8 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handler
import (
"fmt"
"log"

"com.github.SantanuKar43/simple-kv/internal/protocol"
"com.github.SantanuKar43/simple-kv/internal/store"
"com.github.SantanuKar43/simple-kv/internal/wal"
Expand All @@ -15,9 +16,7 @@ func Handle(input string, store *store.Store, wal *wal.Wal) string {
if len(cmd.Args) != 2 {
return "ERR wrong number of arguments"
}

_, err := wal.Append(cmd.Name, cmd.Args[0], cmd.Args[1])
if err != nil {
if err := appendToWal(cmd, wal); err != nil {
return fmt.Sprintf("ERR unable to write to WAL: %s", err.Error())
}

Expand All @@ -36,9 +35,7 @@ func Handle(input string, store *store.Store, wal *wal.Wal) string {
if len(cmd.Args) != 1 {
return "ERR wrong number of arguments"
}

_, err := wal.Append(cmd.Name, cmd.Args[0])
if err != nil {
if err := appendToWal(cmd, wal); err != nil {
return fmt.Sprintf("ERR unable to write to WAL: %s", err.Error())
}

Expand All @@ -49,6 +46,19 @@ func Handle(input string, store *store.Store, wal *wal.Wal) string {
}
}

func appendToWal(cmd protocol.Command, wal *wal.Wal) error {
lsn, err := wal.Append(cmd.Name, cmd.Args[0])
if err != nil {
return err
}
wal.CommitSignal.L.Lock()
for lsn > wal.CommittedLSN {
wal.CommitSignal.Wait()
}
wal.CommitSignal.L.Unlock()
return nil
}

func Replay(input string, store *store.Store) {
cmd := protocol.Parse(input)
switch cmd.Name {
Expand All @@ -57,6 +67,6 @@ func Replay(input string, store *store.Store) {
case "DEL":
store.Delete(cmd.Args[0])
default:
log.Printf("invalid command parsed %s\n", cmd.Name)
log.Printf("invalid command parsed %s\n", cmd.Name)
}
}
}
Loading
Loading