trilium/services/content_hash.js

107 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-11-22 11:11:27 +08:00
const sql = require('./sql');
const utils = require('./utils');
const options = require('./options');
const log = require('./log');
2017-11-22 11:11:27 +08:00
function getHash(rows) {
let hash = '';
2017-11-22 11:11:27 +08:00
for (const row of rows) {
hash = utils.hash(hash + JSON.stringify(row));
}
return hash;
}
async function getHashes() {
const startTime = new Date();
const hashes = {
2018-01-07 04:56:00 +08:00
notes: getHash(await sql.getAll(`
SELECT
2018-01-29 08:30:14 +08:00
noteId,
title,
content,
2018-01-21 10:56:03 +08:00
type,
2018-01-29 08:30:14 +08:00
dateModified,
isProtected,
isDeleted
2018-01-07 04:56:00 +08:00
FROM notes
2018-01-29 08:30:14 +08:00
ORDER BY noteId`)),
2017-11-22 11:11:27 +08:00
note_tree: getHash(await sql.getAll(`
2018-01-07 04:56:00 +08:00
SELECT
2018-01-29 08:30:14 +08:00
noteTreeId,
noteId,
parentNoteId,
notePosition,
dateModified,
isDeleted,
2018-01-07 04:56:00 +08:00
prefix
FROM note_tree
2018-01-29 08:30:14 +08:00
ORDER BY noteTreeId`)),
2017-11-22 11:11:27 +08:00
note_revisions: getHash(await sql.getAll(`
2018-01-07 04:56:00 +08:00
SELECT
noteRevisionId,
2018-01-29 08:30:14 +08:00
noteId,
title,
content,
dateModifiedFrom,
dateModifiedTo
FROM note_revisions
ORDER BY noteRevisionId`)),
2017-11-22 11:11:27 +08:00
2018-01-07 04:56:00 +08:00
recent_notes: getHash(await sql.getAll(`
SELECT
2018-01-29 08:30:14 +08:00
noteTreeId,
notePath,
dateAccessed,
isDeleted
2018-01-07 04:56:00 +08:00
FROM recent_notes
2018-01-29 08:30:14 +08:00
ORDER BY notePath`)),
2017-11-22 11:11:27 +08:00
2018-01-07 04:56:00 +08:00
options: getHash(await sql.getAll(`
SELECT
2018-01-29 08:30:14 +08:00
name,
value
2018-01-07 04:56:00 +08:00
FROM options
2018-01-29 08:30:14 +08:00
WHERE isSynced = 1
ORDER BY name`)),
2018-01-07 04:56:00 +08:00
// we don't include image data on purpose because they are quite large, checksum is good enough
// to represent the data anyway
images: getHash(await sql.getAll(`
SELECT
2018-01-29 08:30:14 +08:00
imageId,
2018-01-07 04:56:00 +08:00
format,
checksum,
name,
2018-01-29 08:30:14 +08:00
isDeleted,
dateModified,
dateCreated
2018-01-07 04:56:00 +08:00
FROM images
2018-01-29 08:30:14 +08:00
ORDER BY imageId`)),
2018-01-14 12:33:09 +08:00
attributes: getHash(await sql.getAll(`
SELECT
2018-01-29 08:30:14 +08:00
attributeId,
noteId
2018-01-14 12:33:09 +08:00
name,
value
2018-01-29 08:30:14 +08:00
dateModified,
dateCreated
2018-01-14 12:33:09 +08:00
FROM attributes
2018-01-29 08:30:14 +08:00
ORDER BY attributeId`))
};
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
}
module.exports = {
getHashes
2017-11-22 11:11:27 +08:00
};