|
1 | 1 | package server |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/base64" |
5 | 4 | "encoding/binary" |
6 | | - "encoding/json" |
7 | 5 |
|
8 | | - "github.com/boltdb/bolt" |
| 6 | + json "github.com/json-iterator/go" |
9 | 7 | ) |
10 | 8 |
|
11 | | -type Database struct { |
12 | | - db *bolt.DB |
13 | | -} |
14 | | - |
15 | | -func OpenDatabase(path string) (*Database, error) { |
16 | | - db, err := bolt.Open(path, 0600, nil) |
17 | | - if err != nil { |
18 | | - return nil, err |
19 | | - } |
20 | | - |
21 | | - db.Update(func(tx *bolt.Tx) error { |
22 | | - _, err := tx.CreateBucketIfNotExists([]byte("activity")) |
23 | | - if err != nil { |
24 | | - return err |
25 | | - } |
26 | | - return nil |
27 | | - }) |
28 | | - |
29 | | - return &Database{ |
30 | | - db: db, |
31 | | - }, nil |
32 | | -} |
33 | | - |
34 | | -func (db *Database) Close() { |
35 | | - db.db.Close() |
36 | | -} |
37 | | - |
38 | 9 | const ( |
39 | 10 | ForumPostType = iota |
40 | 11 | RedditCommentType |
41 | 12 | RedditPostType |
42 | 13 | ) |
43 | 14 |
|
44 | | -func (db *Database) AddActivity(activity []Activity) { |
45 | | - err := db.db.Update(func(tx *bolt.Tx) error { |
46 | | - b := tx.Bucket([]byte("activity")) |
47 | | - for _, a := range activity { |
48 | | - buf, err := json.Marshal(a) |
49 | | - if err != nil { |
50 | | - return err |
51 | | - } |
52 | | - k := make([]byte, 10) |
53 | | - binary.BigEndian.PutUint64(k, uint64(a.ActivityTime().Unix())<<24) |
54 | | - switch a.(type) { |
55 | | - case *ForumPost: |
56 | | - k[5] = ForumPostType |
57 | | - case *RedditComment: |
58 | | - k[5] = RedditCommentType |
59 | | - case *RedditPost: |
60 | | - k[5] = RedditPostType |
61 | | - } |
62 | | - binary.BigEndian.PutUint32(k[6:], a.ActivityKey()) |
63 | | - b.Put(k, buf) |
64 | | - } |
65 | | - return nil |
66 | | - }) |
| 15 | +type Database interface { |
| 16 | + AddActivity(activity []Activity) error |
| 17 | + Activity(locale *Locale, start string, count int) ([]Activity, string, error) |
| 18 | + Close() error |
| 19 | +} |
| 20 | + |
| 21 | +func marshalActivity(a Activity) (key, value []byte, err error) { |
| 22 | + buf, err := json.Marshal(a) |
67 | 23 | if err != nil { |
68 | | - panic(err) |
| 24 | + return nil, nil, err |
69 | 25 | } |
| 26 | + k := make([]byte, 10) |
| 27 | + binary.BigEndian.PutUint64(k, uint64(a.ActivityTime().Unix())<<24) |
| 28 | + switch a.(type) { |
| 29 | + case *ForumPost: |
| 30 | + k[5] = ForumPostType |
| 31 | + case *RedditComment: |
| 32 | + k[5] = RedditCommentType |
| 33 | + case *RedditPost: |
| 34 | + k[5] = RedditPostType |
| 35 | + } |
| 36 | + binary.BigEndian.PutUint32(k[6:], a.ActivityKey()) |
| 37 | + return k, buf, nil |
70 | 38 | } |
71 | 39 |
|
72 | | -func (db *Database) Activity(start string, count int, filter func(Activity) bool) ([]Activity, string) { |
73 | | - ret := []Activity(nil) |
74 | | - next := "" |
75 | | - err := db.db.View(func(tx *bolt.Tx) error { |
76 | | - c := tx.Bucket([]byte("activity")).Cursor() |
77 | | - var k, v []byte |
78 | | - if start == "" { |
79 | | - k, v = c.Last() |
80 | | - } else { |
81 | | - s, err := base64.RawURLEncoding.DecodeString(start) |
82 | | - if err != nil { |
83 | | - k, v = c.Last() |
84 | | - } else { |
85 | | - k, v = c.Seek(s) |
86 | | - if k != nil { |
87 | | - k, v = c.Prev() |
88 | | - } |
89 | | - } |
| 40 | +func unmarshalActivity(key, value []byte) (Activity, error) { |
| 41 | + switch key[5] { |
| 42 | + case ForumPostType: |
| 43 | + post := &ForumPost{} |
| 44 | + err := json.Unmarshal(value, post) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
90 | 47 | } |
91 | | - for len(ret) < count && k != nil { |
92 | | - var activity Activity |
93 | | - switch k[5] { |
94 | | - case ForumPostType: |
95 | | - post := &ForumPost{} |
96 | | - err := json.Unmarshal(v, post) |
97 | | - if err != nil { |
98 | | - return err |
99 | | - } |
100 | | - if post.Host == "" { |
101 | | - post.Host = "www.pathofexile.com" |
102 | | - } |
103 | | - if post.Id != 0 { |
104 | | - activity = post |
105 | | - } |
106 | | - case RedditCommentType: |
107 | | - comment := &RedditComment{} |
108 | | - err := json.Unmarshal(v, comment) |
109 | | - if err != nil { |
110 | | - return err |
111 | | - } |
112 | | - activity = comment |
113 | | - case RedditPostType: |
114 | | - post := &RedditPost{} |
115 | | - err := json.Unmarshal(v, post) |
116 | | - if err != nil { |
117 | | - return err |
118 | | - } |
119 | | - activity = post |
120 | | - } |
121 | | - if activity != nil && (filter == nil || filter(activity)) { |
122 | | - ret = append(ret, activity) |
123 | | - next = base64.RawURLEncoding.EncodeToString(k) |
124 | | - } |
125 | | - k, v = c.Prev() |
| 48 | + if post.Host == "" { |
| 49 | + post.Host = "www.pathofexile.com" |
126 | 50 | } |
127 | | - return nil |
128 | | - }) |
129 | | - if err != nil { |
130 | | - panic(err) |
| 51 | + if post.Id != 0 { |
| 52 | + return post, nil |
| 53 | + } |
| 54 | + case RedditCommentType: |
| 55 | + comment := &RedditComment{} |
| 56 | + err := json.Unmarshal(value, comment) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + return comment, nil |
| 61 | + case RedditPostType: |
| 62 | + post := &RedditPost{} |
| 63 | + err := json.Unmarshal(value, post) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + return post, nil |
131 | 68 | } |
132 | | - return ret, next |
| 69 | + return nil, nil |
133 | 70 | } |
0 commit comments