Commit 7129b4dc authored by Sushant Mahajan's avatar Sushant Mahajan

formatted code

parent 2539813d
...@@ -42,7 +42,7 @@ func main() { ...@@ -42,7 +42,7 @@ func main() {
fmt.Println("Err: ", err) fmt.Println("Err: ", err)
} }
fmt.Println(msg) fmt.Println(msg)
buf := []byte(msg)[0:len(msg)-1] buf := []byte(msg)[0 : len(msg)-1]
buf = append(buf, []byte("\r\n")...) buf = append(buf, []byte("\r\n")...)
fmt.Println(len(buf)) fmt.Println(len(buf))
conn.Write(buf) conn.Write(buf)
......
...@@ -4,14 +4,14 @@ import ( ...@@ -4,14 +4,14 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log"
"net" "net"
"os"
"strconv"
"strings" "strings"
"sync" "sync"
"strconv"
"time" "time"
"log"
"os"
"io/ioutil"
) )
const ( const (
...@@ -22,12 +22,12 @@ const ( ...@@ -22,12 +22,12 @@ const (
CAS = "cas" CAS = "cas"
DELETE = "delete" DELETE = "delete"
NOREPLY = "noreply" NOREPLY = "noreply"
// //
// //response // //response
OK = "OK" OK = "OK"
CRLF = "\r\n" CRLF = "\r\n"
// VALUE = "VALUE" VALUE = "VALUE"
// DELETED = "DELETED" // DELETED = "DELETED"
//errors //errors
ERR_CMD_ERR = "ERR_CMD_ERR" ERR_CMD_ERR = "ERR_CMD_ERR"
...@@ -59,7 +59,7 @@ func startServer() { ...@@ -59,7 +59,7 @@ func startServer() {
} }
//initialize key value store //initialize key value store
table := &KeyValueStore{dictionary:make(map[string]*Data)} table := &KeyValueStore{dictionary: make(map[string]*Data)}
//infinite loop //infinite loop
for { for {
...@@ -73,7 +73,7 @@ func startServer() { ...@@ -73,7 +73,7 @@ func startServer() {
} }
} }
func read(conn net.Conn, toRead uint64) ([]byte, bool){ func read(conn net.Conn, toRead uint64) ([]byte, bool) {
buf := make([]byte, toRead) buf := make([]byte, toRead)
_, err := conn.Read(buf) _, err := conn.Read(buf)
...@@ -103,7 +103,7 @@ func write(conn net.Conn, msg string) { ...@@ -103,7 +103,7 @@ func write(conn net.Conn, msg string) {
func handleClient(conn net.Conn, table *KeyValueStore) { func handleClient(conn net.Conn, table *KeyValueStore) {
defer conn.Close() defer conn.Close()
for { for {
if msg, ok := read(conn, 1024); ok{ if msg, ok := read(conn, 1024); ok {
parseInput(conn, string(msg), table) parseInput(conn, string(msg), table)
} else { } else {
break break
...@@ -111,7 +111,7 @@ func handleClient(conn net.Conn, table *KeyValueStore) { ...@@ -111,7 +111,7 @@ func handleClient(conn net.Conn, table *KeyValueStore) {
} }
} }
func isValid(cmd string, tokens []string, conn net.Conn) int{ func isValid(cmd string, tokens []string, conn net.Conn) int {
var flag int var flag int
switch cmd { switch cmd {
case SET: case SET:
...@@ -161,7 +161,8 @@ func isValid(cmd string, tokens []string, conn net.Conn) int{ ...@@ -161,7 +161,8 @@ func isValid(cmd string, tokens []string, conn net.Conn) int{
} }
switch flag { switch flag {
case 1: write(conn, ERR_CMD_ERR) case 1:
write(conn, ERR_CMD_ERR)
} }
return flag return flag
...@@ -176,23 +177,41 @@ func parseInput(conn net.Conn, msg string, table *KeyValueStore) { ...@@ -176,23 +177,41 @@ func parseInput(conn net.Conn, msg string, table *KeyValueStore) {
if isValid(SET, tokens, conn) != 0 { if isValid(SET, tokens, conn) != 0 {
return return
} }
if ver, ok := performSet(conn, tokens[1:len(tokens)], table); ok { if ver, ok, r := performSet(conn, tokens[1:len(tokens)], table); ok {
logger.Println(ver) logger.Println(ver)
if r {
buffer.Reset()
buffer.WriteString(OK) buffer.WriteString(OK)
buffer.WriteString(" ") buffer.WriteString(" ")
buffer.WriteString(strconv.FormatUint(ver, 10)) buffer.WriteString(strconv.FormatUint(ver, 10))
logger.Println(buffer.String()) logger.Println(buffer.String())
write(conn, buffer.String()) write(conn, buffer.String())
} }
case GET: performGet(tokens[1:len(tokens)]) }
case GET:
if isValid(GET, tokens, conn) != 0 {
return
}
if data, ok := performGet(conn, tokens[1:len(tokens)], table); ok {
logger.Println("sending", tokens[1], "data")
buffer.Reset()
buffer.WriteString(VALUE)
buffer.WriteString(" ")
buffer.WriteString(strconv.FormatUint(data.numBytes, 10))
write(conn, buffer.String())
buffer.Reset()
buffer.Write(data.value)
write(conn, buffer.String())
}
//case GETM: performGetm(tokens[1:len(tokens)]) //case GETM: performGetm(tokens[1:len(tokens)])
//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: logger.Println("Command not found") default:
logger.Println("Command not found")
} }
} }
func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, bool){ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, bool, bool) {
k := tokens[0] k := tokens[0]
e, _ := strconv.ParseUint(tokens[1], 10, 64) e, _ := strconv.ParseUint(tokens[1], 10, 64)
n, _ := strconv.ParseUint(tokens[2], 10, 64) n, _ := strconv.ParseUint(tokens[2], 10, 64)
...@@ -208,7 +227,7 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b ...@@ -208,7 +227,7 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b
v, ok := read(conn, n+2) v, ok := read(conn, n+2)
if !ok { if !ok {
//error here //error here
return 0, false return 0, false, false
} }
table.Lock() table.Lock()
...@@ -217,7 +236,7 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b ...@@ -217,7 +236,7 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b
var val *Data var val *Data
if _, ok := table.dictionary[k]; ok { if _, ok := table.dictionary[k]; ok {
val = table.dictionary[k] val = table.dictionary[k]
} else{ } else {
val = new(Data) val = new(Data)
table.dictionary[k] = val table.dictionary[k] = val
} }
...@@ -230,12 +249,29 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b ...@@ -230,12 +249,29 @@ func performSet(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, b
table.Unlock() table.Unlock()
logger.Println("Table unlocked") logger.Println("Table unlocked")
debug(table) debug(table)
return val.version, true return val.version, true, r
} }
func debug(table *KeyValueStore){ func performGet(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 {
data := new(Data)
data.numBytes = v.numBytes
logger.Println("Value:", v.value)
data.value = v.value[:]
logger.Println("Value:", data.value)
return data, true
} else {
return nil, false
}
}
func debug(table *KeyValueStore) {
logger.Println("----start debug----") logger.Println("----start debug----")
for key,val := range (*table).dictionary { for key, val := range (*table).dictionary {
logger.Println(key, val) logger.Println(key, val)
} }
logger.Println("----end debug----") logger.Println("----end debug----")
...@@ -245,10 +281,10 @@ func main() { ...@@ -245,10 +281,10 @@ func main() {
ver = 1 ver = 1
if LOG { if LOG {
logf, _ := os.OpenFile("serverlog.log", os.O_RDWR | os.O_CREATE | os.O_TRUNC, 0666) logf, _ := os.OpenFile("serverlog.log", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
defer logf.Close() defer logf.Close()
logger = log.New(logf, "SERVER: ", log.Ltime|log.Lshortfile) logger = log.New(logf, "SERVER: ", log.Ltime|log.Lshortfile)
} else{ } else {
logger = log.New(ioutil.Discard, "SERVER: ", log.Ldate) logger = log.New(ioutil.Discard, "SERVER: ", log.Ldate)
} }
......
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