2018-04-07 22:59:47 -04:00
"use strict" ;
2017-11-21 22:11:27 -05:00
const sql = require ( './sql' ) ;
const utils = require ( './utils' ) ;
2018-01-01 19:47:50 -05:00
const log = require ( './log' ) ;
2018-05-22 00:15:54 -04:00
2020-06-20 12:31:38 +02:00
function getEntityHashes ( ) {
2018-01-01 19:47:50 -05:00
const startTime = new Date ( ) ;
2021-07-24 12:04:48 +02:00
const hashRows = sql . getRawRows ( `
2022-01-09 20:16:39 +01:00
SELECT entityName ,
entityId ,
hash
2021-02-09 23:27:09 +01:00
FROM entity _changes
WHERE isSynced = 1
AND entityName != 'note_reordering' ` );
2020-12-07 23:04:17 +01:00
// sorting is faster in memory
// sorting by entityId is enough, hashes will be segmented by entityName later on anyway
2021-07-25 10:58:50 +02:00
hashRows . sort ( ( a , b ) => a [ 1 ] < b [ 1 ] ? - 1 : 1 ) ;
2020-12-07 23:04:17 +01:00
const hashMap = { } ;
2021-07-24 12:04:48 +02:00
for ( const [ entityName , entityId , hash ] of hashRows ) {
2020-12-07 23:04:17 +01:00
const entityHashMap = hashMap [ entityName ] = hashMap [ entityName ] || { } ;
const sector = entityId [ 0 ] ;
entityHashMap [ sector ] = ( entityHashMap [ sector ] || "" ) + hash
}
for ( const entityHashMap of Object . values ( hashMap ) ) {
for ( const key in entityHashMap ) {
entityHashMap [ key ] = utils . hash ( entityHashMap [ key ] ) ;
}
}
2018-01-01 19:47:50 -05:00
2019-12-18 22:24:13 +01:00
const elapsedTimeMs = Date . now ( ) - startTime . getTime ( ) ;
2018-01-01 19:47:50 -05:00
2019-12-18 22:24:13 +01:00
log . info ( ` Content hash computation took ${ elapsedTimeMs } ms ` ) ;
2018-01-01 19:47:50 -05:00
2020-12-07 23:04:17 +01:00
return hashMap ;
2017-11-21 22:11:27 -05:00
}
2020-06-20 12:31:38 +02:00
function checkContentHashes ( otherHashes ) {
const entityHashes = getEntityHashes ( ) ;
2019-12-18 22:24:13 +01:00
const failedChecks = [ ] ;
2018-04-07 22:59:47 -04:00
2019-12-18 22:24:13 +01:00
for ( const entityName in entityHashes ) {
2022-01-10 21:24:07 +01:00
const thisSectorHashes = entityHashes [ entityName ] || { } ;
const otherSectorHashes = otherHashes [ entityName ] || { } ;
2018-04-07 22:59:47 -04:00
2019-12-24 16:00:31 +01:00
const sectors = new Set ( Object . keys ( thisSectorHashes ) . concat ( Object . keys ( otherSectorHashes ) ) ) ;
2019-11-19 19:07:14 +01:00
2019-12-18 22:24:13 +01:00
for ( const sector of sectors ) {
if ( thisSectorHashes [ sector ] !== otherSectorHashes [ sector ] ) {
log . info ( ` Content hash check for ${ entityName } sector ${ sector } FAILED. Local is ${ thisSectorHashes [ sector ] } , remote is ${ otherSectorHashes [ sector ] } ` ) ;
failedChecks . push ( { entityName , sector } ) ;
2018-04-07 22:59:47 -04:00
}
}
}
2019-12-18 22:24:13 +01:00
if ( failedChecks . length === 0 ) {
2018-04-07 22:59:47 -04:00
log . info ( "Content hash checks PASSED" ) ;
}
2019-12-18 22:58:30 +01:00
return failedChecks ;
2018-04-07 22:59:47 -04:00
}
2017-11-21 22:11:27 -05:00
module . exports = {
2019-12-18 22:58:30 +01:00
getEntityHashes ,
2018-04-07 22:59:47 -04:00
checkContentHashes
2020-06-20 12:31:38 +02:00
} ;