Commit da13f06b authored by Sushant Mahajan's avatar Sushant Mahajan

added expiry time check support

parent 7129b4dc
...@@ -31,6 +31,7 @@ const ( ...@@ -31,6 +31,7 @@ const (
//errors //errors
ERR_CMD_ERR = "ERR_CMD_ERR" ERR_CMD_ERR = "ERR_CMD_ERR"
ERR_NOT_FOUND = "ERR_NOT_FOUND"
//logging //logging
LOG = true LOG = true
...@@ -139,12 +140,21 @@ func isValid(cmd string, tokens []string, conn net.Conn) int { ...@@ -139,12 +140,21 @@ func isValid(cmd string, tokens []string, conn net.Conn) int {
case GET: case GET:
if len(tokens) != 2 { if len(tokens) != 2 {
flag = 1 flag = 1
logger.Println(cmd, ":Invalid number of arguments")
}
if len(tokens[1]) > 250 {
flag = 1
logger.Println(cmd, ":Invalid key size")
} }
//other validations //other validations
case GETM: case GETM:
if len(tokens) != 2 { if len(tokens) != 2 {
flag = 1 flag = 1
} }
if len(tokens[1]) > 250 {
flag = 1
logger.Println(cmd, ":Invalid key size")
}
//other validations //other validations
case CAS: case CAS:
if len(tokens) > 6 || len(tokens) < 5 { if len(tokens) > 6 || len(tokens) < 5 {
...@@ -202,8 +212,36 @@ func parseInput(conn net.Conn, msg string, table *KeyValueStore) { ...@@ -202,8 +212,36 @@ func parseInput(conn net.Conn, msg string, table *KeyValueStore) {
buffer.Reset() buffer.Reset()
buffer.Write(data.value) buffer.Write(data.value)
write(conn, buffer.String()) write(conn, buffer.String())
} else {
buffer.Reset()
buffer.WriteString(ERR_NOT_FOUND)
write(conn, buffer.String())
}
case GETM:
if isValid(GETM, tokens, conn) != 0 {
return
} }
//case GETM: performGetm(tokens[1:len(tokens)]) if data, ok := performGetm(conn, tokens[1:len(tokens)], table); ok {
logger.Println("sending", tokens[1], "metadata")
buffer.Reset()
buffer.WriteString(VALUE)
buffer.WriteString(" ")
buffer.WriteString(strconv.FormatUint(data.version, 10))
buffer.WriteString(" ")
buffer.WriteString(strconv.FormatUint(data.expTime - uint64(time.Now().Unix()), 10))
buffer.WriteString(" ")
buffer.WriteString(strconv.FormatUint(data.numBytes, 10))
write(conn, buffer.String())
buffer.Reset()
buffer.Write(data.value)
write(conn, buffer.String())
} else {
buffer.Reset()
buffer.WriteString(ERR_NOT_FOUND)
write(conn, buffer.String())
}
//case CAS: performCas(tokens[1:len(tokens)]) //case CAS: performCas(tokens[1:len(tokens)])
//case DELETE: performDelete(tokens[1:len(tokens)]) //case DELETE: performDelete(tokens[1:len(tokens)])
default: default:
...@@ -243,7 +281,11 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b ...@@ -243,7 +281,11 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b
ver++ ver++
val.numBytes = n val.numBytes = n
val.version = ver val.version = ver
val.expTime = e + uint64(time.Now().Unix()) if e == 0 {
val.expTime = e
} else {
val.expTime = e + uint64(time.Now().Unix())
}
val.value = v val.value = v
table.Unlock() table.Unlock()
...@@ -258,11 +300,33 @@ func performGet(conn net.Conn, tokens []string, table *KeyValueStore) (*Data, bo ...@@ -258,11 +300,33 @@ func performGet(conn net.Conn, tokens []string, table *KeyValueStore) (*Data, bo
table.RLock() table.RLock()
//critical section begin //critical section begin
if v, ok := table.dictionary[k]; ok { if v, ok := table.dictionary[k]; ok {
if v.expTime != 0 && v.expTime < uint64(time.Now().Unix()) {
return nil, false
}
data := new(Data)
data.numBytes = v.numBytes
data.value = v.value[:]
return data, true
} else {
return nil, false
}
}
func performGetm(conn net.Conn, tokens []string, table *KeyValueStore) (*Data, bool) {
k := tokens[0]
defer table.RUnlock()
table.RLock()
//critical section begin
if v, ok := table.dictionary[k]; ok {
if v.expTime != 0 && v.expTime < uint64(time.Now().Unix()) {
return nil, false
}
data := new(Data) data := new(Data)
data.version = v.version
data.expTime = v.expTime
data.numBytes = v.numBytes data.numBytes = v.numBytes
logger.Println("Value:", v.value)
data.value = v.value[:] data.value = v.value[:]
logger.Println("Value:", data.value)
return data, true return data, true
} else { } else {
return nil, false return nil, false
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment