trilium/src/services/content_hash.js

76 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-04-08 10:59:47 +08:00
"use strict";
2017-11-22 11:11:27 +08:00
const sql = require('./sql');
const utils = require('./utils');
const log = require('./log');
2018-04-08 10:59:47 +08:00
const eventLogService = require('./event_log');
const messagingService = require('./messaging');
const ApiToken = require('../entities/api_token');
const Branch = require('../entities/branch');
const Image = require('../entities/image');
const Note = require('../entities/note');
const NoteImage = require('../entities/note_image');
const Label = require('../entities/label');
const NoteRevision = require('../entities/note_revision');
const RecentNote = require('../entities/recent_note');
const Option = require('../entities/option');
async function getHash(entityConstructor, whereBranch) {
let contentToHash = await sql.getValue(`SELECT GROUP_CONCAT(hash) FROM ${entityConstructor.tableName} `
+ (whereBranch ? `WHERE ${whereBranch} ` : '') + `ORDER BY ${entityConstructor.primaryKeyName}`);
if (!contentToHash) { // might be null in case of no rows
contentToHash = "";
2017-11-22 11:11:27 +08:00
}
return utils.hash(contentToHash);
2017-11-22 11:11:27 +08:00
}
async function getHashes() {
const startTime = new Date();
const hashes = {
notes: await getHash(Note),
branches: await getHash(Branch),
note_revisions: await getHash(NoteRevision),
recent_notes: await getHash(RecentNote),
options: await getHash(Option, "isSynced = 1"),
images: await getHash(Image),
note_images: await getHash(NoteImage),
labels: await getHash(Label),
api_tokens: await getHash(ApiToken)
};
const elapseTimeMs = new Date().getTime() - startTime.getTime();
log.info(`Content hash computation took ${elapseTimeMs}ms`);
return hashes;
2017-11-22 11:11:27 +08:00
}
2018-04-08 10:59:47 +08:00
async function checkContentHashes(otherHashes) {
const hashes = await getHashes();
let allChecksPassed = true;
for (const key in hashes) {
if (hashes[key] !== otherHashes[key]) {
allChecksPassed = false;
2018-05-26 22:04:40 +08:00
await eventLogService.addEvent(`Content hash check for ${key} FAILED. Local is ${hashes[key]}, remote is ${otherHashes[key]}`);
2018-04-08 10:59:47 +08:00
if (key !== 'recent_notes') {
// let's not get alarmed about recent notes which get updated often and can cause failures in race conditions
await messagingService.sendMessageToAllClients({type: 'sync-hash-check-failed'});
}
}
}
if (allChecksPassed) {
log.info("Content hash checks PASSED");
}
}
2017-11-22 11:11:27 +08:00
module.exports = {
2018-04-08 10:59:47 +08:00
getHashes,
checkContentHashes
2017-11-22 11:11:27 +08:00
};