mirror of
https://github.com/lightninglabs/faraday.git
synced 2026-08-16 13:01:10 +02:00
Switches the GetChannelEvents query from a timestamp-ordered scan to an id-keyset cursor (WHERE id > $cursor ORDER BY id ASC LIMIT $n). The keyset cursor is stable under concurrent inserts and survives a future retention job that prunes the oldest rows: a positional OFFSET would silently skip events whenever rows below the cursor are deleted, while "id > $cursor" keeps advancing past whatever the caller has already seen. The id field is documented in the proto as a server-assigned monotonic identity, so callers persist last_id as their sync watermark. Adds a (channel_id, id) composite index to back the new query; the existing (channel_id, timestamp) index does not cover it and would force a per-channel filter after a global id scan.
166 lines
3.9 KiB
Go
166 lines
3.9 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 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
|
|
}
|