mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-25 18:11:08 +02:00
graph/db+sqldb: queries and logic for batch fetching node data
In this commit, we add the queries that will be needed to batch-fetch the data of a set of nodes. The logic for using these new queries is also added but not used yet.
This commit is contained in:
@@ -1473,6 +1473,51 @@ func (q *Queries) GetNodeAddresses(ctx context.Context, nodeID int64) ([]GetNode
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getNodeAddressesBatch = `-- name: GetNodeAddressesBatch :many
|
||||
SELECT node_id, type, position, address
|
||||
FROM graph_node_addresses
|
||||
WHERE node_id IN (/*SLICE:ids*/?)
|
||||
ORDER BY node_id, type, position
|
||||
`
|
||||
|
||||
func (q *Queries) GetNodeAddressesBatch(ctx context.Context, ids []int64) ([]GraphNodeAddress, error) {
|
||||
query := getNodeAddressesBatch
|
||||
var queryParams []interface{}
|
||||
if len(ids) > 0 {
|
||||
for _, v := range ids {
|
||||
queryParams = append(queryParams, v)
|
||||
}
|
||||
query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
|
||||
} else {
|
||||
query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
|
||||
}
|
||||
rows, err := q.db.QueryContext(ctx, query, queryParams...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GraphNodeAddress
|
||||
for rows.Next() {
|
||||
var i GraphNodeAddress
|
||||
if err := rows.Scan(
|
||||
&i.NodeID,
|
||||
&i.Type,
|
||||
&i.Position,
|
||||
&i.Address,
|
||||
); 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 getNodeByPubKey = `-- name: GetNodeByPubKey :one
|
||||
SELECT id, version, pub_key, alias, last_update, color, signature
|
||||
FROM graph_nodes
|
||||
@@ -1500,6 +1545,46 @@ func (q *Queries) GetNodeByPubKey(ctx context.Context, arg GetNodeByPubKeyParams
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getNodeExtraTypesBatch = `-- name: GetNodeExtraTypesBatch :many
|
||||
SELECT node_id, type, value
|
||||
FROM graph_node_extra_types
|
||||
WHERE node_id IN (/*SLICE:ids*/?)
|
||||
ORDER BY node_id, type
|
||||
`
|
||||
|
||||
func (q *Queries) GetNodeExtraTypesBatch(ctx context.Context, ids []int64) ([]GraphNodeExtraType, error) {
|
||||
query := getNodeExtraTypesBatch
|
||||
var queryParams []interface{}
|
||||
if len(ids) > 0 {
|
||||
for _, v := range ids {
|
||||
queryParams = append(queryParams, v)
|
||||
}
|
||||
query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
|
||||
} else {
|
||||
query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
|
||||
}
|
||||
rows, err := q.db.QueryContext(ctx, query, queryParams...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GraphNodeExtraType
|
||||
for rows.Next() {
|
||||
var i GraphNodeExtraType
|
||||
if err := rows.Scan(&i.NodeID, &i.Type, &i.Value); 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 getNodeFeatures = `-- name: GetNodeFeatures :many
|
||||
SELECT node_id, feature_bit
|
||||
FROM graph_node_features
|
||||
@@ -1529,6 +1614,46 @@ func (q *Queries) GetNodeFeatures(ctx context.Context, nodeID int64) ([]GraphNod
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getNodeFeaturesBatch = `-- name: GetNodeFeaturesBatch :many
|
||||
SELECT node_id, feature_bit
|
||||
FROM graph_node_features
|
||||
WHERE node_id IN (/*SLICE:ids*/?)
|
||||
ORDER BY node_id, feature_bit
|
||||
`
|
||||
|
||||
func (q *Queries) GetNodeFeaturesBatch(ctx context.Context, ids []int64) ([]GraphNodeFeature, error) {
|
||||
query := getNodeFeaturesBatch
|
||||
var queryParams []interface{}
|
||||
if len(ids) > 0 {
|
||||
for _, v := range ids {
|
||||
queryParams = append(queryParams, v)
|
||||
}
|
||||
query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
|
||||
} else {
|
||||
query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
|
||||
}
|
||||
rows, err := q.db.QueryContext(ctx, query, queryParams...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GraphNodeFeature
|
||||
for rows.Next() {
|
||||
var i GraphNodeFeature
|
||||
if err := rows.Scan(&i.NodeID, &i.FeatureBit); 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 getNodeFeaturesByPubKey = `-- name: GetNodeFeaturesByPubKey :many
|
||||
SELECT f.feature_bit
|
||||
FROM graph_nodes n
|
||||
|
@@ -60,8 +60,11 @@ type Querier interface {
|
||||
GetKVInvoicePaymentHashByAddIndex(ctx context.Context, addIndex int64) ([]byte, error)
|
||||
GetMigration(ctx context.Context, version int32) (time.Time, error)
|
||||
GetNodeAddresses(ctx context.Context, nodeID int64) ([]GetNodeAddressesRow, error)
|
||||
GetNodeAddressesBatch(ctx context.Context, ids []int64) ([]GraphNodeAddress, error)
|
||||
GetNodeByPubKey(ctx context.Context, arg GetNodeByPubKeyParams) (GraphNode, error)
|
||||
GetNodeExtraTypesBatch(ctx context.Context, ids []int64) ([]GraphNodeExtraType, error)
|
||||
GetNodeFeatures(ctx context.Context, nodeID int64) ([]GraphNodeFeature, error)
|
||||
GetNodeFeaturesBatch(ctx context.Context, ids []int64) ([]GraphNodeFeature, error)
|
||||
GetNodeFeaturesByPubKey(ctx context.Context, arg GetNodeFeaturesByPubKeyParams) ([]int32, error)
|
||||
GetNodeIDByPubKey(ctx context.Context, arg GetNodeIDByPubKeyParams) (int64, error)
|
||||
GetNodesByLastUpdateRange(ctx context.Context, arg GetNodesByLastUpdateRangeParams) ([]GraphNode, error)
|
||||
|
@@ -114,6 +114,12 @@ FROM graph_nodes n
|
||||
WHERE n.pub_key = $1
|
||||
AND n.version = $2;
|
||||
|
||||
-- name: GetNodeFeaturesBatch :many
|
||||
SELECT node_id, feature_bit
|
||||
FROM graph_node_features
|
||||
WHERE node_id IN (sqlc.slice('ids')/*SLICE:ids*/)
|
||||
ORDER BY node_id, feature_bit;
|
||||
|
||||
-- name: DeleteNodeFeature :exec
|
||||
DELETE FROM graph_node_features
|
||||
WHERE node_id = $1
|
||||
@@ -140,6 +146,12 @@ FROM graph_node_addresses
|
||||
WHERE node_id = $1
|
||||
ORDER BY type ASC, position ASC;
|
||||
|
||||
-- name: GetNodeAddressesBatch :many
|
||||
SELECT node_id, type, position, address
|
||||
FROM graph_node_addresses
|
||||
WHERE node_id IN (sqlc.slice('ids')/*SLICE:ids*/)
|
||||
ORDER BY node_id, type, position;
|
||||
|
||||
-- name: GetNodesByLastUpdateRange :many
|
||||
SELECT *
|
||||
FROM graph_nodes
|
||||
@@ -170,6 +182,12 @@ SELECT *
|
||||
FROM graph_node_extra_types
|
||||
WHERE node_id = $1;
|
||||
|
||||
-- name: GetNodeExtraTypesBatch :many
|
||||
SELECT node_id, type, value
|
||||
FROM graph_node_extra_types
|
||||
WHERE node_id IN (sqlc.slice('ids')/*SLICE:ids*/)
|
||||
ORDER BY node_id, type;
|
||||
|
||||
-- name: DeleteExtraNodeType :exec
|
||||
DELETE FROM graph_node_extra_types
|
||||
WHERE node_id = $1
|
||||
|
Reference in New Issue
Block a user