@@ -1238,6 +1238,103 @@ func (s *SQLStore) ForEachNodeCached(cb func(node route.Vertex,
1238
1238
}, sqldb .NoOpReset )
1239
1239
}
1240
1240
1241
+ // ForEachChannelCacheable iterates through all the channel edges stored
1242
+ // within the graph and invokes the passed callback for each edge. The
1243
+ // callback takes two edges as since this is a directed graph, both the
1244
+ // in/out edges are visited. If the callback returns an error, then the
1245
+ // transaction is aborted and the iteration stops early.
1246
+ //
1247
+ // NOTE: If an edge can't be found, or wasn't advertised, then a nil
1248
+ // pointer for that particular channel edge routing policy will be
1249
+ // passed into the callback.
1250
+ //
1251
+ // NOTE: this method is like ForEachChannel but fetches only the data
1252
+ // required for the graph cache.
1253
+ func (s * SQLStore ) ForEachChannelCacheable (cb func (* models.CachedEdgeInfo ,
1254
+ * models.CachedEdgePolicy ,
1255
+ * models.CachedEdgePolicy ) error ) error {
1256
+
1257
+ ctx := context .TODO ()
1258
+
1259
+ handleChannel := func (db SQLQueries ,
1260
+ row sqlc.ListChannelsWithPoliciesPaginatedRow ) error {
1261
+
1262
+ node1 , node2 , err := buildNodeVertices (
1263
+ row .Node1Pubkey , row .Node2Pubkey ,
1264
+ )
1265
+ if err != nil {
1266
+ return err
1267
+ }
1268
+
1269
+ edge := buildCacheableChannelInfo (row .Channel , node1 , node2 )
1270
+
1271
+ dbPol1 , dbPol2 , err := extractChannelPolicies (row )
1272
+ if err != nil {
1273
+ return err
1274
+ }
1275
+
1276
+ var pol1 , pol2 * models.CachedEdgePolicy
1277
+ if dbPol1 != nil {
1278
+ policy1 , err := buildChanPolicy (
1279
+ * dbPol1 , edge .ChannelID , nil , node2 , true ,
1280
+ )
1281
+ if err != nil {
1282
+ return err
1283
+ }
1284
+
1285
+ pol1 = models .NewCachedPolicy (policy1 )
1286
+ }
1287
+ if dbPol2 != nil {
1288
+ policy2 , err := buildChanPolicy (
1289
+ * dbPol2 , edge .ChannelID , nil , node1 , false ,
1290
+ )
1291
+ if err != nil {
1292
+ return err
1293
+ }
1294
+
1295
+ pol2 = models .NewCachedPolicy (policy2 )
1296
+ }
1297
+
1298
+ if err := cb (edge , pol1 , pol2 ); err != nil {
1299
+ return err
1300
+ }
1301
+
1302
+ return nil
1303
+ }
1304
+
1305
+ return s .db .ExecTx (ctx , sqldb .ReadTxOpt (), func (db SQLQueries ) error {
1306
+ lastID := int64 (- 1 )
1307
+ for {
1308
+ //nolint:ll
1309
+ rows , err := db .ListChannelsWithPoliciesPaginated (
1310
+ ctx , sqlc.ListChannelsWithPoliciesPaginatedParams {
1311
+ Version : int16 (ProtocolV1 ),
1312
+ ID : lastID ,
1313
+ Limit : pageSize ,
1314
+ },
1315
+ )
1316
+ if err != nil {
1317
+ return err
1318
+ }
1319
+
1320
+ if len (rows ) == 0 {
1321
+ break
1322
+ }
1323
+
1324
+ for _ , row := range rows {
1325
+ err := handleChannel (db , row )
1326
+ if err != nil {
1327
+ return err
1328
+ }
1329
+
1330
+ lastID = row .Channel .ID
1331
+ }
1332
+ }
1333
+
1334
+ return nil
1335
+ }, sqldb .NoOpReset )
1336
+ }
1337
+
1241
1338
// ForEachChannel iterates through all the channel edges stored within the
1242
1339
// graph and invokes the passed callback for each edge. The callback takes two
1243
1340
// edges as since this is a directed graph, both the in/out edges are visited.
0 commit comments