From f2910f8a776411205d079a6ffcce5147498739ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=82=98=EC=83=81=EC=9A=B0=28Roy=29?= Date: Fri, 18 Mar 2022 13:47:56 +0900 Subject: [PATCH] refactor: Unexport unnecessary structs, fields (#8) --- commands.go | 72 +++++++++++++++++++-------------------- constants.go | 29 ++++++++-------- handlers.go | 35 +++++++++---------- minimemcached.go | 78 +++++++++++++++++++++---------------------- minimemcached_test.go | 24 ++++++------- server.go | 12 +++---- 6 files changed, 125 insertions(+), 125 deletions(-) diff --git a/commands.go b/commands.go index 9c0334b..ca8ce1d 100644 --- a/commands.go +++ b/commands.go @@ -22,8 +22,8 @@ func (m *MiniMemcached) gets(keys []string, conn net.Conn) { item := m.items[k] m.mu.RUnlock() if item != nil { - result = append(result, []byte(fmt.Sprintf("%s %s %d %d %d\r\n", value, k, item.Flags, len(item.Value), item.CASToken))...) - result = append(result, item.Value...) + result = append(result, []byte(fmt.Sprintf("%s %s %d %d %d\r\n", value, k, item.flags, len(item.value), item.casToken))...) + result = append(result, item.value...) result = append(result, crlf...) } } @@ -32,12 +32,12 @@ func (m *MiniMemcached) gets(keys []string, conn net.Conn) { } // set() handles memcached `set` command. -func (m *MiniMemcached) set(key string, item *Item, bytes int, conn net.Conn) { +func (m *MiniMemcached) set(key string, item *item, bytes int, conn net.Conn) { if !isLegalKey(key) { _, _ = conn.Write(resultClientErrBadCliFormat) return } - if !isLegalValue(bytes, item.Value) { + if !isLegalValue(bytes, item.value) { _, _ = conn.Write(resultClientErrBadDataChunk) return } @@ -45,20 +45,20 @@ func (m *MiniMemcached) set(key string, item *Item, bytes int, conn net.Conn) { m.invalidate(key) m.mu.Lock() - m.CASToken += 1 - item.CASToken = m.CASToken + m.casToken += 1 + item.casToken = m.casToken m.items[key] = item m.mu.Unlock() _, _ = conn.Write(resultStored) } // add() handles memcached `add` command. -func (m *MiniMemcached) add(key string, item *Item, bytes int, conn net.Conn) { +func (m *MiniMemcached) add(key string, item *item, bytes int, conn net.Conn) { if !isLegalKey(key) { _, _ = conn.Write(resultClientErrBadCliFormat) return } - if !isLegalValue(bytes, item.Value) { + if !isLegalValue(bytes, item.value) { _, _ = conn.Write(resultClientErrBadDataChunk) return } @@ -72,19 +72,19 @@ func (m *MiniMemcached) add(key string, item *Item, bytes int, conn net.Conn) { return } - m.CASToken += 1 - item.CASToken = m.CASToken + m.casToken += 1 + item.casToken = m.casToken m.items[key] = item _, _ = conn.Write(resultStored) } // replace() handles memcached `replace` command. -func (m *MiniMemcached) replace(key string, item *Item, bytes int, conn net.Conn) { +func (m *MiniMemcached) replace(key string, item *item, bytes int, conn net.Conn) { if !isLegalKey(key) { _, _ = conn.Write(resultClientErrBadCliFormat) return } - if !isLegalValue(bytes, item.Value) { + if !isLegalValue(bytes, item.value) { _, _ = conn.Write(resultClientErrBadDataChunk) return } @@ -97,8 +97,8 @@ func (m *MiniMemcached) replace(key string, item *Item, bytes int, conn net.Conn _, _ = conn.Write(resultNotStored) return } - m.CASToken += 1 - item.CASToken = m.CASToken + m.casToken += 1 + item.casToken = m.casToken m.items[key] = item _, _ = conn.Write(resultStored) } @@ -123,9 +123,9 @@ func (m *MiniMemcached) append(key string, bytes int, value []byte, conn net.Con _, _ = conn.Write(resultNotStored) return } - m.CASToken += 1 - prevItem.CASToken = m.CASToken - prevItem.Value = append(prevItem.Value, value...) + m.casToken += 1 + prevItem.casToken = m.casToken + prevItem.value = append(prevItem.value, value...) _, _ = conn.Write(resultStored) } @@ -149,9 +149,9 @@ func (m *MiniMemcached) prepend(key string, bytes int, value []byte, conn net.Co _, _ = conn.Write(resultNotStored) return } - m.CASToken += 1 - prevItem.CASToken = m.CASToken - prevItem.Value = append(value, prevItem.Value...) + m.casToken += 1 + prevItem.casToken = m.casToken + prevItem.value = append(value, prevItem.value...) _, _ = conn.Write(resultStored) } @@ -191,14 +191,14 @@ func (m *MiniMemcached) incr(key string, incrValue uint64, conn net.Conn) { return } - numericItemValue, isNumeric := getNumericValueFromByteArray(item.Value) + numericItemValue, isNumeric := getNumericValueFromByteArray(item.value) if !isNumeric { _, _ = conn.Write(resultClientErrIncrDecrNonNumericValue) return } - m.CASToken += 1 - item.CASToken = m.CASToken + m.casToken += 1 + item.casToken = m.casToken var ( numericItemValueInt big.Int @@ -219,7 +219,7 @@ func (m *MiniMemcached) incr(key string, incrValue uint64, conn net.Conn) { } value := []byte(strconv.FormatUint(incrementedValue, 10)) - item.Value = value + item.value = value result := append(value, crlf...) _, _ = conn.Write(result) } @@ -240,13 +240,13 @@ func (m *MiniMemcached) decr(key string, decrValue uint64, conn net.Conn) { _, _ = conn.Write(resultNotFound) return } - numericItemValue, isNumeric := getNumericValueFromByteArray(item.Value) + numericItemValue, isNumeric := getNumericValueFromByteArray(item.value) if !isNumeric { _, _ = conn.Write(resultClientErrIncrDecrNonNumericValue) return } - m.CASToken += 1 - item.CASToken = m.CASToken + m.casToken += 1 + item.casToken = m.casToken var decrementedValue uint64 if numericItemValue < decrValue { decrementedValue = 0 @@ -254,7 +254,7 @@ func (m *MiniMemcached) decr(key string, decrValue uint64, conn net.Conn) { decrementedValue = numericItemValue - decrValue } value := []byte(strconv.FormatUint(decrementedValue, 10)) - item.Value = value + item.value = value result := append(value, crlf...) _, _ = conn.Write(result) } @@ -275,7 +275,7 @@ func (m *MiniMemcached) touch(key string, expiration int32, conn net.Conn) { _, _ = conn.Write(resultNotFound) return } - item.Expiration = expiration + item.expiration = expiration _, _ = conn.Write(resultTouched) } @@ -283,18 +283,18 @@ func (m *MiniMemcached) touch(key string, expiration int32, conn net.Conn) { func (m *MiniMemcached) flushAll(conn net.Conn) { m.mu.Lock() defer m.mu.Unlock() - m.CASToken += 1 - m.items = map[string]*Item{} + m.casToken += 1 + m.items = map[string]*item{} _, _ = conn.Write(resultOK) } // cas() handles memcached `cas` command. -func (m *MiniMemcached) cas(key string, item *Item, bytes int, casToken uint64, conn net.Conn) { +func (m *MiniMemcached) cas(key string, item *item, bytes int, casToken uint64, conn net.Conn) { if !isLegalKey(key) { _, _ = conn.Write(resultClientErrBadCliFormat) return } - if !isLegalValue(bytes, item.Value) { + if !isLegalValue(bytes, item.value) { _, _ = conn.Write(resultClientErrBadDataChunk) return } @@ -308,12 +308,12 @@ func (m *MiniMemcached) cas(key string, item *Item, bytes int, casToken uint64, _, _ = conn.Write(resultNotFound) return } - if prevItem.CASToken != casToken { + if prevItem.casToken != casToken { _, _ = conn.Write(resultExists) return } - m.CASToken += 1 - item.CASToken = m.CASToken + m.casToken += 1 + item.casToken = m.casToken m.items[key] = item _, _ = conn.Write(resultStored) } diff --git a/constants.go b/constants.go index 42d2857..602012e 100644 --- a/constants.go +++ b/constants.go @@ -3,20 +3,20 @@ package minimemcached import "fmt" const ( - GET = "get" - GETS = "gets" - CAS = "cas" - SET = "set" - TOUCH = "touch" - ADD = "add" - REPLACE = "replace" - APPEND = "append" - PREPEND = "prepend" - DELETE = "delete" - INCR = "incr" - DECR = "decr" - FLUSH_ALL = "flush_all" - VERSION = "version" + getCmd = "get" + getsCmd = "gets" + casCmd = "cas" + setCmd = "set" + touchCmd = "touch" + addCmd = "add" + replaceCmd = "replace" + appendCmd = "append" + prependCmd = "prepend" + deleteCmd = "delete" + incrCmd = "incr" + decrCmd = "decr" + flushAllCmd = "flush_all" + versionCmd = "version" ) var ( @@ -35,7 +35,6 @@ var ( resultClientErrInvalidExpTimeArg = []byte("CLIENT_ERROR invalid exptime argument\r\n") resultEnd = []byte("END\r\n") resultErr = []byte("ERROR\r\n") - resultNotImplementedCmdErr = []byte("SERVER_ERROR command yet not implemented in mini-memcached.\r\n") resultVersion = []byte(fmt.Sprintf("VERSION mini-memcached %s\r\n", Version)) value = "VALUE" ) diff --git a/handlers.go b/handlers.go index 08a8969..b456b44 100644 --- a/handlers.go +++ b/handlers.go @@ -54,16 +54,17 @@ func handleSet(m *MiniMemcached, cmdLine []string, value []byte, conn net.Conn) return } - item := &Item{ - Flags: uint32(flags), - Value: value, - Expiration: int32(expiration), + item := &item{ + flags: uint32(flags), + value: value, + expiration: int32(expiration), createdAt: m.clock.Now().Unix(), } m.set(key, item, bytes, conn) } +// handleAdd() handles `add` request. func handleAdd(m *MiniMemcached, cmdLine []string, value []byte, conn net.Conn) { if len(cmdLine) != 5 { _, _ = conn.Write(resultErr) @@ -89,10 +90,10 @@ func handleAdd(m *MiniMemcached, cmdLine []string, value []byte, conn net.Conn) return } - item := &Item{ - Flags: uint32(flags), - Value: value, - Expiration: int32(expiration), + item := &item{ + flags: uint32(flags), + value: value, + expiration: int32(expiration), createdAt: m.clock.Now().Unix(), } @@ -125,10 +126,10 @@ func handleReplace(m *MiniMemcached, cmdLine []string, value []byte, conn net.Co return } - item := &Item{ - Flags: uint32(flags), - Value: value, - Expiration: int32(expiration), + item := &item{ + flags: uint32(flags), + value: value, + expiration: int32(expiration), createdAt: m.clock.Now().Unix(), } @@ -152,7 +153,7 @@ func handleAppend(m *MiniMemcached, cmdLine []string, value []byte, conn net.Con m.append(key, bytes, value, conn) } -// handlePrepend() handles `append` requests. +// handlePrepend() handles `prepend` requests. func handlePrepend(m *MiniMemcached, cmdLine []string, value []byte, conn net.Conn) { if len(cmdLine) != 5 { _, _ = conn.Write(resultErr) @@ -270,10 +271,10 @@ func handleCas(m *MiniMemcached, cmdLine []string, value []byte, conn net.Conn) return } - item := &Item{ - Flags: uint32(flags), - Value: value, - Expiration: int32(expiration), + item := &item{ + flags: uint32(flags), + value: value, + expiration: int32(expiration), createdAt: m.clock.Now().Unix(), } diff --git a/minimemcached.go b/minimemcached.go index daf8336..8068a6d 100644 --- a/minimemcached.go +++ b/minimemcached.go @@ -14,14 +14,14 @@ import ( ) const ( - Version = "1.0.1" + Version = "1.1.0" ) type MiniMemcached struct { - server *Server + *server mu sync.RWMutex - items map[string]*Item - CASToken uint64 + items map[string]*item + casToken uint64 port uint16 clock clock.Clock } @@ -34,21 +34,21 @@ type Config struct { Port uint16 } -// Item is an object stored in mini-memcached. -type Item struct { - // Value is the actual data stored in the item. - Value []byte - // Flags is a 32-bit unsigned integer that mini-memcached stores with the data +// item is an object stored in mini-memcached. +type item struct { + // value is the actual data stored in the item. + value []byte + // flags is a 32-bit unsigned integer that mini-memcached stores with the data // provided by the user. - Flags uint32 - // Expiration is the expiration time in seconds. - // 0 means no delay. IF Expiration is more than 30 days, mini-memcached + flags uint32 + // expiration is the expiration time in seconds. + // 0 means no delay. IF expiration is more than 30 days, mini-memcached // uses it as a UNIX timestamp for expiration. - Expiration int32 - // CASToken is a unique unsigned 64-bit value of an existing item. - CASToken uint64 + expiration int32 + // casToken is a unique unsigned 64-bit value of an existing item. + casToken uint64 // createdAt is UNIX timestamp of the time when item has been created. - // It is used for invalidations along with Expiration. + // It is used for invalidations along with expiration. createdAt int64 } @@ -57,8 +57,8 @@ type Option func(m *MiniMemcached) // newMiniMemcached returns a newMiniMemcached, non-started, MiniMemcached object. func newMiniMemcached(opts ...Option) *MiniMemcached { m := MiniMemcached{ - items: map[string]*Item{}, - CASToken: 0, + items: map[string]*item{}, + casToken: 0, clock: clock.New(), } @@ -69,7 +69,7 @@ func newMiniMemcached(opts ...Option) *MiniMemcached { return &m } -// WithClock applies custom Clock interface. Clock will be used when Item is created +// WithClock applies custom Clock interface. Clock will be used when item is created. func WithClock(clk clock.Clock) Option { return func(m *MiniMemcached) { m.clock = clk @@ -87,7 +87,7 @@ func Run(cfg *Config, opts ...Option) (*MiniMemcached, error) { func (m *MiniMemcached) Close() { m.mu.Lock() m.items = nil - m.server.close() + m.close() m.mu.Unlock() log.Info().Msg("closed mini-memcached.") } @@ -122,7 +122,7 @@ func (m *MiniMemcached) newServer() { func (m *MiniMemcached) serve() { for { - conn, err := m.server.l.Accept() + conn, err := m.l.Accept() if err != nil { return } @@ -146,11 +146,11 @@ func (m *MiniMemcached) serveConn(conn net.Conn) { cmdLine := strings.Split(req, " ") cmd := strings.ToLower(cmdLine[0]) switch cmd { - case GET: + case getCmd: handleGet(m, cmdLine, conn) - case GETS: + case getsCmd: handleGets(m, cmdLine, conn) - case SET: + case setCmd: value, err := reader.ReadBytes('\n') if err != nil { handleErr(conn) @@ -158,7 +158,7 @@ func (m *MiniMemcached) serveConn(conn net.Conn) { value = gobytes.TrimSuffix(value, crlf) handleSet(m, cmdLine, value, conn) - case ADD: + case addCmd: value, err := reader.ReadBytes('\n') if err != nil { handleErr(conn) @@ -166,7 +166,7 @@ func (m *MiniMemcached) serveConn(conn net.Conn) { value = gobytes.TrimSuffix(value, crlf) handleAdd(m, cmdLine, value, conn) - case REPLACE: + case replaceCmd: value, err := reader.ReadBytes('\n') if err != nil { handleErr(conn) @@ -174,7 +174,7 @@ func (m *MiniMemcached) serveConn(conn net.Conn) { value = gobytes.TrimSuffix(value, crlf) handleReplace(m, cmdLine, value, conn) - case APPEND: + case appendCmd: value, err := reader.ReadBytes('\n') if err != nil { handleErr(conn) @@ -182,7 +182,7 @@ func (m *MiniMemcached) serveConn(conn net.Conn) { value = gobytes.TrimSuffix(value, crlf) handleAppend(m, cmdLine, value, conn) - case PREPEND: + case prependCmd: value, err := reader.ReadBytes('\n') if err != nil { handleErr(conn) @@ -190,17 +190,17 @@ func (m *MiniMemcached) serveConn(conn net.Conn) { value = gobytes.TrimSuffix(value, crlf) handlePrepend(m, cmdLine, value, conn) - case DELETE: + case deleteCmd: handleDelete(m, cmdLine, conn) - case INCR: + case incrCmd: handleIncr(m, cmdLine, conn) - case DECR: + case decrCmd: handleDecr(m, cmdLine, conn) - case TOUCH: + case touchCmd: handleTouch(m, cmdLine, conn) - case FLUSH_ALL: + case flushAllCmd: handleFlushAll(m, conn) - case CAS: + case casCmd: value, err := reader.ReadBytes('\n') if err != nil { handleErr(conn) @@ -208,7 +208,7 @@ func (m *MiniMemcached) serveConn(conn net.Conn) { value = gobytes.TrimSuffix(value, crlf) handleCas(m, cmdLine, value, conn) - case VERSION: + case versionCmd: handleVersion(m, conn) default: handleErr(conn) @@ -225,17 +225,17 @@ func (m *MiniMemcached) invalidate(key string) { if item == nil { return } - if item.Expiration == 0 { + if item.expiration == 0 { return } - if item.Expiration > ttlUnixTimestamp { - if currentTimestamp > int64(item.Expiration) { + if item.expiration > ttlUnixTimestamp { + if currentTimestamp > int64(item.expiration) { delete(m.items, key) return } return } - if currentTimestamp-item.createdAt >= int64(item.Expiration) { + if currentTimestamp-item.createdAt >= int64(item.expiration) { delete(m.items, key) return } diff --git a/minimemcached_test.go b/minimemcached_test.go index 2a28cac..4405ae9 100644 --- a/minimemcached_test.go +++ b/minimemcached_test.go @@ -242,8 +242,8 @@ func TestCASToken(t *testing.T) { t.Error("i1 nil") return } - if i1.CASToken != 1 { - t.Errorf("i1.CASToken wrong. want: 1, got: %d", i1.CASToken) + if i1.casToken != 1 { + t.Errorf("i1.casToken wrong. want: 1, got: %d", i1.casToken) return } @@ -252,8 +252,8 @@ func TestCASToken(t *testing.T) { t.Error("i2 nil") return } - if i2.CASToken != 2 { - t.Errorf("i2.CASToken wrong. want: 2, got: %d", i2.CASToken) + if i2.casToken != 2 { + t.Errorf("i2.casToken wrong. want: 2, got: %d", i2.casToken) return } @@ -267,8 +267,8 @@ func TestCASToken(t *testing.T) { t.Error("i1 nil") return } - if i1.CASToken != 3 { - t.Errorf("i1.CASToken wrong. want: 3, got: %d", i1.CASToken) + if i1.casToken != 3 { + t.Errorf("i1.casToken wrong. want: 3, got: %d", i1.casToken) return } @@ -282,8 +282,8 @@ func TestCASToken(t *testing.T) { t.Error("i3 nil") return } - if i3.CASToken != 4 { - t.Errorf("i3.CASToken wrong. want: 4, got: %d", i3.CASToken) + if i3.casToken != 4 { + t.Errorf("i3.casToken wrong. want: 4, got: %d", i3.casToken) return } @@ -292,8 +292,8 @@ func TestCASToken(t *testing.T) { t.Error("i2 nil") return } - if i2.CASToken != 2 { - t.Errorf("i2.CASToken wrong. want: 2, got: %d", i2.CASToken) + if i2.casToken != 2 { + t.Errorf("i2.casToken wrong. want: 2, got: %d", i2.casToken) return } } @@ -486,7 +486,7 @@ func writeAppend(port uint16, key string, value []byte) error { return err } - message := fmt.Sprintf("%s %s %d %d %d", APPEND, key, 0, 0, len(value)) + message := fmt.Sprintf("%s %s %d %d %d", appendCmd, key, 0, 0, len(value)) if _, err := conn.Write(append([]byte(message), crlf...)); err != nil { return err @@ -513,7 +513,7 @@ func writePrepend(port uint16, key string, value []byte) error { return err } - message := fmt.Sprintf("%s %s %d %d %d", PREPEND, key, 0, 0, len(value)) + message := fmt.Sprintf("%s %s %d %d %d", prependCmd, key, 0, 0, len(value)) if _, err := conn.Write(append([]byte(message), crlf...)); err != nil { return err diff --git a/server.go b/server.go index aae45e7..0235c42 100644 --- a/server.go +++ b/server.go @@ -6,22 +6,22 @@ import ( "net" ) -type Server struct { +type server struct { l net.Listener } -// NewServer starts and returns a server listening on a given port. -func newServer(port uint16) (*Server, error) { +// newServer starts and returns a server listening on a given port. +func newServer(port uint16) (*server, error) { l, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) if err != nil { log.Printf("failed to listen on port: %d", port) return nil, err } - return &Server{l: l}, nil + return &server{l: l}, nil } -// Close closes a server started with NewServer(). -func (s *Server) close() { +// close closes server started with NewServer(). +func (s *server) close() { if s.l != nil { _ = s.l.Close() s.l = nil