-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
126 lines (109 loc) · 2.69 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"avitest/commons"
"avitest/lrucache"
"bytes"
"database/sql"
"errors"
"fmt"
"log"
"net"
"os"
"strings"
_ "github.com/mattn/go-sqlite3"
)
const (
CONN_HOST = "localhost"
CONN_PORT = "3000"
CONN_TYPE = "tcp"
)
var cache *lrucache.LRUCache
func getAvgMarks(querystmt *sql.Stmt, uid string) (float32, error) {
if cache.Get(uid) == nil {
// load data from db
// put data in cache
log.Println("cache miss for:", uid)
studentMarks := new(commons.MarksStudent)
fmt.Println(uid)
rows, err := querystmt.Query(uid)
if err != nil {
log.Fatal(err)
}
fmt.Println(rows)
for rows.Next() {
rows.Scan(&studentMarks.UID, &studentMarks.Name, &studentMarks.Maths, &studentMarks.Physics, &studentMarks.Chem)
}
defer rows.Close()
if studentMarks.UID == 0 {
return 0.0, errors.New("Entry Not Found")
}
fmt.Println(studentMarks)
cache.Put(uid, studentMarks)
}
marks := cache.Get(uid).(*commons.MarksStudent)
return float32(marks.Maths+marks.Physics+marks.Chem) / float32(3), nil
}
func generateInputUidList(uidStr string) []string {
uidStr = strings.Trim(uidStr, ",")
uidStrList := strings.Split(uidStr, ",")
return uidStrList
}
func getOutputBytes(resultMap map[string]string) []byte {
var buffer bytes.Buffer
for k, v := range resultMap {
buffer.WriteString(fmt.Sprintf("Avg Marks of %s: %s", k, v))
buffer.Write([]byte("\n"))
}
return buffer.Bytes()
}
func handleRequest(querystmt *sql.Stmt, conn net.Conn) {
buf := make([]byte, 1024)
n, err := conn.Read(buf[0:])
if err != nil {
fmt.Println("Error reading:", err.Error())
conn.Close()
return
}
uidStr := string(buf[:n])
if strings.Trim(uidStr, " ") == "" {
fmt.Println("uid cannot be empty")
conn.Close()
return
}
uidStrList := generateInputUidList(uidStr)
resultMap := make(map[string]string)
for _, uid := range uidStrList {
avgMarks, err := getAvgMarks(querystmt, uid)
if err != nil {
resultMap[uid] = fmt.Sprintf("Entry Not Found.")
continue
}
resultMap[uid] = fmt.Sprintf("%.2f", avgMarks)
}
conn.Write(getOutputBytes(resultMap))
conn.Close()
}
func main() {
cache = new(lrucache.LRUCache).Init(100000)
database, _ := sql.Open("sqlite3", "./students_marks.db")
querystmt, err := database.Prepare("SELECT uid, name, maths, physics, chemistry FROM student_marks WHERE uid in ($1)")
if err != nil {
log.Fatal(err)
}
l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
go handleRequest(querystmt, conn)
}
querystmt.Close()
database.Close()
}