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
/
primers.go
92 lines (77 loc) · 1.91 KB
/
primers.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
package core
import (
"database/sql"
"github.com/datatogether/sqlutil"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
)
// CrawlingPrimers
// func CrawlingPrimers(db sqlutil.Queryable, limit, offset int) (primers []*Primer, err error) {
// rows, err := db.Query(qPrimersCrawling, limit, offset)
// if err != nil {
// return primers, err
// }
// defer rows.Close()
// for rows.Next() {
// d := &Primer{}
// if err := d.UnmarshalSQL(rows); err != nil {
// return primers, err
// }
// primers = append(primers, d)
// }
// return
// }
// CountPrimers returns the total number of primers
func CountPrimers(db sqlutil.Queryable) (count int64, err error) {
err = db.QueryRow(qPrimersCount).Scan(&count)
return
}
// ListPrimers
func ListPrimers(store datastore.Datastore, limit, offset int) ([]*Primer, error) {
q := query.Query{
Prefix: Primer{}.DatastoreType(),
Limit: limit,
Offset: offset,
}
res, err := store.Query(q)
if err != nil {
return nil, err
}
primers := make([]*Primer, limit)
i := 0
for r := range res.Next() {
if r.Error != nil {
return nil, err
}
c, ok := r.Value.(*Primer)
if !ok {
return nil, ErrInvalidResponse
}
primers[i] = c
i++
}
return primers[:i], nil
}
// BasePrimers lists primers that have no parent
func BasePrimers(db sqlutil.Queryable, limit, offset int) (primers []*Primer, err error) {
rows, err := db.Query(qBasePrimersList, limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
return UnmarshalBoundedPrimers(rows, limit)
}
// UnmarshalBoundedPrimers turns sql.Rows into primers, expecting len(rows) <= limit
func UnmarshalBoundedPrimers(rows *sql.Rows, limit int) (primers []*Primer, err error) {
primers = make([]*Primer, limit)
i := 0
for rows.Next() {
p := &Primer{}
if err := p.UnmarshalSQL(rows); err != nil {
return primers, err
}
primers[i] = p
i++
}
return primers[:i], nil
}