check / check (push) Canceled after 0s
Takes .golangci.yml, .dockerignore, .gitignore, .editorconfig, the workflow and REPO_POLICIES.md from sneak/prompts 0653579, with this repository's own entries after the shared content. Lint (golangci-lint v2.14.0) and test are Dockerfile phases that script/lint and script/test build alone with a new CACHEBUST value, keeping Go's and golangci-lint's caches; the image build depends on both. make fmt and make fmt-check also format the markdown with prettier, pinned in yarn.lock. The agent guidance moves to AGENTS.md. The Go code is fixed for what the new configuration finds, and the AS and prefix detail reads now return the rows' error after their loops. Model: opus-5-5
105 lines
3.7 KiB
Go
105 lines
3.7 KiB
Go
// Package database provides SQLite storage for BGP routing data including ASNs,
|
|
// prefixes, announcements, peerings, and live route tables.
|
|
package database
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// Stats contains database statistics including counts for ASNs, prefixes,
|
|
// peerings, peers, and live routes, as well as file size and prefix distribution data.
|
|
type Stats struct {
|
|
ASNs int
|
|
Prefixes int
|
|
IPv4Prefixes int
|
|
IPv6Prefixes int
|
|
Peerings int
|
|
Peers int
|
|
FileSizeBytes int64
|
|
LiveRoutes int
|
|
IPv4Routes int
|
|
IPv6Routes int
|
|
OldestRoute *time.Time
|
|
NewestRoute *time.Time
|
|
IPv4PrefixDistribution []PrefixDistribution
|
|
IPv6PrefixDistribution []PrefixDistribution
|
|
}
|
|
|
|
// Store defines the interface for database operations. It provides methods for
|
|
// managing ASNs, prefixes, announcements, peerings, BGP peers, and live routes.
|
|
// Implementations must be safe for concurrent use.
|
|
//
|
|
//nolint:interfacebloat // the one seam handlers and tests depend on
|
|
type Store interface {
|
|
// ASN operations
|
|
GetOrCreateASN(number int, timestamp time.Time) (*ASN, error)
|
|
GetOrCreateASNBatch(asns map[int]time.Time) error
|
|
|
|
// Prefix operations
|
|
GetOrCreatePrefix(prefix string, timestamp time.Time) (*Prefix, error)
|
|
UpdatePrefixesBatch(prefixes map[string]time.Time) error
|
|
|
|
// Announcement operations
|
|
RecordAnnouncement(announcement *Announcement) error
|
|
|
|
// Peering operations
|
|
RecordPeering(asA, asB int, timestamp time.Time) error
|
|
|
|
// Statistics
|
|
GetStats() (Stats, error)
|
|
GetStatsContext(ctx context.Context) (Stats, error)
|
|
|
|
// Peer operations
|
|
UpdatePeer(peerIP string, peerASN int, messageType string, timestamp time.Time) error
|
|
UpdatePeerBatch(peers map[string]PeerUpdate) error
|
|
|
|
// Live route operations
|
|
UpsertLiveRoute(route *LiveRoute) error
|
|
UpsertLiveRouteBatch(routes []*LiveRoute) error
|
|
DeleteLiveRoute(prefix, peerIP string) error
|
|
DeleteLiveRouteBatch(deletions []LiveRouteDeletion) error
|
|
DeleteLiveRoutesOfPeer(peerIP string) (deleted int, err error)
|
|
GetPrefixDistribution() (
|
|
ipv4 []PrefixDistribution, ipv6 []PrefixDistribution, err error)
|
|
GetPrefixDistributionContext(ctx context.Context) (
|
|
ipv4 []PrefixDistribution, ipv6 []PrefixDistribution, err error)
|
|
GetLiveRouteCounts() (ipv4Count, ipv6Count int, err error)
|
|
GetLiveRouteCountsContext(ctx context.Context) (ipv4Count, ipv6Count int, err error)
|
|
|
|
// IP lookup operations
|
|
GetIPInfo(ip string) (*IPInfo, error)
|
|
GetIPInfoContext(ctx context.Context, ip string) (*IPInfo, error)
|
|
|
|
// ASN WHOIS operations
|
|
GetNextStaleASN(ctx context.Context, staleThreshold time.Duration) (int, error)
|
|
UpdateASNWHOIS(ctx context.Context, update *ASNWHOISUpdate) error
|
|
GetWHOISStats(ctx context.Context, staleThreshold time.Duration) (*WHOISStats, error)
|
|
|
|
// AS and prefix detail operations
|
|
GetASDetails(asn int) (*ASN, []LiveRoute, error)
|
|
GetASDetailsContext(ctx context.Context, asn int) (*ASN, []LiveRoute, error)
|
|
GetASPeers(asn int) ([]ASPeer, error)
|
|
GetASPeersContext(ctx context.Context, asn int) ([]ASPeer, error)
|
|
GetPrefixDetails(prefix string) ([]LiveRoute, map[int]ASN, error)
|
|
GetPrefixDetailsContext(
|
|
ctx context.Context, prefix string,
|
|
) ([]LiveRoute, map[int]ASN, error)
|
|
GetRandomPrefixesByLength(maskLength, ipVersion, limit int) ([]LiveRoute, error)
|
|
GetRandomPrefixesByLengthContext(
|
|
ctx context.Context, maskLength, ipVersion, limit int,
|
|
) ([]LiveRoute, error)
|
|
|
|
// Lifecycle
|
|
Close() error
|
|
|
|
// Maintenance operations
|
|
Vacuum(ctx context.Context) error
|
|
Analyze(ctx context.Context) error
|
|
Checkpoint(ctx context.Context) error
|
|
Ping(ctx context.Context) error
|
|
}
|
|
|
|
// Ensure Database implements Store
|
|
var _ Store = (*Database)(nil)
|