Skip to content

Commit

Permalink
refactor: Unexport unnecessary structs, fields (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
sang-w0o authored Mar 18, 2022
1 parent 337857e commit f2910f8
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 125 deletions.
72 changes: 36 additions & 36 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
}
Expand All @@ -32,33 +32,33 @@ 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
}

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
}
Expand All @@ -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
}
Expand All @@ -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)
}
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -240,21 +240,21 @@ 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
} else {
decrementedValue = numericItemValue - decrValue
}
value := []byte(strconv.FormatUint(decrementedValue, 10))
item.Value = value
item.value = value
result := append(value, crlf...)
_, _ = conn.Write(result)
}
Expand All @@ -275,26 +275,26 @@ func (m *MiniMemcached) touch(key string, expiration int32, conn net.Conn) {
_, _ = conn.Write(resultNotFound)
return
}
item.Expiration = expiration
item.expiration = expiration
_, _ = conn.Write(resultTouched)
}

// flushAll() handles memcached `flush_all` command.
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
}
Expand All @@ -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)
}
Expand Down
29 changes: 14 additions & 15 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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"
)
Expand Down
35 changes: 18 additions & 17 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(),
}

Expand Down Expand Up @@ -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(),
}

Expand All @@ -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)
Expand Down Expand Up @@ -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(),
}

Expand Down
Loading

0 comments on commit f2910f8

Please sign in to comment.