]> git.lizzy.rs Git - go-anidb.git/blob - groupcache.go
anidb: Simplify documentation
[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.
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         if g := gid.Group(); !g.IsStale() {
76                 intentMap.Notify(g, keys...)
77                 return ch
78         }
79
80         go func() {
81                 reply := <-adb.udp.SendRecv("GROUP",
82                         paramMap{"gid": gid})
83
84                 var g *Group
85                 if reply.Error() == nil {
86                         g = parseGroupReply(reply)
87                 } else if reply.Code() == 350 {
88                         cache.MarkInvalid(keys...)
89                 }
90                 if g != nil {
91                         cache.Set(&gidCache{GID: g.GID}, "gid", "by-name", g.Name)
92                         cache.Set(&gidCache{GID: g.GID}, "gid", "by-shortname", g.ShortName)
93                         cache.Set(g, keys...)
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 func (adb *AniDB) GroupByName(gname string) <-chan *Group {
103         keys := []cacheKey{"gid", "by-name", gname}
104         altKeys := []cacheKey{"gid", "by-shortname", gname}
105         ch := make(chan *Group, 1)
106
107         ic := make(chan Cacheable, 1)
108         go func() {
109                 gid := (<-ic).(GID)
110                 if gid > 0 {
111                         ch <- <-adb.GroupByID(gid)
112                 }
113                 close(ch)
114         }()
115         if intentMap.Intent(ic, keys...) {
116                 return ch
117         }
118
119         if !cache.CheckValid(keys...) {
120                 intentMap.Notify(GID(0), keys...)
121                 return ch
122         }
123
124         var gc gidCache
125         if cache.Get(&gc, keys...) == nil {
126                 intentMap.Notify(gc.GID, keys...)
127                 return ch
128         }
129
130         if cache.Get(&gc, altKeys...) == nil {
131                 intentMap.Notify(gc.GID, keys...)
132                 return ch
133         }
134
135         go func() {
136                 reply := <-adb.udp.SendRecv("GROUP",
137                         paramMap{"gname": gname})
138
139                 var g *Group
140                 if reply.Error() == nil {
141                         g = parseGroupReply(reply)
142                 } else if reply.Code() == 350 {
143                         cache.MarkInvalid(keys...)
144                 }
145
146                 gid := GID(0)
147                 if g != nil {
148                         gid = g.GID
149
150                         cache.Set(&gidCache{GID: gid}, keys...)
151                         cache.Set(&gidCache{GID: gid}, altKeys...)
152                         cache.Set(g, "gid", gid)
153                 }
154                 intentMap.Notify(gid, keys...)
155         }()
156         return ch
157 }
158
159 func parseGroupReply(reply udpapi.APIReply) *Group {
160         parts := strings.Split(reply.Lines()[1], "|")
161         ints := make([]int64, len(parts))
162         for i := range parts {
163                 ints[i], _ = strconv.ParseInt(parts[i], 10, 32)
164         }
165
166         irc := ""
167         if parts[7] != "" {
168                 irc = "irc://" + parts[8] + "/" + parts[7][1:]
169         }
170
171         pic := ""
172         if parts[10] != "" {
173                 pic = httpapi.AniDBImageBaseURL + parts[10]
174         }
175
176         rellist := strings.Split(parts[16], "'")
177         relations := make(map[GID]GroupRelationType, len(rellist))
178         for _, rel := range rellist {
179                 r := strings.Split(rel, ",")
180                 if len(r) < 2 {
181                         continue
182                 }
183                 gid, _ := strconv.ParseInt(r[0], 10, 32)
184                 typ, _ := strconv.ParseInt(r[1], 10, 32)
185
186                 relations[GID(gid)] = GroupRelationType(typ)
187         }
188
189         ft := time.Unix(ints[11], 0)
190         if ints[11] == 0 {
191                 ft = time.Time{}
192         }
193         dt := time.Unix(ints[12], 0)
194         if ints[12] == 0 {
195                 dt = time.Time{}
196         }
197         lr := time.Unix(ints[14], 0)
198         if ints[14] == 0 {
199                 lr = time.Time{}
200         }
201         la := time.Unix(ints[15], 0)
202         if ints[15] == 0 {
203                 la = time.Time{}
204         }
205
206         return &Group{
207                 GID: GID(ints[0]),
208
209                 Name:      parts[5],
210                 ShortName: parts[6],
211
212                 IRC:     irc,
213                 URL:     parts[9],
214                 Picture: pic,
215
216                 Founded:   ft,
217                 Disbanded: dt,
218                 // ignore ints[13]
219                 LastRelease:  lr,
220                 LastActivity: la,
221
222                 Rating: Rating{
223                         Rating:    float32(ints[1]) / 100,
224                         VoteCount: int(ints[2]),
225                 },
226                 AnimeCount: int(ints[3]),
227                 FileCount:  int(ints[4]),
228
229                 RelatedGroups: relations,
230
231                 Cached: time.Now(),
232         }
233 }