@@ -249,7 +249,7 @@ func hashRelation(relation knowledge.Relation) uint64 {
249
249
}
250
250
251
251
// InsertAssets insert multiple assets into the graph of the given source
252
- func (m * MariaDB ) InsertAssets (source string , assets []knowledge.Asset ) error {
252
+ func (m * MariaDB ) InsertAssets (ctx context. Context , source string , assets []knowledge.Asset ) error {
253
253
sourceID , err := m .resolveSourceID (source )
254
254
if err != nil {
255
255
return fmt .Errorf ("Unable to resolve source ID of source %s for inserting assets: %v" , source , err )
@@ -263,7 +263,7 @@ func (m *MariaDB) InsertAssets(source string, assets []knowledge.Asset) error {
263
263
for _ , asset := range assets {
264
264
h := hashAsset (asset )
265
265
266
- _ , err = tx .ExecContext (context . Background () ,
266
+ _ , err = tx .ExecContext (ctx ,
267
267
`INSERT INTO assets (id, type, value) VALUES (?, ?, ?)` ,
268
268
h , asset .Type , asset .Key )
269
269
if err != nil {
@@ -275,7 +275,7 @@ func (m *MariaDB) InsertAssets(source string, assets []knowledge.Asset) error {
275
275
}
276
276
}
277
277
278
- _ , err = tx .ExecContext (context . Background () ,
278
+ _ , err = tx .ExecContext (ctx ,
279
279
`INSERT INTO assets_by_source (source_id, asset_id) VALUES (?, ?)` , sourceID , h )
280
280
if err != nil {
281
281
if driverErr , ok := err .(* mysql.MySQLError ); ok && driverErr .Number == mysqlerr .ER_DUP_ENTRY {
@@ -294,7 +294,7 @@ func (m *MariaDB) InsertAssets(source string, assets []knowledge.Asset) error {
294
294
}
295
295
296
296
// InsertRelations upsert one relation into the graph of the given source
297
- func (m * MariaDB ) InsertRelations (source string , relations []knowledge.Relation ) error {
297
+ func (m * MariaDB ) InsertRelations (ctx context. Context , source string , relations []knowledge.Relation ) error {
298
298
sourceID , err := m .resolveSourceID (source )
299
299
if err != nil {
300
300
return fmt .Errorf ("Unable to resolve source ID of source %s for inserting relations: %v" , source , err )
@@ -311,7 +311,7 @@ func (m *MariaDB) InsertRelations(source string, relations []knowledge.Relation)
311
311
aTo := hashAsset (knowledge .Asset (relation .To ))
312
312
rH := hashRelation (relation )
313
313
314
- _ , err = tx .ExecContext (context . Background () ,
314
+ _ , err = tx .ExecContext (ctx ,
315
315
"INSERT INTO relations (id, from_id, to_id, type) VALUES (?, ?, ?, ?)" ,
316
316
rH , aFrom , aTo , relation .Type )
317
317
if err != nil {
@@ -323,7 +323,7 @@ func (m *MariaDB) InsertRelations(source string, relations []knowledge.Relation)
323
323
}
324
324
}
325
325
326
- _ , err = tx .ExecContext (context . Background () ,
326
+ _ , err = tx .ExecContext (ctx ,
327
327
`INSERT INTO relations_by_source (source_id, relation_id) VALUES (?, ?)` , sourceID , rH )
328
328
if err != nil {
329
329
if driverErr , ok := err .(* mysql.MySQLError ); ok && driverErr .Number == mysqlerr .ER_DUP_ENTRY {
@@ -342,7 +342,7 @@ func (m *MariaDB) InsertRelations(source string, relations []knowledge.Relation)
342
342
}
343
343
344
344
// RemoveAssets remove one asset from the graph of the given source
345
- func (m * MariaDB ) RemoveAssets (source string , assets []knowledge.Asset ) error {
345
+ func (m * MariaDB ) RemoveAssets (ctx context. Context , source string , assets []knowledge.Asset ) error {
346
346
sourceID , err := m .resolveSourceID (source )
347
347
if err != nil {
348
348
return fmt .Errorf ("Unable to resolve source ID of source %s for removing assets: %v" , source , err )
@@ -356,15 +356,15 @@ func (m *MariaDB) RemoveAssets(source string, assets []knowledge.Asset) error {
356
356
for _ , asset := range assets {
357
357
h := hashAsset (asset )
358
358
359
- _ , err = tx .ExecContext (context . Background () ,
359
+ _ , err = tx .ExecContext (ctx ,
360
360
`DELETE FROM assets_by_source WHERE asset_id = ? AND source_id = ?` ,
361
361
h , sourceID )
362
362
if err != nil {
363
363
tx .Rollback ()
364
364
return fmt .Errorf ("Unable to remove binding between asset %v (%d) and source %s: %v" , asset , h , source , err )
365
365
}
366
366
367
- _ , err = tx .ExecContext (context . Background () ,
367
+ _ , err = tx .ExecContext (ctx ,
368
368
`DELETE FROM assets WHERE id = ? AND NOT EXISTS (
369
369
SELECT * FROM assets_by_source WHERE asset_id = ?
370
370
)` ,
@@ -383,7 +383,7 @@ func (m *MariaDB) RemoveAssets(source string, assets []knowledge.Asset) error {
383
383
}
384
384
385
385
// RemoveRelations remove relations from the graph of the given source
386
- func (m * MariaDB ) RemoveRelations (source string , relations []knowledge.Relation ) error {
386
+ func (m * MariaDB ) RemoveRelations (ctx context. Context , source string , relations []knowledge.Relation ) error {
387
387
sourceID , err := m .resolveSourceID (source )
388
388
if err != nil {
389
389
return fmt .Errorf ("Unable to resolve source ID of source %s for removing relations: %v" , source , err )
@@ -397,19 +397,18 @@ func (m *MariaDB) RemoveRelations(source string, relations []knowledge.Relation)
397
397
for _ , relation := range relations {
398
398
rH := hashRelation (relation )
399
399
400
- _ , err = tx .ExecContext (context . Background () ,
400
+ _ , err = tx .ExecContext (ctx ,
401
401
`DELETE FROM relations_by_source WHERE relation_id = ? AND source_id = ?` ,
402
402
rH , sourceID )
403
403
if err != nil {
404
404
tx .Rollback ()
405
405
return fmt .Errorf ("Unable to remove binding between relation %v (%d) and source %s: %v" , relation , rH , source , err )
406
406
}
407
407
408
- _ , err = tx .ExecContext (context . Background () ,
408
+ _ , err = tx .ExecContext (ctx ,
409
409
`DELETE FROM relations WHERE id = ? AND NOT EXISTS (
410
410
SELECT * FROM relations_by_source WHERE relation_id = ?
411
- )` ,
412
- rH , rH )
411
+ )` , rH , rH )
413
412
if err != nil {
414
413
tx .Rollback ()
415
414
return fmt .Errorf ("Unable to remove relation %v (%d) from source %s: %v" , relation , rH , source , err )
@@ -423,7 +422,7 @@ func (m *MariaDB) RemoveRelations(source string, relations []knowledge.Relation)
423
422
}
424
423
425
424
// ReadGraph read source subgraph
426
- func (m * MariaDB ) ReadGraph (sourceName string , graph * knowledge.Graph ) error {
425
+ func (m * MariaDB ) ReadGraph (ctx context. Context , sourceName string , graph * knowledge.Graph ) error {
427
426
logrus .Debugf ("Start reading graph of data source with name %s" , sourceName )
428
427
sourceID , err := m .resolveSourceID (sourceName )
429
428
if err != nil {
@@ -439,7 +438,7 @@ func (m *MariaDB) ReadGraph(sourceName string, graph *knowledge.Graph) error {
439
438
440
439
{
441
440
// Select all relations produced by this source
442
- rows , err := tx .QueryContext (context . Background () , `
441
+ rows , err := tx .QueryContext (ctx , `
443
442
SELECT a.type, a.value, b.type, b.value, r.type FROM relations_by_source rbs
444
443
INNER JOIN relations r ON rbs.relation_id = r.id
445
444
INNER JOIN assets a ON a.id=r.from_id
@@ -477,7 +476,7 @@ WHERE rbs.source_id = ?
477
476
{
478
477
// Select all assets produced by this source. This is useful in case there are some standalone nodes in the graph of the source.
479
478
// 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.
480
- rows , err := tx .QueryContext (context . Background () , `
479
+ rows , err := tx .QueryContext (ctx , `
481
480
SELECT a.type, a.value FROM assets_by_source abs
482
481
INNER JOIN assets a ON a.id=abs.asset_id
483
482
WHERE abs.source_id = ?
@@ -510,53 +509,53 @@ WHERE abs.source_id = ?
510
509
}
511
510
512
511
// FlushAll flush the database
513
- func (m * MariaDB ) FlushAll () error {
512
+ func (m * MariaDB ) FlushAll (ctx context. Context ) error {
514
513
tx , err := m .db .Begin ()
515
514
if err != nil {
516
515
return err
517
516
}
518
517
519
- _ , err = tx .ExecContext (context . Background () , "DROP TABLE relations_by_source" )
518
+ _ , err = tx .ExecContext (ctx , "DROP TABLE relations_by_source" )
520
519
if err != nil {
521
520
if ! isUnknownTableError (err ) {
522
521
tx .Rollback ()
523
522
return err
524
523
}
525
524
}
526
525
527
- _ , err = tx .ExecContext (context . Background () , "DROP TABLE assets_by_source" )
526
+ _ , err = tx .ExecContext (ctx , "DROP TABLE assets_by_source" )
528
527
if err != nil {
529
528
if ! isUnknownTableError (err ) {
530
529
tx .Rollback ()
531
530
return err
532
531
}
533
532
}
534
533
535
- _ , err = tx .ExecContext (context . Background () , "DROP TABLE relations" )
534
+ _ , err = tx .ExecContext (ctx , "DROP TABLE relations" )
536
535
if err != nil {
537
536
if ! isUnknownTableError (err ) {
538
537
tx .Rollback ()
539
538
return err
540
539
}
541
540
}
542
541
543
- _ , err = tx .ExecContext (context . Background () , "DROP TABLE assets" )
542
+ _ , err = tx .ExecContext (ctx , "DROP TABLE assets" )
544
543
if err != nil {
545
544
if ! isUnknownTableError (err ) {
546
545
tx .Rollback ()
547
546
return err
548
547
}
549
548
}
550
549
551
- _ , err = tx .ExecContext (context . Background () , "DROP TABLE graph_schema" )
550
+ _ , err = tx .ExecContext (ctx , "DROP TABLE graph_schema" )
552
551
if err != nil {
553
552
if ! isUnknownTableError (err ) {
554
553
tx .Rollback ()
555
554
return err
556
555
}
557
556
}
558
557
559
- _ , err = tx .ExecContext (context . Background () , "DROP TABLE query_history" )
558
+ _ , err = tx .ExecContext (ctx , "DROP TABLE query_history" )
560
559
if err != nil {
561
560
if ! isUnknownTableError (err ) {
562
561
tx .Rollback ()
@@ -568,9 +567,9 @@ func (m *MariaDB) FlushAll() error {
568
567
}
569
568
570
569
// CountAssets count the total number of assets in db.
571
- func (m * MariaDB ) CountAssets () (int64 , error ) {
570
+ func (m * MariaDB ) CountAssets (ctx context. Context ) (int64 , error ) {
572
571
var count int64
573
- row := m .db .QueryRowContext (context . Background () , "SELECT COUNT(*) FROM assets" )
572
+ row := m .db .QueryRowContext (ctx , "SELECT COUNT(*) FROM assets" )
574
573
575
574
err := row .Scan (& count )
576
575
if err != nil {
@@ -580,9 +579,9 @@ func (m *MariaDB) CountAssets() (int64, error) {
580
579
}
581
580
582
581
// CountRelations count the total number of relations in db.
583
- func (m * MariaDB ) CountRelations () (int64 , error ) {
582
+ func (m * MariaDB ) CountRelations (ctx context. Context ) (int64 , error ) {
584
583
var count int64
585
- row := m .db .QueryRowContext (context . Background () , "SELECT COUNT(*) FROM relations" )
584
+ row := m .db .QueryRowContext (ctx , "SELECT COUNT(*) FROM relations" )
586
585
587
586
err := row .Scan (& count )
588
587
if err != nil {
0 commit comments