mirror of
https://github.com/lightninglabs/faraday.git
synced 2026-08-13 12:33:35 +02:00
Add a PruneChannelEvents query that bounds the channel_events table by both size and age in a single statement: an id-keyset offset enforces a maximum event count and a timestamp filter enforces a retention window, OR-joined so each limit applies independently. The query returns the number of rows deleted so callers can surface pruning activity. Add a standalone timestamp index so the global age-based prune does not scan the full table. The existing composite index leads with channel_id and cannot serve a channel-agnostic timestamp filter.
267 lines
6.8 KiB
Go
267 lines
6.8 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.25.0
|
|
// source: chanevents.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"time"
|
|
)
|
|
|
|
const getChannelByChanPoint = `-- name: GetChannelByChanPoint :one
|
|
SELECT id, channel_point, short_channel_id, peer_id FROM channels WHERE channel_point = $1
|
|
`
|
|
|
|
func (q *Queries) GetChannelByChanPoint(ctx context.Context, channelPoint string) (Channel, error) {
|
|
row := q.db.QueryRowContext(ctx, getChannelByChanPoint, channelPoint)
|
|
var i Channel
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ChannelPoint,
|
|
&i.ShortChannelID,
|
|
&i.PeerID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getChannelByShortChanID = `-- name: GetChannelByShortChanID :one
|
|
SELECT id, channel_point, short_channel_id, peer_id FROM channels WHERE short_channel_id = $1
|
|
`
|
|
|
|
func (q *Queries) GetChannelByShortChanID(ctx context.Context, shortChannelID int64) (Channel, error) {
|
|
row := q.db.QueryRowContext(ctx, getChannelByShortChanID, shortChannelID)
|
|
var i Channel
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ChannelPoint,
|
|
&i.ShortChannelID,
|
|
&i.PeerID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getChannelEvents = `-- name: GetChannelEvents :many
|
|
SELECT id, channel_id, event_type, timestamp, local_balance_sat, remote_balance_sat, is_sync FROM channel_events
|
|
WHERE channel_id = $1
|
|
AND id > $2
|
|
AND timestamp >= $3
|
|
AND timestamp < $4
|
|
ORDER BY id ASC
|
|
LIMIT $5
|
|
`
|
|
|
|
type GetChannelEventsParams struct {
|
|
ChannelID int64
|
|
ID int64
|
|
Timestamp time.Time
|
|
Timestamp_2 time.Time
|
|
Limit int32
|
|
}
|
|
|
|
func (q *Queries) GetChannelEvents(ctx context.Context, arg GetChannelEventsParams) ([]ChannelEvent, error) {
|
|
rows, err := q.db.QueryContext(ctx, getChannelEvents,
|
|
arg.ChannelID,
|
|
arg.ID,
|
|
arg.Timestamp,
|
|
arg.Timestamp_2,
|
|
arg.Limit,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ChannelEvent
|
|
for rows.Next() {
|
|
var i ChannelEvent
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.ChannelID,
|
|
&i.EventType,
|
|
&i.Timestamp,
|
|
&i.LocalBalanceSat,
|
|
&i.RemoteBalanceSat,
|
|
&i.IsSync,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getChannels = `-- name: GetChannels :many
|
|
SELECT c.id, c.short_channel_id, p.pubkey
|
|
FROM channels c
|
|
JOIN peers p ON c.peer_id = p.id
|
|
`
|
|
|
|
type GetChannelsRow struct {
|
|
ID int64
|
|
ShortChannelID int64
|
|
Pubkey string
|
|
}
|
|
|
|
func (q *Queries) GetChannels(ctx context.Context) ([]GetChannelsRow, error) {
|
|
rows, err := q.db.QueryContext(ctx, getChannels)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []GetChannelsRow
|
|
for rows.Next() {
|
|
var i GetChannelsRow
|
|
if err := rows.Scan(&i.ID, &i.ShortChannelID, &i.Pubkey); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getLatestChannelEventBefore = `-- name: GetLatestChannelEventBefore :one
|
|
SELECT id, channel_id, event_type, timestamp, local_balance_sat, remote_balance_sat, is_sync FROM channel_events
|
|
WHERE channel_id = $1 AND event_type = $2 AND timestamp < $3
|
|
ORDER BY timestamp DESC, id DESC
|
|
LIMIT 1
|
|
`
|
|
|
|
type GetLatestChannelEventBeforeParams struct {
|
|
ChannelID int64
|
|
EventType int16
|
|
Timestamp time.Time
|
|
}
|
|
|
|
func (q *Queries) GetLatestChannelEventBefore(ctx context.Context, arg GetLatestChannelEventBeforeParams) (ChannelEvent, error) {
|
|
row := q.db.QueryRowContext(ctx, getLatestChannelEventBefore, arg.ChannelID, arg.EventType, arg.Timestamp)
|
|
var i ChannelEvent
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ChannelID,
|
|
&i.EventType,
|
|
&i.Timestamp,
|
|
&i.LocalBalanceSat,
|
|
&i.RemoteBalanceSat,
|
|
&i.IsSync,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getPeerByPubKey = `-- name: GetPeerByPubKey :one
|
|
SELECT id, pubkey FROM peers WHERE pubkey = $1
|
|
`
|
|
|
|
func (q *Queries) GetPeerByPubKey(ctx context.Context, pubkey string) (Peer, error) {
|
|
row := q.db.QueryRowContext(ctx, getPeerByPubKey, pubkey)
|
|
var i Peer
|
|
err := row.Scan(&i.ID, &i.Pubkey)
|
|
return i, err
|
|
}
|
|
|
|
const insertChannel = `-- name: InsertChannel :one
|
|
INSERT INTO channels (channel_point, short_channel_id, peer_id) VALUES ($1, $2, $3) RETURNING id
|
|
`
|
|
|
|
type InsertChannelParams struct {
|
|
ChannelPoint string
|
|
ShortChannelID int64
|
|
PeerID int64
|
|
}
|
|
|
|
func (q *Queries) InsertChannel(ctx context.Context, arg InsertChannelParams) (int64, error) {
|
|
row := q.db.QueryRowContext(ctx, insertChannel, arg.ChannelPoint, arg.ShortChannelID, arg.PeerID)
|
|
var id int64
|
|
err := row.Scan(&id)
|
|
return id, err
|
|
}
|
|
|
|
const insertChannelEvent = `-- name: InsertChannelEvent :exec
|
|
INSERT INTO channel_events (
|
|
channel_id, event_type, timestamp, local_balance_sat, remote_balance_sat,
|
|
is_sync
|
|
) VALUES ($1, $2, $3, $4, $5, $6)
|
|
`
|
|
|
|
type InsertChannelEventParams struct {
|
|
ChannelID int64
|
|
EventType int16
|
|
Timestamp time.Time
|
|
LocalBalanceSat sql.NullInt64
|
|
RemoteBalanceSat sql.NullInt64
|
|
IsSync bool
|
|
}
|
|
|
|
func (q *Queries) InsertChannelEvent(ctx context.Context, arg InsertChannelEventParams) error {
|
|
_, err := q.db.ExecContext(ctx, insertChannelEvent,
|
|
arg.ChannelID,
|
|
arg.EventType,
|
|
arg.Timestamp,
|
|
arg.LocalBalanceSat,
|
|
arg.RemoteBalanceSat,
|
|
arg.IsSync,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const insertPeer = `-- name: InsertPeer :one
|
|
INSERT INTO peers (pubkey) VALUES ($1) RETURNING id
|
|
`
|
|
|
|
func (q *Queries) InsertPeer(ctx context.Context, pubkey string) (int64, error) {
|
|
row := q.db.QueryRowContext(ctx, insertPeer, pubkey)
|
|
var id int64
|
|
err := row.Scan(&id)
|
|
return id, err
|
|
}
|
|
|
|
const pruneChannelEventsByAge = `-- name: PruneChannelEventsByAge :execrows
|
|
DELETE FROM channel_events
|
|
WHERE channel_events.timestamp < $1
|
|
`
|
|
|
|
// PruneChannelEventsByAge enforces the retention window on the channel_events
|
|
// table, returning the number of rows deleted. It deletes any row whose
|
|
// timestamp predates the given cutoff.
|
|
func (q *Queries) PruneChannelEventsByAge(ctx context.Context, timestamp time.Time) (int64, error) {
|
|
result, err := q.db.ExecContext(ctx, pruneChannelEventsByAge, timestamp)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected()
|
|
}
|
|
|
|
const pruneChannelEventsBySize = `-- name: PruneChannelEventsBySize :execrows
|
|
DELETE FROM channel_events
|
|
WHERE channel_events.id < COALESCE((
|
|
SELECT id FROM channel_events
|
|
ORDER BY id DESC
|
|
LIMIT 1 OFFSET $1
|
|
), 0)
|
|
`
|
|
|
|
// PruneChannelEventsBySize enforces the size ceiling on the channel_events
|
|
// table, returning the number of rows deleted. It keeps the newest rows by
|
|
// deleting everything with a smaller (earlier-inserted) id than the id found at
|
|
// the given offset from the newest row, so an offset of (max-events - 1) keeps
|
|
// exactly max-events rows.
|
|
func (q *Queries) PruneChannelEventsBySize(ctx context.Context, offset int32) (int64, error) {
|
|
result, err := q.db.ExecContext(ctx, pruneChannelEventsBySize, offset)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected()
|
|
}
|