This repository has been archived by the owner on Nov 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urls.go
152 lines (134 loc) · 3.02 KB
/
urls.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package core
import (
"database/sql"
"fmt"
"github.com/datatogether/sqlutil"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
)
func ListUrls(store datastore.Datastore, limit, offset int) ([]*Url, error) {
q := query.Query{
Prefix: Url{}.DatastoreType(),
Limit: limit,
Offset: offset,
}
res, err := store.Query(q)
if err != nil {
return nil, err
}
urls := make([]*Url, limit)
i := 0
for r := range res.Next() {
if r.Error != nil {
return nil, err
}
c, ok := r.Value.(*Url)
if !ok {
return nil, ErrInvalidResponse
}
urls[i] = c
i++
}
return urls[:i], nil
// rows, err := db.Query(qUrlsList, limit, skip)
// if err != nil {
// return nil, err
// }
// defer rows.Close()
// return UnmarshalBoundedUrls(rows, limit)
}
func ContentUrls(db sqlutil.Queryable, limit, skip int) ([]*Url, error) {
rows, err := db.Query(qContentUrlsList, limit, skip)
if err != nil {
return nil, err
}
return UnmarshalBoundedUrls(rows, limit)
}
func ContentUrlsCount(db sqlutil.Queryable) (count int, err error) {
err = db.QueryRow(qContentUrlsCount).Scan(&count)
return
}
func FetchedUrls(db sqlutil.Queryable, limit, offset int) ([]*Url, error) {
if limit == 0 {
limit = 100
}
rows, err := db.Query(qUrlsFetched, limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
urls := []*Url{}
for rows.Next() {
u := &Url{}
if err := u.UnmarshalSQL(rows); err != nil {
return nil, err
}
urls = append(urls, u)
}
return urls, nil
}
func UnfetchedUrls(db sqlutil.Queryable, limit, offset int) ([]*Url, error) {
if limit == 0 {
limit = 50
}
rows, err := db.Query(qUrlsUnfetched, limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
urls := []*Url{}
for rows.Next() {
u := &Url{}
if err := u.UnmarshalSQL(rows); err != nil {
return nil, err
}
urls = append(urls, u)
}
return urls, nil
}
func UrlsForHash(db sqlutil.Queryable, hash string) ([]*Url, error) {
rows, err := db.Query(qUrlsForHash, hash)
if err != nil {
return nil, err
}
return UnmarshalUrls(rows)
}
func ValidArchivingUrl(db sqlutil.Queryable, url string) error {
var exists bool
err := db.QueryRow("select exists(select 1 from suburls where $1 ilike concat('%', url ,'%'))", url).Scan(&exists)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("Oops! Only urls contained in suburls can be archived. cannot archive %s", url)
}
return nil
}
func UnmarshalBoundedUrls(rows *sql.Rows, limit int) ([]*Url, error) {
defer rows.Close()
urls := make([]*Url, limit)
i := 0
for rows.Next() {
u := &Url{}
if err := u.UnmarshalSQL(rows); err != nil {
return nil, err
}
urls[i] = u
i++
}
return urls[:i], nil
}
// UnmarshalUrls takes an sql cursor & returns a slice of url pointers
// expects columns to math urlCols()
func UnmarshalUrls(rows *sql.Rows) ([]*Url, error) {
defer rows.Close()
urls := []*Url{}
for rows.Next() {
u := &Url{}
if err := u.UnmarshalSQL(rows); err != nil {
return nil, err
}
urls = append(urls, u)
}
return urls, nil
}