Skip to content

Commit

Permalink
feat: add lastAccessd for dict object
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Jul 23, 2024
1 parent aeba7ef commit 00d8c16
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 39 deletions.
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,4 @@ build:
go build -o rotom -ldflags "-s -w -X main.buildTime=$(shell date +%y%m%d_%H%M%S%z)"

build-docker:
docker build --build-arg BUILD_TIME=$(shell date +%y%m%d_%H%M%S%z) -t rotom .

# tmp command
# rsync -av --exclude='.git' rotom/ 2:~/xgz/rotom
docker build --build-arg BUILD_TIME=$(shell date +%y%m%d_%H%M%S%z) -t rotom .
20 changes: 14 additions & 6 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ func startup() {
AppendFileName: "appendonly-test.aof",
}
os.Remove(config.AppendFileName)
if err := InitDB(config); err != nil {
log.Panic().Msgf("init db error: %v", err)
}
if err := initServer(config); err != nil {
log.Panic().Msgf("init server error: %v", err)
}
config4Server(config)
server.aeLoop.AddRead(server.fd, AcceptHandler, nil)
server.aeLoop.AddTimeEvent(AE_NORMAL, 500, CheckOutOfMemory, nil)
server.aeLoop.AeMain()
Expand Down Expand Up @@ -220,3 +215,16 @@ func TestCommand(t *testing.T) {
rdb.Close()
})
}

func TestConfig(t *testing.T) {
assert := assert.New(t)

cfg, _ := LoadConfig("config.json")
assert.Equal(cfg.Port, 6379)

_, err := LoadConfig("not-exist.json")
assert.NotNil(err)

_, err = LoadConfig("go.mod")
assert.NotNil(err)
}
43 changes: 33 additions & 10 deletions internal/dict/dict.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
package dict

import (
"sync/atomic"
"time"

"github.com/cockroachdb/swiss"
)

var (
_sec atomic.Uint32
_nsec atomic.Int64
)

func init() {
// init backend ticker
tk := time.NewTicker(time.Microsecond)
go func() {
for t := range tk.C {
_sec.Store(uint32(t.Unix()))
_nsec.Store(t.UnixNano())
}
}()
}

// Dict is the hashmap for Rotom.
type Dict struct {
data *swiss.Map[string, *Object]
Expand All @@ -25,28 +42,33 @@ func (dict *Dict) Get(key string) (*Object, bool) {
return nil, false
}

// if object.hasTTL {
// ttl, ok := dict.expire.Get(key)
// if ttl > 0 || !ok { //
// }
// }
if object.hasTTL {
nsecTTL, ok := dict.expire.Get(key)
if !ok || nsecTTL < _nsec.Load() {
// expired
dict.data.Delete(key)
dict.expire.Delete(key)
return nil, false
}
}

switch object.typ {
case TypeZipMapC, TypeZipSetC:
object.data.(Compressor).Decompress()
object.typ -= 1
}

object.updateLRU()
// update access time
object.lastAccessd = _sec.Load()

return object, true
}

func (dict *Dict) Set(key string, typ Type, data any) {
dict.data.Put(key, &Object{
typ: typ,
lru: uint32(time.Now().Unix()),
data: data,
typ: typ,
lastAccessd: _sec.Load(),
data: data,
})
}

Expand All @@ -58,10 +80,11 @@ func (dict *Dict) Remove(key string) bool {
}

func (dict *Dict) SetTTL(key string, expiration int64) bool {
_, ok := dict.data.Get(key)
object, ok := dict.data.Get(key)
if !ok {
return false
}
object.hasTTL = true
dict.expire.Put(key, expiration)
return true
}
Expand Down
11 changes: 11 additions & 0 deletions internal/dict/dict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ func TestDict(t *testing.T) {
assert := assert.New(t)
dict := New()

dict.Set("key1", TypeString, []byte("hello"))
object, ok := dict.Get("key1")
assert.True(ok)
assert.Equal(object.Data(), []byte("hello"))
assert.Equal(object.Type(), TypeString)
}

func TestDictMultiSet(t *testing.T) {
assert := assert.New(t)
dict := New()

for i := 0; i < 10000; i++ {
key, value := genKV(rand.Int())
dict.Set(key, TypeString, value)
Expand Down
13 changes: 5 additions & 8 deletions internal/dict/object.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dict

import "time"

// Type defines all rotom data types.
type Type byte

Expand All @@ -23,17 +21,16 @@ type Compressor interface {
Decompress()
}

// Object is the basic unit for storing in dict.
// Object is the basic element for storing in dict.
type Object struct {
typ Type
lru uint32
data any
typ Type
hasTTL bool
lastAccessd uint32
data any
}

func (o *Object) Type() Type { return o.typ }

func (o *Object) Data() any { return o.data }

func (o *Object) SetData(data any) { o.data = data }

func (o *Object) updateLRU() { o.lru = uint32(time.Now().Unix()) }
22 changes: 11 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ var (
buildTime string
)

func runDebug() {
go http.ListenAndServe(":6060", nil)
}

func initLogger() zerolog.Logger {
return zerolog.
New(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.DateTime}).
Expand All @@ -28,6 +24,15 @@ func initLogger() zerolog.Logger {
Logger()
}

func config4Server(config *Config) {
if err := initServer(config); err != nil {
log.Fatal().Msgf("init server error: %v", err)
}
if err := InitDB(config); err != nil {
log.Fatal().Msgf("init db error: %v", err)
}
}

func main() {
var path string
var debug bool
Expand All @@ -43,14 +48,9 @@ func main() {
if err != nil {
log.Fatal().Msgf("load config error: %v", err)
}
if err = initServer(config); err != nil {
log.Fatal().Msgf("init server error: %v", err)
}
if err = InitDB(config); err != nil {
log.Fatal().Msgf("init db error: %v", err)
}
config4Server(config)
if debug {
runDebug()
go http.ListenAndServe(":6060", nil)
}

log.Info().Int("port", config.Port).Msg("running on")
Expand Down

0 comments on commit 00d8c16

Please sign in to comment.