Skip to content

Commit

Permalink
Optimize codes
Browse files Browse the repository at this point in the history
  • Loading branch information
diiyw committed Aug 6, 2024
1 parent 803e764 commit 4cc5e04
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 64 deletions.
52 changes: 26 additions & 26 deletions key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
func TestKey_Expire(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{
Path: "testdata",
TidyDuration: 60 * time.Second,
Path: "testdata",
GCDuration: 60 * time.Second,
})
n.Set("test", []byte("test1"), false)
n.Expire("test", 2)
Expand Down Expand Up @@ -43,8 +43,8 @@ func TestKey_ExpireXX(t *testing.T) {
func TestKey_ExpireNX(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{
Path: "testdata",
TidyDuration: 60 * time.Second,
Path: "testdata",
GCDuration: 60 * time.Second,
})
n.Set("test", []byte("test1"), false)
n.ExpireNX("test", 2)
Expand All @@ -61,8 +61,8 @@ func TestKey_ExpireNX(t *testing.T) {
func TestKey_ExpireLT(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{
Path: "testdata",
TidyDuration: 60 * time.Second,
Path: "testdata",
GCDuration: 60 * time.Second,
})
n.Set("test", []byte("test1"), false)
v := n.ExpireLT("test", 2)
Expand All @@ -79,8 +79,8 @@ func TestKey_ExpireLT(t *testing.T) {
func TestKey_ExpireGT(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{
Path: "testdata",
TidyDuration: 60 * time.Second,
Path: "testdata",
GCDuration: 60 * time.Second,
})
n.Set("test", []byte("test1"), false)
v := n.ExpireGT("test", 2)
Expand All @@ -97,8 +97,8 @@ func TestKey_ExpireGT(t *testing.T) {
func TestKey_ExpireAt(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{
Path: "testdata",
TidyDuration: 60 * time.Second,
Path: "testdata",
GCDuration: 60 * time.Second,
})
n.Set("test", []byte("test1"), false)
n.ExpireAt("test", time.Now().Add(2*time.Second))
Expand All @@ -112,8 +112,8 @@ func TestKey_ExpireAt(t *testing.T) {
func TestKey_TTL(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{
Path: "testdata",
TidyDuration: 60 * time.Second,
Path: "testdata",
GCDuration: 60 * time.Second,
})
n.Set("test", []byte("test1"), false)
n.Expire("test", 300)
Expand All @@ -131,8 +131,8 @@ func TestKey_TTL(t *testing.T) {
func TestKey_Rename(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{
Path: "testdata",
TidyDuration: 60 * time.Second,
Path: "testdata",
GCDuration: 60 * time.Second,
})
n.Set("test", []byte("test1"), false)
n.Rename("test", "test2")
Expand Down Expand Up @@ -166,8 +166,8 @@ func TestKey_RenameNX(t *testing.T) {
func TestKey_Keys(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{
Path: "testdata",
TidyDuration: 60 * time.Second,
Path: "testdata",
GCDuration: 60 * time.Second,
})
n.Set("test1", []byte("test1"), false)
n.Set("test2", []byte("test2"), false)
Expand All @@ -181,9 +181,9 @@ func TestKey_Keys(t *testing.T) {
func TestKey_Type(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{
Path: "testdata",
TidyDuration: 60 * time.Second,
Filesystem: &fs.Disk{},
Path: "testdata",
GCDuration: 60 * time.Second,
Filesystem: &fs.Disk{},
})
n.Set("test1", []byte("test1"), false)
n.LPush("test2", []byte("test2"))
Expand All @@ -210,9 +210,9 @@ func TestKey_Type(t *testing.T) {
t.Fatalf("Close() = %v, want %v", err, nil)
}
n = Open(&Options{
Path: "testdata",
TidyDuration: 60 * time.Second,
Filesystem: &fs.Disk{},
Path: "testdata",
GCDuration: 60 * time.Second,
Filesystem: &fs.Disk{},
})
if n.Type("test1") != "string" {
t.Errorf("Type() = %v, want %v", n.Type("test1"), "string")
Expand All @@ -234,8 +234,8 @@ func TestKey_Type(t *testing.T) {
func TestKey_Scan(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{
Path: "testdata",
TidyDuration: 60 * time.Second,
Path: "testdata",
GCDuration: 60 * time.Second,
})
n.Set("test1", []byte("test1"), false)
n.Set("test2", []byte("test2"), false)
Expand Down Expand Up @@ -293,8 +293,8 @@ func TestKey_Scan(t *testing.T) {
func TestKey_Exists(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{
Path: "testdata",
TidyDuration: 60 * time.Second,
Path: "testdata",
GCDuration: 60 * time.Second,
})
n.Set("test", []byte("test1"), false)
if n.Exists("test") != 1 {
Expand Down
10 changes: 5 additions & 5 deletions metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ type metadata struct {
writeable bool
}

func newMetadata(key *Key, value ds.Value, writeable bool) *metadata {
func newMetadata() *metadata {
return &metadata{
RWMutex: new(sync.RWMutex),
useTimes: 1,
key: key,
value: value,
writeable: writeable,
useTimes: 0,
key: &Key{},
value: nil,
writeable: false,
}
}

Expand Down
6 changes: 3 additions & 3 deletions nodis.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ func Open(opt *Options) *Nodis {
}
n.store = newStore(opt.Path, opt.FileSize, opt.Filesystem)
go func() {
if opt.TidyDuration != 0 {
if opt.GCDuration != 0 {
for {
time.Sleep(opt.TidyDuration)
n.store.tidy(opt.MaxKeyUseTimes)
time.Sleep(opt.GCDuration)
n.store.gc(opt.MaxKeyUseTimes)
}
}
}()
Expand Down
18 changes: 9 additions & 9 deletions nodis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

func TestNodis_Open(t *testing.T) {
opt := Options{
Path: "testdata",
TidyDuration: 60 * time.Second,
FileSize: FileSizeGB,
Path: "testdata",
GCDuration: 60 * time.Second,
FileSize: FileSizeGB,
}
got := Open(&opt)
if got == nil {
Expand All @@ -24,9 +24,9 @@ func TestNodis_Open(t *testing.T) {
func TestNodis_OpenAndCloseBigdata10000(t *testing.T) {
_ = os.RemoveAll("testdata")
opt := Options{
Path: "testdata",
TidyDuration: 60 * time.Second,
FileSize: FileSizeGB,
Path: "testdata",
GCDuration: 60 * time.Second,
FileSize: FileSizeGB,
}
n := Open(&opt)
for i := 0; i < 10000; i++ {
Expand Down Expand Up @@ -100,11 +100,11 @@ func TestNodis_SnapshotChanged(t *testing.T) {
_ = n.Close()
}

func TestNodis_Tidy(t *testing.T) {
func TestNodis_GC(t *testing.T) {
_ = os.RemoveAll("testdata")
opt := &Options{
Path: "testdata",
TidyDuration: time.Second,
Path: "testdata",
GCDuration: time.Second,
}
n := Open(opt)
n.SetEX("test", []byte("test"), 1)
Expand Down
12 changes: 6 additions & 6 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ type Options struct {
// Path is the path to the database.
Path string

// TidyDuration is the interval which the database is flushing unused keys to disk.
// GCDuration is the interval which the database is flushing unused keys to disk.
// This is useful for reducing the risk of data loss in the event of a crash.
// It is also used for refreshing hot keys.
TidyDuration time.Duration
GCDuration time.Duration

// MaxKeyUseTimes is the maximum number of times a key can be used before it is considered hot.
// The default value is 0, which means that the key will never be considered hot.
// Hot keys are refreshed every TidyDuration.
// Hot keys are refreshed every GCDuration.
MaxKeyUseTimes uint64

// FileSize is the size of each file. The default value is 1GB.
Expand All @@ -42,7 +42,7 @@ type Options struct {
}

var DefaultOptions = &Options{
Path: "data",
FileSize: FileSizeGB,
TidyDuration: 60 * time.Second,
Path: "data",
FileSize: FileSizeGB,
GCDuration: 60 * time.Second,
}
15 changes: 7 additions & 8 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ func newStore(path string, fileSize int64, filesystem fs.Fs) *store {
if len(data) == 0 {
break
}
var m = newMetadata(&Key{}, nil, false)
m.unmarshal(data)
var m = newMetadata().unmarshal(data)
data = data[metadataSize:]
m.state |= KeyStateNormal
s.metadata.Set(key, m)
Expand Down Expand Up @@ -206,8 +205,8 @@ func (s *store) saveKeyIndex() error {
return nil
}

// tidy removes expired and unused keys
func (s *store) tidy(keyMaxUseTimes uint64) {
// gc removes expired and unused keys
func (s *store) gc(keyMaxUseTimes uint64) {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
Expand All @@ -216,7 +215,7 @@ func (s *store) tidy(keyMaxUseTimes uint64) {
now := time.Now().UnixMilli()
err := s.saveKeyIndex()
if err != nil {
log.Println("Tidy: ", err)
log.Println("GC: ", err)
}
s.metadata.Scan(func(key string, m *metadata) bool {
m.Lock()
Expand All @@ -228,9 +227,9 @@ func (s *store) tidy(keyMaxUseTimes uint64) {
if m.useTimes < keyMaxUseTimes {
if m.modified() {
// saveData to disk
err := s.saveMetadata(key, m)
err = s.saveMetadata(key, m)
if err != nil {
log.Println("Tidy: ", err)
log.Println("GC: ", err)
}
}
m.reset()
Expand Down Expand Up @@ -297,7 +296,7 @@ func (s *store) saveMetadata(name string, m *metadata) error {

// saveValueEntry a key-value pair into store
func (s *store) saveValueEntry(entry *ValueEntry) error {
var m = newMetadata(&Key{}, nil, false)
var m = newMetadata()
offset, err := s.check()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func TestStorePutRaw(t *testing.T) {
expiration := time.Now().Unix() + 3600
var e = newValueEntry(name, strVal, expiration)
data := e.encode()
m := newMetadata(&Key{}, nil, false)
m := newMetadata()
m.expiration = expiration
// Call the saveValueRaw method
err := store.saveValueRaw(name, m, data)
Expand Down
12 changes: 6 additions & 6 deletions str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

func TestStr_Set(t *testing.T) {
n := Open(&Options{
Path: "testdata",
TidyDuration: 60 * time.Second,
Path: "testdata",
GCDuration: 60 * time.Second,
})
n.Set("a", []byte("b"), false)
v := n.Get("a")
Expand All @@ -29,8 +29,8 @@ func TestStr_Set(t *testing.T) {

func TestStr_SetBit(t *testing.T) {
n := Open(&Options{
Path: "testdata",
TidyDuration: 60 * time.Second,
Path: "testdata",
GCDuration: 60 * time.Second,
})
n.SetBit("a", 0, true)
if n.GetBit("a", 0) != 1 {
Expand All @@ -44,8 +44,8 @@ func TestStr_SetBit(t *testing.T) {

func TestStr_BitCount(t *testing.T) {
n := Open(&Options{
Path: "testdata",
TidyDuration: 60 * time.Second,
Path: "testdata",
GCDuration: 60 * time.Second,
})
n.SetBit("a", 0, true)
n.SetBit("a", 1, true)
Expand Down

0 comments on commit 4cc5e04

Please sign in to comment.