43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type HostRow struct {
|
|
ID int64
|
|
Hostname string
|
|
Protocol string
|
|
HtmlTitle string
|
|
IframeAllowed bool
|
|
BestIconHash string
|
|
}
|
|
|
|
// fetchHostsPage gets a page of hosts with titles, ordered by id for disk locality.
|
|
// Icons were downloaded roughly in host-ID order, so reading by ID approximates
|
|
// the physical write order on disk — improves EBS readahead cache hits.
|
|
func fetchHostsPage(ctx context.Context, pool *pgxpool.Pool, lastID int64, limit int) ([]HostRow, error) {
|
|
rows, err := pool.Query(ctx, `
|
|
SELECT id, hostname, protocol, html_title, COALESCE(iframe_allowed, true), COALESCE(best_icon_hash, '')
|
|
FROM hosts
|
|
WHERE html_title IS NOT NULL AND id > $1
|
|
ORDER BY id
|
|
LIMIT $2
|
|
`, lastID, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var hosts []HostRow
|
|
for rows.Next() {
|
|
var h HostRow
|
|
if err := rows.Scan(&h.ID, &h.Hostname, &h.Protocol, &h.HtmlTitle, &h.IframeAllowed, &h.BestIconHash); err != nil {
|
|
return nil, err
|
|
}
|
|
hosts = append(hosts, h)
|
|
}
|
|
return hosts, rows.Err()
|
|
}
|