@@ -17,33 +17,35 @@ function version(cacheKey) {
1717 for ( let i = 0 ; i < 5 ; i ++ ) {
1818 const byte = digestBuffer . readUInt8 ( i ) ;
1919 shiftCounter += 8 ;
20+ // eslint-disable-next-line operator-assignment,no-bitwise
2021 residue = ( byte << ( shiftCounter - 8 ) ) | residue ;
22+ // eslint-disable-next-line no-bitwise
2123 while ( residue >> 5 ) {
2224 result += hashCharset . charAt ( residue % 32 ) ;
2325 shiftCounter -= 5 ;
26+ // eslint-disable-next-line operator-assignment,no-bitwise
2427 residue = residue >> 5 ;
2528 }
2629 }
2730 result += hashCharset . charAt ( residue % 32 ) ;
2831 return result ;
2932}
3033
31- const tablesToVersionEntries = ( schema , tables ) => {
32- return R . sortBy (
33- table => - table . last_updated_at ,
34- tables . map ( table => {
35- const match = ( table . table_name || table . TABLE_NAME ) . match ( / ( .+ ) _ ( .+ ) _ ( .+ ) _ ( .+ ) / ) ;
36- if ( match ) {
37- return {
38- table_name : `${ schema } .${ match [ 1 ] } ` ,
39- content_version : match [ 2 ] ,
40- structure_version : match [ 3 ] ,
41- last_updated_at : parseInt ( match [ 4 ] , 10 )
42- }
43- }
44- } ) . filter ( R . identity )
45- )
46- } ;
34+ const tablesToVersionEntries = ( schema , tables ) => R . sortBy (
35+ table => - table . last_updated_at ,
36+ tables . map ( table => {
37+ const match = ( table . table_name || table . TABLE_NAME ) . match ( / ( .+ ) _ ( .+ ) _ ( .+ ) _ ( .+ ) / ) ;
38+ if ( match ) {
39+ return {
40+ table_name : `${ schema } .${ match [ 1 ] } ` ,
41+ content_version : match [ 2 ] ,
42+ structure_version : match [ 3 ] ,
43+ last_updated_at : parseInt ( match [ 4 ] , 10 )
44+ } ;
45+ }
46+ return null ;
47+ } ) . filter ( R . identity )
48+ ) ;
4749
4850class PreAggregationLoadCache {
4951 constructor ( redisPrefix , clientFactory , queryCache , preAggregations , options ) {
@@ -177,6 +179,7 @@ class PreAggregationLoader {
177179 this . waitForRenew = options . waitForRenew ;
178180 this . externalDriverFactory = preAggregations . externalDriverFactory ;
179181 this . requestId = options . requestId ;
182+ this . structureVersionPersistTime = preAggregations . structureVersionPersistTime ;
180183 }
181184
182185 async loadPreAggregation ( ) {
@@ -225,7 +228,7 @@ class PreAggregationLoader {
225228
226229 const versionEntries = await this . loadCache . getVersionEntries ( this . preAggregation ) ;
227230
228- const getVersionEntryByContentVersion = ( versionEntries ) => versionEntries . find (
231+ const getVersionEntryByContentVersion = ( entries ) => entries . find (
229232 v => v . table_name === this . preAggregation . tableName && v . content_version === contentVersion
230233 ) ;
231234
@@ -237,6 +240,7 @@ class PreAggregationLoader {
237240 // TODO this check can be redundant due to structure version is already checked in loadPreAggregation()
238241 if (
239242 ! this . waitForRenew &&
243+ // eslint-disable-next-line no-use-before-define
240244 await this . loadCache . getQueryStage ( PreAggregations . preAggregationQueryCacheKey ( this . preAggregation ) )
241245 ) {
242246 const versionEntryByStructureVersion = versionEntries . find (
@@ -253,7 +257,8 @@ class PreAggregationLoader {
253257 await this . driverFactory ( ) ;
254258 await client . createSchemaIfNotExists ( this . preAggregation . preAggregationsSchema ) ;
255259 }
256- const versionEntry = versionEntries . find ( e => e . table_name === this . preAggregation . tableName ) ; // TODO can be array instead of last
260+ // TODO can be array instead of last
261+ const versionEntry = versionEntries . find ( e => e . table_name === this . preAggregation . tableName ) ;
257262 const newVersionEntry = {
258263 table_name : this . preAggregation . tableName ,
259264 structure_version : structureVersion ,
@@ -363,6 +368,7 @@ class PreAggregationLoader {
363368 requestId : this . requestId
364369 } ,
365370 priority ,
371+ // eslint-disable-next-line no-use-before-define
366372 { stageQueryKey : PreAggregations . preAggregationQueryCacheKey ( this . preAggregation ) , requestId : this . requestId }
367373 ) ;
368374 }
@@ -526,8 +532,17 @@ class PreAggregationLoader {
526532 R . toPairs ,
527533 R . map ( p => p [ 1 ] [ 0 ] )
528534 ) ( versionEntries ) ;
535+
536+ const structureVersionsToSave = R . pipe (
537+ R . filter ( v => new Date ( ) . getTime ( ) - v . last_updated_at < this . structureVersionPersistTime * 1000 ) ,
538+ R . groupBy ( v => `${ v . table_name } _${ v . structure_version } ` ) ,
539+ R . toPairs ,
540+ R . map ( p => p [ 1 ] [ 0 ] )
541+ ) ( versionEntries ) ;
542+
529543 const tablesToSave =
530544 ( await this . preAggregations . tablesUsed ( ) )
545+ . concat ( structureVersionsToSave . map ( v => this . targetTableName ( v ) ) )
531546 . concat ( versionEntriesToSave . map ( v => this . targetTableName ( v ) ) )
532547 . concat ( [ justCreatedTable ] ) ;
533548 const toDrop = actualTables
@@ -552,14 +567,16 @@ class PreAggregations {
552567 new RedisCacheDriver ( { pool : options . redisPool } ) :
553568 new LocalCacheDriver ( ) ;
554569 this . externalDriverFactory = options . externalDriverFactory ;
570+ this . structureVersionPersistTime = options . structureVersionPersistTime || 60 * 60 * 24 * 30 ;
571+ this . usedTablePersistTime = options . usedTablePersistTime || 600 ;
555572 }
556573
557574 tablesUsedRedisKey ( tableName ) {
558575 return `SQL_PRE_AGGREGATIONS_${ this . redisPrefix } _TABLES_USED_${ tableName } ` ;
559576 }
560577
561578 async addTableUsed ( tableName ) {
562- return this . cacheDriver . set ( this . tablesUsedRedisKey ( tableName ) , true , 600 ) ;
579+ return this . cacheDriver . set ( this . tablesUsedRedisKey ( tableName ) , true , this . usedTablePersistTime ) ;
563580 }
564581
565582 async tablesUsed ( ) {
0 commit comments