-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
115 lines (97 loc) · 2.51 KB
/
http.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
package main
import (
"encoding/json"
"fmt"
"log"
"unicode/utf8"
"git.restream.ru/itv-backend/reindexer/benchmarks/repo"
"github.com/buaazp/fasthttprouter"
"github.com/valyala/fasthttp"
)
func QueryGetByIDHandler(ctx *fasthttp.RequestCtx) {
r := ctx.UserValue("repo").(string)
item := repo.Get(r).QueryByID(1, false)
ret, _ := json.Marshal(item)
ctx.Write(ret)
}
func Query1CondHandler(ctx *fasthttp.RequestCtx) {
r := ctx.UserValue("repo").(string)
items := repo.Get(r).Query1Cond(1, false, 10)
ret, _ := json.Marshal(items)
ctx.Write(ret)
}
func Query2CondHandler(ctx *fasthttp.RequestCtx) {
r := ctx.UserValue("repo").(string)
items := repo.Get(r).Query2Cond(1, false, 10)
ret, _ := json.Marshal(items)
ctx.Write(ret)
}
func randStringWord() string { return repo.RandStringWord() }
func randStringPref() string {
s := randStringWord()
if utf8.RuneCountInString(s) > 4 {
ns := ""
for i, r := range s {
ns += string(r)
if i+1 == 4 {
return ns
}
}
}
return s
}
func QueryTextHandler(ctx *fasthttp.RequestCtx) {
r := ctx.UserValue("repo").(string)
dsl := ""
switch r {
case "elastic", "mysql", "reindex":
dsl = "%s %s"
case "sqlite":
dsl = "%s OR %s"
case "sphinx":
dsl = "%s | %s"
case "mongo":
dsl = "%s %s"
case "arango":
dsl = "%s,|%s"
}
items := repo.Get(r).QueryFullText(func() string { return fmt.Sprintf(dsl, randStringWord(), randStringWord()) }, 1, 10)
ret, _ := json.Marshal(items)
ctx.Write(ret)
}
func QueryTextPrefixHandler(ctx *fasthttp.RequestCtx) {
r := ctx.UserValue("repo").(string)
dsl := ""
switch r {
case "elastic", "mysql", "reindex":
dsl = "%s* %s*"
case "sqlite":
dsl = "%s* OR %s*"
case "sphinx":
dsl = "%s* | %s*"
case "arango":
dsl = "prefix:%s,|prefix:%s"
}
items := repo.Get(r).QueryFullText(func() string { return fmt.Sprintf(dsl, randStringPref(), randStringPref()) }, 1, 10)
ret, _ := json.Marshal(items)
ctx.Write(ret)
}
func UpdateHandler(ctx *fasthttp.RequestCtx) {
r := ctx.UserValue("repo").(string)
repo.Get(r).Update(1)
ctx.WriteString("{}")
}
func StartHTTP() {
router := fasthttprouter.New()
router.GET("/byid/:repo", QueryGetByIDHandler)
router.GET("/1cond/:repo", Query1CondHandler)
router.GET("/2cond/:repo", Query2CondHandler)
router.GET("/text/:repo", QueryTextHandler)
router.GET("/text_prefix/:repo", QueryTextPrefixHandler)
router.GET("/update/:repo", UpdateHandler)
log.Printf("Starting listen fasthttp on 8081")
err := fasthttp.ListenAndServe(":8081", router.Handler)
if err != nil {
panic(err)
}
}