Skip to content

Commit f1b7ccc

Browse files
committed
sqldb+graph/db: prune graph nodes in a single query
Update the pruneGraphNodes routine to prune the graph nodes in a single query instead of two.
1 parent 714e528 commit f1b7ccc

File tree

4 files changed

+65
-81
lines changed

4 files changed

+65
-81
lines changed

graph/db/sql_store.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type SQLQueries interface {
6464
ListNodesPaginated(ctx context.Context, arg sqlc.ListNodesPaginatedParams) ([]sqlc.Node, error)
6565
ListNodeIDsAndPubKeys(ctx context.Context, arg sqlc.ListNodeIDsAndPubKeysParams) ([]sqlc.ListNodeIDsAndPubKeysRow, error)
6666
IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, error)
67-
GetUnconnectedNodes(ctx context.Context) ([]sqlc.GetUnconnectedNodesRow, error)
67+
DeleteUnconnectedNodes(ctx context.Context) ([][]byte, error)
6868
DeleteNodeByPubKey(ctx context.Context, arg sqlc.DeleteNodeByPubKeyParams) (sql.Result, error)
6969
DeleteNode(ctx context.Context, id int64) error
7070

@@ -2551,30 +2551,21 @@ func (s *SQLStore) PruneTip() (*chainhash.Hash, uint32, error) {
25512551
func (s *SQLStore) pruneGraphNodes(ctx context.Context,
25522552
db SQLQueries) ([]route.Vertex, error) {
25532553

2554-
// Fetch all un-connected nodes from the database.
2555-
// NOTE: this will not include any nodes that are listed in the
2556-
// source table.
2557-
nodes, err := db.GetUnconnectedNodes(ctx)
2554+
nodeKeys, err := db.DeleteUnconnectedNodes(ctx)
25582555
if err != nil {
2559-
return nil, fmt.Errorf("unable to fetch unconnected nodes: %w",
2560-
err)
2556+
return nil, fmt.Errorf("unable to delete unconnected "+
2557+
"nodes: %w", err)
25612558
}
25622559

2563-
prunedNodes := make([]route.Vertex, 0, len(nodes))
2564-
for _, node := range nodes {
2565-
// TODO(elle): update to use sqlc.slice() once that works.
2566-
if err = db.DeleteNode(ctx, node.ID); err != nil {
2567-
return nil, fmt.Errorf("unable to delete "+
2568-
"node(id=%d): %w", node.ID, err)
2569-
}
2570-
2571-
pubKey, err := route.NewVertexFromBytes(node.PubKey)
2560+
prunedNodes := make([]route.Vertex, len(nodeKeys))
2561+
for i, nodeKey := range nodeKeys {
2562+
pub, err := route.NewVertexFromBytes(nodeKey)
25722563
if err != nil {
25732564
return nil, fmt.Errorf("unable to parse pubkey "+
2574-
"for node(id=%d): %w", node.ID, err)
2565+
"from bytes: %w", err)
25752566
}
25762567

2577-
prunedNodes = append(prunedNodes, pubKey)
2568+
prunedNodes[i] = pub
25782569
}
25792570

25802571
return prunedNodes, nil

sqldb/sqlc/graph.sql.go

Lines changed: 40 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqldb/sqlc/querier.go

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqldb/sqlc/queries/graph.sql

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,21 @@ SELECT EXISTS (
6565
AND n.pub_key = $1
6666
);
6767

68-
-- name: GetUnconnectedNodes :many
69-
SELECT n.id, n.pub_key
70-
FROM nodes n
71-
-- Select all nodes that do not have any channels.
72-
WHERE NOT EXISTS (
73-
SELECT 1
74-
FROM channels c
75-
WHERE c.node_id_1 = n.id OR c.node_id_2 = n.id
76-
)
77-
-- Ignore any of our source nodes.
78-
AND NOT EXISTS (
79-
SELECT 1
80-
FROM source_nodes sn
81-
WHERE sn.node_id = n.id
82-
);
68+
-- name: DeleteUnconnectedNodes :many
69+
DELETE FROM nodes
70+
WHERE
71+
-- Ignore any of our source nodes.
72+
NOT EXISTS (
73+
SELECT 1
74+
FROM source_nodes sn
75+
WHERE sn.node_id = nodes.id
76+
)
77+
-- Select all nodes that do not have any channels.
78+
AND NOT EXISTS (
79+
SELECT 1
80+
FROM channels c
81+
WHERE c.node_id_1 = nodes.id OR c.node_id_2 = nodes.id
82+
) RETURNING pub_key;
8383

8484
-- name: DeleteNodeByPubKey :execresult
8585
DELETE FROM nodes

0 commit comments

Comments
 (0)