From eca8525e5811cad371e407507594fa95d8db98c1 Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Thu, 6 Jun 2019 02:21:00 -0400 Subject: deduplicated duplicate deduplication functions --- svc/handlers.go | 26 ++------------------------ svc/query.go | 32 ++++++++++++++++---------------- 2 files changed, 18 insertions(+), 40 deletions(-) (limited to 'svc') diff --git a/svc/handlers.go b/svc/handlers.go index 45f3022..7f7c730 100644 --- a/svc/handlers.go +++ b/svc/handlers.go @@ -5,7 +5,6 @@ import ( "fmt" "net/http" "strconv" - "strings" "github.com/getwtxt/registry" "github.com/gorilla/mux" @@ -167,28 +166,7 @@ func apiTagsHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) tags := vars["tags"] - tags = strings.ToLower(tags) - out, err := twtxtCache.QueryInStatus("#" + tags) - if err != nil { - log500(w, r, err) - return - } - tags = strings.Title(tags) - out2, err := twtxtCache.QueryInStatus("#" + tags) - if err != nil { - log500(w, r, err) - return - } - tags = strings.ToUpper(tags) - out3, err := twtxtCache.QueryInStatus("#" + tags) - if err != nil { - log500(w, r, err) - return - } - - out = append(out, out2...) - out = append(out, out3...) - out = uniq(out) + out := compositeStatusQuery("#"+tags, r) out = registry.ReduceToPage(1, out) data := parseQueryOut(out) @@ -197,7 +175,7 @@ func apiTagsHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("ETag", etag) w.Header().Set("Content-Type", txtutf8) - _, err = w.Write(data) + _, err := w.Write(data) if err != nil { log500(w, r, err) return diff --git a/svc/query.go b/svc/query.go index 037b0e5..a27747d 100644 --- a/svc/query.go +++ b/svc/query.go @@ -19,6 +19,20 @@ func apiErrCheck(err error, r *http.Request) { } } +func dedupe(list []string) []string { + out := []string{} + seen := make(map[string]bool) + + for _, e := range list { + if !seen[e] { + out = append(out, e) + seen[e] = true + } + } + + return out +} + // Takes the output of queries and formats it for // an HTTP response. Iterates over the string slice, // appending each entry to a byte slice, and adding @@ -37,20 +51,7 @@ func parseQueryOut(out []string) []byte { return data } -// Removes duplicate statuses from query output -func uniq(str []string) []string { - keys := make(map[string]bool) - out := []string{} - for _, e := range str { - if _, ok := keys[e]; !ok { - keys[e] = true - out = append(out, e) - } - } - return out -} - -// apiUserQuery is called via apiEndpointHandler when +// apiEndpointQuery is called via apiEndpointHandler when // the endpoint is "users" and r.FormValue("q") is not empty. // It queries the registry cache for users or user URLs // matching the term supplied via r.FormValue("q") @@ -123,9 +124,8 @@ func joinQueryOuts(data ...[]string) []string { for _, e := range data { single = append(single, e...) } - single = uniq(single) - return single + return dedupe(single) } func compositeStatusQuery(query string, r *http.Request) []string { -- cgit 1.4.1-2-gfad0 py?id=26962ded19264ae1885386783c861d002d8fd1dc'>^
dc92d375 ^

c344d89c ^
9f5eeff7 ^
e52629c1 ^
20f0f2ba ^
d8b807c5 ^
31277e06 ^

b3d031a9 ^
6fc14005 ^

20f0f2ba ^





6fc14005 ^
144de554 ^
20f0f2ba ^
6fc14005 ^
20f0f2ba ^


d8b807c5 ^

1a6d1846 ^
d4598be6 ^
8a5944e1 ^
3cd8a402 ^
b3d031a9 ^
397106b6 ^
d8b807c5 ^



397106b6 ^
d8b807c5 ^


397106b6 ^

d8b807c5 ^

397106b6 ^
d8b807c5 ^
397106b6 ^

31277e06 ^

20f0f2ba ^












144de554 ^


d1a1173d ^





b3660b9a ^
fc5648cd ^





















144de554 ^
20f0f2ba ^

397106b6 ^



3b96a37f ^
6bc13af0 ^












1a6d1846 ^



3cd8a402 ^
76791a70 ^

6bc13af0 ^

76791a70 ^


6bc13af0 ^







fe5f90d3 ^
6bc13af0 ^



144de554 ^



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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147