diff --git a/key_test.go b/key_test.go index 657f6cb..c0c3d2f 100644 --- a/key_test.go +++ b/key_test.go @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)) @@ -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) @@ -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") @@ -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) @@ -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")) @@ -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") @@ -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) @@ -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 { diff --git a/metadata.go b/metadata.go index e18f70e..5a0900c 100644 --- a/metadata.go +++ b/metadata.go @@ -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, } } diff --git a/nodis.go b/nodis.go index 817921a..7b65aa5 100644 --- a/nodis.go +++ b/nodis.go @@ -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) } } }() diff --git a/nodis_test.go b/nodis_test.go index 4fc1eea..ffc2ff0 100644 --- a/nodis_test.go +++ b/nodis_test.go @@ -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 { @@ -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++ { @@ -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) diff --git a/options.go b/options.go index f83ad8f..6b412b7 100644 --- a/options.go +++ b/options.go @@ -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. @@ -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, } diff --git a/store.go b/store.go index 27a0341..20667c9 100644 --- a/store.go +++ b/store.go @@ -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) @@ -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 { @@ -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() @@ -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() @@ -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 diff --git a/store_test.go b/store_test.go index 16c72eb..1e4050d 100644 --- a/store_test.go +++ b/store_test.go @@ -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) diff --git a/str_test.go b/str_test.go index fabe737..73ea6de 100644 --- a/str_test.go +++ b/str_test.go @@ -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") @@ -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 { @@ -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)