@@ -434,9 +434,14 @@ func (m *MariaDB) ReadGraph(sourceName string, graph *knowledge.Graph) error {
434
434
435
435
now := time .Now ()
436
436
437
+ tx , err := m .db .Begin ()
438
+ if err != nil {
439
+ return fmt .Errorf ("Unable to create transaction: %v" , err )
440
+ }
441
+
437
442
{
438
443
// Select all relations produced by this source
439
- rows , err := m . db .QueryContext (context .Background (), `
444
+ rows , err := tx .QueryContext (context .Background (), `
440
445
SELECT a.type, a.value, b.type, b.value, r.type FROM relations_by_source rbs
441
446
INNER JOIN relations r ON rbs.relation_id = r.id
442
447
INNER JOIN assets a ON a.id=r.from_id
@@ -445,6 +450,7 @@ WHERE rbs.source_id = ?
445
450
` , sourceID )
446
451
447
452
if err != nil {
453
+ tx .Rollback ()
448
454
return fmt .Errorf ("Unable to retrieve relations: %v" , err )
449
455
}
450
456
@@ -472,13 +478,15 @@ WHERE rbs.source_id = ?
472
478
473
479
{
474
480
// Select all assets produced by this source. This is useful in case there are some standalone nodes in the graph of the source.
475
- rows , err := m .db .QueryContext (context .Background (), `
481
+ // TODO(c.michaud): optimization could be done by only selecting assets without any relation since the others have already have been retrieved in the previous query.
482
+ rows , err := tx .QueryContext (context .Background (), `
476
483
SELECT a.type, a.value FROM assets_by_source abs
477
484
INNER JOIN assets a ON a.id=abs.asset_id
478
485
WHERE abs.source_id = ?
479
486
` , sourceID )
480
487
481
488
if err != nil {
489
+ tx .Rollback ()
482
490
return fmt .Errorf ("Unable to retrieve assets: %v" , err )
483
491
}
484
492
@@ -487,12 +495,17 @@ WHERE abs.source_id = ?
487
495
for rows .Next () {
488
496
var Key , Type string
489
497
if err := rows .Scan (& Type , & Key ); err != nil {
490
- return err
498
+ tx .Rollback ()
499
+ return fmt .Errorf ("Unable to read standalone asset: %v" , err )
491
500
}
492
501
graph .AddAsset (schema .AssetType (Type ), Key )
493
502
}
494
503
}
495
504
505
+ if err = tx .Commit (); err != nil {
506
+ return fmt .Errorf ("Unable to commit transaction: %v" , err )
507
+ }
508
+
496
509
elapsed := time .Since (now )
497
510
fmt .Printf ("Read graph of data source with name %s in %fs\n " , sourceName , elapsed .Seconds ())
498
511
return nil
0 commit comments