Skip to content

Commit

Permalink
rename variables,optimize entry.go
Browse files Browse the repository at this point in the history
  • Loading branch information
diiyw committed Oct 25, 2024
1 parent 07e2a08 commit 84d6347
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 42 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/diiyw/nodis
go 1.21

require (
github.com/cockroachdb/pebble v1.1.1
github.com/alecthomas/kong v0.9.0
github.com/cockroachdb/pebble v1.1.2
github.com/gorilla/websocket v1.5.3
github.com/tidwall/btree v1.7.0
google.golang.org/protobuf v1.34.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce h1:giXvy4KSc/6g/e
github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce/go.mod h1:9/y3cnZ5GKakj/H4y9r9GTjCvAFta7KLgSHPJJYc52M=
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b h1:r6VH0faHjZeQy818SGhaone5OnYfxFR/+AzdY3sf5aE=
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b/go.mod h1:Vz9DsVWQQhf3vs21MhPMZpMGSht7O/2vFW2xusFUVOs=
github.com/cockroachdb/pebble v1.1.1 h1:XnKU22oiCLy2Xn8vp1re67cXg4SAasg/WDt1NtcRFaw=
github.com/cockroachdb/pebble v1.1.1/go.mod h1:4exszw1r40423ZsmkG/09AFEG83I0uDgfujJdbL6kYU=
github.com/cockroachdb/pebble v1.1.2 h1:CUh2IPtR4swHlEj48Rhfzw6l/d0qA31fItcIszQVIsA=
github.com/cockroachdb/pebble v1.1.2/go.mod h1:4exszw1r40423ZsmkG/09AFEG83I0uDgfujJdbL6kYU=
github.com/cockroachdb/redact v1.1.5 h1:u1PMllDkdFfPWaNGMyLD1+so+aq3uUItthCFqzwPJ30=
github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg=
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAKVxetITBuuhv3BI9cMrmStnpT18zmgmTxunpo=
Expand Down
4 changes: 1 addition & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func info(n *Nodis, conn *redis.Conn, cmd redis.Command) {
`maxmemory_policy:noeviction` + "\r\n" +
`# Client` + "\r\n" +
`maxclients:10000` + "\r\n" +
`connected_clients:` + strconv.FormatInt(redis.ClientNum.Load(), 64) + "\r\n" +
`connected_clients:` + strconv.FormatInt(redis.ClientNum.Load(), 10) + "\r\n" +
`# Keyspace` + "\r\n" + keyspace +
"\r\n")
})
Expand Down Expand Up @@ -2660,8 +2660,6 @@ func zScan(n *Nodis, conn *redis.Conn, cmd redis.Command) {

func save(n *Nodis, conn *redis.Conn, cmd redis.Command) {
execCommand(conn, func() {
n.store.mu.Lock()
defer n.store.mu.Unlock()
n.store.flush()
conn.WriteString("OK")
})
Expand Down
2 changes: 1 addition & 1 deletion metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (m *metadata) isOk() bool {
return m.state&KeyStateNormal == KeyStateNormal
}

func (m *metadata) memoryDeleted() {
func (m *metadata) removeFromMemory() {
m.value = nil
}

Expand Down
2 changes: 1 addition & 1 deletion nodis.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Open(opt *Options) *Nodis {

// Snapshot saves the data to disk
func (n *Nodis) Snapshot() error {
return n.store.sg.Snapshot()
return n.store.ss.Snapshot()
}

// Close the store
Expand Down
32 changes: 16 additions & 16 deletions storage/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (e *Entry) encode() []byte {
return b
}

func (e *Entry) decode(b []byte) error {
func (e *Entry) from(b []byte) error {
if len(b) < 1 {
return ErrCorruptedData
}
Expand All @@ -46,39 +46,39 @@ func NewEntry(v ds.Value) *Entry {
return e
}

func parseEntry(data []byte) (*Entry, error) {
var entry = &Entry{}
if err := entry.decode(data); err != nil {
return nil, err
}
return entry, nil
}

func parseValue(typ ds.ValueType, data []byte) (ds.Value, error) {
func (e *Entry) GetValue() (ds.Value, error) {
var value ds.Value
switch typ {
switch ds.ValueType(e.Type) {
case ds.String:
v := str.NewString()
v.SetValue(data)
v.SetValue(e.Value)
value = v
case ds.ZSet:
z := zset.NewSortedSet()
z.SetValue(data)
z.SetValue(e.Value)
value = z
case ds.List:
l := list.NewLinkedList()
l.SetValue(data)
l.SetValue(e.Value)
value = l
case ds.Hash:
h := hash.NewHashMap()
h.SetValue(data)
h.SetValue(e.Value)
value = h
case ds.Set:
v := set.NewSet()
v.SetValue(data)
v.SetValue(e.Value)
value = v
default:
panic("unhandled default case")
}
return value, nil
}

func parseEntry(data []byte) (*Entry, error) {
var entry = &Entry{}
if err := entry.from(data); err != nil {
return nil, err
}
return entry, nil
}
4 changes: 2 additions & 2 deletions storage/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestValueEntry_EncodeDecode(t *testing.T) {
encoded := entry.encode()

decoded := &Entry{}
err := decoded.decode(encoded)
err := decoded.from(encoded)
if err != nil {
t.Errorf("decode failed: %v", err)
}
Expand Down Expand Up @@ -44,7 +44,7 @@ func TestParseValue(t *testing.T) {
if !reflect.DeepEqual(parsedEntry, entry) {
t.Errorf("parsedEntry = %v, want %v", parsedEntry, entry)
}
parsedValue, err := parseValue(ds.String, entry.Value)
parsedValue, err := parsedEntry.GetValue()
if err != nil {
t.Errorf("parseValue failed: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion storage/pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (p *Pebble) Get(key string) (ds.Value, error) {
if err != nil {
return nil, err
}
return parseValue(ds.ValueType(entry.Type), entry.Value)
return entry.GetValue()
}

// Put the value to the storage
Expand Down
26 changes: 13 additions & 13 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ import (
type store struct {
mu sync.RWMutex
metadata btree.Map[string, *metadata]
sg storage.Storage
ss storage.Storage
closed bool
watchMu sync.RWMutex
watchedKeys btree.Map[string, *list.LinkedListG[*redis.Conn]]
}

func newStore(sg storage.Storage) *store {
s := &store{sg: sg}
err := s.sg.Init()
func newStore(ss storage.Storage) *store {
s := &store{ss: ss}
err := s.ss.Init()
if err != nil {
log.Fatal(err)
}
s.sg.ScanKeys(func(key *ds.Key) bool {
s.ss.ScanKeys(func(key *ds.Key) bool {
var m = newMetadata()
m.key = key
m.state |= KeyStateNormal
Expand All @@ -40,6 +40,8 @@ func newStore(sg storage.Storage) *store {

// flush changed keys to storage
func (s *store) flush() {
s.mu.Lock()
defer s.mu.Unlock()
now := time.Now().UnixMilli()
s.metadata.Scan(func(key string, m *metadata) bool {
m.Lock()
Expand All @@ -51,7 +53,7 @@ func (s *store) flush() {
return true
}
// save to storage
err := s.sg.Put(m.key, m.value)
err := s.ss.Put(m.key, m.value)
if err != nil {
log.Println("Flush changes: ", err)
}
Expand All @@ -75,32 +77,30 @@ func (s *store) gc() {
return true
}
if m.modified() {
err := s.sg.Put(m.key, m.value)
err := s.ss.Put(m.key, m.value)
if err != nil {
log.Println("GC: ", err)
}
}
m.reset()
if m.count < 0 {
m.memoryDeleted()
m.removeFromMemory()
}
return true
})
}

// close the store
func (s *store) close() error {
s.mu.Lock()
defer s.mu.Unlock()
s.flush()
s.closed = true
return s.sg.Close()
s.flush()
return s.ss.Close()
}

// clear the store
func (s *store) clear() error {
s.mu.Lock()
defer s.mu.Unlock()
s.metadata.Clear()
return s.sg.Clear()
return s.ss.Clear()
}
4 changes: 2 additions & 2 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (tx *Tx) writeKey(key string, newFn func() ds.Value) *metadata {
return m
}
// if not found in memory, read from storage
v, err := tx.store.sg.Get(key)
v, err := tx.store.ss.Get(key)
if err != nil {
return tx.newKey(m, key, newFn)
}
Expand All @@ -101,7 +101,7 @@ func (tx *Tx) readKey(key string) *metadata {
return m
}
// if not found in memory, read from storage
v, err := tx.store.sg.Get(key)
v, err := tx.store.ss.Get(key)
if err != nil {
return m.empty()
}
Expand Down

0 comments on commit 84d6347

Please sign in to comment.