entities are now changed only if entity hash changed which will limit number of events emitted

This commit is contained in:
azivner 2018-08-12 20:04:48 +02:00
parent 13f524fb39
commit 9fb0599c45
8 changed files with 41 additions and 20 deletions

View File

@ -8,7 +8,7 @@ const sql = require('../services/sql');
class Attribute extends Entity { class Attribute extends Entity {
static get tableName() { return "attributes"; } static get tableName() { return "attributes"; }
static get primaryKeyName() { return "attributeId"; } static get primaryKeyName() { return "attributeId"; }
static get hashedProperties() { return ["attributeId", "noteId", "type", "name", "value", "isInheritable", "dateModified", "dateCreated"]; } static get hashedProperties() { return ["attributeId", "noteId", "type", "name", "value", "isInheritable", "dateCreated"]; }
constructor(row) { constructor(row) {
super(row); super(row);
@ -66,9 +66,11 @@ class Attribute extends Entity {
this.dateCreated = dateUtils.nowDate(); this.dateCreated = dateUtils.nowDate();
} }
this.dateModified = dateUtils.nowDate();
super.beforeSaving(); super.beforeSaving();
if (this.isChanged) {
this.dateModified = dateUtils.nowDate();
}
} }
} }

View File

@ -9,7 +9,7 @@ class Branch extends Entity {
static get tableName() { return "branches"; } static get tableName() { return "branches"; }
static get primaryKeyName() { return "branchId"; } static get primaryKeyName() { return "branchId"; }
// notePosition is not part of hash because it would produce a lot of updates in case of reordering // notePosition is not part of hash because it would produce a lot of updates in case of reordering
static get hashedProperties() { return ["branchId", "noteId", "parentNoteId", "dateModified", "isDeleted", "prefix"]; } static get hashedProperties() { return ["branchId", "noteId", "parentNoteId", "isDeleted", "prefix"]; }
async getNote() { async getNote() {
return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]); return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]);
@ -29,9 +29,11 @@ class Branch extends Entity {
this.dateCreated = dateUtils.nowDate(); this.dateCreated = dateUtils.nowDate();
} }
this.dateModified = dateUtils.nowDate();
super.beforeSaving(); super.beforeSaving();
if (this.isChanged) {
this.dateModified = dateUtils.nowDate();
}
} }
} }

View File

@ -18,13 +18,21 @@ class Entity {
this[this.constructor.primaryKeyName] = utils.newEntityId(); this[this.constructor.primaryKeyName] = utils.newEntityId();
} }
const origHash = this.hash;
this.hash = this.generateHash();
this.isChanged = origHash !== this.hash;
}
generateHash() {
let contentToHash = ""; let contentToHash = "";
for (const propertyName of this.constructor.hashedProperties) { for (const propertyName of this.constructor.hashedProperties) {
contentToHash += "|" + this[propertyName]; contentToHash += "|" + this[propertyName];
} }
this["hash"] = utils.hash(contentToHash).substr(0, 10); return utils.hash(contentToHash).substr(0, 10);
} }
async save() { async save() {

View File

@ -6,7 +6,7 @@ const dateUtils = require('../services/date_utils');
class Image extends Entity { class Image extends Entity {
static get tableName() { return "images"; } static get tableName() { return "images"; }
static get primaryKeyName() { return "imageId"; } static get primaryKeyName() { return "imageId"; }
static get hashedProperties() { return ["imageId", "format", "checksum", "name", "isDeleted", "dateModified", "dateCreated"]; } static get hashedProperties() { return ["imageId", "format", "checksum", "name", "isDeleted", "dateCreated"]; }
beforeSaving() { beforeSaving() {
if (!this.isDeleted) { if (!this.isDeleted) {
@ -17,9 +17,11 @@ class Image extends Entity {
this.dateCreated = dateUtils.nowDate(); this.dateCreated = dateUtils.nowDate();
} }
this.dateModified = dateUtils.nowDate();
super.beforeSaving(); super.beforeSaving();
if (this.isChanged) {
this.dateModified = dateUtils.nowDate();
}
} }
} }

View File

@ -8,7 +8,7 @@ const dateUtils = require('../services/date_utils');
class Note extends Entity { class Note extends Entity {
static get tableName() { return "notes"; } static get tableName() { return "notes"; }
static get primaryKeyName() { return "noteId"; } static get primaryKeyName() { return "noteId"; }
static get hashedProperties() { return ["noteId", "title", "content", "type", "dateModified", "isProtected", "isDeleted"]; } static get hashedProperties() { return ["noteId", "title", "content", "type", "isProtected", "isDeleted"]; }
constructor(row) { constructor(row) {
super(row); super(row);
@ -186,8 +186,6 @@ class Note extends Entity {
} }
beforeSaving() { beforeSaving() {
super.beforeSaving();
if (this.isJson() && this.jsonContent) { if (this.isJson() && this.jsonContent) {
this.content = JSON.stringify(this.jsonContent, null, '\t'); this.content = JSON.stringify(this.jsonContent, null, '\t');
} }
@ -204,8 +202,12 @@ class Note extends Entity {
this.dateCreated = dateUtils.nowDate(); this.dateCreated = dateUtils.nowDate();
} }
super.beforeSaving();
if (this.isChanged) {
this.dateModified = dateUtils.nowDate(); this.dateModified = dateUtils.nowDate();
} }
} }
}
module.exports = Note; module.exports = Note;

View File

@ -7,7 +7,7 @@ const dateUtils = require('../services/date_utils');
class NoteImage extends Entity { class NoteImage extends Entity {
static get tableName() { return "note_images"; } static get tableName() { return "note_images"; }
static get primaryKeyName() { return "noteImageId"; } static get primaryKeyName() { return "noteImageId"; }
static get hashedProperties() { return ["noteImageId", "noteId", "imageId", "isDeleted", "dateModified", "dateCreated"]; } static get hashedProperties() { return ["noteImageId", "noteId", "imageId", "isDeleted", "dateCreated"]; }
async getNote() { async getNote() {
return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]); return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]);
@ -26,9 +26,11 @@ class NoteImage extends Entity {
this.dateCreated = dateUtils.nowDate(); this.dateCreated = dateUtils.nowDate();
} }
this.dateModified = dateUtils.nowDate();
super.beforeSaving(); super.beforeSaving();
if (this.isChanged) {
this.dateModified = dateUtils.nowDate();
}
} }
} }

View File

@ -15,9 +15,11 @@ class Option extends Entity {
} }
beforeSaving() { beforeSaving() {
this.dateModified = dateUtils.nowDate();
super.beforeSaving(); super.beforeSaving();
if (this.isChanged) {
this.dateModified = dateUtils.nowDate();
}
} }
} }

View File

@ -60,6 +60,7 @@ async function updateEntity(entity) {
delete clone.jsonContent; delete clone.jsonContent;
delete clone.isOwned; delete clone.isOwned;
delete clone.isChanged;
for (const key in clone) { for (const key in clone) {
// !isBuffer is for images and attachments // !isBuffer is for images and attachments
@ -73,7 +74,7 @@ async function updateEntity(entity) {
const primaryKey = entity[entity.constructor.primaryKeyName]; const primaryKey = entity[entity.constructor.primaryKeyName];
if (entity.constructor.tableName !== 'options' || entity.isSynced) { if (entity.isChanged && (entity.constructor.tableName !== 'options' || entity.isSynced)) {
await syncTableService.addEntitySync(entity.constructor.tableName, primaryKey); await syncTableService.addEntitySync(entity.constructor.tableName, primaryKey);
} }
}); });