fixed oom in bundle_gen and added randomOrder, still need a full redesign
This commit is contained in:
parent
cf17fc42b1
commit
e6d5d5175c
2 changed files with 132 additions and 100 deletions
|
|
@ -7,57 +7,36 @@ import (
|
|||
)
|
||||
|
||||
type HostRow struct {
|
||||
ID int64
|
||||
Hostname string
|
||||
Protocol string
|
||||
HtmlTitle string
|
||||
IframeAllowed bool
|
||||
BestIconS3Key string
|
||||
RandomOrder float32
|
||||
}
|
||||
|
||||
// fetchHosts gets all hosts with titles, randomized order.
|
||||
func fetchHosts(ctx context.Context, pool *pgxpool.Pool, limit int) ([]HostRow, error) {
|
||||
query := `
|
||||
SELECT hostname, protocol, html_title, COALESCE(iframe_allowed, true), COALESCE(best_icon_s3_key, '')
|
||||
// fetchHostsPage gets a page of hosts with titles, ordered by random_order for shuffled bundles.
|
||||
func fetchHostsPage(ctx context.Context, pool *pgxpool.Pool, lastRandom float32, limit int) ([]HostRow, error) {
|
||||
rows, err := pool.Query(ctx, `
|
||||
SELECT id, hostname, protocol, html_title, COALESCE(iframe_allowed, true), COALESCE(best_icon_s3_key, ''), random_order
|
||||
FROM hosts
|
||||
WHERE html_title IS NOT NULL
|
||||
ORDER BY random()
|
||||
`
|
||||
if limit > 0 {
|
||||
query += " LIMIT $1"
|
||||
}
|
||||
|
||||
var rows interface{ Query(context.Context, string, ...interface{}) (interface{ Close(); Next() bool; Scan(...interface{}) error; Err() error }, error) }
|
||||
_ = rows // unused, using pool directly
|
||||
|
||||
var hosts []HostRow
|
||||
|
||||
if limit > 0 {
|
||||
pgRows, err := pool.Query(ctx, query, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer pgRows.Close()
|
||||
for pgRows.Next() {
|
||||
var h HostRow
|
||||
if err := pgRows.Scan(&h.Hostname, &h.Protocol, &h.HtmlTitle, &h.IframeAllowed, &h.BestIconS3Key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hosts = append(hosts, h)
|
||||
}
|
||||
return hosts, pgRows.Err()
|
||||
}
|
||||
|
||||
pgRows, err := pool.Query(ctx, query)
|
||||
WHERE html_title IS NOT NULL AND random_order > $1
|
||||
ORDER BY random_order
|
||||
LIMIT $2
|
||||
`, lastRandom, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer pgRows.Close()
|
||||
for pgRows.Next() {
|
||||
defer rows.Close()
|
||||
|
||||
var hosts []HostRow
|
||||
for rows.Next() {
|
||||
var h HostRow
|
||||
if err := pgRows.Scan(&h.Hostname, &h.Protocol, &h.HtmlTitle, &h.IframeAllowed, &h.BestIconS3Key); err != nil {
|
||||
if err := rows.Scan(&h.ID, &h.Hostname, &h.Protocol, &h.HtmlTitle, &h.IframeAllowed, &h.BestIconS3Key, &h.RandomOrder); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hosts = append(hosts, h)
|
||||
}
|
||||
return hosts, pgRows.Err()
|
||||
return hosts, rows.Err()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue