-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
110 lines (99 loc) · 2.56 KB
/
storage.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
package main
import (
"context"
"database/sql"
"net/url"
"time"
"github.com/maxnilz/feed/errors"
)
func NewStorage(cfg Config) (Storage, error) {
if cfg.DSN == "" {
return nil, errors.Newf(errors.InvalidArgument, nil, "missing dsn")
}
u, err := url.Parse(cfg.DSN)
if err != nil {
return nil, errors.Newf(errors.InvalidArgument, err, "invalid dsn: %s", cfg.DSN)
}
switch u.Scheme {
case "sqlite", "sqlite3":
return newSQLite(u.Path)
default:
return nil, errors.Newf(errors.Unimplemented, nil, "unsupported db: %s", u.Scheme)
}
}
type Storage interface {
NewSession(ctx context.Context) (Session, error)
NewAutoSession(ctx context.Context) (Session, error)
SaveFeeds(ses Session, feeds ...*Feed) error
AckFeeds(ses Session, at time.Time, feedIds ...string) error
GetLatestFeedWaterMark(ses Session, email, site string) (time.Time, error)
Close() error
}
type Session interface {
// Begin starts a transactional session.
//
// It's the user's responsibility to manage the session,
// Either Rollback or Commit MUST be called to pair with Begin to avoid transaction leak.
Begin() (Session, error)
// Rollback aborts the changes made by the transactional session.
Rollback() error
// Commit commits the changes made by the transactional session.
Commit() error
Exec(query string, args ...any) (sql.Result, error)
Query(query string, args ...any) (*sql.Rows, error)
QueryRow(query string, args ...any) *sql.Row
}
type Feed struct {
Id string
Email Email
SiteURL string
SiteName string
Title string
Description string
Content string
Link string
UpdatedAt string
PublishedAt string
Author string
FetchAt time.Time
}
type Email string
func (e Email) String() string {
return string(e)
}
type Feeds struct {
Emails []Email
Sites []Site
List []*Feed
Map map[Email]map[Site][]*Feed
}
func (fs *Feeds) Append(feeds ...*Feed) {
for _, feed := range feeds {
fs.List = append(fs.List, feed)
if fs.Map == nil {
fs.Map = make(map[Email]map[Site][]*Feed)
}
sitesFeeds, ok := fs.Map[feed.Email]
if !ok {
sitesFeeds = make(map[Site][]*Feed)
fs.Map[feed.Email] = sitesFeeds
fs.Emails = append(fs.Emails, feed.Email)
}
site := Site{Name: feed.SiteName, URL: feed.SiteURL}
if _, ok = sitesFeeds[site]; !ok {
fs.Sites = append(fs.Sites, site)
}
sitesFeeds[site] = append(sitesFeeds[site], feed)
}
}
func (fs *Feeds) SiteFeeds(email Email, site Site) []*Feed {
sitesFeeds, ok := fs.Map[email]
if !ok {
return nil
}
out, ok := sitesFeeds[site]
if !ok {
return nil
}
return out
}