Skip to content

Commit

Permalink
feat: support hello command
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Nov 20, 2024
1 parent ed128b2 commit 7d4bc39
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 36 deletions.
7 changes: 6 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ ARG BUILD_TIME

WORKDIR /build

# 挂载本地缓存加速
VOLUME ["/go/pkg/mod", "/go/cache"]

COPY . .

RUN go build -ldflags="-s -w -X main.buildTime=${BUILD_TIME}" -o rotom .
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go build -ldflags="-s -w -X main.buildTime=${BUILD_TIME}" -o rotom .

FROM alpine:latest

Expand Down
39 changes: 36 additions & 3 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"github.com/xgzlucario/rotom/internal/resp"
"github.com/xgzlucario/rotom/internal/timer"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -64,6 +63,7 @@ var cmdTable = []*Command{
{"zrange", zrangeCommand, 3, false},
{"eval", evalCommand, 2, true},
{"ping", pingCommand, 0, false},
{"hello", helloCommand, 0, false},
{"flushdb", flushdbCommand, 0, true},
{"load", loadCommand, 0, false},
{"save", saveCommand, 0, false},
Expand Down Expand Up @@ -110,7 +110,7 @@ func setCommand(writer *resp.Writer, args []resp.RESP) {
writer.WriteError(errParseInteger)
return
}
ttl = timer.GetNanoTime() + int64(n*time.Second)
ttl = time.Now().Add(n * time.Second).UnixNano()
extra = extra[2:]

// PX
Expand All @@ -120,7 +120,7 @@ func setCommand(writer *resp.Writer, args []resp.RESP) {
writer.WriteError(errParseInteger)
return
}
ttl = timer.GetNanoTime() + int64(n*time.Millisecond)
ttl = time.Now().Add(n * time.Millisecond).UnixNano()
extra = extra[2:]

// KEEPTTL
Expand Down Expand Up @@ -551,6 +551,39 @@ func flushdbCommand(writer *resp.Writer, _ []resp.RESP) {
writer.WriteSString("OK")
}

func helloCommand(writer *resp.Writer, args []resp.RESP) {
var protoVersion int
var err error

if len(args) > 0 {
protoVersion, err = args[0].ToInt()
if err != nil {
writer.WriteError(err)
return
}
}

result := map[string]any{
"server": "rotom",
"version": "1.0.0",
"proto": protoVersion,
}
if protoVersion == 3 {
writer.WriteMapHead(len(result))
} else {
writer.WriteMapHead(len(result))
}
for k, v := range result {
writer.WriteBulkString(k)
switch val := v.(type) {
case string:
writer.WriteBulkString(val)
case int:
writer.WriteInteger(val)
}
}
}

func loadCommand(writer *resp.Writer, _ []resp.RESP) {
db.dict = New()
if err := db.rdb.LoadDB(); err != nil {
Expand Down
12 changes: 4 additions & 8 deletions dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"github.com/cockroachdb/swiss"
"github.com/xgzlucario/rotom/internal/timer"
"time"
)

Expand All @@ -12,10 +11,6 @@ type Dict struct {
expire *swiss.Map[string, int64]
}

func init() {
timer.Init()
}

func New() *Dict {
return &Dict{
data: swiss.New[string, any](64),
Expand All @@ -36,7 +31,7 @@ func (dict *Dict) Get(key string) (any, int) {
}

// key expired
now := timer.GetNanoTime()
now := time.Now().UnixNano()
if ts < now {
dict.delete(key)
return nil, KEY_NOT_EXIST
Expand Down Expand Up @@ -82,7 +77,7 @@ func (dict *Dict) SetTTL(key string, ttl int64) int {

// check key if already expired
ts, ok := dict.expire.Get(key)
if ok && ts < timer.GetNanoTime() {
if ok && ts < time.Now().UnixNano() {
dict.delete(key)
return 0
}
Expand All @@ -94,8 +89,9 @@ func (dict *Dict) SetTTL(key string, ttl int64) int {

func (dict *Dict) EvictExpired() {
var count int
now := time.Now().UnixNano()
dict.expire.All(func(key string, ts int64) bool {
if timer.GetNanoTime() > ts {
if now > ts {
dict.Delete(key)
}
count++
Expand Down
8 changes: 7 additions & 1 deletion internal/resp/resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
INTEGER = ':'
BULK = '$'
ARRAY = '*'
MAP = '%' // TODO: https://redis.io/docs/latest/develop/reference/protocol-spec/#maps
MAP = '%'
)

var (
Expand Down Expand Up @@ -161,6 +161,12 @@ func (w *Writer) WriteArrayHead(n int) {
w.b = append(w.b, CRLF...)
}

func (w *Writer) WriteMapHead(n int) {
w.b = append(w.b, MAP)
w.b = strconv.AppendUint(w.b, uint64(n), 10)
w.b = append(w.b, CRLF...)
}

// WriteBulk writes a RESP bulk string from a byte slice.
func (w *Writer) WriteBulk(bulk []byte) {
w.WriteBulkString(b2s(bulk))
Expand Down
23 changes: 0 additions & 23 deletions internal/timer/timer.go

This file was deleted.

0 comments on commit 7d4bc39

Please sign in to comment.