Skip to content

Commit

Permalink
fix: golint warning
Browse files Browse the repository at this point in the history
  • Loading branch information
brkss committed Jan 18, 2024
1 parent e196c98 commit 510f173
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ SRC=src/main.go
all: clean
go build -o $(NAME) $(SRC)

scheck:
staticcheck ./...

clean:
rm -Rf $(NAME)

4 changes: 4 additions & 0 deletions src/aof/aof.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import (
"github.com/brkss/redis/src/resp"
)

// AOF is struct that hold file / reader and mutex related to data sync
type AOF struct {
file *os.File
rd *bufio.Reader
mu sync.Mutex
}

// NewAOF create a new AOF object create or open file provided in params
// alse lunch a parallel thread to keep the file synced
func NewAOF(path string) (*AOF, error) {
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
Expand All @@ -40,6 +43,7 @@ func NewAOF(path string) (*AOF, error) {
return aof, nil
}

// Close closes the file after the problem exited
func (aof *AOF) Close() error {
aof.mu.Lock()
defer aof.mu.Unlock()
Expand Down
4 changes: 4 additions & 0 deletions src/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import (
"github.com/brkss/redis/src/resp"
)

// SETs is a global variable to hold database data
var SETs = map[string]string{}

// SETsMutex is a global variable that hold mutexed to handle editing SETs's data
var SETsMutex = sync.RWMutex{}

// Handlers is global variable that hold function for each command !
var Handlers = map[string]func([]resp.Value) resp.Value{
"PING": ping,
"GET": get,
Expand Down
3 changes: 3 additions & 0 deletions src/handler/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"github.com/brkss/redis/src/resp"
)

// HSETs is a global variable to hold database hashed data
var HSETs = map[string]map[string]string{}

// HSETsMutext is a global variable that hold mutexed to handle editing HSETs's data
var HSETsMutext = sync.RWMutex{}

func hset(args []resp.Value) resp.Value {
Expand Down
14 changes: 7 additions & 7 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ import (
"github.com/brkss/redis/src/resp"
)

// Port is a global const representing the server port
const (
PORT = ":6379"
BUFFER_SIZE = 1024
AOF_FILE = "./database.aof"
Port = ":6379"
BufferSize = 1024
AOFFile = "./database.aof"
)

func main() {

fmt.Println("Start listening for connection on 127.0.0.1" + PORT)
fmt.Println("Start listening for connection on 127.0.0.1" + Port)

// create new server !
l, err := net.Listen("tcp", PORT)
l, err := net.Listen("tcp", Port)
if err != nil {
log.Fatal("Can't run redos server :", err)
}
Expand All @@ -34,7 +35,7 @@ func main() {
}

// init aof
aof, err := aof.NewAOF(AOF_FILE)
aof, err := aof.NewAOF(AOFFile)
if err != nil {
log.Fatal("Something went wrong opening aof : ", err)
}
Expand All @@ -50,7 +51,6 @@ func main() {
return
}
handler(args)
return
})

fmt.Println("read error : ", err)
Expand Down
8 changes: 6 additions & 2 deletions src/resp/resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
)

// STRING, ... holds pre defined
const (
STRING = '+'
ERROR = '-'
Expand All @@ -15,6 +16,7 @@ const (
ARRAY = '*'
)

// Value hold request / response value later will be reformed to RESP
type Value struct {
Typ string
Str string
Expand All @@ -23,10 +25,12 @@ type Value struct {
Arr []Value
}

// Resp holds pointer to bufio reader
type Resp struct {
reader *bufio.Reader
}

// NewReader create a pointer to Resp
func NewReader(rd io.Reader) *Resp {
return &Resp{bufio.NewReader(rd)}
}
Expand All @@ -38,7 +42,7 @@ func (r *Resp) readLine() (line []byte, n int, err error) {
if err != nil {
return nil, 0, err
}
n += 1
n++
line = append(line, b)
if len(line) >= 2 && line[len(line)-2] == '\r' {
break
Expand Down Expand Up @@ -73,7 +77,7 @@ func (r *Resp) Read() (Value, error) {
case BULK:
return r.readBulk()
default:
err := fmt.Errorf("Unkown type %v\n", string(_type))
err := fmt.Errorf("unkown type %v", string(_type))
return Value{}, err
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/resp/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"strconv"
)

// Writer struct that holds writer value will be used to write resp response to the connection
type Writer struct {
writer io.Writer
}

// NewWriter create pointer to Writer struct
func NewWriter(w io.Writer) *Writer {
return &Writer{writer: w}
}
Expand All @@ -24,6 +26,7 @@ func (w *Writer) Write(v Value) error {
return nil
}

// Marshal convert from strut Value to Resp in bytes
func (v Value) Marshal() []byte {
switch v.Typ {
case "array":
Expand Down

0 comments on commit 510f173

Please sign in to comment.