-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbutils.go
122 lines (109 loc) · 3.07 KB
/
dbutils.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
116
117
118
119
120
121
122
package main
import (
"encoding/json"
"github.com/boltdb/bolt"
"net/http"
"strconv"
)
// createBucket creates the bucket if it does not exist.
func createBucket(name string) func(*bolt.Tx) error {
return func(tx *bolt.Tx) error {
if _, err := tx.CreateBucketIfNotExists([]byte(name)); err != nil {
return err
}
return nil
}
}
// updateRiver prepends the new feed update to the stored JSON.
func updateRiver(name string, newUpdate *UpdatedFeed) func(*bolt.Tx) error {
return func(tx *bolt.Tx) error {
var updates []*UpdatedFeed
// Get the JSON out of boltdb
b := tx.Bucket([]byte(name))
obj := b.Get([]byte("river"))
// Decode the byte slice into a slice of *UpdateFeed and
// prepend the new update
if obj != nil {
json.Unmarshal(obj, &updates)
updates = append([]*UpdatedFeed{newUpdate}, updates...)
} else {
updates = []*UpdatedFeed{newUpdate}
}
// Trim the update slice down to size
if len(updates) > maxFeedUpdates {
updates = updates[:maxFeedUpdates]
}
// Encode the new river object and update bolt with it
updatedRiver, err := json.Marshal(updates)
err = b.Put([]byte("river"), updatedRiver)
if err != nil {
return err
}
return nil
}
}
// getRiver places the slice of *UpdateFeeds onto the RiverJS struct.
func getRiver(name string, js *RiverJS) func(*bolt.Tx) error {
return func(tx *bolt.Tx) error {
var updates []*UpdatedFeed
b := tx.Bucket([]byte(name))
raw := b.Get([]byte("river"))
json.Unmarshal(raw, &updates)
js.UpdatedFeeds.UpdatedFeed = updates
return nil
}
}
// checkFingerprint determines whether the given fingerprint has been seen before.
func checkFingerprint(name, fingerprint string, seen *bool) func(*bolt.Tx) error {
return func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(name))
result := b.Get([]byte(fingerprint))
if result != nil {
*seen = true
} else {
*seen = false
err := b.Put([]byte(fingerprint), []byte{1})
if err != nil {
return err
}
}
return nil
}
}
// getCacheHeaders gets Last-Modified and ETag out of boltdb.
func getCacheHeaders(name, url string, req *http.Request) func(*bolt.Tx) error {
return func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(name))
lm := b.Get([]byte("lastModified:" + url))
e := b.Get([]byte("etag:" + url))
if lm != nil {
req.Header.Add("If-Modified-Since", string(lm))
}
if e != nil {
req.Header.Add("If-None-Match", string(e))
}
return nil
}
}
// setCacheHeaders stores Last-Modified and ETag HTTP headers in boltdb.
func setCacheHeaders(name, url string, resp *http.Response) func(*bolt.Tx) error {
return func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(name))
lm := resp.Header.Get("Last-Modified")
e := resp.Header.Get("ETag")
err := b.Put([]byte("lastModified:"+url), []byte(lm))
err = b.Put([]byte("etag:"+url), []byte(e))
return err
}
}
func assignNextID(name string, update *UpdatedFeedItem) func(*bolt.Tx) error {
return func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(name))
seq, err := b.NextSequence()
if err != nil {
return err
}
update.Id = strconv.Itoa(int(seq))
return nil
}
}