]> git.lizzy.rs Git - go-anidb.git/blob - groupcache.go
anidb: Document which backend API is used on the Get* methods
[go-anidb.git] / groupcache.go
1 package anidb
2
3 import (
4         "encoding/gob"
5         "github.com/Kovensky/go-anidb/http"
6         "github.com/Kovensky/go-anidb/udp"
7         "strconv"
8         "strings"
9         "time"
10 )
11
12 func init() {
13         gob.RegisterName("*github.com/Kovensky/go-anidb.Group", &Group{})
14         gob.RegisterName("github.com/Kovensky/go-anidb.GID", GID(0))
15         gob.RegisterName("*github.com/Kovensky/go-anidb.gidCache", &gidCache{})
16 }
17
18 func (g *Group) Touch() {
19         g.Cached = time.Now()
20 }
21
22 func (g *Group) IsStale() bool {
23         if g == nil {
24                 return true
25         }
26         return time.Now().Sub(g.Cached) > GroupCacheDuration
27 }
28
29 // Unique Group IDentifier
30 type GID int
31
32 // make GID cacheable
33
34 func (e GID) Touch()        {}
35 func (e GID) IsStale() bool { return false }
36
37 // Retrieves the Group from the cache.
38 func (gid GID) Group() *Group {
39         var g Group
40         if cache.Get(&g, "gid", gid) == nil {
41                 return &g
42         }
43         return nil
44 }
45
46 type gidCache struct {
47         GID
48         Time time.Time
49 }
50
51 func (c *gidCache) Touch() { c.Time = time.Now() }
52 func (c *gidCache) IsStale() bool {
53         if c != nil && time.Now().Sub(c.Time) < GroupCacheDuration {
54                 return false
55         }
56         return true
57 }
58
59 // Retrieves a Group by its GID. Uses the UDP API.
60 func (adb *AniDB) GroupByID(gid GID) <-chan *Group {
61         keys := []cacheKey{"gid", gid}
62         ch := make(chan *Group, 1)
63
64         ic := make(chan Cacheable, 1)
65         go func() { ch <- (<-ic).(*Group); close(ch) }()
66         if intentMap.Intent(ic, keys...) {
67                 return ch
68         }
69
70         if !cache.CheckValid(keys...) {
71                 intentMap.Notify((*Group)(nil), keys...)
72                 return ch
73         }
74
75         g := gid.Group()
76         if !g.IsStale() {
77                 intentMap.Notify(g, keys...)
78                 return ch
79         }
80
81         go func() {
82                 reply := <-adb.udp.SendRecv("GROUP",
83                         paramMap{"gid": gid})
84
85                 if reply.Error() == nil {
86                         g = parseGroupReply(reply)
87
88                         cache.Set(&gidCache{GID: g.GID}, "gid", "by-name", g.Name)
89                         cache.Set(&gidCache{GID: g.GID}, "gid", "by-shortname", g.ShortName)
90                         cache.Set(g, keys...)
91                 } else if reply.Code() == 350 {
92                         cache.MarkInvalid(keys...)
93                         cache.Delete(keys...) // deleted group?
94                 }
95
96                 intentMap.Notify(g, keys...)
97         }()
98         return ch
99 }
100
101 // Retrieves a Group by its name. Either full or short names are matched.
102 // Uses the UDP API.
103 func (adb *AniDB) GroupByName(gname string) <-chan *Group {
104         keys := []cacheKey{"gid", "by-name", gname}
105         altKeys := []cacheKey{"gid", "by-shortname", gname}
106         ch := make(chan *Group, 1)
107
108         ic := make(chan Cacheable, 1)
109         go func() {
110                 gid := (<-ic).(GID)
111                 if gid > 0 {
112                         ch <- <-adb.GroupByID(gid)
113                 }
114                 close(ch)
115         }()
116         if intentMap.Intent(ic, keys...) {
117                 return ch
118         }
119
120         if !cache.CheckValid(keys...) {
121                 intentMap.Notify(GID(0), keys...)
122                 return ch
123         }
124
125         gid := GID(0)
126
127         var gc gidCache
128         if cache.Get(&gc, keys...) == nil && !gc.IsStale() {
129                 intentMap.Notify(gc.GID, keys...)
130                 return ch
131         }
132         gid = gc.GID
133
134         if gid == 0 {
135                 if cache.Get(&gc, altKeys...) == nil && !gc.IsStale() {
136                         intentMap.Notify(gc.GID, keys...)
137                         return ch
138                 }
139                 gid = gc.GID
140         }
141
142         go func() {
143                 reply := <-adb.udp.SendRecv("GROUP",
144                         paramMap{"gname": gname})
145
146                 var g *Group
147                 if reply.Error() == nil {
148                         g = parseGroupReply(reply)
149
150                         gid = g.GID
151
152                         cache.Set(&gidCache{GID: gid}, keys...)
153                         cache.Set(&gidCache{GID: gid}, altKeys...)
154                         cache.Set(g, "gid", gid)
155                 } else if reply.Code() == 350 {
156                         cache.MarkInvalid(keys...)
157                         cache.Delete(keys...) // renamed group?
158                         cache.Delete(altKeys...)
159                 }
160
161                 intentMap.Notify(gid, keys...)
162         }()
163         return ch
164 }
165
166 func parseGroupReply(reply udpapi.APIReply) *Group {
167         parts := strings.Split(reply.Lines()[1], "|")
168         ints := make([]int64, len(parts))
169         for i := range parts {
170                 ints[i], _ = strconv.ParseInt(parts[i], 10, 32)
171         }
172
173         irc := ""
174         if parts[7] != "" {
175                 irc = "irc://" + parts[8] + "/" + parts[7][1:]
176         }
177
178         pic := ""
179         if parts[10] != "" {
180                 pic = httpapi.AniDBImageBaseURL + parts[10]
181         }
182
183         rellist := strings.Split(parts[16], "'")
184         relations := make(map[GID]GroupRelationType, len(rellist))
185         for _, rel := range rellist {
186                 r := strings.Split(rel, ",")
187                 if len(r) < 2 {
188                         continue
189                 }
190                 gid, _ := strconv.ParseInt(r[0], 10, 32)
191                 typ, _ := strconv.ParseInt(r[1], 10, 32)
192
193                 relations[GID(gid)] = GroupRelationType(typ)
194         }
195
196         ft := time.Unix(ints[11], 0)
197         if ints[11] == 0 {
198                 ft = time.Time{}
199         }
200         dt := time.Unix(ints[12], 0)
201         if ints[12] == 0 {
202                 dt = time.Time{}
203         }
204         lr := time.Unix(ints[14], 0)
205         if ints[14] == 0 {
206                 lr = time.Time{}
207         }
208         la := time.Unix(ints[15], 0)
209         if ints[15] == 0 {
210                 la = time.Time{}
211         }
212
213         return &Group{
214                 GID: GID(ints[0]),
215
216                 Name:      parts[5],
217                 ShortName: parts[6],
218
219                 IRC:     irc,
220                 URL:     parts[9],
221                 Picture: pic,
222
223                 Founded:   ft,
224                 Disbanded: dt,
225                 // ignore ints[13]
226                 LastRelease:  lr,
227                 LastActivity: la,
228
229                 Rating: Rating{
230                         Rating:    float32(ints[1]) / 100,
231                         VoteCount: int(ints[2]),
232                 },
233                 AnimeCount: int(ints[3]),
234                 FileCount:  int(ints[4]),
235
236                 RelatedGroups: relations,
237
238                 Cached: time.Now(),
239         }
240 }