11const crypto = require ( 'crypto' ) ;
22const R = require ( 'ramda' ) ;
3+ const { cancelCombinator } = require ( '../driver/utils' ) ;
34const RedisCacheDriver = require ( './RedisCacheDriver' ) ;
45const LocalCacheDriver = require ( './LocalCacheDriver' ) ;
56
@@ -380,13 +381,13 @@ class PreAggregationLoader {
380381 refreshStrategy = readOnly ?
381382 this . refreshImplStreamExternalStrategy : this . refreshImplTempTableExternalStrategy ;
382383 }
383- const resultPromise = refreshStrategy . bind ( this ) ( client , newVersionEntry ) ;
384- resultPromise . cancel = ( ) => { } ; // TODO implement cancel (loading rollup into table and external upload )
385- return resultPromise ;
384+ return cancelCombinator (
385+ saveCancelFn => refreshStrategy . bind ( this ) ( client , newVersionEntry , saveCancelFn )
386+ ) ;
386387 } ;
387388 }
388389
389- async refreshImplStoreInSourceStrategy ( client , newVersionEntry ) {
390+ async refreshImplStoreInSourceStrategy ( client , newVersionEntry , saveCancelFn ) {
390391 const [ loadSql , params ] =
391392 Array . isArray ( this . preAggregation . loadSql ) ? this . preAggregation . loadSql : [ this . preAggregation . loadSql , [ ] ] ;
392393 const targetTableName = this . targetTableName ( newVersionEntry ) ;
@@ -402,18 +403,18 @@ class PreAggregationLoader {
402403 targetTableName,
403404 requestId : this . requestId
404405 } ) ;
405- await client . loadPreAggregationIntoTable (
406+ await saveCancelFn ( client . loadPreAggregationIntoTable (
406407 targetTableName ,
407408 query ,
408409 params
409- ) ;
410- await this . createIndexes ( client , newVersionEntry ) ;
410+ ) ) ;
411+ await this . createIndexes ( client , newVersionEntry , saveCancelFn ) ;
411412 await this . loadCache . reset ( this . preAggregation ) ;
412- await this . dropOrphanedTables ( client , targetTableName ) ;
413+ await this . dropOrphanedTables ( client , targetTableName , saveCancelFn ) ;
413414 await this . loadCache . reset ( this . preAggregation ) ;
414415 }
415416
416- async refreshImplTempTableExternalStrategy ( client , newVersionEntry ) {
417+ async refreshImplTempTableExternalStrategy ( client , newVersionEntry , saveCancelFn ) {
417418 const [ loadSql , params ] =
418419 Array . isArray ( this . preAggregation . loadSql ) ? this . preAggregation . loadSql : [ this . preAggregation . loadSql , [ ] ] ;
419420 await client . createSchemaIfNotExists ( this . preAggregation . preAggregationsSchema ) ;
@@ -430,18 +431,18 @@ class PreAggregationLoader {
430431 targetTableName,
431432 requestId : this . requestId
432433 } ) ;
433- await client . loadPreAggregationIntoTable (
434+ await saveCancelFn ( client . loadPreAggregationIntoTable (
434435 targetTableName ,
435436 query ,
436437 params
437- ) ;
438- const tableData = await this . downloadTempExternalPreAggregation ( client , newVersionEntry ) ;
439- await this . uploadExternalPreAggregation ( tableData , newVersionEntry ) ;
438+ ) ) ;
439+ const tableData = await this . downloadTempExternalPreAggregation ( client , newVersionEntry , saveCancelFn ) ;
440+ await this . uploadExternalPreAggregation ( tableData , newVersionEntry , saveCancelFn ) ;
440441 await this . loadCache . reset ( this . preAggregation ) ;
441- await this . dropOrphanedTables ( client , targetTableName ) ;
442+ await this . dropOrphanedTables ( client , targetTableName , saveCancelFn ) ;
442443 }
443444
444- async refreshImplStreamExternalStrategy ( client , newVersionEntry ) {
445+ async refreshImplStreamExternalStrategy ( client , newVersionEntry , saveCancelFn ) {
445446 const [ sql , params ] =
446447 Array . isArray ( this . preAggregation . sql ) ? this . preAggregation . sql : [ this . preAggregation . sql , [ ] ] ;
447448 if ( ! client . downloadQueryResults ) {
@@ -452,12 +453,12 @@ class PreAggregationLoader {
452453 preAggregation : this . preAggregation ,
453454 requestId : this . requestId
454455 } ) ;
455- const tableData = await client . downloadQueryResults ( sql , params ) ;
456- await this . uploadExternalPreAggregation ( tableData , newVersionEntry ) ;
456+ const tableData = await saveCancelFn ( client . downloadQueryResults ( sql , params ) ) ;
457+ await this . uploadExternalPreAggregation ( tableData , newVersionEntry , saveCancelFn ) ;
457458 await this . loadCache . reset ( this . preAggregation ) ;
458459 }
459460
460- async downloadTempExternalPreAggregation ( client , newVersionEntry ) {
461+ async downloadTempExternalPreAggregation ( client , newVersionEntry , saveCancelFn ) {
461462 if ( ! client . downloadTable ) {
462463 throw new Error ( `Can't load external pre-aggregation: source driver doesn't support downloadTable()` ) ;
463464 }
@@ -466,12 +467,12 @@ class PreAggregationLoader {
466467 preAggregation : this . preAggregation ,
467468 requestId : this . requestId
468469 } ) ;
469- const tableData = await client . downloadTable ( table ) ;
470- tableData . types = await client . tableColumnTypes ( table ) ;
470+ const tableData = await saveCancelFn ( client . downloadTable ( table ) ) ;
471+ tableData . types = await saveCancelFn ( client . tableColumnTypes ( table ) ) ;
471472 return tableData ;
472473 }
473474
474- async uploadExternalPreAggregation ( tableData , newVersionEntry ) {
475+ async uploadExternalPreAggregation ( tableData , newVersionEntry , saveCancelFn ) {
475476 const table = this . targetTableName ( newVersionEntry ) ;
476477 const externalDriver = await this . externalDriverFactory ( ) ;
477478 if ( ! externalDriver . uploadTable ) {
@@ -481,13 +482,13 @@ class PreAggregationLoader {
481482 preAggregation : this . preAggregation ,
482483 requestId : this . requestId
483484 } ) ;
484- await externalDriver . uploadTable ( table , tableData . types , tableData ) ;
485- await this . createIndexes ( externalDriver , newVersionEntry ) ;
485+ await saveCancelFn ( externalDriver . uploadTable ( table , tableData . types , tableData ) ) ;
486+ await this . createIndexes ( externalDriver , newVersionEntry , saveCancelFn ) ;
486487 await this . loadCache . reset ( this . preAggregation ) ;
487- await this . dropOrphanedTables ( externalDriver , table ) ;
488+ await this . dropOrphanedTables ( externalDriver , table , saveCancelFn ) ;
488489 }
489490
490- async createIndexes ( driver , newVersionEntry ) {
491+ async createIndexes ( driver , newVersionEntry , saveCancelFn ) {
491492 if ( ! this . preAggregation . indexesSql || ! this . preAggregation . indexesSql . length ) {
492493 return ;
493494 }
@@ -503,7 +504,7 @@ class PreAggregationLoader {
503504 requestId : this . requestId ,
504505 sql
505506 } ) ;
506- await driver . query (
507+ await saveCancelFn ( driver . query (
507508 QueryCache . replacePreAggregationTableNames (
508509 query ,
509510 this . preAggregationsTablesToTempTables . concat ( [
@@ -512,11 +513,11 @@ class PreAggregationLoader {
512513 ] )
513514 ) ,
514515 params
515- ) ;
516+ ) ) ;
516517 }
517518 }
518519
519- async dropOrphanedTables ( client , justCreatedTable ) {
520+ async dropOrphanedTables ( client , justCreatedTable , saveCancelFn ) {
520521 await this . preAggregations . addTableUsed ( justCreatedTable ) ;
521522 const actualTables = await client . getTablesQuery ( this . preAggregation . preAggregationsSchema ) ;
522523 const versionEntries = tablesToVersionEntries ( this . preAggregation . preAggregationsSchema , actualTables ) ;
@@ -536,7 +537,7 @@ class PreAggregationLoader {
536537 tablesToDrop : JSON . stringify ( toDrop ) ,
537538 requestId : this . requestId
538539 } ) ;
539- await Promise . all ( toDrop . map ( table => client . dropTable ( table ) ) ) ;
540+ await Promise . all ( toDrop . map ( table => saveCancelFn ( client . dropTable ( table ) ) ) ) ;
540541 }
541542}
542543
0 commit comments