forked from grahamking/Key-Value-Polyglot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgraded Go version to Go v1. Ran gofmt on it
- Loading branch information
1 parent
dddcf3b
commit babbc22
Showing
1 changed file
with
85 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,110 @@ | ||
#!/usr/bin/env gorun | ||
package main; | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"bufio" | ||
"strings" | ||
"os" | ||
"strconv" | ||
//"runtime/pprof" // Uncomment to profile | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"net" | ||
"os" | ||
"strconv" | ||
"strings" | ||
//"runtime/pprof" // Uncomment to profile | ||
) | ||
|
||
var CACHE map[string] string; | ||
var CACHE map[string]string | ||
|
||
func main() { | ||
|
||
// Uncomment these three lines to profile | ||
/* | ||
handle, _ := os.Create("memg.prof") | ||
pprof.StartCPUProfile(handle) | ||
defer pprof.StopCPUProfile() | ||
*/ | ||
// Uncomment these three lines to profile | ||
/* | ||
handle, _ := os.Create("memg.prof") | ||
pprof.StartCPUProfile(handle) | ||
defer pprof.StopCPUProfile() | ||
*/ | ||
|
||
listener, err := net.Listen("tcp", "127.0.0.1:11211") | ||
if err != nil { | ||
panic("Error listening on 11211: " + err.String()) | ||
} | ||
listener, err := net.Listen("tcp", "127.0.0.1:11211") | ||
if err != nil { | ||
panic("Error listening on 11211: " + err.Error()) | ||
} | ||
|
||
CACHE = make(map[string] string) | ||
CACHE = make(map[string]string) | ||
|
||
if isSingle() { | ||
netconn, err := listener.Accept() | ||
if err != nil { | ||
panic("Accept error: " + err.String()) | ||
} | ||
if isSingle() { | ||
netconn, err := listener.Accept() | ||
if err != nil { | ||
panic("Accept error: " + err.Error()) | ||
} | ||
|
||
handleConn(netconn) | ||
handleConn(netconn) | ||
|
||
} else { | ||
for { | ||
netconn, err := listener.Accept() | ||
if err != nil { | ||
panic("Accept error: " + err.String()) | ||
} | ||
} else { | ||
for { | ||
netconn, err := listener.Accept() | ||
if err != nil { | ||
panic("Accept error: " + err.Error()) | ||
} | ||
|
||
go handleConn(netconn) | ||
} | ||
} | ||
go handleConn(netconn) | ||
} | ||
} | ||
|
||
} | ||
|
||
func isSingle() bool { | ||
for _, arg := range os.Args { | ||
if arg == "--single" { | ||
return true | ||
} | ||
} | ||
return false | ||
for _, arg := range os.Args { | ||
if arg == "--single" { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
/* | ||
* Networking | ||
*/ | ||
func handleConn(conn net.Conn) { | ||
|
||
reader := bufio.NewReader(conn) | ||
for { | ||
|
||
// Fetch | ||
|
||
content, err := reader.ReadString('\n') | ||
if err == os.EOF { | ||
break | ||
} else if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
content = content[:len(content) - 2] // Chop \r\n | ||
|
||
// Handle | ||
|
||
parts := strings.Split(content, " ") | ||
cmd := parts[0] | ||
switch cmd { | ||
|
||
case "get": | ||
key := parts[1] | ||
val, ok := CACHE[key] | ||
if ok { | ||
length := strconv.Itoa(len(val)) | ||
conn.Write([]uint8("VALUE " + key + " 0 " + length + "\r\n")) | ||
conn.Write([]uint8(val + "\r\n")) | ||
} | ||
conn.Write([]uint8("END\r\n")) | ||
|
||
case "set": | ||
key := parts[1] | ||
//exp := parts[2] | ||
//flags := parts[3] | ||
length, _ := strconv.Atoi(parts[4]) | ||
// Really we should read exactly 'length' bytes + \r\n | ||
val := make([]byte, length); | ||
reader.Read(val) | ||
CACHE[key] = string(val); | ||
conn.Write([]uint8("STORED\r\n")) | ||
} | ||
} | ||
reader := bufio.NewReader(conn) | ||
for { | ||
|
||
// Fetch | ||
|
||
content, err := reader.ReadString('\n') | ||
if err == io.EOF { | ||
break | ||
} else if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
content = content[:len(content)-2] // Chop \r\n | ||
|
||
// Handle | ||
|
||
parts := strings.Split(content, " ") | ||
cmd := parts[0] | ||
switch cmd { | ||
|
||
case "get": | ||
key := parts[1] | ||
val, ok := CACHE[key] | ||
if ok { | ||
length := strconv.Itoa(len(val)) | ||
conn.Write([]uint8("VALUE " + key + " 0 " + length + "\r\n")) | ||
conn.Write([]uint8(val + "\r\n")) | ||
} | ||
conn.Write([]uint8("END\r\n")) | ||
|
||
case "set": | ||
key := parts[1] | ||
//exp := parts[2] | ||
//flags := parts[3] | ||
length, _ := strconv.Atoi(parts[4]) | ||
// Really we should read exactly 'length' bytes + \r\n | ||
val := make([]byte, length) | ||
reader.Read(val) | ||
CACHE[key] = string(val) | ||
conn.Write([]uint8("STORED\r\n")) | ||
} | ||
} | ||
} |